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.ValueEnforcer;
23 import com.helger.commons.annotation.ReturnsMutableCopy;
24 import com.helger.commons.error.SingleError;
25 import com.helger.commons.error.SingleError.SingleErrorBuilder;
26 import com.helger.commons.error.level.IErrorLevel;
27 import com.helger.commons.error.list.ErrorList;
28 import com.helger.commons.error.list.IErrorList;
29 import com.helger.commons.error.location.ErrorLocation;
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
44 public abstract class AbstractCollectingPSErrorHandler extends AbstractPSErrorHandler
45 {
46 private final ErrorList m_aErrorList;
47
48 public AbstractCollectingPSErrorHandler (@Nonnull final ErrorList aErrorList,
49 @Nullable final IPSErrorHandler aNestedErrorHandler)
50 {
51 super (aNestedErrorHandler);
52 m_aErrorList = ValueEnforcer.notNull (aErrorList, "ErrorList");
53 }
54
55 @Override
56 protected void handle (@Nullable final IReadableResource aRes,
57 @Nonnull final IErrorLevel aErrorLevel,
58 @Nullable final IPSElement aSourceElement,
59 @Nonnull final String sMessage,
60 @Nullable final Throwable t)
61 {
62 final SingleErrorBuilder aBuilder = SingleError.builder ()
63 .setErrorLevel (aErrorLevel)
64 .setErrorLocation (aRes == null ? null
65 : new ErrorLocation (aRes.getResourceID ()))
66 .setErrorText (sMessage)
67 .setLinkedException (t);
68
69 if (aSourceElement != null)
70 {
71 String sField = ClassHelper.getClassLocalName (aSourceElement);
72 if (aSourceElement instanceof IPSHasID && ((IPSHasID) aSourceElement).hasID ())
73 sField += " [ID=" + ((IPSHasID) aSourceElement).getID () + "]";
74 aBuilder.setErrorFieldName (sField);
75 }
76 m_aErrorList.add (aBuilder.build ());
77 }
78
79 @Nonnull
80 @ReturnsMutableCopy
81 public IErrorList getErrorList ()
82 {
83 return m_aErrorList.getClone ();
84 }
85
86 @Nonnull
87 @ReturnsMutableCopy
88 public IErrorList getAllFailures ()
89 {
90 return m_aErrorList.getAllFailures ();
91 }
92
93 @Nonnull
94 @ReturnsMutableCopy
95 public IErrorList getAllErrors ()
96 {
97 return m_aErrorList.getAllErrors ();
98 }
99
100
101
102
103
104
105
106 @Nonnull
107 public EChange clearResourceErrors ()
108 {
109 return m_aErrorList.clear ();
110 }
111
112
113
114
115
116 public boolean isEmpty ()
117 {
118 return m_aErrorList.isEmpty ();
119 }
120
121 @Override
122 public String toString ()
123 {
124 return ToStringGenerator.getDerived (super.toString ()).appendIfNotNull ("ErrorList", m_aErrorList).getToString ();
125 }
126 }