View Javadoc
1   package org.kuali.ole.docstore.common.document;
2   
3   import org.apache.commons.lang3.StringUtils;
4   import org.apache.log4j.Logger;
5   import org.kuali.ole.docstore.common.util.ParseXml;
6   import org.w3c.dom.Document;
7   import org.w3c.dom.Node;
8   import org.w3c.dom.NodeList;
9   import org.xml.sax.InputSource;
10  import org.xml.sax.SAXException;
11  
12  import javax.xml.bind.JAXBContext;
13  import javax.xml.bind.Marshaller;
14  import javax.xml.bind.annotation.*;
15  import javax.xml.parsers.DocumentBuilder;
16  import javax.xml.parsers.DocumentBuilderFactory;
17  import javax.xml.parsers.ParserConfigurationException;
18  import java.io.IOException;
19  import java.io.StringReader;
20  import java.io.StringWriter;
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  
25  /**
26   * <p>Java class for bibTree complex type.
27   * <p/>
28   * <p>The following schema fragment specifies the expected content contained within this class.
29   * <p/>
30   * <pre>
31   * &lt;complexType name="bibTree">
32   *   &lt;complexContent>
33   *     &lt;extension base="{}docstoreDocument">
34   *       &lt;sequence>
35   *         &lt;element name="holdingsTrees" type="{}holdingsTree" maxOccurs="unbounded" minOccurs="0"/>
36   *         &lt;element name="bib" type="{}bib" minOccurs="0"/>
37   *       &lt;/sequence>
38   *     &lt;/extension>
39   *   &lt;/complexContent>
40   * &lt;/complexType>
41   * </pre>
42   */
43  @XmlAccessorType(XmlAccessType.FIELD)
44  @XmlType(name = "bibTree", propOrder = {
45          "holdingsTrees",
46          "bib"
47  })
48  
49  @XmlRootElement(name = "bibDocTree")
50  public class BibTree
51          extends DocstoreDocument {
52  
53      private static final Logger LOG = Logger.getLogger(BibTree.class);
54      // XmLElementWrapper generates a wrapper element around XML representation
55      @XmlElementWrapper(name = "holdingsDocsTree")
56      // XmlElement sets the name of the entities
57      @XmlElement(name = "holdingsDocTree")
58      protected List<HoldingsTree> holdingsTrees;
59  
60      @XmlElement(name = "bibDoc")
61      protected Bib bib;
62  
63      /**
64       * Gets the value of the holdingsTrees property.
65       * <p/>
66       * <p/>
67       * This accessor method returns a reference to the live list,
68       * not a snapshot. Therefore any modification you make to the
69       * returned list will be present inside the JAXB object.
70       * This is why there is not a <CODE>set</CODE> method for the holdingsTrees property.
71       * <p/>
72       * <p/>
73       * For example, to add a new item, do as follows:
74       * <pre>
75       *    getHoldingsTrees().add(newItem);
76       * </pre>
77       * <p/>
78       * <p/>
79       * <p/>
80       * Objects of the following type(s) are allowed in the list
81       * {@link org.kuali.ole.docstore.common.document.HoldingsTree }
82       */
83      public List<HoldingsTree> getHoldingsTrees() {
84          if (holdingsTrees == null) {
85              holdingsTrees = new ArrayList<HoldingsTree>();
86          }
87          return this.holdingsTrees;
88      }
89  
90      /**
91       * Gets the value of the bib property.
92       *
93       * @return possible object is
94       *         {@link org.kuali.ole.docstore.common.document.Bib }
95       */
96      public Bib getBib() {
97          return bib;
98      }
99  
100     /**
101      * Sets the value of the bib property.
102      *
103      * @param value allowed object is
104      *              {@link org.kuali.ole.docstore.common.document.Bib }
105      */
106     public void setBib(Bib value) {
107         this.bib = value;
108     }
109 
110     @Override
111     public String serialize(Object object) {
112         String result = null;
113         StringWriter sw = new StringWriter();
114         BibTree bibTree = (BibTree) object;
115         try {
116             JAXBContext jaxbContext = JAXBContext.newInstance(BibTree.class);
117             Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
118             jaxbMarshaller.marshal(bibTree, sw);
119             result = sw.toString();
120         } catch (Exception e) {
121             LOG.error("Exception :", e);
122         }
123         return result;
124     }
125 
126     @Override
127     public Object deserialize(String bibTreeXml) {
128 
129         BibTree bibTree = new BibTree();
130         try {
131             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
132             DocumentBuilder builder = factory.newDocumentBuilder();
133             Document doc = builder.parse(new InputSource(new StringReader(bibTreeXml)));
134             NodeList root = doc.getChildNodes();
135             Node bibTreeNode = ParseXml.getNode("bibDocTree", root);
136             Node bibNode = ParseXml.getNode("bibDoc", bibTreeNode.getChildNodes());
137             String BibXml = ParseXml.nodeToString(bibNode);
138             Bib bib = new Bib();
139             bib = (Bib) bib.deserialize(BibXml);
140             bibTree.setBib(bib);
141             Node holdingsTreesNode = ParseXml.getNode("holdingsDocsTree", bibTreeNode.getChildNodes());
142             String holdingsTreeXml = null;
143             HoldingsTree holdingsTree = new HoldingsTree();
144             NodeList nodeList = doc.getElementsByTagName("holdingsDocTree");
145             for (int i = 0; i < nodeList.getLength(); i++) {
146                 Node holdingsTreeNode = ParseXml.getNode("holdingsDocTree", holdingsTreesNode.getChildNodes(), i);
147                 holdingsTreeXml = ParseXml.nodeToString(holdingsTreeNode);
148                 bibTree.getHoldingsTrees().add((HoldingsTree) holdingsTree.deserialize(holdingsTreeXml));
149             }
150         } catch (SAXException e) {
151             LOG.error("Exception :", e);
152         } catch (IOException e) {
153             LOG.error("Exception :", e);
154         } catch (ParserConfigurationException e) {
155             LOG.error("Exception :", e);
156         }
157 
158 
159         return bibTree;
160     }
161 
162     @Override
163     public Object deserializeContent(Object object) {
164         return null;  //To change body of implemented methods use File | Settings | File Templates.
165     }
166 
167     @Override
168     public Object deserializeContent(String content) {
169         return null;  //To change body of implemented methods use File | Settings | File Templates.
170     }
171 
172     @Override
173     public String serializeContent(Object object) {
174         return null;  //To change body of implemented methods use File | Settings | File Templates.
175     }
176 }