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.basket;
20  
21  import org.jdom2.Document;
22  import org.jdom2.Element;
23  
24  /**
25   * Builds xml representations of a basket and its entries.
26   * 
27   * @author Frank L\u00FCtzenkirchen
28   */
29  public class MCRBasketXMLBuilder {
30  
31      /** If true, the XML data representing the objects in basket entries is included too. */
32      private boolean addContent;
33  
34      /**
35       * Creates a new XML builder. 
36       * 
37       * @param addContent if true, the XML data representing the objects in basket entries is included too.
38       */
39      public MCRBasketXMLBuilder(boolean addContent) {
40          this.addContent = addContent;
41      }
42  
43      /**
44       * Builds an XML representation of a basket entry.
45       * Note that setContent() or resolveContent() must have been called before
46       * if XML content of the basket entry's object should be included.
47       */
48      public Element buildXML(MCRBasketEntry entry) {
49          Element xml = new Element("entry");
50          xml.setAttribute("id", entry.getID());
51          xml.setAttribute("uri", entry.getURI());
52  
53          if (addContent) {
54              Element content = entry.getContent();
55              if (content != null) {
56                  xml.addContent(content.clone());
57              }
58          }
59  
60          String comment = entry.getComment();
61          if (comment != null) {
62              xml.addContent(new Element("comment").setText(comment));
63          }
64  
65          return xml;
66      }
67  
68      /**
69       * Builds an XML representation of a basket and its entries.
70       */
71      public Document buildXML(MCRBasket basket) {
72          Element xml = new Element("basket");
73          xml.setAttribute("type", basket.getType());
74  
75          String derivateID = basket.getDerivateID();
76          if (derivateID != null) {
77              xml.setAttribute("id", derivateID);
78          }
79  
80          for (MCRBasketEntry entry : basket) {
81              xml.addContent(buildXML(entry));
82          }
83          return new Document(xml);
84      }
85  }