KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > container > configuration > 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.container.configuration;
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.5 2004/10/29 01:55:23 tuan08 Exp $
15  */

16 public class XMLParser {
17     
18     static public Configuration readConfiguration(ExoXPPParser xpp ) throws Exception JavaDoc {
19         Configuration conf = new Configuration() ;
20     if(xpp.node("global-param")) readGlobalInitParam(conf, xpp) ;
21     while(xpp.node("service-configuration")) {
22       conf.addServiceConfiguration(readServiceConfiguration(xpp)) ;
23     }
24     while(xpp.node("import")) {
25       conf.addImportConfiguration(xpp.getContent()) ;
26     }
27     while(xpp.node("remove-configuration")) {
28       conf.addRemoveConfiguration(xpp.getContent()) ;
29     }
30         return conf ;
31     }
32     
33   static public void readGlobalInitParam(Configuration conf, ExoXPPParser xpp) throws Exception JavaDoc {
34     while(xpp.node("init-param")) conf.addInitParam(readValuesParam(xpp)) ;
35   }
36   
37   static public ValuesParam readValuesParam(ExoXPPParser xpp) throws Exception JavaDoc {
38     ValuesParam param = new ValuesParam() ;
39     param.setName(xpp.mandatoryNodeContent("name")) ;
40     param.setDescription(xpp.nodeContent("description")) ;
41     while(xpp.node("value")) param.addValue(xpp.getContent()) ;
42     return param ;
43   }
44   
45   static public ValueParam readValueParam(ExoXPPParser xpp) throws Exception JavaDoc {
46     ValueParam param = new ValueParam() ;
47     param.setName(xpp.mandatoryNodeContent("name")) ;
48     param.setDescription(xpp.nodeContent("description")) ;
49     param.setValue(xpp.nodeContent("value")) ;
50     return param ;
51   }
52   
53   static public PropertiesParam readPropertiesParam(ExoXPPParser xpp) throws Exception JavaDoc {
54     PropertiesParam param = new PropertiesParam() ;
55     param.setName(xpp.mandatoryNodeContent("name")) ;
56     param.setDescription(xpp.nodeContent("description")) ;
57     while(xpp.node("property")) {
58       param.setProperty(xpp.getNodeAttributeValue("name"), xpp.getNodeAttributeValue("value")) ;
59     }
60     return param ;
61   }
62   
63   static public ObjectParam readObjectParam(ExoXPPParser xpp) throws Exception JavaDoc {
64     ObjectParam param = new ObjectParam() ;
65     param.setName(xpp.mandatoryNodeContent("name")) ;
66     param.setType(xpp.mandatoryNodeContent("type")) ;
67     param.setDescription(xpp.nodeContent("description")) ;
68     while(xpp.node("property")) {
69       param.addProperty(xpp.getNodeAttributeValue("name"), xpp.getNodeAttributeValue("value")) ;
70     }
71     return param ;
72   }
73     
74   static public ServiceConfiguration readServiceConfiguration(ExoXPPParser xpp) throws Exception JavaDoc {
75     ServiceConfiguration sconf = new ServiceConfiguration() ;
76     sconf.setServiceType(xpp.getNodeAttributeValue("type"));
77     sconf.setServiceKey(xpp.getNodeAttributeValue("key"));
78     boolean paramNode = true ;
79     while(paramNode) {
80       if(xpp.node("values-param")) {
81         sconf.addParameter(readValuesParam(xpp)) ;
82       } else if(xpp.node("value-param")) {
83         sconf.addParameter(readValueParam(xpp)) ;
84       } else if(xpp.node("properties-param")) {
85         sconf.addParameter(readPropertiesParam(xpp)) ;
86       } else if(xpp.node("object-param")) {
87         sconf.addParameter(readObjectParam(xpp)) ;
88       } else {
89         paramNode = false ;
90       }
91     }
92     return sconf ;
93   }
94   
95   
96     static public Configuration parse(InputStream JavaDoc is) throws Exception JavaDoc {
97         ExoXPPParser xpp = ExoXPPParser.getInstance() ;
98         xpp.setInput(is, "UTF8");
99         xpp.mandatoryNode("configuration") ;
100         return readConfiguration(xpp) ;
101     }
102 }
Popular Tags