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