View Javadoc
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.assessment.schematron;
28  
29  import gov.nist.secauto.decima.core.assessment.Assessment;
30  import gov.nist.secauto.decima.core.assessment.AssessmentException;
31  import gov.nist.secauto.decima.core.assessment.result.AssessmentResultBuilder;
32  import gov.nist.secauto.decima.core.assessment.result.AssessmentResults;
33  import gov.nist.secauto.decima.core.assessment.result.BasicTestResult;
34  import gov.nist.secauto.decima.core.assessment.result.TestStatus;
35  import gov.nist.secauto.decima.xml.assessment.result.XPathContext;
36  import gov.nist.secauto.decima.xml.document.SimpleXPathContext;
37  import gov.nist.secauto.decima.xml.document.XMLDocument;
38  import gov.nist.secauto.decima.xml.document.XPathEvaluator;
39  
40  import org.apache.logging.log4j.LogManager;
41  import org.apache.logging.log4j.Logger;
42  import org.jdom2.Element;
43  
44  import java.util.HashMap;
45  import java.util.List;
46  import java.util.Map;
47  
48  import javax.xml.xpath.XPathExpressionException;
49  import javax.xml.xpath.XPathFactoryConfigurationException;
50  
51  public abstract class AbstractSVRLHandler implements SVRLHandler {
52    private static final Logger log = LogManager.getLogger(AbstractSVRLHandler.class);
53  
54    private final AssessmentResultBuilder assessmentResultBuilder;
55    private final Assessment<? extends XMLDocument> assessment;
56    private final XMLDocument assessedDocument;
57    private final XPathEvaluator xpathEvaluator;
58    private final Map<String, String> prefixToNamespaceMap = new HashMap<>();
59  
60    /**
61     * Constructs a handler that is capable of processing a SVRL result to produce an intermediate form
62     * of {@link AssessmentResults}.
63     *
64     * @param assessment
65     *          the assessment driving this analysis
66     * @param sourceDocument
67     *          the document being analyzed by the Schematron assessment
68     * @param assessmentResultBuilder
69     *          the builder that will be used to produce the {@link AssessmentResults}
70     * @throws AssessmentException
71     *           if an error occurred while parsing the SVRL information
72     */
73    public AbstractSVRLHandler(Assessment<? extends XMLDocument> assessment, XMLDocument sourceDocument,
74        AssessmentResultBuilder assessmentResultBuilder) throws AssessmentException {
75      this.assessment = assessment;
76      this.assessedDocument = sourceDocument;
77      this.assessmentResultBuilder = assessmentResultBuilder;
78      try {
79        this.xpathEvaluator = assessedDocument.newXPathEvaluator();
80      } catch (XPathFactoryConfigurationException e) {
81        throw new AssessmentException("Unable to create new XPathEvaluator", e);
82      }
83    }
84  
85    /**
86     * Retrieve the assessment this handler supports.
87     * 
88     * @return the assessment
89     */
90    public Assessment<? extends XMLDocument> getAssessment() {
91      return assessment;
92    }
93  
94    /**
95     * Retrieve the document for which this handler is identifying issues.
96     * 
97     * @return the document
98     */
99    public XMLDocument getAssessedDocument() {
100     return assessedDocument;
101   }
102 
103   /**
104    * Retrieve the XPathEvaluator that can be used to resolve XPaths against the target document.
105    * 
106    * @return the evaluator
107    */
108   protected XPathEvaluator getXPathEvaluator() {
109     return xpathEvaluator;
110 
111   }
112 
113   public AssessmentResultBuilder getValidationResultBuilder() {
114     return assessmentResultBuilder;
115   }
116 
117   @Override
118   public void handleNSPrefix(Element prefix) {
119     prefixToNamespaceMap.put(prefix.getAttributeValue("prefix"), prefix.getAttributeValue("uri"));
120   }
121 
122   @Override
123   public void handleActivePattern(Element activePattern) {
124     // do nothing by default
125   }
126 
127   @Override
128   public void handleFiredRule(Element xmlObject) {
129     // do nothing by default
130   }
131 
132   protected void handleAssertionResult(String derivedRequirementId, String assertionId, TestStatus testStatus,
133       String xpath, List<String> values) {
134     if (log.isTraceEnabled()) {
135       log.trace("Adding '{}' assertion result for: {}", testStatus, derivedRequirementId);
136     }
137     XPathEvaluator evaluator = getXPathEvaluator();
138     XPathContext context = null;
139 
140     if (xpath != null) {
141       try {
142         context = evaluator.getContext(xpath);
143       } catch (XPathExpressionException e) {
144         log.error("Unable to resolve XPath context", e);
145       }
146     }
147 
148     if (context == null) {
149       context = new SimpleXPathContext(xpath, getAssessedDocument().getSystemId(), -1, -1);
150     }
151     BasicTestResult result = new BasicTestResult(assertionId, testStatus, context);
152     result.addResultValues(values);
153     Assessment<? extends XMLDocument> assessment = getAssessment();
154     XMLDocument document = getAssessedDocument();
155     getValidationResultBuilder().addTestResult(assessment, document, derivedRequirementId, result);
156   }
157 
158 }