KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > portal > impl > XMLParser


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.services.portal.impl;
6
7 import java.io.InputStream JavaDoc;
8 import org.exoplatform.commons.xml.ExoXPPParser;
9 import org.exoplatform.services.portal.model.*;
10 import org.exoplatform.services.portletcontainer.pci.model.ExoPortletPreferences;
11 import org.exoplatform.services.portletcontainer.pci.model.Preference;
12
13
14 /**
15  * Jul 8, 2004
16  * @author: Tuan Nguyen
17  * @email: tuan08@users.sourceforge.net
18  * @version: $Id: XMLParser.java,v 1.4 2004/09/11 01:58:10 tuan08 Exp $
19  */

20 public class XMLParser {
21     
22     static public PortalConfig readPortalConfig(ExoXPPParser xpp ) throws Exception JavaDoc {
23     PortalConfig pc = new PortalConfig() ;
24     xpp.mandatoryNode("owner"); pc.setOwner(xpp.getContent()) ;
25     xpp.mandatoryNode("locale"); pc.setLocale(xpp.getContent()) ;
26     xpp.mandatoryNode("security"); pc.setViewPermission(xpp.getContent()) ;
27         return pc ;
28     }
29   
30   static public NodeImpl readNode(ExoXPPParser xpp ) throws Exception JavaDoc {
31     NodeImpl node = new NodeImpl() ;
32     xpp.mandatoryNode("uri"); node.setUri(xpp.getContent()) ;
33     xpp.mandatoryNode("name"); node.setName(xpp.getContent()) ;
34     xpp.mandatoryNode("label"); node.setLabel(xpp.getContent()) ;
35     if(xpp.node("description")) node.setDescription(xpp.getContent()) ;
36     xpp.mandatoryNode("pageReference"); node.setUri(xpp.getContent()) ;
37     int eventType = xpp.getEventType() ;
38     String JavaDoc tagName = xpp.getName() ;
39     while(!(eventType == ExoXPPParser.END_TAG && "node".equals(tagName))) {
40         if(eventType == ExoXPPParser.START_TAG && "node".equals(tagName)) {
41         xpp.nextTag() ;
42             node.addChild(readNode(xpp)) ;
43         } else {
44             xpp.nextTag() ;
45             eventType = xpp.getEventType() ;
46             tagName = xpp.getName() ;
47         }
48     }
49     xpp.nextTag() ;
50     return node ;
51   }
52   
53   static public Container readContainer(ExoXPPParser xpp ) throws Exception JavaDoc {
54     Container c = new Container() ;
55     readBasicAttribute(c, xpp) ;
56     int eventType = xpp.getEventType() ;
57     String JavaDoc tagName = xpp.getName() ;
58     while(eventType != ExoXPPParser.END_TAG && !"container".equals(tagName)) {
59         if(eventType == ExoXPPParser.START_TAG && "portlet".equals(tagName)) {
60             c.addChild(readPortlet(xpp)) ;
61         } else if(eventType == ExoXPPParser.START_TAG && "container".equals(tagName)) {
62             c.addChild(readContainer(xpp)) ;
63         } else if(eventType == ExoXPPParser.START_TAG && "body".equals(tagName)) {
64             c.addChild(readBody(xpp)) ;
65         } else {
66             xpp.next() ;
67             eventType = xpp.getEventType() ;
68             tagName = xpp.getName() ;
69         }
70     }
71     return c ;
72   }
73   
74   static public Portlet readPortlet(ExoXPPParser xpp ) throws Exception JavaDoc {
75     Portlet p = new Portlet() ;
76     readBasicAttribute(p, xpp) ;
77     if(xpp.node("portlet-style")); p.setPortletStyle(xpp.getContent()) ;
78     if(xpp.node("showInfoBar")); p.setShowInfoBar("true".equals(xpp.getContent())) ;
79     if(xpp.node("showWindowState")); p.setShowWindowState("true".equals(xpp.getContent()));
80     xpp.mandatoryNode("windowId"); p.setWindowId(xpp.getContent()) ;
81     if(xpp.node("portlet-preferences")); p.setPortletPreferences(readPortletPreferences(xpp));
82     return p ;
83   }
84   
85   static public Body readBody(ExoXPPParser xpp ) throws Exception JavaDoc {
86     Body b = new Body() ;
87     readBasicAttribute(b, xpp) ;
88     xpp.mandatoryNode("componentType"); b.setComponentType(xpp.getContent()) ;
89     xpp.mandatoryNode("componentId"); b.setComponentType(xpp.getContent()) ;
90     return b ;
91   }
92   
93   static public ExoPortletPreferences readPortletPreferences(ExoXPPParser xpp) throws Exception JavaDoc {
94     ExoPortletPreferences prefs = new ExoPortletPreferences() ;
95     while(xpp.node("preference"))prefs.addPreference(readPreference(xpp)) ;
96     return prefs ;
97   }
98   
99   static public Preference readPreference(ExoXPPParser xpp) throws Exception JavaDoc {
100     Preference pref = new Preference() ;
101     xpp.mandatoryNode("name"); pref.setName(xpp.getContent()) ;
102     while(xpp.node("value"))pref.addValue(xpp.getContent()) ;
103     if(xpp.node("read-only")) pref.setReadOnly(xpp.getContent()) ;
104     return pref ;
105   }
106   
107   static private void readBasicAttribute(Component comp, ExoXPPParser xpp) throws Exception JavaDoc {
108     comp.setId(xpp.getNodeAttributeValue("id")) ;
109     comp.setRenderer(xpp.getNodeAttributeValue("renderer")) ;
110     comp.setDecorator(xpp.getNodeAttributeValue("decorator")) ;
111     comp.setWidth(xpp.getNodeAttributeValue("width")) ;
112     comp.setHeight(xpp.getNodeAttributeValue("height")) ;
113   }
114   
115     static public PortalConfig parsePortal(InputStream JavaDoc is) throws Exception JavaDoc {
116         ExoXPPParser xpp = ExoXPPParser.getInstance() ;
117         xpp.setInput (is, "UTF8");
118         xpp.mandatoryNode("portal-config") ;
119         return readPortalConfig(xpp) ;
120     }
121 }
Popular Tags