001package org.kuali.ole.docstore.common.document;
002
003import org.apache.log4j.Logger;
004import org.kuali.ole.docstore.common.util.ParseXml;
005import org.w3c.dom.Document;
006import org.w3c.dom.Node;
007import org.w3c.dom.NodeList;
008import org.xml.sax.InputSource;
009import org.xml.sax.SAXException;
010
011import javax.xml.bind.JAXBContext;
012import javax.xml.bind.Marshaller;
013import javax.xml.bind.annotation.*;
014import javax.xml.parsers.DocumentBuilder;
015import javax.xml.parsers.DocumentBuilderFactory;
016import javax.xml.parsers.ParserConfigurationException;
017import java.io.IOException;
018import java.io.StringReader;
019import java.io.StringWriter;
020import java.util.ArrayList;
021import java.util.List;
022
023
024/**
025 * <p>Java class for bibTrees complex type.
026 * <p/>
027 * <p>The following schema fragment specifies the expected content contained within this class.
028 * <p/>
029 * <pre>
030 * &lt;complexType name="bibsTrees">
031 *   &lt;complexContent>
032 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
033 *       &lt;sequence>
034 *         &lt;element name="bibTrees" type="{}bibTree" maxOccurs="unbounded" minOccurs="0"/>
035 *       &lt;/sequence>
036 *     &lt;/restriction>
037 *   &lt;/complexContent>
038 * &lt;/complexType>
039 * </pre>
040 */
041@XmlAccessorType(XmlAccessType.FIELD)
042@XmlType(name = "bibTrees", propOrder = {
043        "bibTrees"
044})
045@XmlRootElement(name = "bibDocsTree")
046public class BibTrees {
047
048    private static final Logger LOG = Logger.getLogger(BibTrees.class);
049    @XmlElement(name = "bibDocTree")
050    protected List<BibTree> bibTrees;
051
052    public static String serialize(Object object) {
053        String result = null;
054        StringWriter sw = new StringWriter();
055        BibTrees bibsTrees = (BibTrees) object;
056        try {
057            JAXBContext jaxbContext = JAXBContext.newInstance(BibTrees.class);
058            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
059            jaxbMarshaller.marshal(bibsTrees, sw);
060            result = sw.toString();
061        } catch (Exception e) {
062            LOG.error("Exception :", e);
063        }
064       return result;
065    }
066
067    public static Object deserialize(String bibTreesXml) {
068
069        BibTrees bibTrees = new BibTrees();
070        try {
071            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
072            DocumentBuilder builder = factory.newDocumentBuilder();
073            Document doc = builder.parse(new InputSource(new StringReader(bibTreesXml)));
074            NodeList root = doc.getChildNodes();
075            Node bibTreesNode = ParseXml.getNode("bibDocsTree", root);
076            BibTree bibTree = new BibTree();
077            NodeList nodeList = doc.getElementsByTagName("bibDocTree");
078            for (int i = 0; i < nodeList.getLength(); i++) {
079                Node bibTreeNode = ParseXml.getNodeXml(bibTreesNode.getChildNodes(), i);
080                bibTrees.getBibTrees().add((BibTree) bibTree.deserialize(ParseXml.nodeToString(bibTreeNode)));
081            }
082        } catch (SAXException e) {
083            LOG.error("Exception :", e);
084        } catch (IOException e) {
085            LOG.error("Exception :", e);
086        } catch (ParserConfigurationException e) {
087            LOG.error("Exception :", e);
088        }
089
090
091        return bibTrees;
092    }
093
094    /**
095     * Gets the value of the bibTrees property.
096     * <p/>
097     * <p/>
098     * This accessor method returns a reference to the live list,
099     * not a snapshot. Therefore any modification you make to the
100     * returned list will be present inside the JAXB object.
101     * This is why there is not a <CODE>set</CODE> method for the bibTrees property.
102     * <p/>
103     * <p/>
104     * For example, to add a new item, do as follows:
105     * <pre>
106     *    getBibTrees().add(newItem);
107     * </pre>
108     * <p/>
109     * <p/>
110     * <p/>
111     * Objects of the following type(s) are allowed in the list
112     * {@link BibTree }
113     */
114    public List<BibTree> getBibTrees() {
115        if (bibTrees == null) {
116            bibTrees = new ArrayList<BibTree>();
117        }
118        return this.bibTrees;
119    }
120
121
122}