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.annotations.PresentForCodeCoverage;
33  import com.helger.commons.annotations.ReturnsMutableCopy;
34  import com.helger.commons.error.EErrorLevel;
35  
36  /**
37   * Miscellaneous utility methods for handling Schematron output (SVRL).
38   *
39   * @author Philip Helger
40   */
41  @ThreadSafe
42  public final class SVRLUtils
43  {
44    private static final ReadWriteLock s_aRWLock = new ReentrantReadWriteLock ();
45  
46    private static ISVRLErrorLevelDeterminator s_aELD = new DefaultSVRLErrorLevelDeterminator ();
47  
48    @PresentForCodeCoverage
49    @SuppressWarnings ("unused")
50    private static final SVRLUtils s_aInstance = new SVRLUtils ();
51  
52    private SVRLUtils ()
53    {}
54  
55    /**
56     * Get a list of all failed assertions in a given schematron output.
57     *
58     * @param aSchematronOutput
59     *        The schematron output to be used. May not be <code>null</code>.
60     * @return A non-<code>null</code> list with all failed assertions.
61     */
62    @Nonnull
63    @ReturnsMutableCopy
64    public static List <SVRLFailedAssert> getAllFailedAssertions (@Nonnull final SchematronOutputType aSchematronOutput)
65    {
66      final List <SVRLFailedAssert> ret = new ArrayList <SVRLFailedAssert> ();
67      for (final Object aObj : aSchematronOutput.getActivePatternAndFiredRuleAndFailedAssert ())
68        if (aObj instanceof FailedAssert)
69          ret.add (new SVRLFailedAssert ((FailedAssert) aObj));
70      return ret;
71    }
72  
73    /**
74     * Get a list of all failed assertions in a given schematron output, with an
75     * error level equally or more severe than the passed error level.
76     *
77     * @param aSchematronOutput
78     *        The schematron output to be used. May not be <code>null</code>.
79     * @param eErrorLevel
80     *        Minimum error level to be queried
81     * @return A non-<code>null</code> list with all failed assertions.
82     */
83    @Nonnull
84    @ReturnsMutableCopy
85    public static List <SVRLFailedAssert> getAllFailedAssertionsMoreOrEqualSevereThan (@Nonnull final SchematronOutputType aSchematronOutput,
86                                                                                       @Nonnull final EErrorLevel eErrorLevel)
87    {
88      final List <SVRLFailedAssert> ret = new ArrayList <SVRLFailedAssert> ();
89      for (final Object aObj : aSchematronOutput.getActivePatternAndFiredRuleAndFailedAssert ())
90        if (aObj instanceof FailedAssert)
91        {
92          final SVRLFailedAssert aFA = new SVRLFailedAssert ((FailedAssert) aObj);
93          if (aFA.getFlag ().isMoreOrEqualSevereThan (eErrorLevel))
94            ret.add (aFA);
95        }
96      return ret;
97    }
98  
99    /**
100    * Get a list of all successful reports in a given schematron output.
101    *
102    * @param aSchematronOutput
103    *        The schematron output to be used. May not be <code>null</code>.
104    * @return A non-<code>null</code> list with all successful reports.
105    */
106   @Nonnull
107   @ReturnsMutableCopy
108   public static List <SVRLSuccessfulReport> getAllSuccesssfulReports (@Nonnull final SchematronOutputType aSchematronOutput)
109   {
110     final List <SVRLSuccessfulReport> ret = new ArrayList <SVRLSuccessfulReport> ();
111     for (final Object aObj : aSchematronOutput.getActivePatternAndFiredRuleAndFailedAssert ())
112       if (aObj instanceof SuccessfulReport)
113         ret.add (new SVRLSuccessfulReport ((SuccessfulReport) aObj));
114     return ret;
115   }
116 
117   /**
118    * Get a list of all successful reports in a given schematron output, with an
119    * error level equally or more severe than the passed error level.
120    *
121    * @param aSchematronOutput
122    *        The schematron output to be used. May not be <code>null</code>.
123    * @param eErrorLevel
124    *        Minimum error level to be queried
125    * @return A non-<code>null</code> list with all successful reports.
126    */
127   @Nonnull
128   @ReturnsMutableCopy
129   public static List <SVRLSuccessfulReport> getAllSuccessfulReportsMoreOrEqualSevereThan (@Nonnull final SchematronOutputType aSchematronOutput,
130                                                                                           @Nonnull final EErrorLevel eErrorLevel)
131   {
132     final List <SVRLSuccessfulReport> ret = new ArrayList <SVRLSuccessfulReport> ();
133     for (final Object aObj : aSchematronOutput.getActivePatternAndFiredRuleAndFailedAssert ())
134       if (aObj instanceof SuccessfulReport)
135       {
136         final SVRLSuccessfulReport aFA = new SVRLSuccessfulReport ((SuccessfulReport) aObj);
137         if (aFA.getFlag ().isMoreOrEqualSevereThan (eErrorLevel))
138           ret.add (aFA);
139       }
140     return ret;
141   }
142 
143   /**
144    * Get the error level associated with a single failed assertion.
145    *
146    * @param aFailedAssert
147    *        The failed assert to be queried. May not be <code>null</code>.
148    * @return The error level and never <code>null</code>.
149    */
150   @Nonnull
151   public static EErrorLevel getErrorLevelFromFailedAssert (@Nonnull final FailedAssert aFailedAssert)
152   {
153     return getErrorLevelDeterminator ().getErrorLevelFromFailedAssert (aFailedAssert);
154   }
155 
156   /**
157    * Get the error level associated with a single successful report.
158    *
159    * @param aSuccessfulReport
160    *        The failed assert to be queried. May not be <code>null</code>.
161    * @return The error level and never <code>null</code>.
162    */
163   @Nonnull
164   public static EErrorLevel getErrorLevelFromSuccessfulReport (@Nonnull final SuccessfulReport aSuccessfulReport)
165   {
166     return getErrorLevelDeterminator ().getErrorLevelFromSuccessfulReport (aSuccessfulReport);
167   }
168 
169   /**
170    * Get the error level associated with a single failed assertion.
171    *
172    * @param sFlag
173    *        The flag to be queried. May not be <code>null</code>.
174    * @return The error level and never <code>null</code>.
175    * @deprecated Use {@link ISVRLErrorLevelDeterminator} implementations instead
176    */
177   @Nonnull
178   @Deprecated
179   public static EErrorLevel getErrorLevelFromFlag (@Nonnull final String sFlag)
180   {
181     ValueEnforcer.notNull (sFlag, "Flag");
182 
183     if (sFlag.equalsIgnoreCase ("warning") || sFlag.equalsIgnoreCase ("warn"))
184       return EErrorLevel.WARN;
185     if (sFlag.equalsIgnoreCase ("error") || sFlag.equalsIgnoreCase ("err"))
186       return EErrorLevel.ERROR;
187     if (sFlag.equalsIgnoreCase ("fatal") ||
188         sFlag.equalsIgnoreCase ("fatal_error") ||
189         sFlag.equalsIgnoreCase ("fatalerror"))
190       return EErrorLevel.FATAL_ERROR;
191     throw new IllegalArgumentException ("Cannot convert the SVRL failed assertion flag '" +
192                                         sFlag +
193                                         "' to an error level. Please extend the preceeding list!");
194   }
195 
196   @Nonnull
197   public static ISVRLErrorLevelDeterminator getErrorLevelDeterminator ()
198   {
199     s_aRWLock.readLock ().lock ();
200     try
201     {
202       return s_aELD;
203     }
204     finally
205     {
206       s_aRWLock.readLock ().unlock ();
207     }
208   }
209 
210   public static void setErrorLevelDeterminator (@Nonnull final ISVRLErrorLevelDeterminator aELD)
211   {
212     ValueEnforcer.notNull (aELD, "ErrorLevelDeterminator");
213 
214     s_aRWLock.readLock ().lock ();
215     try
216     {
217       s_aELD = aELD;
218     }
219     finally
220     {
221       s_aRWLock.readLock ().unlock ();
222     }
223   }
224 }