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.ArrayList;
22  import java.util.List;
23  import java.util.stream.Collectors;
24  
25  import org.apache.logging.log4j.LogManager;
26  import org.apache.logging.log4j.Logger;
27  import org.jdom2.Content;
28  import org.jdom2.Element;
29  import org.mycore.common.MCRException;
30  import org.mycore.common.xml.MCRXMLHelper;
31  
32  import com.google.gson.JsonArray;
33  import com.google.gson.JsonElement;
34  import com.google.gson.JsonObject;
35  
36  /**
37   * This class implements all method for handling with the MCRMetaLangText part
38   * of a metadata object. The MCRMetaLangText class present a single item, which
39   * has triples of a text and his corresponding language and optional a type.
40   * 
41   * @author Thomas Scheffler (yagee)
42   * @author Jens Kupferschmidt
43   * @author Johannes B\u00fchler
44   * @version $Revision$ $Date$
45   */
46  public class MCRMetaXML extends MCRMetaDefault {
47      List<Content> content;
48  
49      private static final Logger LOGGER = LogManager.getLogger();
50  
51      /**
52       * This is the constructor. <br>
53       * Set the java.util.ArrayList of child elements to new.
54       */
55      public MCRMetaXML() {
56          super();
57      }
58  
59      public MCRMetaXML(String subtag, String type, int inherited) throws MCRException {
60          super(subtag, null, type, inherited);
61      }
62  
63      /**
64       * This method read the XML input stream part from a DOM part for the
65       * metadata of the document.
66       * 
67       * @param element
68       *            a relevant JDOM element for the metadata
69       */
70      @Override
71      public void setFromDOM(Element element) {
72          super.setFromDOM(element);
73          content = element.cloneContent();
74      }
75  
76      public void addContent(Content content) {
77          if (this.content == null) {
78              this.content = new ArrayList<>();
79          }
80  
81          this.content.add(content);
82      }
83  
84      public List<Content> getContent() {
85          return content;
86      }
87  
88      /**
89       * This method create a XML stream for all data in this class, defined by
90       * the MyCoRe XML MCRMetaLangText definition for the given subtag.
91       * 
92       * @exception MCRException
93       *                if the content of this class is not valid
94       * @return a JDOM Element with the XML MCRMetaLangText part
95       */
96      @Override
97      public Element createXML() throws MCRException {
98          Element elm = super.createXML();
99          List<Content> addedContent = new ArrayList<>(content.size());
100         cloneListContent(addedContent, content);
101         elm.addContent(addedContent);
102 
103         return elm;
104     }
105 
106     /**
107      * Creates the JSON representation. Extends the {@link MCRMetaDefault#createJSON()} method
108      * with the following data.
109      * 
110      * <pre>
111      *   {
112      *     content: [
113      *       ... json objects of parsed content ...
114      *     ]
115      *   }
116      * </pre>
117      * 
118      * @see MCRXMLHelper#jsonSerialize(Element)
119      */
120     @Override
121     public JsonObject createJSON() {
122         JsonObject json = super.createJSON();
123         JsonArray jsonContentArray = new JsonArray();
124         getContent().forEach(content -> {
125             JsonElement jsonContent = MCRXMLHelper.jsonSerialize(content);
126             if (jsonContent == null) {
127                 LOGGER.warn("Unable to serialize xml content '{}' to json.", content);
128                 return;
129             }
130             jsonContentArray.add(jsonContent);
131         });
132         json.add("content", jsonContentArray);
133         return json;
134     }
135 
136     private static void cloneListContent(List<Content> dest, List<Content> source) {
137         dest.clear();
138         for (Content c : source) {
139             dest.add(c.clone());
140         }
141     }
142 
143     /**
144      * Validates this MCRMetaXML. This method throws an exception if:
145      * <ul>
146      * <li>the subtag is not null or empty</li>
147      * <li>the lang value was supported</li>
148      * <li>the inherited value is lower than zero</li>
149      * <li>the content is null</li>
150      * </ul>
151      * 
152      * @throws MCRException the MCRMetaXML is invalid
153      */
154     public void validate() throws MCRException {
155         super.validate();
156         if (content == null) {
157             throw new MCRException(getSubTag() + ": content is null or empty");
158         }
159     }
160 
161     /**
162      * This method make a clone of this class.
163      */
164     @Override
165     public MCRMetaXML clone() {
166         MCRMetaXML clone = (MCRMetaXML) super.clone();
167 
168         clone.content = this.content.stream().map(Content::clone).collect(Collectors.toList());
169 
170         return clone;
171     }
172 
173     /**
174      * This method put debug data to the logger (for the debug mode).
175      */
176     @Override
177     public void debug() {
178         if (LOGGER.isDebugEnabled()) {
179             super.debugDefault();
180             LOGGER.debug("Number of contents  = \n{}", content.size());
181         }
182     }
183 
184     @Override
185     public boolean equals(Object obj) {
186         if (!super.equals(obj)) {
187             return false;
188         }
189         final MCRMetaXML other = (MCRMetaXML) obj;
190         return MCRXMLHelper.deepEqual(this.createXML(), other.createXML());
191     }
192 
193 }