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 javax.annotation.Nonnull;
20  import javax.annotation.Nullable;
21  import javax.annotation.concurrent.NotThreadSafe;
22  
23  import com.helger.commons.microdom.IMicroElement;
24  import com.helger.commons.microdom.MicroElement;
25  import com.helger.commons.string.StringHelper;
26  import com.helger.commons.string.ToStringGenerator;
27  import com.helger.schematron.CSchematron;
28  import com.helger.schematron.CSchematronXML;
29  import com.helger.schematron.pure.errorhandler.IPSErrorHandler;
30  
31  /**
32   * A single Schematron param-element.<br>
33   * A name-value pair providing parameters for an abstract pattern. The required
34   * name attribute is an XML name with no colon. The required value attribute is
35   * a fragment of a query.
36   *
37   * @author Philip Helger
38   */
39  @NotThreadSafe
40  public class PSParam implements IPSElement
41  {
42    private String m_sName;
43    private String m_sValue;
44  
45    public PSParam ()
46    {}
47  
48    public boolean isValid (@Nonnull final IPSErrorHandler aErrorHandler)
49    {
50      if (StringHelper.hasNoText (m_sName))
51      {
52        aErrorHandler.error (this, "<param> has no 'name'");
53        return false;
54      }
55      if (StringHelper.hasNoText (m_sValue))
56      {
57        aErrorHandler.error (this, "<param> has no 'value'");
58        return false;
59      }
60      return true;
61    }
62  
63    public void validateCompletely (@Nonnull final IPSErrorHandler aErrorHandler)
64    {
65      if (StringHelper.hasNoText (m_sName))
66        aErrorHandler.error (this, "<param> has no 'name'");
67      if (StringHelper.hasNoText (m_sValue))
68        aErrorHandler.error (this, "<param> has no 'value'");
69    }
70  
71    public boolean isMinimal ()
72    {
73      return false;
74    }
75  
76    public void setName (@Nullable final String sName)
77    {
78      m_sName = sName;
79    }
80  
81    @Nullable
82    public String getName ()
83    {
84      return m_sName;
85    }
86  
87    public void setValue (@Nullable final String sValue)
88    {
89      if (sValue != null && sValue.length () == 0)
90        throw new IllegalArgumentException ("value may not be empty!");
91      m_sValue = sValue;
92    }
93  
94    @Nullable
95    public String getValue ()
96    {
97      return m_sValue;
98    }
99  
100   @Nonnull
101   public IMicroElement getAsMicroElement ()
102   {
103     final IMicroElement ret = new MicroElement (CSchematron.NAMESPACE_SCHEMATRON, CSchematronXML.ELEMENT_PARAM);
104     ret.setAttribute (CSchematronXML.ATTR_NAME, m_sName);
105     ret.setAttribute (CSchematronXML.ATTR_VALUE, m_sValue);
106     return ret;
107   }
108 
109   @Override
110   public String toString ()
111   {
112     return new ToStringGenerator (this).appendIfNotNull ("name", m_sName)
113                                        .appendIfNotNull ("value", m_sValue)
114                                        .toString ();
115   }
116 }