KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > portal > impl > converter > PreferenceConverter


1 package org.exoplatform.services.portal.impl.converter;
2
3 import java.util.* ;
4 import org.exoplatform.services.portletcontainer.pci.model.Preference;
5
6 import com.thoughtworks.xstream.converters.MarshallingContext;
7 import com.thoughtworks.xstream.converters.UnmarshallingContext;
8 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
9 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
10
11 /**
12  * @author Tuan Nguyen
13  */

14 public class PreferenceConverter extends ComponentConverter {
15
16     public boolean canConvert(Class JavaDoc type) {
17         return type.equals(Preference.class) ;
18     }
19
20     public void marshal(Object JavaDoc source, HierarchicalStreamWriter w,
21                             MarshallingContext context) {
22         Preference pref = (Preference) source ;
23         w. startNode("name"); w.setValue(pref.getName()) ; w.endNode() ;
24         List values = pref.getValues() ;
25         for(int i = 0; i < values.size(); i++) {
26             String JavaDoc value = (String JavaDoc)values.get(i) ;
27           if(value != null) {
28             w.startNode("value"); w.setValue(value) ; w.endNode() ;
29           }
30         }
31         if(pref.isReadOnly()) {
32             w.startNode("read-only"); w.setValue("true") ; w.endNode() ;
33         }
34     }
35
36     public Object JavaDoc unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
37         Preference pref = new Preference() ;
38         while (reader.hasMoreChildren()) {
39             reader.moveDown();
40             String JavaDoc nodeName = reader.getNodeName() ;
41             if("name".equals(nodeName)) {
42                 pref.setName(reader.getValue());
43             } else if("value".equals(nodeName)) {
44               String JavaDoc value = reader.getValue() ;
45                 pref.addValue(value) ;
46             } else if("read-only".equals(nodeName)) {
47                 pref.setReadOnly("true".equals(reader.getValue())) ;
48             }
49             reader.moveUp();
50         }
51         return pref;
52     }
53 }
Popular Tags