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 javax.annotation.Nonnull;
20  import javax.annotation.concurrent.NotThreadSafe;
21  
22  import com.helger.commons.ValueEnforcer;
23  import com.helger.commons.annotation.Nonempty;
24  import com.helger.commons.annotation.ReturnsMutableCopy;
25  import com.helger.commons.collection.CollectionHelper;
26  import com.helger.commons.collection.ext.CommonsArrayList;
27  import com.helger.commons.collection.ext.ICommonsList;
28  import com.helger.commons.string.ToStringGenerator;
29  import com.helger.schematron.CSchematron;
30  import com.helger.schematron.CSchematronXML;
31  import com.helger.schematron.pure.errorhandler.IPSErrorHandler;
32  import com.helger.xml.microdom.IMicroElement;
33  import com.helger.xml.microdom.MicroElement;
34  
35  /**
36   * A single Schematron title-element.<br>
37   * A summary of the purpose or role of the schema or pattern, for the purpose of
38   * documentation or a rich user interface.<br>
39   * An implementation is not required to make use of this element.
40   *
41   * @author Philip Helger
42   */
43  @NotThreadSafe
44  public class PSTitle implements IPSClonableElement <PSTitle>, IPSOptionalElement, IPSHasMixedContent
45  {
46    private final ICommonsList <Object> m_aContent = new CommonsArrayList <> ();
47  
48    public PSTitle ()
49    {}
50  
51    public boolean isValid (@Nonnull final IPSErrorHandler aErrorHandler)
52    {
53      for (final Object aContent : m_aContent)
54        if (aContent instanceof IPSElement)
55          if (!((IPSElement) aContent).isValid (aErrorHandler))
56            return false;
57      return true;
58    }
59  
60    public void validateCompletely (@Nonnull final IPSErrorHandler aErrorHandler)
61    {
62      for (final Object aContent : m_aContent)
63        if (aContent instanceof IPSElement)
64          ((IPSElement) aContent).validateCompletely (aErrorHandler);
65    }
66  
67    public boolean isMinimal ()
68    {
69      return false;
70    }
71  
72    public void addText (@Nonnull @Nonempty final String sText)
73    {
74      ValueEnforcer.notEmpty (sText, "Text");
75      m_aContent.add (sText);
76    }
77  
78    public boolean hasAnyText ()
79    {
80      return m_aContent.containsAny (e -> e instanceof String);
81    }
82  
83    @Nonnull
84    @ReturnsMutableCopy
85    public ICommonsList <String> getAllTexts ()
86    {
87      return m_aContent.getAllInstanceOf (String.class);
88    }
89  
90    public void addDir (@Nonnull final PSDir aDir)
91    {
92      ValueEnforcer.notNull (aDir, "Dir");
93      m_aContent.add (aDir);
94    }
95  
96    @Nonnull
97    @ReturnsMutableCopy
98    public ICommonsList <PSDir> getAllDirs ()
99    {
100     return m_aContent.getAllInstanceOf (PSDir.class);
101   }
102 
103   /**
104    * @return A list of {@link String} and {@link PSDir} elements.
105    */
106   @Nonnull
107   @ReturnsMutableCopy
108   public ICommonsList <Object> getAllContentElements ()
109   {
110     return m_aContent.getClone ();
111   }
112 
113   @Nonnull
114   public IMicroElement getAsMicroElement ()
115   {
116     final IMicroElement ret = new MicroElement (CSchematron.NAMESPACE_SCHEMATRON, CSchematronXML.ELEMENT_TITLE);
117     for (final Object aContent : m_aContent)
118       if (aContent instanceof String)
119         ret.appendText ((String) aContent);
120       else
121         ret.appendChild (((IPSElement) aContent).getAsMicroElement ());
122     return ret;
123   }
124 
125   @Nonnull
126   public PSTitle getClone ()
127   {
128     final PSTitle ret = new PSTitle ();
129     for (final Object aContent : m_aContent)
130       if (aContent instanceof String)
131         ret.addText ((String) aContent);
132       else
133         if (aContent instanceof PSDir)
134           ret.addDir (((PSDir) aContent).getClone ());
135     return ret;
136   }
137 
138   @Override
139   public String toString ()
140   {
141     return new ToStringGenerator (this).appendIf ("content", m_aContent, CollectionHelper::isNotEmpty).getToString ();
142   }
143 }