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.regex.Matcher;
20
21 import javax.annotation.Nonnull;
22 import javax.annotation.Nullable;
23 import javax.annotation.concurrent.ThreadSafe;
24
25 import org.oclc.purl.dsdl.svrl.FailedAssert;
26 import org.oclc.purl.dsdl.svrl.SchematronOutputType;
27 import org.oclc.purl.dsdl.svrl.SuccessfulReport;
28
29 import com.helger.commons.ValueEnforcer;
30 import com.helger.commons.annotation.PresentForCodeCoverage;
31 import com.helger.commons.annotation.ReturnsMutableCopy;
32 import com.helger.commons.collection.impl.CommonsArrayList;
33 import com.helger.commons.collection.impl.ICommonsList;
34 import com.helger.commons.concurrent.SimpleReadWriteLock;
35 import com.helger.commons.error.level.IErrorLevel;
36 import com.helger.commons.regex.RegExHelper;
37 import com.helger.commons.string.StringHelper;
38
39
40
41
42
43
44 @ThreadSafe
45 public final class SVRLHelper
46 {
47 private static final SimpleReadWriteLock s_aRWLock = new SimpleReadWriteLock ();
48
49 private static ISVRLErrorLevelDeterminator s_aELD = new DefaultSVRLErrorLevelDeterminator ();
50
51 @PresentForCodeCoverage
52 private static final SVRLHelper.html#SVRLHelper">SVRLHelper s_aInstance = new SVRLHelper ();
53
54 private SVRLHelper ()
55 {}
56
57
58
59
60
61
62
63
64 @Nonnull
65 @ReturnsMutableCopy
66 public static ICommonsList <SVRLFailedAssert> getAllFailedAssertions (@Nullable final SchematronOutputType aSchematronOutput)
67 {
68 final ICommonsList <SVRLFailedAssert> ret = new CommonsArrayList <> ();
69 if (aSchematronOutput != null)
70 for (final Object aObj : aSchematronOutput.getActivePatternAndFiredRuleAndFailedAssert ())
71 if (aObj instanceof FailedAssert)
72 ret.add (new SVRLFailedAssert ((FailedAssert) aObj));
73 return ret;
74 }
75
76
77
78
79
80
81
82
83
84
85
86 @Nonnull
87 @ReturnsMutableCopy
88 public static ICommonsList <SVRLFailedAssert> getAllFailedAssertionsMoreOrEqualSevereThan (@Nullable final SchematronOutputType aSchematronOutput,
89 @Nonnull final IErrorLevel aErrorLevel)
90 {
91 final ICommonsList <SVRLFailedAssert> ret = new CommonsArrayList <> ();
92 if (aSchematronOutput != null)
93 for (final Object aObj : aSchematronOutput.getActivePatternAndFiredRuleAndFailedAssert ())
94 if (aObj instanceof FailedAssert)
95 {
96 final SVRLFailedAssertedAssert.html#SVRLFailedAssert">SVRLFailedAssert aFA = new SVRLFailedAssert ((FailedAssert) aObj);
97 if (aFA.getFlag ().isGE (aErrorLevel))
98 ret.add (aFA);
99 }
100 return ret;
101 }
102
103
104
105
106
107
108
109
110 @Nonnull
111 @ReturnsMutableCopy
112 public static ICommonsList <SVRLSuccessfulReport> getAllSuccessfulReports (@Nullable final SchematronOutputType aSchematronOutput)
113 {
114 final ICommonsList <SVRLSuccessfulReport> ret = new CommonsArrayList <> ();
115 if (aSchematronOutput != null)
116 for (final Object aObj : aSchematronOutput.getActivePatternAndFiredRuleAndFailedAssert ())
117 if (aObj instanceof SuccessfulReport)
118 ret.add (new SVRLSuccessfulReport ((SuccessfulReport) aObj));
119 return ret;
120 }
121
122
123
124
125
126
127
128
129
130
131
132 @Nonnull
133 @ReturnsMutableCopy
134 public static ICommonsList <SVRLSuccessfulReport> getAllSuccessfulReportsMoreOrEqualSevereThan (@Nullable final SchematronOutputType aSchematronOutput,
135 @Nonnull final IErrorLevel aErrorLevel)
136 {
137 final ICommonsList <SVRLSuccessfulReport> ret = new CommonsArrayList <> ();
138 if (aSchematronOutput != null)
139 for (final Object aObj : aSchematronOutput.getActivePatternAndFiredRuleAndFailedAssert ())
140 if (aObj instanceof SuccessfulReport)
141 {
142 final SVRLSuccessfulReportulReport.html#SVRLSuccessfulReport">SVRLSuccessfulReport aSR = new SVRLSuccessfulReport ((SuccessfulReport) aObj);
143 if (aSR.getFlag ().isGE (aErrorLevel))
144 ret.add (aSR);
145 }
146 return ret;
147 }
148
149
150
151
152
153
154
155
156
157
158
159 @Nonnull
160 @ReturnsMutableCopy
161 public static ICommonsList <AbstractSVRLMessage> getAllFailedAssertionsAndSuccessfulReports (@Nullable final SchematronOutputType aSchematronOutput)
162 {
163 final ICommonsList <AbstractSVRLMessage> ret = new CommonsArrayList <> ();
164 if (aSchematronOutput != null)
165 for (final Object aObj : aSchematronOutput.getActivePatternAndFiredRuleAndFailedAssert ())
166 if (aObj instanceof FailedAssert)
167 ret.add (new SVRLFailedAssert ((FailedAssert) aObj));
168 else
169 if (aObj instanceof SuccessfulReport)
170 ret.add (new SVRLSuccessfulReport ((SuccessfulReport) aObj));
171 return ret;
172 }
173
174
175
176
177
178
179
180
181 @Nonnull
182 public static IErrorLevel getErrorLevelFromFailedAssert (@Nonnull final FailedAssert aFailedAssert)
183 {
184 return getErrorLevelDeterminator ().getErrorLevelFromFailedAssert (aFailedAssert);
185 }
186
187
188
189
190
191
192
193
194 @Nonnull
195 public static IErrorLevel getErrorLevelFromSuccessfulReport (@Nonnull final SuccessfulReport aSuccessfulReport)
196 {
197 return getErrorLevelDeterminator ().getErrorLevelFromSuccessfulReport (aSuccessfulReport);
198 }
199
200
201
202
203 @Nonnull
204 public static ISVRLErrorLevelDeterminator getErrorLevelDeterminator ()
205 {
206 return s_aRWLock.readLocked ( () -> s_aELD);
207 }
208
209
210
211
212
213
214
215 public static void setErrorLevelDeterminator (@Nonnull final ISVRLErrorLevelDeterminator aELD)
216 {
217 ValueEnforcer.notNull (aELD, "ErrorLevelDeterminator");
218
219 s_aRWLock.readLocked ( () -> s_aELD = aELD);
220 }
221
222
223
224
225
226
227
228
229
230
231
232
233
234 @Nonnull
235 public static String getBeautifiedLocation (@Nonnull final String sLocation)
236 {
237 String sResult = sLocation;
238
239
240
241 final Matcher aMatcher = RegExHelper.getMatcher ("\\Q*:\\E([a-zA-Z0-9_]+)\\Q[namespace-uri()='\\E([^']+)\\Q']\\E",
242 sResult);
243 while (aMatcher.find ())
244 {
245 final String sLocalName = aMatcher.group (1);
246 final String sNamespaceURI = aMatcher.group (2);
247
248
249
250 final String sBeautified = SVRLLocationBeautifierRegistry.getBeautifiedLocation (sNamespaceURI, sLocalName);
251 if (sBeautified != null)
252 sResult = StringHelper.replaceAll (sResult, aMatcher.group (), sBeautified);
253 }
254 return sResult;
255 }
256 }