1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package gov.nist.secauto.swid.swidval;
28
29 import gov.nist.secauto.decima.core.assessment.Assessment;
30 import gov.nist.secauto.decima.core.assessment.AssessmentExecutor;
31 import gov.nist.secauto.decima.core.assessment.ConcurrentAssessmentExecutor;
32 import gov.nist.secauto.decima.core.classpath.ClasspathHandler;
33 import gov.nist.secauto.decima.xml.assessment.Factory;
34 import gov.nist.secauto.decima.xml.assessment.schema.SchemaAssessment;
35 import gov.nist.secauto.decima.xml.assessment.schematron.SchematronAssessment;
36 import gov.nist.secauto.decima.xml.document.XMLDocument;
37 import gov.nist.secauto.decima.xml.schematron.DefaultSchematronCompiler;
38 import gov.nist.secauto.decima.xml.schematron.Schematron;
39 import gov.nist.secauto.decima.xml.schematron.SchematronCompilationException;
40
41 import java.io.File;
42 import java.net.MalformedURLException;
43 import java.net.URL;
44 import java.util.ArrayList;
45 import java.util.Collections;
46 import java.util.List;
47 import java.util.concurrent.ExecutorService;
48
49 import javax.xml.transform.stream.StreamSource;
50
51 public class SWIDAssessmentFactory {
52 private static final SWIDAssessmentFactory INSTANCE;
53
54 public static String toPhase(TagType tagType, boolean authoritative) {
55 return "swid." + tagType.getName() + "." + (authoritative ? "auth" : "non-auth");
56 }
57
58 public static SWIDAssessmentFactory getInstance() {
59 return INSTANCE;
60 }
61
62 static {
63 INSTANCE = new SWIDAssessmentFactory();
64 }
65
66 private final Schematron schematron;
67 private final SchemaAssessment schemaAssessment;
68 private File resultDirectory;
69
70 private SWIDAssessmentFactory() {
71 ClasspathHandler.initialize();
72 this.schematron = createSchematron();
73 this.schemaAssessment = createSchemaAssessment();
74 }
75
76 public Schematron getSchematron() {
77 return schematron;
78 }
79
80
81
82
83
84
85 public File getResultDirectory() {
86 return resultDirectory;
87 }
88
89
90
91
92
93
94
95 public void setResultDirectory(File resultDirectory) {
96 this.resultDirectory = resultDirectory;
97 }
98
99
100
101
102
103
104
105
106
107
108
109
110 public AssessmentExecutor<XMLDocument> newAssessmentExecutor(TagType tagType, boolean authoritative,
111 ExecutorService executorService) {
112
113 List<Assessment<XMLDocument>> assessments = new ArrayList<Assessment<XMLDocument>>(2);
114 assessments.add(schemaAssessment);
115
116 SchematronAssessment assessment = Factory.newSchematronAssessment(schematron, toPhase(tagType, authoritative));
117 assessment.addParameter("authoritative", Boolean.toString(authoritative));
118 assessment.addParameter("type", tagType.getName());
119 assessments.add(assessment);
120 if (resultDirectory != null) {
121 resultDirectory.mkdirs();
122 assessment.setResultDirectory(resultDirectory);
123 }
124
125 AssessmentExecutor<XMLDocument> executor
126 = new ConcurrentAssessmentExecutor<XMLDocument>(executorService, assessments);
127 return executor;
128 }
129
130 protected SchemaAssessment createSchemaAssessment() {
131 return Factory.newSchemaAssessment("GEN-1-1",
132 Collections.singletonList(new StreamSource("classpath:swid-schema-fixed-20160908.xsd")));
133 }
134
135 protected Schematron createSchematron() {
136 URL schematronURL;
137 try {
138 schematronURL = new URL("classpath:schematron/swid-nistir-8060.sch");
139 return new DefaultSchematronCompiler().newSchematron(schematronURL);
140 } catch (MalformedURLException | SchematronCompilationException e) {
141
142 throw new RuntimeException(e);
143 }
144 }
145 }