1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.helger.schematron.pure.errorhandler;
18
19 import javax.annotation.Nonnull;
20 import javax.annotation.Nullable;
21
22 import com.helger.commons.annotation.ReturnsMutableCopy;
23 import com.helger.commons.collection.ext.ICommonsList;
24 import com.helger.commons.error.IErrorLevel;
25 import com.helger.commons.error.IResourceError;
26 import com.helger.commons.error.IResourceErrorGroup;
27 import com.helger.commons.error.ResourceError;
28 import com.helger.commons.error.ResourceErrorGroup;
29 import com.helger.commons.error.ResourceLocation;
30 import com.helger.commons.io.resource.IReadableResource;
31 import com.helger.commons.lang.ClassHelper;
32 import com.helger.commons.state.EChange;
33 import com.helger.commons.string.ToStringGenerator;
34 import com.helger.schematron.pure.model.IPSElement;
35 import com.helger.schematron.pure.model.IPSHasID;
36
37
38
39
40
41
42
43 public class CollectingPSErrorHandler extends AbstractPSErrorHandler
44 {
45 private final ResourceErrorGroup m_aErrors = new ResourceErrorGroup ();
46
47 public CollectingPSErrorHandler ()
48 {
49 super ();
50 }
51
52 public CollectingPSErrorHandler (@Nullable final IPSErrorHandler aNestedErrorHandler)
53 {
54 super (aNestedErrorHandler);
55 }
56
57 @Override
58 protected void handle (@Nullable final IReadableResource aRes,
59 @Nonnull final IErrorLevel aErrorLevel,
60 @Nullable final IPSElement aSourceElement,
61 @Nonnull final String sMessage,
62 @Nullable final Throwable t)
63 {
64 String sField = "";
65 if (aSourceElement != null)
66 {
67 sField += ClassHelper.getClassLocalName (aSourceElement);
68 if (aSourceElement instanceof IPSHasID && ((IPSHasID) aSourceElement).hasID ())
69 sField += " [ID=" + ((IPSHasID) aSourceElement).getID () + "]";
70 }
71 final ResourceLocation aLocation = new ResourceLocation (aRes == null ? null : aRes.getResourceID (), sField);
72 m_aErrors.addResourceError (new ResourceError (aLocation, aErrorLevel, sMessage, t));
73 }
74
75 @Nonnull
76 @ReturnsMutableCopy
77 public IResourceErrorGroup getResourceErrors ()
78 {
79 return m_aErrors.getClone ();
80 }
81
82 @Nonnull
83 public ICommonsList <IResourceError> getAllResourceErrors ()
84 {
85 return m_aErrors.getAllResourceErrors ();
86 }
87
88 @Nonnull
89 @ReturnsMutableCopy
90 public IResourceErrorGroup getAllFailures ()
91 {
92 return m_aErrors.getAllFailures ();
93 }
94
95 @Nonnull
96 @ReturnsMutableCopy
97 public IResourceErrorGroup getAllErrors ()
98 {
99 return m_aErrors.getAllErrors ();
100 }
101
102
103
104
105
106
107
108 @Nonnull
109 public EChange clearResourceErrors ()
110 {
111 return m_aErrors.clear ();
112 }
113
114
115
116
117
118 public boolean isEmpty ()
119 {
120 return m_aErrors.isEmpty ();
121 }
122
123 @Override
124 public String toString ()
125 {
126 return ToStringGenerator.getDerived (super.toString ()).appendIfNotNull ("errors", m_aErrors).toString ();
127 }
128 }