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