View Javadoc
1   package org.kuali.ole.docstore.common.document;
2   
3   import org.apache.log4j.Logger;
4   import org.kuali.ole.docstore.common.util.ParseXml;
5   import org.w3c.dom.Document;
6   import org.w3c.dom.Node;
7   import org.w3c.dom.NodeList;
8   import org.xml.sax.InputSource;
9   import org.xml.sax.SAXException;
10  
11  import java.io.IOException;
12  import java.io.StringReader;
13  import java.io.StringWriter;
14  import java.util.ArrayList;
15  import java.util.List;
16  import javax.xml.bind.JAXBContext;
17  import javax.xml.bind.Marshaller;
18  import javax.xml.bind.annotation.*;
19  import javax.xml.parsers.DocumentBuilder;
20  import javax.xml.parsers.DocumentBuilderFactory;
21  import javax.xml.parsers.ParserConfigurationException;
22  
23  
24  /**
25   * <p>Java class for holdingsTrees complex type.
26   *
27   * <p>The following schema fragment specifies the expected content contained within this class.
28   *
29   * <pre>
30   * &lt;complexType name="holdingsTrees">
31   *   &lt;complexContent>
32   *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
33   *       &lt;sequence>
34   *         &lt;element name="holdingsTrees" type="{}holdingsTree" maxOccurs="unbounded" minOccurs="0"/>
35   *       &lt;/sequence>
36   *     &lt;/restriction>
37   *   &lt;/complexContent>
38   * &lt;/complexType>
39   * </pre>
40   *
41   *
42   */
43  @XmlAccessorType(XmlAccessType.FIELD)
44  @XmlType(name = "holdingsTrees", propOrder = {
45          "holdingsTrees"
46  })
47  
48  @XmlRootElement(name = "holdingsDocsTree")
49  public class HoldingsTrees {
50  
51      private static final Logger LOG = Logger.getLogger(HoldingsTrees.class);
52      @XmlElement(name = "holdingsDocTree")
53      protected List<HoldingsTree> holdingsTrees;
54  
55      /**
56       * Gets the value of the holdingsTrees property.
57       *
58       * <p>
59       * This accessor method returns a reference to the live list,
60       * not a snapshot. Therefore any modification you make to the
61       * returned list will be present inside the JAXB object.
62       * This is why there is not a <CODE>set</CODE> method for the holdingsTrees property.
63       *
64       * <p>
65       * For example, to add a new item, do as follows:
66       * <pre>
67       *    getHoldingsTrees().add(newItem);
68       * </pre>
69       *
70       *
71       * <p>
72       * Objects of the following type(s) are allowed in the list
73       * {@link org.kuali.ole.docstore.common.document.HoldingsTree }
74       *
75       *
76       */
77      public List<HoldingsTree> getHoldingsTrees() {
78          if (holdingsTrees == null) {
79              holdingsTrees = new ArrayList<HoldingsTree>();
80          }
81          return this.holdingsTrees;
82      }
83  
84  
85      public static String serialize(Object object) {
86          String result = null;
87          StringWriter sw = new StringWriter();
88          HoldingsTrees bibsTrees = (HoldingsTrees) object;
89          try {
90              JAXBContext jaxbContext = JAXBContext.newInstance(HoldingsTrees.class);
91              Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
92              jaxbMarshaller.marshal(bibsTrees, sw);
93              result = sw.toString();
94          } catch (Exception e) {
95              LOG.error("Exception ", e);
96          }
97          return result;
98      }
99  
100     public static Object deserialize(String holdingsTreesXml) {
101 
102         HoldingsTrees holdingsTrees = new HoldingsTrees();
103         try {
104             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
105             DocumentBuilder builder = factory.newDocumentBuilder();
106             Document doc = builder.parse(new InputSource(new StringReader(holdingsTreesXml)));
107             NodeList root = doc.getChildNodes();
108             Node holdingsTreesNode = ParseXml.getNode("holdingsDocsTree", root);
109             HoldingsTree holdingsTree = new HoldingsTree();
110             NodeList nodeList = doc.getElementsByTagName("holdingsDocTree");
111             for (int i = 0; i < nodeList.getLength(); i++) {
112                 Node bibTreeNode = ParseXml.getNodeXml(holdingsTreesNode.getChildNodes(),i);
113                 holdingsTrees.getHoldingsTrees().add((HoldingsTree) holdingsTree.deserialize(ParseXml.nodeToString(bibTreeNode)));
114             }
115         } catch (SAXException e) {
116             LOG.error("Exception ", e);
117         } catch (IOException e) {
118             LOG.error("Exception ", e);
119         } catch (ParserConfigurationException e) {
120             LOG.error("Exception ", e);
121         }
122         return holdingsTrees;
123     }
124 
125 }