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