1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.helger.schematron.pure.model;
18
19 import java.util.ArrayList;
20 import java.util.LinkedHashMap;
21 import java.util.List;
22 import java.util.Map;
23
24 import javax.annotation.Nonnull;
25 import javax.annotation.Nullable;
26 import javax.annotation.concurrent.NotThreadSafe;
27
28 import com.helger.commons.ValueEnforcer;
29 import com.helger.commons.annotation.ReturnsMutableCopy;
30 import com.helger.commons.collection.CollectionHelper;
31 import com.helger.commons.microdom.IMicroElement;
32 import com.helger.commons.microdom.MicroElement;
33 import com.helger.commons.string.StringHelper;
34 import com.helger.commons.string.ToStringGenerator;
35 import com.helger.schematron.CSchematron;
36 import com.helger.schematron.CSchematronXML;
37 import com.helger.schematron.pure.errorhandler.IPSErrorHandler;
38
39
40
41
42
43
44
45
46 @NotThreadSafe
47 public class PSDiagnostics implements IPSElement, IPSOptionalElement, IPSHasForeignElements, IPSHasIncludes
48 {
49 private final List <PSInclude> m_aIncludes = new ArrayList <PSInclude> ();
50 private final List <PSDiagnostic> m_aDiagnostics = new ArrayList <PSDiagnostic> ();
51 private Map <String, String> m_aForeignAttrs;
52 private List <IMicroElement> m_aForeignElements;
53
54 public PSDiagnostics ()
55 {}
56
57 public boolean isValid (@Nonnull final IPSErrorHandler aErrorHandler)
58 {
59 for (final PSInclude aInclude : m_aIncludes)
60 if (!aInclude.isValid (aErrorHandler))
61 return false;
62 for (final PSDiagnostic aDiagnostic : m_aDiagnostics)
63 if (!aDiagnostic.isValid (aErrorHandler))
64 return false;
65 return true;
66 }
67
68 public void validateCompletely (@Nonnull final IPSErrorHandler aErrorHandler)
69 {
70 for (final PSInclude aInclude : m_aIncludes)
71 aInclude.validateCompletely (aErrorHandler);
72 for (final PSDiagnostic aDiagnostic : m_aDiagnostics)
73 aDiagnostic.validateCompletely (aErrorHandler);
74 }
75
76 public boolean isMinimal ()
77 {
78 return false;
79 }
80
81 public void addForeignElement (@Nonnull final IMicroElement aForeignElement)
82 {
83 ValueEnforcer.notNull (aForeignElement, "ForeignElement");
84 if (aForeignElement.hasParent ())
85 throw new IllegalArgumentException ("ForeignElement already has a parent!");
86 if (m_aForeignElements == null)
87 m_aForeignElements = new ArrayList <IMicroElement> ();
88 m_aForeignElements.add (aForeignElement);
89 }
90
91 public void addForeignElements (@Nonnull final List <IMicroElement> aForeignElements)
92 {
93 ValueEnforcer.notNull (aForeignElements, "ForeignElements");
94 for (final IMicroElement aForeignElement : aForeignElements)
95 addForeignElement (aForeignElement);
96 }
97
98 public boolean hasForeignElements ()
99 {
100 return m_aForeignElements != null && !m_aForeignElements.isEmpty ();
101 }
102
103 @Nonnull
104 @ReturnsMutableCopy
105 public List <IMicroElement> getAllForeignElements ()
106 {
107 return CollectionHelper.newList (m_aForeignElements);
108 }
109
110 public void addForeignAttribute (@Nonnull final String sAttrName, @Nonnull final String sAttrValue)
111 {
112 ValueEnforcer.notNull (sAttrName, "AttrName");
113 ValueEnforcer.notNull (sAttrValue, "AttrValue");
114 if (m_aForeignAttrs == null)
115 m_aForeignAttrs = new LinkedHashMap <String, String> ();
116 m_aForeignAttrs.put (sAttrName, sAttrValue);
117 }
118
119 public void addForeignAttributes (@Nonnull final Map <String, String> aForeignAttrs)
120 {
121 ValueEnforcer.notNull (aForeignAttrs, "ForeignAttrs");
122 for (final Map.Entry <String, String> aEntry : aForeignAttrs.entrySet ())
123 addForeignAttribute (aEntry.getKey (), aEntry.getValue ());
124 }
125
126 public boolean hasForeignAttributes ()
127 {
128 return m_aForeignAttrs != null && !m_aForeignAttrs.isEmpty ();
129 }
130
131 @Nonnull
132 @ReturnsMutableCopy
133 public Map <String, String> getAllForeignAttributes ()
134 {
135 return CollectionHelper.newOrderedMap (m_aForeignAttrs);
136 }
137
138 public void addInclude (@Nonnull final PSInclude aInclude)
139 {
140 ValueEnforcer.notNull (aInclude, "Include");
141 m_aIncludes.add (aInclude);
142 }
143
144 public boolean hasAnyInclude ()
145 {
146 return !m_aIncludes.isEmpty ();
147 }
148
149 @Nonnull
150 @ReturnsMutableCopy
151 public List <PSInclude> getAllIncludes ()
152 {
153 return CollectionHelper.newList (m_aIncludes);
154 }
155
156 public void addDiagnostic (@Nonnull final PSDiagnostic aDiagnostic)
157 {
158 ValueEnforcer.notNull (aDiagnostic, "Diagnostic");
159 m_aDiagnostics.add (aDiagnostic);
160 }
161
162 @Nullable
163 public PSDiagnostic getDiagnosticOfID (@Nullable final String sID)
164 {
165 if (StringHelper.hasText (sID))
166 for (final PSDiagnostic aDiagnostic : m_aDiagnostics)
167 if (sID.equals (aDiagnostic.getID ()))
168 return aDiagnostic;
169 return null;
170 }
171
172 @Nonnull
173 @ReturnsMutableCopy
174 public List <PSDiagnostic> getAllDiagnostics ()
175 {
176 return CollectionHelper.newList (m_aDiagnostics);
177 }
178
179 @Nonnull
180 public IMicroElement getAsMicroElement ()
181 {
182 final IMicroElement ret = new MicroElement (CSchematron.NAMESPACE_SCHEMATRON, CSchematronXML.ELEMENT_DIAGNOSTICS);
183 if (m_aForeignElements != null)
184 for (final IMicroElement aForeignElement : m_aForeignElements)
185 ret.appendChild (aForeignElement.getClone ());
186 for (final PSInclude aInclude : m_aIncludes)
187 ret.appendChild (aInclude.getAsMicroElement ());
188 for (final PSDiagnostic aDiagnostic : m_aDiagnostics)
189 ret.appendChild (aDiagnostic.getAsMicroElement ());
190 if (m_aForeignAttrs != null)
191 for (final Map.Entry <String, String> aEntry : m_aForeignAttrs.entrySet ())
192 ret.setAttribute (aEntry.getKey (), aEntry.getValue ());
193 return ret;
194 }
195
196 @Override
197 public String toString ()
198 {
199 return new ToStringGenerator (this).appendIfNotEmpty ("includes", m_aIncludes)
200 .appendIfNotEmpty ("diagnostics", m_aDiagnostics)
201 .appendIfNotEmpty ("foreignAttrs", m_aForeignAttrs)
202 .appendIfNotEmpty ("foreignElements", m_aForeignElements)
203 .toString ();
204 }
205 }