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.Nullable;
21  import javax.annotation.concurrent.NotThreadSafe;
22  
23  import com.helger.commons.ValueEnforcer;
24  import com.helger.commons.annotation.Nonempty;
25  import com.helger.commons.annotation.ReturnsMutableCopy;
26  import com.helger.commons.collection.CollectionHelper;
27  import com.helger.commons.collection.ext.CommonsArrayList;
28  import com.helger.commons.collection.ext.ICommonsList;
29  import com.helger.commons.string.StringHelper;
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 emph-element.<br>
39   * A portion of text that should be rendered with some emphasis.<br>
40   * An implementation is not required to make use of this element.
41   *
42   * @author Philip Helger
43   */
44  @NotThreadSafe
45  public class PSEmph implements IPSClonableElement <PSEmph>, IPSOptionalElement, IPSHasTexts
46  {
47    private final ICommonsList <String> m_aContent = new CommonsArrayList <> ();
48  
49    public PSEmph ()
50    {}
51  
52    public boolean isValid (@Nonnull final IPSErrorHandler aErrorHandler)
53    {
54      if (m_aContent.isEmpty ())
55      {
56        aErrorHandler.error (this, "<emph> has no content");
57        return false;
58      }
59      return true;
60    }
61  
62    public void validateCompletely (@Nonnull final IPSErrorHandler aErrorHandler)
63    {
64      if (m_aContent.isEmpty ())
65        aErrorHandler.error (this, "<emph> has no content");
66    }
67  
68    public boolean isMinimal ()
69    {
70      return true;
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      return m_aContent.isNotEmpty ();
82    }
83  
84    @Nonnull
85    @ReturnsMutableCopy
86    public ICommonsList <String> getAllTexts ()
87    {
88      return m_aContent.getClone ();
89    }
90  
91    @Nullable
92    public String getAsText ()
93    {
94      return StringHelper.getImploded (m_aContent);
95    }
96  
97    @Nonnull
98    public IMicroElement getAsMicroElement ()
99    {
100     final IMicroElement ret = new MicroElement (CSchematron.NAMESPACE_SCHEMATRON, CSchematronXML.ELEMENT_EMPH);
101     for (final String sContent : m_aContent)
102       ret.appendText (sContent);
103     return ret;
104   }
105 
106   @Nonnull
107   public PSEmph getClone ()
108   {
109     final PSEmph ret = new PSEmph ();
110     for (final String sContent : m_aContent)
111       ret.addText (sContent);
112     return ret;
113   }
114 
115   @Override
116   public String toString ()
117   {
118     return new ToStringGenerator (this).appendIf ("content", m_aContent, CollectionHelper::isNotEmpty).getToString ();
119   }
120 }