View Javadoc

1   package org.kuali.ole.docstore.common.document;
2   
3   import org.apache.log4j.Logger;
4   
5   import java.io.ByteArrayInputStream;
6   import java.io.StringWriter;
7   import java.util.ArrayList;
8   import java.util.List;
9   import javax.xml.bind.JAXBContext;
10  import javax.xml.bind.JAXBElement;
11  import javax.xml.bind.Marshaller;
12  import javax.xml.bind.Unmarshaller;
13  import javax.xml.bind.annotation.*;
14  import javax.xml.transform.stream.StreamSource;
15  
16  
17  /**
18   * <p>Java class for items complex type.
19   * <p/>
20   * <p>The following schema fragment specifies the expected content contained within this class.
21   * <p/>
22   * <pre>
23   * &lt;complexType name="items">
24   *   &lt;complexContent>
25   *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
26   *       &lt;sequence>
27   *         &lt;element name="items" type="{}item" maxOccurs="unbounded" minOccurs="0"/>
28   *       &lt;/sequence>
29   *     &lt;/restriction>
30   *   &lt;/complexContent>
31   * &lt;/complexType>
32   * </pre>
33   */
34  @XmlAccessorType(XmlAccessType.FIELD)
35  @XmlType(name = "items", propOrder = {
36          "items"
37  })
38  @XmlRootElement
39  public class Items {
40  
41      private static final Logger LOG = Logger.getLogger(Items.class);
42      @XmlElement(name = "item")
43      protected List<Item> items;
44  
45      public static String serialize(Object object) {
46          String result = null;
47          StringWriter sw = new StringWriter();
48          Items items = (Items) object;
49          try {
50              JAXBContext jaxbContext = JAXBContext.newInstance(Items.class);
51              Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
52              jaxbMarshaller.marshal(items, sw);
53              result = sw.toString();
54          } catch (Exception e) {
55              LOG.error("Exception ", e);
56          }
57          return result;
58      }
59  
60      public static Object deserialize(String itemsXml) {
61  
62          JAXBElement<Items> itemsElement = null;
63          try {
64              JAXBContext jaxbContext = JAXBContext.newInstance(Items.class);
65              Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
66              ByteArrayInputStream input = new ByteArrayInputStream(itemsXml.getBytes("UTF-8"));
67              itemsElement = jaxbUnmarshaller.unmarshal(new StreamSource(input), Items.class);
68          } catch (Exception e) {
69              LOG.error("Exception ", e);
70          }
71          return itemsElement.getValue();
72      }
73  
74      /**
75       * Gets the value of the items property.
76       * <p/>
77       * <p/>
78       * This accessor method returns a reference to the live list,
79       * not a snapshot. Therefore any modification you make to the
80       * returned list will be present inside the JAXB object.
81       * This is why there is not a <CODE>set</CODE> method for the items property.
82       * <p/>
83       * <p/>
84       * For example, to add a new item, do as follows:
85       * <pre>
86       *    getItems().add(newItem);
87       * </pre>
88       * <p/>
89       * <p/>
90       * <p/>
91       * Objects of the following type(s) are allowed in the list
92       * {@link org.kuali.ole.docstore.common.document.Item }
93       */
94      public List<Item> getItems() {
95          if (items == null) {
96              items = new ArrayList<Item>();
97          }
98          return this.items;
99      }
100 
101 }