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.frontend.export;
20  
21  import org.jdom2.Document;
22  import org.jdom2.Element;
23  import org.jdom2.Namespace;
24  import org.mycore.common.content.MCRJDOMContent;
25  import org.mycore.common.xml.MCRURIResolver;
26  import org.mycore.frontend.basket.MCRBasket;
27  import org.mycore.frontend.basket.MCRBasketEntry;
28  import org.mycore.frontend.basket.MCRBasketXMLBuilder;
29  
30  /**
31   * Represents a collection of XML data to export.
32   * XML can be added by URI or by JDOM Element, 
33   * or the contents of a complete MCRBasket can be added.
34   * The collected XML data is wrapped by a root element 
35   * thats name and namespace can be set.
36   * 
37   * @author Frank L\u00FCtzenkirchen
38   */
39  public class MCRExportCollection {
40  
41      /** The collection to export */
42      private Element collection;
43  
44      private static MCRBasketXMLBuilder basketBuilder = new MCRBasketXMLBuilder(true);
45  
46      public MCRExportCollection() {
47          collection = new Element("exportCollection");
48          new Document(collection);
49      }
50  
51      /** Sets the name and namespace of the root element that wraps the collected data */
52      public void setRootElement(String elementName, String namespaceURI) {
53          collection.setName(elementName);
54          if ((namespaceURI != null) && (!namespaceURI.isEmpty())) {
55              collection.setNamespace(Namespace.getNamespace(namespaceURI));
56          }
57      }
58  
59      /**
60       * Adds the contents of the given basket.
61       */
62      public void add(MCRBasket basketOfMODS) throws Exception {
63          for (MCRBasketEntry entry : basketOfMODS) {
64              collection.addContent(basketBuilder.buildXML(entry));
65          }
66      }
67  
68      /**
69       * Adds XML data from the given URI.
70       */
71      public void add(String uri) {
72          add(MCRURIResolver.instance().resolve(uri));
73      }
74  
75      /**
76       * Adds the given XML element, making a clone.
77       */
78      public void add(Element element) {
79          collection.addContent(element.clone());
80      }
81  
82      /**
83       * Returns the collected XML data as MCRJDOMContent.
84       */
85      public MCRJDOMContent getContent() {
86          return new MCRJDOMContent(collection.getDocument());
87      }
88  }