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 include-element.<br>
33   * The required href attribute references an external well-formed XML document
34   * whose document element is a Schematron element of a type which is allowed by
35   * the grammar for Schematron at the current position in the schema. The
36   * external document is inserted in place of the include element.
37   *
38   * @author Philip Helger
39   */
40  @NotThreadSafe
41  public class PSInclude implements IPSElement
42  {
43    private String m_sHref;
44  
45    public PSInclude ()
46    {}
47  
48    public boolean isValid (@Nonnull final IPSErrorHandler aErrorHandler)
49    {
50      if (StringHelper.hasNoText (m_sHref))
51      {
52        aErrorHandler.error (this, "<include> has no 'href'");
53        return false;
54      }
55      return true;
56    }
57  
58    public void validateCompletely (@Nonnull final IPSErrorHandler aErrorHandler)
59    {
60      if (StringHelper.hasNoText (m_sHref))
61        aErrorHandler.error (this, "<include> has no 'href'");
62    }
63  
64    public boolean isMinimal ()
65    {
66      return false;
67    }
68  
69    /**
70     * @param sHref
71     *        The path to the object to include. May be <code>null</code>.
72     */
73    public void setHref (@Nullable final String sHref)
74    {
75      m_sHref = sHref;
76    }
77  
78    /**
79     * @return The path to the object to include. May be <code>null</code>.
80     */
81    @Nullable
82    public String getHref ()
83    {
84      return m_sHref;
85    }
86  
87    @Nonnull
88    public IMicroElement getAsMicroElement ()
89    {
90      final IMicroElement ret = new MicroElement (CSchematron.NAMESPACE_SCHEMATRON, CSchematronXML.ELEMENT_INCLUDE);
91      ret.setAttribute (CSchematronXML.ATTR_HREF, m_sHref);
92      return ret;
93    }
94  
95    @Override
96    public String toString ()
97    {
98      return new ToStringGenerator (this).appendIfNotNull ("href", m_sHref).toString ();
99    }
100 }