View Javadoc
1   /**
2    * Copyright (C) 2014-2017 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.svrl;
18  
19  import javax.annotation.Nonnull;
20  import javax.annotation.concurrent.ThreadSafe;
21  
22  import org.oclc.purl.dsdl.svrl.FailedAssert;
23  import org.oclc.purl.dsdl.svrl.SchematronOutputType;
24  import org.oclc.purl.dsdl.svrl.SuccessfulReport;
25  
26  import com.helger.commons.ValueEnforcer;
27  import com.helger.commons.annotation.PresentForCodeCoverage;
28  import com.helger.commons.annotation.ReturnsMutableCopy;
29  import com.helger.commons.collection.ext.CommonsArrayList;
30  import com.helger.commons.collection.ext.ICommonsList;
31  import com.helger.commons.concurrent.SimpleReadWriteLock;
32  import com.helger.commons.error.level.IErrorLevel;
33  
34  /**
35   * Miscellaneous utility methods for handling Schematron output (SVRL).
36   *
37   * @author Philip Helger
38   */
39  @ThreadSafe
40  public final class SVRLHelper
41  {
42    private static final SimpleReadWriteLock s_aRWLock = new SimpleReadWriteLock ();
43  
44    private static ISVRLErrorLevelDeterminator s_aELD = new DefaultSVRLErrorLevelDeterminator ();
45  
46    @PresentForCodeCoverage
47    private static final SVRLHelper s_aInstance = new SVRLHelper ();
48  
49    private SVRLHelper ()
50    {}
51  
52    /**
53     * Get a list of all failed assertions in a given schematron output.
54     *
55     * @param aSchematronOutput
56     *        The schematron output to be used. May not be <code>null</code>.
57     * @return A non-<code>null</code> list with all failed assertions.
58     */
59    @Nonnull
60    @ReturnsMutableCopy
61    public static ICommonsList <SVRLFailedAssert> getAllFailedAssertions (@Nonnull final SchematronOutputType aSchematronOutput)
62    {
63      final ICommonsList <SVRLFailedAssert> ret = new CommonsArrayList <> ();
64      for (final Object aObj : aSchematronOutput.getActivePatternAndFiredRuleAndFailedAssert ())
65        if (aObj instanceof FailedAssert)
66          ret.add (new SVRLFailedAssert ((FailedAssert) aObj));
67      return ret;
68    }
69  
70    /**
71     * Get a list of all failed assertions in a given schematron output, with an
72     * error level equally or more severe than the passed error level.
73     *
74     * @param aSchematronOutput
75     *        The schematron output to be used. May not be <code>null</code>.
76     * @param aErrorLevel
77     *        Minimum error level to be queried
78     * @return A non-<code>null</code> list with all failed assertions.
79     */
80    @Nonnull
81    @ReturnsMutableCopy
82    public static ICommonsList <SVRLFailedAssert> getAllFailedAssertionsMoreOrEqualSevereThan (@Nonnull final SchematronOutputType aSchematronOutput,
83                                                                                               @Nonnull final IErrorLevel aErrorLevel)
84    {
85      final ICommonsList <SVRLFailedAssert> ret = new CommonsArrayList <> ();
86      for (final Object aObj : aSchematronOutput.getActivePatternAndFiredRuleAndFailedAssert ())
87        if (aObj instanceof FailedAssert)
88        {
89          final SVRLFailedAssert aFA = new SVRLFailedAssert ((FailedAssert) aObj);
90          if (aFA.getFlag ().isGE (aErrorLevel))
91            ret.add (aFA);
92        }
93      return ret;
94    }
95  
96    /**
97     * Get a list of all successful reports in a given schematron output.
98     *
99     * @param aSchematronOutput
100    *        The schematron output to be used. May not be <code>null</code>.
101    * @return A non-<code>null</code> list with all successful reports.
102    */
103   @Nonnull
104   @ReturnsMutableCopy
105   public static ICommonsList <SVRLSuccessfulReport> getAllSuccessfulReports (@Nonnull final SchematronOutputType aSchematronOutput)
106   {
107     final ICommonsList <SVRLSuccessfulReport> ret = new CommonsArrayList <> ();
108     for (final Object aObj : aSchematronOutput.getActivePatternAndFiredRuleAndFailedAssert ())
109       if (aObj instanceof SuccessfulReport)
110         ret.add (new SVRLSuccessfulReport ((SuccessfulReport) aObj));
111     return ret;
112   }
113 
114   /**
115    * Get a list of all successful reports in a given schematron output, with an
116    * error level equally or more severe than the passed error level.
117    *
118    * @param aSchematronOutput
119    *        The schematron output to be used. May not be <code>null</code>.
120    * @param aErrorLevel
121    *        Minimum error level to be queried
122    * @return A non-<code>null</code> list with all successful reports.
123    */
124   @Nonnull
125   @ReturnsMutableCopy
126   public static ICommonsList <SVRLSuccessfulReport> getAllSuccessfulReportsMoreOrEqualSevereThan (@Nonnull final SchematronOutputType aSchematronOutput,
127                                                                                                   @Nonnull final IErrorLevel aErrorLevel)
128   {
129     final ICommonsList <SVRLSuccessfulReport> ret = new CommonsArrayList <> ();
130     for (final Object aObj : aSchematronOutput.getActivePatternAndFiredRuleAndFailedAssert ())
131       if (aObj instanceof SuccessfulReport)
132       {
133         final SVRLSuccessfulReport aFA = new SVRLSuccessfulReport ((SuccessfulReport) aObj);
134         if (aFA.getFlag ().isGE (aErrorLevel))
135           ret.add (aFA);
136       }
137     return ret;
138   }
139 
140   /**
141    * Get the error level associated with a single failed assertion.
142    *
143    * @param aFailedAssert
144    *        The failed assert to be queried. May not be <code>null</code>.
145    * @return The error level and never <code>null</code>.
146    */
147   @Nonnull
148   public static IErrorLevel getErrorLevelFromFailedAssert (@Nonnull final FailedAssert aFailedAssert)
149   {
150     return getErrorLevelDeterminator ().getErrorLevelFromFailedAssert (aFailedAssert);
151   }
152 
153   /**
154    * Get the error level associated with a single successful report.
155    *
156    * @param aSuccessfulReport
157    *        The failed assert to be queried. May not be <code>null</code>.
158    * @return The error level and never <code>null</code>.
159    */
160   @Nonnull
161   public static IErrorLevel getErrorLevelFromSuccessfulReport (@Nonnull final SuccessfulReport aSuccessfulReport)
162   {
163     return getErrorLevelDeterminator ().getErrorLevelFromSuccessfulReport (aSuccessfulReport);
164   }
165 
166   @Nonnull
167   public static ISVRLErrorLevelDeterminator getErrorLevelDeterminator ()
168   {
169     return s_aRWLock.readLocked ( () -> s_aELD);
170   }
171 
172   public static void setErrorLevelDeterminator (@Nonnull final ISVRLErrorLevelDeterminator aELD)
173   {
174     ValueEnforcer.notNull (aELD, "ErrorLevelDeterminator");
175 
176     s_aRWLock.readLocked ( () -> s_aELD = aELD);
177   }
178 }