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