1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
38
39
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
57
58
59
60
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
75
76
77
78
79
80
81
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
101
102
103
104
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
119
120
121
122
123
124
125
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
145
146
147
148
149
150 @Nonnull
151 public static EErrorLevel getErrorLevelFromFailedAssert (@Nonnull final FailedAssert aFailedAssert)
152 {
153 return getErrorLevelDeterminator ().getErrorLevelFromFailedAssert (aFailedAssert);
154 }
155
156
157
158
159
160
161
162
163 @Nonnull
164 public static EErrorLevel getErrorLevelFromSuccessfulReport (@Nonnull final SuccessfulReport aSuccessfulReport)
165 {
166 return getErrorLevelDeterminator ().getErrorLevelFromSuccessfulReport (aSuccessfulReport);
167 }
168
169
170
171
172
173
174
175
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 }