View Javadoc
1   /**
2    * Copyright (C) 2014-2018 Philip Helger (www.helger.com)
3    * philip[at]helger[dot]com
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *         http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package com.helger.schematron.pure.validation;
18  
19  import java.io.Serializable;
20  
21  import javax.annotation.Nonnull;
22  import javax.annotation.Nullable;
23  
24  import org.w3c.dom.Node;
25  
26  import com.helger.commons.state.EContinue;
27  import com.helger.schematron.pure.model.PSAssertReport;
28  import com.helger.schematron.pure.model.PSPattern;
29  import com.helger.schematron.pure.model.PSPhase;
30  import com.helger.schematron.pure.model.PSRule;
31  import com.helger.schematron.pure.model.PSSchema;
32  
33  /**
34   * Base interface for a Schematron validation callback handler. It is only
35   * invoked when validating an XML against a Schematron file.
36   *
37   * @see com.helger.schematron.pure.bound.IPSBoundSchema#validate(Node,String,
38   *      IPSValidationHandler)
39   * @author Philip Helger
40   */
41  public interface IPSValidationHandler extends Serializable
42  {
43    /**
44     * This is the first method called.
45     *
46     * @param aSchema
47     *        The Schematron to be validated. Never <code>null</code>.
48     * @param aActivePhase
49     *        The selected phase, if any special phase was selected. May be
50     *        <code>null</code>.
51     * @param sBaseURI
52     *        The Base URI of the XML to be validated. May be <code>null</code>.
53     * @see #onEnd(PSSchema, PSPhase)
54     * @throws SchematronValidationException
55     *         In case of validation errors
56     */
57    default void onStart (@Nonnull final PSSchema aSchema,
58                          @Nullable final PSPhase aActivePhase,
59                          @Nullable final String sBaseURI) throws SchematronValidationException
60    {}
61  
62    /**
63     * This method is called for every pattern inside the schema.
64     *
65     * @param aPattern
66     *        The current pattern. Never <code>null</code>.
67     * @throws SchematronValidationException
68     *         In case of validation errors
69     */
70    default void onPattern (@Nonnull final PSPattern aPattern) throws SchematronValidationException
71    {}
72  
73    /**
74     * This method is called for every rule inside the current pattern.
75     *
76     * @param aRule
77     *        The current rule. Never <code>null</code>.
78     * @param sContext
79     *        The real context to be used in validation. May differ from the
80     *        result of {@link PSRule#getContext()} because of replaced variables
81     *        from &lt;let&gt; elements.
82     * @throws SchematronValidationException
83     *         In case of validation errors
84     */
85    default void onRule (@Nonnull final PSRule aRule, @Nonnull final String sContext) throws SchematronValidationException
86    {}
87  
88    /**
89     * This method is called for every failed assert.
90     *
91     * @param aAssertReport
92     *        The current assert element. Never <code>null</code>.
93     * @param sTestExpression
94     *        The source XPath expression that was evaluated for this node. It may
95     *        be different from the test expression contained in the passed
96     *        assert/report element because of replaced &lt;let&gt; elements.
97     *        Never <code>null</code>.
98     * @param aRuleMatchingNode
99     *        The XML node of the document to be validated.
100    * @param nNodeIndex
101    *        The index of the matched node, relative to the current rule.
102    * @param aContext
103    *        A context object - implementation dependent. For the default query
104    *        binding this is e.g. an
105    *        {@link com.helger.schematron.pure.bound.xpath.PSXPathBoundAssertReport}
106    *        object.
107    * @return {@link EContinue#BREAK} to stop validating immediately.
108    * @throws SchematronValidationException
109    *         In case of validation errors
110    */
111   @Nonnull
112   default EContinue onFailedAssert (@Nonnull final PSAssertReport aAssertReport,
113                                     @Nonnull final String sTestExpression,
114                                     @Nonnull final Node aRuleMatchingNode,
115                                     final int nNodeIndex,
116                                     @Nullable final Object aContext) throws SchematronValidationException
117   {
118     return EContinue.CONTINUE;
119   }
120 
121   /**
122    * This method is called for every failed assert.
123    *
124    * @param aAssertReport
125    *        The current assert element. Never <code>null</code>.
126    * @param sTestExpression
127    *        The source XPath expression that was evaluated for this node. It may
128    *        be different from the test expression contained in the passed
129    *        assert/report element because of replaced &lt;let&gt; elements.
130    *        Never <code>null</code>.
131    * @param aRuleMatchingNode
132    *        The XML node of the document to be validated.
133    * @param nNodeIndex
134    *        The index of the matched node, relative to the current rule.
135    * @param aContext
136    *        A context object - implementation dependent. For the default query
137    *        binding this is e.g. an
138    *        {@link com.helger.schematron.pure.bound.xpath.PSXPathBoundAssertReport}
139    *        object.
140    * @return {@link EContinue#BREAK} to stop validating immediately.
141    * @throws SchematronValidationException
142    *         In case of validation errors
143    */
144   @Nonnull
145   default EContinue onSuccessfulReport (@Nonnull final PSAssertReport aAssertReport,
146                                         @Nonnull final String sTestExpression,
147                                         @Nonnull final Node aRuleMatchingNode,
148                                         final int nNodeIndex,
149                                         @Nullable final Object aContext) throws SchematronValidationException
150   {
151     return EContinue.CONTINUE;
152   }
153 
154   /**
155    * This is the last method called. It indicates that the validation for the
156    * current scheme ended.
157    *
158    * @param aSchema
159    *        The Schematron that was be validated. Never <code>null</code>.
160    * @param aActivePhase
161    *        The selected phase, if any special phase was selected. May be
162    *        <code>null</code>.
163    * @see #onStart(PSSchema, PSPhase, String)
164    * @throws SchematronValidationException
165    *         In case of validation errors
166    */
167   default void onEnd (@Nonnull final PSSchema aSchema,
168                       @Nullable final PSPhase aActivePhase) throws SchematronValidationException
169   {}
170 }