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 java.io.IOException; 012import java.io.StringReader; 013import java.io.StringWriter; 014import java.util.ArrayList; 015import java.util.List; 016import javax.xml.bind.JAXBContext; 017import javax.xml.bind.Marshaller; 018import javax.xml.bind.annotation.*; 019import javax.xml.parsers.DocumentBuilder; 020import javax.xml.parsers.DocumentBuilderFactory; 021import javax.xml.parsers.ParserConfigurationException; 022 023 024/** 025 * <p>Java class for holdingsTrees complex type. 026 * 027 * <p>The following schema fragment specifies the expected content contained within this class. 028 * 029 * <pre> 030 * <complexType name="holdingsTrees"> 031 * <complexContent> 032 * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> 033 * <sequence> 034 * <element name="holdingsTrees" type="{}holdingsTree" maxOccurs="unbounded" minOccurs="0"/> 035 * </sequence> 036 * </restriction> 037 * </complexContent> 038 * </complexType> 039 * </pre> 040 * 041 * 042 */ 043@XmlAccessorType(XmlAccessType.FIELD) 044@XmlType(name = "holdingsTrees", propOrder = { 045 "holdingsTrees" 046}) 047 048@XmlRootElement 049public class HoldingsTrees { 050 051 private static final Logger LOG = Logger.getLogger(HoldingsTrees.class); 052 @XmlElement(name = "holdingsTree") 053 protected List<HoldingsTree> holdingsTrees; 054 055 /** 056 * Gets the value of the holdingsTrees property. 057 * 058 * <p> 059 * This accessor method returns a reference to the live list, 060 * not a snapshot. Therefore any modification you make to the 061 * returned list will be present inside the JAXB object. 062 * This is why there is not a <CODE>set</CODE> method for the holdingsTrees property. 063 * 064 * <p> 065 * For example, to add a new item, do as follows: 066 * <pre> 067 * getHoldingsTrees().add(newItem); 068 * </pre> 069 * 070 * 071 * <p> 072 * Objects of the following type(s) are allowed in the list 073 * {@link org.kuali.ole.docstore.common.document.HoldingsTree } 074 * 075 * 076 */ 077 public List<HoldingsTree> getHoldingsTrees() { 078 if (holdingsTrees == null) { 079 holdingsTrees = new ArrayList<HoldingsTree>(); 080 } 081 return this.holdingsTrees; 082 } 083 084 085 public static String serialize(Object object) { 086 String result = null; 087 StringWriter sw = new StringWriter(); 088 HoldingsTrees bibsTrees = (HoldingsTrees) object; 089 try { 090 JAXBContext jaxbContext = JAXBContext.newInstance(HoldingsTrees.class); 091 Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); 092 jaxbMarshaller.marshal(bibsTrees, sw); 093 result = sw.toString(); 094 } catch (Exception e) { 095 LOG.error("Exception ", e); 096 } 097 return result; 098 } 099 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("holdingsTrees", root); 109 HoldingsTree holdingsTree = new HoldingsTree(); 110 NodeList nodeList = doc.getElementsByTagName("holdingsTree"); 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}