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