View Javadoc
1   package org.kuali.ole.docstore.common.document;
2   
3   import org.apache.log4j.Logger;
4   import org.kuali.ole.docstore.common.document.factory.JAXBContextFactory;
5   
6   import javax.xml.bind.Marshaller;
7   import javax.xml.bind.Unmarshaller;
8   import javax.xml.bind.annotation.*;
9   import javax.xml.transform.stream.StreamSource;
10  import java.io.ByteArrayInputStream;
11  import java.io.StringWriter;
12  import java.util.HashMap;
13  
14  
15  /**
16   * <p>Java class for items complex type.
17   * <p/>
18   * <p>The following schema fragment specifies the expected content contained within this class.
19   * <p/>
20   * <pre>
21   * &lt;complexType name="items">
22   *   &lt;complexContent>
23   *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
24   *       &lt;sequence>
25   *         &lt;element name="itemMap" type="{}item" maxOccurs="unbounded" minOccurs="0"/>
26   *       &lt;/sequence>
27   *     &lt;/restriction>
28   *   &lt;/complexContent>
29   * &lt;/complexType>
30   * </pre>
31   */
32  @XmlAccessorType(XmlAccessType.FIELD)
33  @XmlType(name = "itemMap", propOrder = {
34          "itemMap"
35  })
36  @XmlRootElement(name="itemsDocs")
37  public class ItemMap {
38  
39      private static final Logger LOG = Logger.getLogger(ItemMap.class);
40      @XmlElement(name = "itemsDoc")
41      protected HashMap<String, Item> itemMap;
42  
43      public static String serialize(Object object) {
44          String result = null;
45          ItemMap itemMap = (ItemMap) object;
46          try {
47              StringWriter sw = new StringWriter();
48              Marshaller jaxbMarshaller = JAXBContextFactory.getInstance().getMarshaller(ItemMap.class);
49              synchronized (jaxbMarshaller) {
50                  jaxbMarshaller.marshal(itemMap, sw);
51              }
52              result = sw.toString();
53          } catch (Exception e) {
54              LOG.error("Exception ", e);
55          }
56          return result;
57      }
58  
59  
60      public static Object deserialize(String content) {
61          ItemMap itemMap = new ItemMap();
62          try {
63              Unmarshaller unmarshaller = JAXBContextFactory.getInstance().getUnMarshaller(ItemMap.class);
64              ByteArrayInputStream input = new ByteArrayInputStream(content.getBytes("UTF-8"));
65              synchronized (unmarshaller) {
66                  itemMap = unmarshaller.unmarshal(new StreamSource(input), ItemMap.class).getValue();
67              }
68          } catch (Exception e) {
69              LOG.error("Exception ", e);
70          }
71          return itemMap;
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 Item }
93       */
94      public HashMap<String, Item> getItemMap() {
95          if (itemMap == null) {
96              itemMap = new HashMap<>();
97          }
98          return this.itemMap;
99      }
100 }