KickJava   Java API By Example, From Geeks To Geeks.

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


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

13 public class PortletConverter extends ComponentConverter {
14
15     public boolean canConvert(Class JavaDoc type) {
16         return type.equals(Portlet.class) ;
17     }
18
19     public void marshal(Object JavaDoc source, HierarchicalStreamWriter w,
20                             MarshallingContext context) {
21         Portlet portlet = (Portlet) source ;
22         writeBasicProperties(w, portlet) ;
23         writePortletProperties(w, portlet) ;
24         if(portlet.getPortletPreferences() != null) {
25             w.startNode("portlet-preferences") ;
26             context.convertAnother(portlet.getPortletPreferences()) ;
27             w.endNode() ;
28         }
29     }
30
31     public Object JavaDoc unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
32         Portlet portlet = new Portlet();
33         readBasicProperty(reader, portlet) ;
34         while (reader.hasMoreChildren()) {
35             reader.moveDown();
36             if(readPortletProperty(reader, portlet)) {
37             } else {
38                 ExoPortletPreferences prefs =
39                     (ExoPortletPreferences)context.convertAnother(portlet, ExoPortletPreferences.class) ;
40                 portlet.setPortletPreferences(prefs) ;
41             }
42             reader.moveUp();
43         }
44         return portlet ;
45     }
46     
47     public void writePortletProperties(HierarchicalStreamWriter w, Portlet comp) {
48         if(comp.getTitle() != null) {
49             w.startNode("title"); w.setValue(comp.getTitle()) ;w.endNode();
50         }
51         if(comp.getWindowId() != null) {
52             w.startNode("windowId"); w.setValue(comp.getWindowId()) ;w.endNode();
53         }
54         if(comp.getPortletStyle() != null) {
55             w.startNode("portlet-style"); w.setValue(comp.getPortletStyle()) ;w.endNode();
56         }
57         if(comp.getShowInfoBar() == false) {
58             w.startNode("showInfoBar"); w.setValue("false") ;w.endNode();
59         }
60         if(comp.getShowWindowState() == false) {
61             w.startNode("showWindowState"); w.setValue("false") ;w.endNode();
62         }
63     if(comp.getShowPortletMode() == false) {
64       w.startNode("showPortletMode"); w.setValue("false") ;w.endNode();
65     }
66     }
67     
68     public boolean readPortletProperty(HierarchicalStreamReader reader, Portlet comp) {
69         String JavaDoc nodeName = reader.getNodeName() ;
70         if("title".equals(nodeName)) {
71             comp.setTitle(reader.getValue()) ;
72             return true ;
73         } else if("windowId".equals(nodeName)) {
74             comp.setWindowId(reader.getValue()) ;
75             return true ;
76         } else if("portlet-style".equals(nodeName)) {
77             comp.setPortletStyle(reader.getValue()) ;
78             return true ;
79         } else if("showInfoBar".equals(nodeName)) {
80             comp.setShowInfoBar("true".equals(reader.getValue())) ;
81             return true ;
82         } else if("showWindowState".equals(nodeName)) {
83             comp.setShowWindowState("true".equals(reader.getValue())) ;
84             return true ;
85     } else if("showPortletMode".equals(nodeName)) {
86       comp.setShowPortletMode("true".equals(reader.getValue())) ;
87       return true ;
88         }
89         return false ;
90     }
91 }
Popular Tags