View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.datamodel.metadata;
20  
21  import java.util.Objects;
22  
23  import org.apache.logging.log4j.LogManager;
24  import org.apache.logging.log4j.Logger;
25  import org.jdom2.Element;
26  import org.mycore.common.MCRException;
27  import org.mycore.common.MCRUtils;
28  
29  import com.google.gson.JsonObject;
30  
31  /**
32   * This class implements all method for handling with the MCRMetaLangText part
33   * of a metadata object. The MCRMetaLangText class present a single item, which
34   * has triples of a text and his corresponding language and optional a type.
35   * 
36   * @author Jens Kupferschmidt
37   * @version $Revision$ $Date$
38   */
39  public class MCRMetaLangText extends MCRMetaDefault {
40  
41      protected String text;
42  
43      protected String form;
44  
45      private static final Logger LOGGER = LogManager.getLogger();
46  
47      /**
48       * This is the constructor. <br>
49       * The language element was set to <b>en </b>. All other elements was set to
50       * an empty string. The <em>form</em> Attribute is set to 'plain'.
51       */
52      public MCRMetaLangText() {
53          super();
54          text = "";
55          form = "plain";
56      }
57  
58      /**
59       * This is the constructor. <br>
60       * The language element was set. If the value of <em>lang</em> is
61       * null, empty or false <b>en </b> was set. The subtag element was set to
62       * the value of <em>subtag</em>. If the value of <em>subtag</em>
63       * is null or empty an exception was thrown. The type element was set to
64       * the value of <em>type</em>, if it is null, an empty string was set
65       * to the type element. The text element was set to the value of
66       * <em>text</em>, if it is null, an empty string was set
67       * to the text element.
68       * @param subtag       the name of the subtag
69       * @param lang     the default language
70       * @param type         the optional type string
71       * @param inherted     a value &gt;= 0
72       * @param form         the format string, if it is empty 'plain' is set.
73       * @param text         the text string
74       *
75       * @exception MCRException if the subtag value is null or empty
76       */
77      public MCRMetaLangText(String subtag, String lang, String type, int inherted, String form, String text)
78          throws MCRException {
79          super(subtag, lang, type, inherted);
80          this.text = "";
81  
82          if (text != null) {
83              this.text = text.trim();
84          }
85  
86          this.form = "plain";
87  
88          if (form != null) {
89              this.form = form.trim();
90          }
91      }
92  
93      /**
94       * This method set the language, type and text.
95       * 
96       * @param lang
97       *            the new language string, if this is null or empty, nothing is
98       *            to do
99       * @param type
100      *            the optional type string
101      * @param text
102      *            the new text string
103      */
104     public final void set(String lang, String type, String form, String text) {
105         setLang(lang);
106         setType(type);
107 
108         if (text != null) {
109             this.text = text.trim();
110         }
111 
112         this.form = "plain";
113 
114         if (form != null) {
115             this.form = form.trim();
116         }
117     }
118 
119     /**
120      * This method set the text.
121      * 
122      * @param text
123      *            the new text string
124      */
125     public final void setText(String text) {
126         if (text != null) {
127             this.text = text.trim();
128         }
129     }
130 
131     /**
132      * This method set the form attribute.
133      * 
134      * @param form
135      *            the new form string
136      */
137     public final void setForm(String form) {
138         if (form != null) {
139             text = form.trim();
140         }
141     }
142 
143     /**
144      * This method get the text element.
145      * 
146      * @return the text
147      */
148     public final String getText() {
149         return text;
150     }
151 
152     /**
153      * This method get the form attribute.
154      * 
155      * @return the form attribute
156      */
157     public final String getForm() {
158         return form;
159     }
160 
161     /**
162      * This method read the XML input stream part from a DOM part for the
163      * metadata of the document.
164      * 
165      * @param element
166      *            a relevant JDOM element for the metadata
167      */
168     @Override
169     public void setFromDOM(Element element) {
170         super.setFromDOM(element);
171 
172         String tempText = element.getText();
173 
174         if (tempText == null) {
175             tempText = "";
176         }
177 
178         text = tempText.trim();
179 
180         String tempForm = element.getAttributeValue("form");
181 
182         if (tempForm == null) {
183             tempForm = "plain";
184         }
185 
186         form = tempForm.trim();
187     }
188 
189     /**
190      * This method create a XML stream for all data in this class, defined by
191      * the MyCoRe XML MCRMetaLangText definition for the given subtag.
192      * 
193      * @exception MCRException
194      *                if the content of this class is not valid
195      * @return a JDOM Element with the XML MCRMetaLangText part
196      */
197     @Override
198     public Element createXML() throws MCRException {
199         Element elm = super.createXML();
200         MCRUtils.filterTrimmedNotEmpty(form)
201             .ifPresent(s -> elm.setAttribute("form", s));
202         elm.addContent(text);
203 
204         return elm;
205     }
206 
207     /**
208      * Creates the JSON representation. Extends the {@link MCRMetaDefault#createJSON()} method
209      * with the following data.
210      * 
211      * <pre>
212      *   {
213      *     text: "Hallo Welt",
214      *     form: "plain"
215      *   }
216      * </pre>
217      * 
218      */
219     @Override
220     public JsonObject createJSON() {
221         JsonObject obj = super.createJSON();
222         if (getForm() != null) {
223             obj.addProperty("form", getForm());
224         }
225         obj.addProperty("text", getText());
226         return obj;
227     }
228 
229     /**
230      * Validates this MCRMetaLangText. This method throws an exception if:
231      * <ul>
232      * <li>the subtag is not null or empty</li>
233      * <li>the lang value was supported</li>
234      * <li>the inherited value is lower than zero</li>
235      * <li>the trimmed text is null or empty</li>
236      * </ul>
237      * 
238      * @throws MCRException the MCRMetaLangText is invalid
239      */
240     public void validate() throws MCRException {
241         super.validate();
242         text = MCRUtils.filterTrimmedNotEmpty(text)
243             .orElseThrow(() -> new MCRException(getSubTag() + ": text is null or empty"));
244         form = MCRUtils.filterTrimmedNotEmpty(form)
245             .orElse("plain");
246     }
247 
248     /**
249      * clone of this instance
250      * 
251      * you will get a (deep) clone of this element
252      * 
253      * @see java.lang.Object#clone()
254      */
255     @Override
256     public MCRMetaLangText clone() {
257         MCRMetaLangText clone = (MCRMetaLangText) super.clone();
258 
259         clone.form = this.form;
260         clone.text = this.text;
261 
262         return clone;
263     }
264 
265     /**
266      * This method put debug data to the logger (for the debug mode).
267      */
268     @Override
269     public final void debug() {
270         if (LOGGER.isDebugEnabled()) {
271             super.debugDefault();
272             LOGGER.debug("Format             = {}", form);
273             LOGGER.debug("Text               = {}", text);
274             LOGGER.debug(" ");
275         }
276     }
277 
278     /**
279      * Check the equivalence between this instance and the given object.
280      * 
281      * @param obj the MCRMetaLangText object
282      * @return true if its equal
283      */
284     @Override
285     public boolean equals(Object obj) {
286         if (!super.equals(obj)) {
287             return false;
288         }
289         final MCRMetaLangText other = (MCRMetaLangText) obj;
290         return Objects.equals(this.text, other.text) && Objects.equals(this.form, other.form);
291     }
292 }