View Javadoc

1   package org.kuali.ole.docstore.model.xstream.ingest;
2   
3   import com.thoughtworks.xstream.converters.Converter;
4   import com.thoughtworks.xstream.converters.MarshallingContext;
5   import com.thoughtworks.xstream.converters.UnmarshallingContext;
6   import com.thoughtworks.xstream.io.HierarchicalStreamReader;
7   import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
8   import org.kuali.ole.docstore.model.xmlpojo.ingest.AdditionalAttributes;
9   
10  import java.util.Collection;
11  import java.util.Iterator;
12  
13  /**
14   * Created by IntelliJ IDEA.
15   * User: Pranitha
16   * Date: 6/13/12
17   * Time: 1:40 PM
18   * To change this template use File | Settings | File Templates.
19   */
20  public class AdditionalAttributeConverter implements Converter {
21  
22  
23      @Override
24      public void marshal(Object obj, HierarchicalStreamWriter hierarchicalStreamWriter, MarshallingContext context) {
25             AdditionalAttributes additionalAttributes = (AdditionalAttributes)obj;
26            if (additionalAttributes != null) {
27              hierarchicalStreamWriter.startNode("additionalAttributes");
28              Collection<String> attributeNames = additionalAttributes.getAttributeNames();
29              if (attributeNames != null && attributeNames.size() > 0) {
30                  for (Iterator<String> iterator = attributeNames.iterator(); iterator.hasNext(); ) {
31                      String name = iterator.next();
32                      String value = additionalAttributes.getAttribute(name);
33                      hierarchicalStreamWriter.startNode(name);
34                      hierarchicalStreamWriter.setValue(value);
35                      hierarchicalStreamWriter.endNode();
36                  }
37              }
38              hierarchicalStreamWriter.endNode();
39          }
40  
41      }
42  
43      @Override
44      public Object unmarshal(HierarchicalStreamReader hierarchicalStreamReader, UnmarshallingContext context) {
45          AdditionalAttributes addAttributes = new AdditionalAttributes();
46          while (hierarchicalStreamReader.hasMoreChildren()) {
47              hierarchicalStreamReader.moveDown();
48              String key = hierarchicalStreamReader.getNodeName();
49              String value = hierarchicalStreamReader.getValue();
50              addAttributes.setAttribute(key, value);
51              hierarchicalStreamReader.moveUp();
52          }
53          return addAttributes;
54      }
55  
56      @Override
57      public boolean canConvert(Class aClass) {
58            return aClass.equals(AdditionalAttributes.class);
59      }
60  
61  
62  }