1 /**
2 * Portions of this software was developed by employees of the National Institute
3 * of Standards and Technology (NIST), an agency of the Federal Government and is
4 * being made available as a public service. Pursuant to title 17 United States
5 * Code Section 105, works of NIST employees are not subject to copyright
6 * protection in the United States. This software may be subject to foreign
7 * copyright. Permission in the United States and in foreign countries, to the
8 * extent that NIST may hold copyright, to use, copy, modify, create derivative
9 * works, and distribute this software and its documentation without fee is hereby
10 * granted on a non-exclusive basis, provided that this notice and disclaimer
11 * of warranty appears in all copies.
12 *
13 * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
14 * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
15 * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
17 * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
18 * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT
19 * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
20 * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
21 * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
22 * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
23 * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
24 * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
25 */
26
27 package gov.nist.secauto.decima.xml.testing;
28
29 import gov.nist.secauto.decima.xml.assessment.schematron.SchematronHandler;
30
31 import org.jdom2.Element;
32 import org.jdom2.filter.Filters;
33
34 import java.net.MalformedURLException;
35 import java.net.URL;
36 import java.util.Collections;
37 import java.util.HashMap;
38 import java.util.List;
39 import java.util.Map;
40
41 class SchematronAssessmentInfo {
42 private final URL rulsetLocation;
43 private final String phase;
44 private final String handlerClass;
45 private final Map<String, String> parameters;
46
47 /**
48 * Creates a new data transfer object for a Schematron configuration based on the contents of the
49 * "schematron-assessment" element.
50 *
51 * @param assessmentElement
52 * the element to load the data from
53 * @throws MalformedURLException
54 * if the ruleset URL is malformed
55 */
56 public SchematronAssessmentInfo(Element assessmentElement) throws MalformedURLException {
57 this.rulsetLocation = new URL(assessmentElement.getAttributeValue("ruleset"));
58 this.phase = assessmentElement.getAttributeValue("phase");
59 this.handlerClass = assessmentElement.getAttributeValue("handler-class");
60
61 this.parameters = handleParameters(
62 assessmentElement.getContent(Filters.element("parameter", assessmentElement.getNamespace())));
63 }
64
65 private static Map<String, String> handleParameters(List<Element> content) {
66 Map<String, String> retval;
67
68 if (content.isEmpty()) {
69 retval = Collections.emptyMap();
70 } else {
71 retval = new HashMap<>();
72 for (Element p : content) {
73 String key = p.getAttributeValue("name");
74 String value = p.getText();
75 retval.put(key, value);
76 }
77 retval = Collections.unmodifiableMap(retval);
78 }
79 return retval;
80 }
81
82 /**
83 * Retrieves a {@link URL} pointing to the location of the Schematron rules.
84 *
85 * @return the rulsetLocation
86 */
87 public URL getRulsetLocation() {
88 return rulsetLocation;
89 }
90
91 /**
92 * Retrieves the identified Schematron phase to use when performing Schematron validation.
93 *
94 * @return the phase
95 */
96 public String getPhase() {
97 return phase;
98 }
99
100 /**
101 * Retrieves the class name of the {@link SchematronHandler} instance to use to process the SVRL
102 * results.
103 *
104 * @return the handlerClass
105 */
106 public String getHandlerClass() {
107 return handlerClass;
108 }
109
110 /**
111 * Retrieve the mapping of Schematron XSL parameters to use when performing Schematron validation.
112 *
113 * @return the parameters
114 */
115 public Map<String, String> getParameters() {
116 return parameters;
117 }
118
119 @Override
120 public int hashCode() {
121 int hash = 7;
122 hash = 31 * hash + rulsetLocation.hashCode();
123 hash = 31 * hash + (null == phase ? 0 : phase.hashCode());
124 hash = 31 * hash + (null == handlerClass ? 0 : handlerClass.hashCode());
125 hash = 31 * hash + parameters.hashCode();
126 return hash;
127 }
128
129 @Override
130 public boolean equals(Object obj) {
131 if (this == obj) {
132 return true;
133 }
134
135 if ((obj == null) || (obj.getClass() != this.getClass())) {
136 return false;
137 }
138
139 // object must be an instance of the same class at this point
140 SchematronAssessmentInfo/gov/nist/secauto/decima/xml/testing/SchematronAssessmentInfo.html#SchematronAssessmentInfo">SchematronAssessmentInfo other = (SchematronAssessmentInfo) obj;
141 return rulsetLocation.equals(other.rulsetLocation)
142 && (phase == other.phase || (phase != null && phase.equals(other.phase)))
143 && (handlerClass == other.handlerClass || (handlerClass != null && handlerClass.equals(other.handlerClass)))
144 && parameters.equals(other.parameters);
145 }
146
147 }