KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > portletcontainer > pci > model > 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.portletcontainer.pci.model;
6
7 import java.io.InputStream JavaDoc;
8 import org.exoplatform.commons.xml.ExoXPPParser;
9
10 /**
11  * Jul 8, 2004
12  * @author: Tuan Nguyen
13  * @email: tuan08@users.sourceforge.net
14  * @version: $Id: XMLParser.java,v 1.1 2004/07/13 02:31:13 tuan08 Exp $
15  */

16 public class XMLParser {
17     
18     static public PortletApp readPortletApp(ExoXPPParser xpp ) throws Exception JavaDoc {
19         PortletApp pa = new PortletApp() ;
20     pa.setVersion(xpp.getNodeAttributeValue("version")) ;
21     while(xpp.node("portlet")) pa.addPortlet(readPortlet(xpp)) ;
22     while(xpp.node("custom-portlet-mode")) pa.addCustomPortletMode(readCustomPortletMode(xpp)) ;
23     while(xpp.node("custom-window-state")) pa.addCustomWindowState(readCustomWindowState(xpp)) ;
24     while(xpp.node("user-attribute")) pa.addUserAttribute(readUserAttribute(xpp)) ;
25     while(xpp.node("security-constraint")) pa.addSecurityConstraint(readSecurityConstraint(xpp)) ;
26         return pa ;
27     }
28     
29     static public Portlet readPortlet(ExoXPPParser xpp ) throws Exception JavaDoc {
30         Portlet p = new Portlet() ;
31     while(xpp.node("description")) p.addDescription(readDescription(xpp)) ;
32     xpp.mandatoryNode("portlet-name"); p.setPortletName(xpp.getContent()) ;
33     while(xpp.node("display-name")) p.addDisplayName(readDisplayName(xpp)) ;
34     xpp.mandatoryNode("portlet-class"); p.setPortletClass(xpp.getContent()) ;
35     while(xpp.node("init-param")) p.addInitParam(readInitParam(xpp)) ;
36     if(xpp.node("expiration-cache")) p.setExpirationCache(xpp.getContent()) ;
37     while(xpp.node("supports")) p.addSupports(readSupports(xpp)) ;
38     while(xpp.node("supported-locale")) p.addSupportedLocale(xpp.getContent()) ;
39     if(xpp.node("resource-bundle")) p.setResourceBundle(xpp.getContent()) ;
40     if(xpp.node("portlet-info")) p.setPortletInfo(readPortletInfo(xpp)) ;
41     if(xpp.node("portlet-preferences")) p.setPortletPreferences(readPortletPreferences(xpp)) ;
42     while(xpp.node("security-role-ref")) p.addSecurityRoleRef(readSecurityRoleRef(xpp)) ;
43     while(xpp.node("filter")) p.addFilter(readFilter(xpp)) ;
44     while(xpp.node("message-listener")) p.addMessageListener(readMessageListener(xpp)) ;
45     if(xpp.node("global-cache")) p.setGlobalCache(xpp.getContent()) ;
46         return p ;
47     }
48     
49   static public CustomPortletMode readCustomPortletMode(ExoXPPParser xpp ) throws Exception JavaDoc {
50     CustomPortletMode mode = new CustomPortletMode() ;
51      while(xpp.node("description")) mode.addDescription(readDescription(xpp)) ;
52      xpp.mandatoryNode("portlet-mode"); mode.setPortletMode(xpp.getContent()) ;
53     return mode ;
54   }
55   
56   static public CustomWindowState readCustomWindowState(ExoXPPParser xpp ) throws Exception JavaDoc {
57     CustomWindowState state = new CustomWindowState() ;
58     while(xpp.node("description")) state.addDescription(readDescription(xpp)) ;
59     xpp.mandatoryNode("window-state"); state.setWindowState(xpp.getContent()) ;
60     return state ;
61   }
62   
63   static public UserAttribute readUserAttribute(ExoXPPParser xpp ) throws Exception JavaDoc {
64     UserAttribute att = new UserAttribute() ;
65     while(xpp.node("description")) att.addDescription(readDescription(xpp)) ;
66     xpp.mandatoryNode("name"); att.setName(xpp.getContent()) ;
67     return att ;
68   }
69   
70   static public SecurityConstraint readSecurityConstraint(ExoXPPParser xpp ) throws Exception JavaDoc {
71     SecurityConstraint sc = new SecurityConstraint() ;
72     if(xpp.node("displayName"))sc.setDisplayName(xpp.getContent()) ;
73     xpp.mandatoryNode("portlet-collection"); sc.setPortletCollection(readPortletCollection(xpp)) ;
74     xpp.mandatoryNode("user-data-constraint"); sc.setUserDataConstraint(readUserDataConstraint(xpp)) ;
75     return sc ;
76   }
77   
78   static public Description readDescription(ExoXPPParser xpp) throws Exception JavaDoc {
79     Description desc = new Description() ;
80     desc.setLang(xpp.getNodeAttributeValue("xml:lang")) ;
81     desc.setDescription(xpp.getContent()) ;
82     return desc ;
83   }
84   
85   static public DisplayName readDisplayName(ExoXPPParser xpp) throws Exception JavaDoc {
86     DisplayName name = new DisplayName() ;
87     name.setLang(xpp.getNodeAttributeValue("xml:lang")) ;
88     name.setDisplayName(xpp.getContent()) ;
89     return name ;
90   }
91   
92   static public InitParam readInitParam(ExoXPPParser xpp) throws Exception JavaDoc {
93     InitParam param = new InitParam() ;
94     while(xpp.node("description")) param.addDescription(readDescription(xpp)) ;
95     xpp.mandatoryNode("name"); param.setName(xpp.getContent()) ;
96     xpp.mandatoryNode("value"); param.setValue(xpp.getContent()) ;
97     return param ;
98   }
99   
100   static public Supports readSupports(ExoXPPParser xpp) throws Exception JavaDoc {
101     Supports supports = new Supports() ;
102     xpp.mandatoryNode("mime-type"); supports.setMimeType(xpp.getContent()) ;
103     while(xpp.node("portlet-mode")) supports.addPortletMode(xpp.getContent()) ;
104     return supports ;
105   }
106   
107   static public PortletInfo readPortletInfo(ExoXPPParser xpp) throws Exception JavaDoc {
108     PortletInfo info = new PortletInfo() ;
109     xpp.mandatoryNode("title"); info.setTitle(xpp.getContent()) ;
110     if(xpp.node("short-title")) info.setShortTitle(xpp.getContent()) ;
111     if(xpp.node("keywords")) info.setKeywords(xpp.getContent()) ;
112     return info ;
113   }
114   
115   static public ExoPortletPreferences readPortletPreferences(ExoXPPParser xpp) throws Exception JavaDoc {
116     ExoPortletPreferences prefs = new ExoPortletPreferences() ;
117     while(xpp.node("preference"))prefs.addPreference(readPreference(xpp)) ;
118     if(xpp.node("preferences-validator")) prefs.setPreferencesValidator(xpp.getContent()) ;
119     return prefs ;
120   }
121   
122   static public Preference readPreference(ExoXPPParser xpp) throws Exception JavaDoc {
123     Preference pref = new Preference() ;
124     xpp.mandatoryNode("name"); pref.setName(xpp.getContent()) ;
125     while(xpp.node("value"))pref.addValue(xpp.getContent()) ;
126     if(xpp.node("read-only")) pref.setReadOnly(xpp.getContent()) ;
127     return pref ;
128   }
129   
130   static public SecurityRoleRef readSecurityRoleRef(ExoXPPParser xpp) throws Exception JavaDoc {
131     SecurityRoleRef ref = new SecurityRoleRef() ;
132     xpp.mandatoryNode("role-name"); ref.setRoleName(xpp.getContent()) ;
133     if(xpp.node("role-link")) ref.setRoleLink(xpp.getContent()) ;
134     return ref ;
135   }
136   
137   static public Filter readFilter(ExoXPPParser xpp) throws Exception JavaDoc {
138     Filter filter = new Filter() ;
139     xpp.mandatoryNode("filter-name"); filter.setFilterName(xpp.getContent()) ;
140     xpp.mandatoryNode("filter-class"); filter.setFilterClass(xpp.getContent()) ;
141     while(xpp.node("init-param")) filter.addInitParam(readInitParam(xpp)) ;
142     return filter ;
143   }
144   
145   static public MessageListener readMessageListener(ExoXPPParser xpp) throws Exception JavaDoc {
146     MessageListener listener = new MessageListener() ;
147     xpp.mandatoryNode("listener-name"); listener.setListenerName(xpp.getContent()) ;
148     xpp.mandatoryNode("listener-class"); listener.setListenerClass(xpp.getContent()) ;
149     while(xpp.node("description")) listener.addDescription(readDescription(xpp)) ;
150     return listener ;
151   }
152   
153   static public PortletCollection readPortletCollection(ExoXPPParser xpp) throws Exception JavaDoc {
154     PortletCollection collection = new PortletCollection() ;
155     while(xpp.node("portlet-name")) collection.addPortletName(xpp.getContent()) ;
156     return collection ;
157   }
158   
159   static public UserDataConstraint readUserDataConstraint(ExoXPPParser xpp) throws Exception JavaDoc {
160     UserDataConstraint u = new UserDataConstraint() ;
161     while(xpp.node("description")) u.addDescription(readDescription(xpp)) ;
162     xpp.mandatoryNode("transport-guarantee"); u.setTransportGuarantie(xpp.getContent()) ;
163     return u ;
164   }
165   
166     static public PortletApp parse(InputStream JavaDoc is) throws Exception JavaDoc {
167         ExoXPPParser xpp = ExoXPPParser.getInstance() ;
168         xpp.setInput(is, "UTF8");
169         xpp.mandatoryNode("portlet-app") ;
170         return readPortletApp(xpp) ;
171     }
172 }
Popular Tags