View Javadoc
1   /**
2    * Copyright (C) 2014-2015 Philip Helger (www.helger.com)
3    * philip[at]helger[dot]com
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *         http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * A single Schematron extends-element.<br>
39   * Abstract rules are named lists of assertions without a context expression.
40   * The required rule attribute references an abstract rule. The current rule
41   * uses all the assertions from the abstract rule it extends.
42   *
43   * @author Philip Helger
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    * @return Reference to a {@link PSRule} element
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 }