KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.exoplatform.services.portal.impl.converter;
2
3 import org.exoplatform.services.portal.model.*;
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
9 /**
10  * @author Tuan Nguyen
11  */

12 public class PageConverter extends ContainerConverter {
13
14     public boolean canConvert(Class JavaDoc type) {
15         return type.equals(Page.class) ;
16     }
17
18     public void marshal(Object JavaDoc source, HierarchicalStreamWriter w,
19                             MarshallingContext context) {
20         Page page = (Page) source ;
21         writeBasicProperties(w, page) ;
22         writePageProperties(w, page) ;
23         writeChildren(page, w, context) ;
24     }
25
26     public Object JavaDoc unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
27         Page page = new Page();
28         readBasicProperty(reader, page) ;
29         while (reader.hasMoreChildren()) {
30             reader.moveDown();
31             if(readPageProperty(reader, page)) {
32             } else {
33                 String JavaDoc nodeName = reader.getNodeName() ;
34                 readChildren(page , nodeName, context) ;
35             }
36             reader.moveUp();
37         }
38         return page ;
39     }
40     
41     public void writePageProperties(HierarchicalStreamWriter w, Page page) {
42         if(page.getOwner() != null) {
43             w.startNode("owner"); w.setValue(page.getOwner()) ;w.endNode();
44         }
45         if(page.getName() != null) {
46             w.startNode("name"); w.setValue(page.getName()) ;w.endNode();
47         }
48         if(page.getTitle() != null) {
49             w.startNode("title"); w.setValue(page.getTitle()) ;w.endNode();
50         }
51         if(page.getIcon() != null) {
52             w.startNode("icon"); w.setValue(page.getIcon()) ;w.endNode();
53         }
54         if(page.getState() != null) {
55             w.startNode("state"); w.setValue(page.getState()) ;w.endNode();
56         }
57         if(page.getViewPermission() != null) {
58             w.startNode("viewPermission"); w.setValue(page.getViewPermission()) ;w.endNode();
59         }
60         if(page.getEditPermission() != null) {
61             w.startNode("editPermission"); w.setValue(page.getEditPermission()) ;w.endNode();
62         }
63     }
64     
65     public boolean readPageProperty(HierarchicalStreamReader reader, Page page) {
66         String JavaDoc nodeName = reader.getNodeName() ;
67         if("owner".equals(nodeName)) {
68             page.setOwner(reader.getValue()) ; return true ;
69         } else if("name".equals(nodeName)) {
70             page.setName(reader.getValue()) ; return true ;
71         } else if("title".equals(nodeName)) {
72             page.setTitle(reader.getValue()) ; return true ;
73         } else if("icon".equals(nodeName)) {
74             page.setIcon(reader.getValue()) ; return true ;
75         } else if("state".equals(nodeName)) {
76             page.setState(reader.getValue()) ; return true ;
77         } else if("viewPermission".equals(nodeName)) {
78             page.setViewPermission(reader.getValue()) ; return true ;
79         } else if("editPermission".equals(nodeName)) {
80             page.setEditPermission(reader.getValue()) ; return true ;
81         }
82         return false ;
83     }
84 }
Popular Tags