KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.exoplatform.services.portal.impl.converter;
2
3 import java.util.* ;
4 import org.exoplatform.services.portal.model.*;
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 ContainerConverter extends ComponentConverter {
15
16     public boolean canConvert(Class JavaDoc type) {
17         return type.equals(Container.class) ;
18     }
19
20     public void marshal(Object JavaDoc source, HierarchicalStreamWriter w,
21                             MarshallingContext context) {
22         Container container = (Container) source ;
23         writeBasicProperties(w, container) ;
24     String JavaDoc title = container.getTitle() ;
25     if(title != null && title.length() > 0) {
26       w.startNode("title"); w.setValue(container.getTitle()) ;w.endNode();
27     }
28         writeChildren(container, w, context) ;
29     }
30
31     public Object JavaDoc unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
32         Container container = new Container();
33         readBasicProperty(reader, container) ;
34         while (reader.hasMoreChildren()) {
35             reader.moveDown();
36             String JavaDoc nodeName = reader.getNodeName() ;
37             if("title".equals(nodeName)) {
38               container.setTitle(reader.getValue()) ;
39       } else {
40               readChildren(container , nodeName, context) ;
41       }
42             reader.moveUp();
43         }
44         return container ;
45     }
46     
47     final protected void writeChildren(Container container,HierarchicalStreamWriter w ,
48                                            MarshallingContext context) {
49         List children = container.getChildren();
50         for(int i = 0; i < children.size(); i++) {
51             Object JavaDoc o = children.get(i) ;
52             if(o instanceof Body) w.startNode("body") ;
53             else if (o instanceof Portlet) w.startNode("portlet") ;
54             else w.startNode("container") ;
55             context.convertAnother(o) ;
56             w.endNode() ;
57         }
58     }
59     
60     final protected void readChildren(Container container , String JavaDoc nodeName,
61                                           UnmarshallingContext context) {
62         Object JavaDoc comp = null ;
63         if("portlet".equals(nodeName)) {
64             comp = context.convertAnother(container, Portlet.class) ;
65         } else if("container".equals(nodeName)) {
66             comp = context.convertAnother(container, Container.class) ;
67         } else {
68             comp = context.convertAnother(container, Body.class) ;
69         }
70       container.getChildren().add(comp) ;
71     }
72 }
Popular Tags