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.ToStringGenerator;
32  import com.helger.schematron.CSchematron;
33  import com.helger.schematron.CSchematronXML;
34  import com.helger.schematron.pure.errorhandler.IPSErrorHandler;
35  
36  /**
37   * A single Schematron name-element.<br>
38   * Provides the names of nodes from the instance document to allow clearer
39   * assertions and diagnostics. The optional path attribute is an expression
40   * evaluated in the current context that returns a string that is the name of a
41   * node. In the latter case, the name of the node is used.<br>
42   * An implementation which does not report natural-language assertions is not
43   * required to make use of this element.
44   *
45   * @author Philip Helger
46   */
47  @NotThreadSafe
48  public class PSName implements IPSClonableElement <PSName>, IPSHasForeignAttributes
49  {
50    private String m_sPath;
51    private Map <String, String> m_aForeignAttrs;
52  
53    public PSName ()
54    {}
55  
56    public boolean isValid (@Nonnull final IPSErrorHandler aErrorHandler)
57    {
58      return true;
59    }
60  
61    public void validateCompletely (@Nonnull final IPSErrorHandler aErrorHandler)
62    {
63      // Nothing to do
64    }
65  
66    public boolean isMinimal ()
67    {
68      return true;
69    }
70  
71    public void addForeignAttribute (@Nonnull final String sAttrName, @Nonnull final String sAttrValue)
72    {
73      ValueEnforcer.notNull (sAttrName, "AttrName");
74      ValueEnforcer.notNull (sAttrValue, "AttrValue");
75      if (m_aForeignAttrs == null)
76        m_aForeignAttrs = new LinkedHashMap <String, String> ();
77      m_aForeignAttrs.put (sAttrName, sAttrValue);
78    }
79  
80    public void addForeignAttributes (@Nonnull final Map <String, String> aForeignAttrs)
81    {
82      ValueEnforcer.notNull (aForeignAttrs, "ForeignAttrs");
83      for (final Map.Entry <String, String> aEntry : aForeignAttrs.entrySet ())
84        addForeignAttribute (aEntry.getKey (), aEntry.getValue ());
85    }
86  
87    public boolean hasForeignAttributes ()
88    {
89      return m_aForeignAttrs != null && !m_aForeignAttrs.isEmpty ();
90    }
91  
92    @Nonnull
93    @ReturnsMutableCopy
94    public Map <String, String> getAllForeignAttributes ()
95    {
96      return CollectionHelper.newOrderedMap (m_aForeignAttrs);
97    }
98  
99    /**
100    * @param sPath
101    *        The path to use. May be <code>null</code>.
102    */
103   public void setPath (@Nullable final String sPath)
104   {
105     m_sPath = sPath;
106   }
107 
108   /**
109    * @return <code>true</code> if a path is specified, <code>false</code>
110    *         otherwise.
111    */
112   public boolean hasPath ()
113   {
114     return m_sPath != null;
115   }
116 
117   /**
118    * @return The optional path to use.
119    */
120   @Nullable
121   public String getPath ()
122   {
123     return m_sPath;
124   }
125 
126   @Nonnull
127   public IMicroElement getAsMicroElement ()
128   {
129     final IMicroElement ret = new MicroElement (CSchematron.NAMESPACE_SCHEMATRON, CSchematronXML.ELEMENT_NAME);
130     ret.setAttribute (CSchematronXML.ATTR_PATH, m_sPath);
131     if (m_aForeignAttrs != null)
132       for (final Map.Entry <String, String> aEntry : m_aForeignAttrs.entrySet ())
133         ret.setAttribute (aEntry.getKey (), aEntry.getValue ());
134     return ret;
135   }
136 
137   @Nonnull
138   public PSName getClone ()
139   {
140     final PSName ret = new PSName ();
141     ret.setPath (m_sPath);
142     if (hasForeignAttributes ())
143       ret.addForeignAttributes (m_aForeignAttrs);
144     return ret;
145   }
146 
147   @Override
148   public String toString ()
149   {
150     return new ToStringGenerator (this).appendIfNotNull ("path", m_sPath)
151                                        .appendIfNotEmpty ("foreignAttrs", m_aForeignAttrs)
152                                        .toString ();
153   }
154 }