View Javadoc
1   /**
2    * Copyright (C) 2014-2016 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.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   * A single Schematron extends-element.<br>
40   * Abstract rules are named lists of assertions without a context expression.
41   * The required rule attribute references an abstract rule. The current rule
42   * uses all the assertions from the abstract rule it extends.
43   *
44   * @author Philip Helger
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    * @return Reference to a {@link PSRule} element
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 }