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.LinkedHashMap;
20 import java.util.Map;
21
22 import javax.annotation.Nonnull;
23 import javax.annotation.Nullable;
24 import javax.annotation.concurrent.NotThreadSafe;
25
26 import com.helger.commons.ValueEnforcer;
27 import com.helger.commons.annotation.ReturnsMutableCopy;
28 import com.helger.commons.collection.CollectionHelper;
29 import com.helger.commons.microdom.IMicroElement;
30 import com.helger.commons.microdom.MicroElement;
31 import com.helger.commons.string.StringHelper;
32 import com.helger.commons.string.ToStringGenerator;
33 import com.helger.schematron.CSchematron;
34 import com.helger.schematron.CSchematronXML;
35 import com.helger.schematron.pure.errorhandler.IPSErrorHandler;
36
37
38
39
40
41
42
43
44
45 @NotThreadSafe
46 public class PSExtends implements IPSElement, IPSHasForeignAttributes
47 {
48 private String m_sRule;
49 private Map <String, String> m_aForeignAttrs;
50
51 public PSExtends ()
52 {}
53
54 public boolean isValid (@Nonnull final IPSErrorHandler aErrorHandler)
55 {
56 if (StringHelper.hasNoText (m_sRule))
57 {
58 aErrorHandler.error (this, "<extends> has no 'rule'");
59 return false;
60 }
61 return true;
62 }
63
64 public void validateCompletely (@Nonnull final IPSErrorHandler aErrorHandler)
65 {
66 if (StringHelper.hasNoText (m_sRule))
67 aErrorHandler.error (this, "<extends> has no 'rule'");
68 }
69
70 public boolean isMinimal ()
71 {
72 return false;
73 }
74
75 public void addForeignAttribute (@Nonnull final String sAttrName, @Nonnull final String sAttrValue)
76 {
77 ValueEnforcer.notNull (sAttrName, "AttrName");
78 ValueEnforcer.notNull (sAttrValue, "AttrValue");
79 if (m_aForeignAttrs == null)
80 m_aForeignAttrs = new LinkedHashMap <String, String> ();
81 m_aForeignAttrs.put (sAttrName, sAttrValue);
82 }
83
84 public void addForeignAttributes (@Nonnull final Map <String, String> aForeignAttrs)
85 {
86 ValueEnforcer.notNull (aForeignAttrs, "ForeignAttrs");
87 for (final Map.Entry <String, String> aEntry : aForeignAttrs.entrySet ())
88 addForeignAttribute (aEntry.getKey (), aEntry.getValue ());
89 }
90
91 public boolean hasForeignAttributes ()
92 {
93 return m_aForeignAttrs != null && !m_aForeignAttrs.isEmpty ();
94 }
95
96 @Nonnull
97 @ReturnsMutableCopy
98 public Map <String, String> getAllForeignAttributes ()
99 {
100 return CollectionHelper.newOrderedMap (m_aForeignAttrs);
101 }
102
103 public void setRule (@Nullable final String sRule)
104 {
105 m_sRule = sRule;
106 }
107
108
109
110
111 @Nullable
112 public String getRule ()
113 {
114 return m_sRule;
115 }
116
117 @Nonnull
118 public IMicroElement getAsMicroElement ()
119 {
120 final IMicroElement ret = new MicroElement (CSchematron.NAMESPACE_SCHEMATRON, CSchematronXML.ELEMENT_EXTENDS);
121 ret.setAttribute (CSchematronXML.ATTR_RULE, m_sRule);
122 if (m_aForeignAttrs != null)
123 for (final Map.Entry <String, String> aEntry : m_aForeignAttrs.entrySet ())
124 ret.setAttribute (aEntry.getKey (), aEntry.getValue ());
125 return ret;
126 }
127
128 @Override
129 public String toString ()
130 {
131 return new ToStringGenerator (this).appendIfNotNull ("rule", m_sRule)
132 .appendIfNotEmpty ("foreignAttrs", m_aForeignAttrs)
133 .toString ();
134 }
135 }