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