KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > launcher > parser > ParserZeus


1 /*====================================================================
2
3  ObjectWeb Util Launcher Package.
4  Copyright (C) 2004 INRIA & USTL - LIFL - GOAL
5  Contact: openccm@objectweb.org
6
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 2.1 of the License, or any later version.
11
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20  USA
21
22  Initial developer(s): Romain Rouvoy.
23  Contributor(s): Christophe Contreras.
24
25 --------------------------------------------------------------------
26 $Id: ParserZeus.java,v 1.3 2004/11/18 16:47:12 rouvoy Exp $
27 ====================================================================*/

28
29 package org.objectweb.util.launcher.parser ;
30
31
32 import java.util.Iterator JavaDoc;
33
34 import java.net.URL JavaDoc ;
35
36 import org.objectweb.util.launcher.LauncherException ;
37 import org.objectweb.util.trace.TraceSystem;
38
39
40 /**
41  * This class is an implementation of the Parser interface for providing
42  * an XML parser using Zeus library.<BR>
43  * <p>
44  * This library uses the Zeus library for parsing the file.
45  * </p>
46  *
47  * @author <a HREF="mailto:Romain.Rouvoy@lifl.fr">Romain Rouvoy</a>
48  * @version 0.1
49  */

50 public class ParserZeus
51   implements Parser
52 {
53     /** List of parsed file (with includes). */
54     protected java.util.ArrayList JavaDoc parsed_files_ ;
55
56     /**
57      * Default constructor.
58      */

59     public ParserZeus() {
60         parsed_files_ = new java.util.ArrayList JavaDoc();
61     }
62
63     /**
64      * Replace tags @NAME@ in a string.<BR>
65      * NAME must be a jave property.
66      *
67      * @param tagged - The string to replace.
68      *
69      * @return A new String with all tag replacements.
70      */

71     protected String JavaDoc replaceTag(String JavaDoc tagged) {
72         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
73         int start = 0;
74         int end = tagged.indexOf(RepositoryZeus.separator);
75         while(end != -1) {
76             result.append(tagged.substring(start,end));
77             start=end+1;
78             end= tagged.indexOf(RepositoryZeus.separator,start);
79             if (end != -1) {
80                 String JavaDoc tag = tagged.substring(start,end);
81                 String JavaDoc value = System.getProperty(tag);
82                 if (value == null)
83                     result.append("@"+tag+"@");
84                 else {
85                     TraceSystem.get("repository").debug("Replacing tag @"+tag+"@ with value "+value);
86                     result.append(value);
87                 }
88             }
89             start = end+1;
90             end = tagged.indexOf(RepositoryZeus.separator,start);
91         }
92         result.append(tagged.substring(start));
93         return result.toString();
94     }
95
96     /**
97      * Reads a file using the Zeus library.
98      *
99      * @param url the url of the file to parse.
100      */

101     protected Launcher readFile(String JavaDoc url) {
102         try {
103             return readFile(new java.net.URL JavaDoc(replaceTag(url)));
104         } catch (Exception JavaDoc ex) {
105             TraceSystem.get("decoder").error("Exception raised: "+ex);
106             throw new LauncherException(ex);
107         }
108     }
109
110     /**
111      * Reads a file using the Zeus library.
112      *
113      * @param url the url of the file to parse.
114      */

115     protected Launcher readFile(URL JavaDoc url) {
116         TraceSystem.get("parser").info("Reading "+url+" ...");
117         try {
118             return LauncherUnmarshaller.unmarshal(url.openStream()) ;
119         } catch (java.io.IOException JavaDoc e) {
120             TraceSystem.get("decoder").error("[I/O Error] : "+e);
121             throw new LauncherException(e);
122         }
123     }
124
125     
126     /**
127      * Parses an XML file and load the content of the file in a ZeusRepository.
128      *
129      * @param config the parsed file.
130      * @return the Respository containing the descriptions.
131      */

132     protected RepositoryZeus parseFile(Launcher config) {
133         RepositoryZeus repository = new RepositoryZeus();
134         parseFile(repository,config);
135         return repository;
136     }
137     
138     /**
139      * Parses an XML file and load the content of the file in a ZeusRepository.
140      *
141      * @param config the parsed file.
142      * @param repository the Respository containing the descriptions.
143      */

144     protected void parseFile(RepositoryZeus repository, Launcher config) {
145         TraceSystem.get("parser").info("Zeus Parsing for "+config+" ...");
146         if (config.getIncludeList()!=null) {
147             for(Iterator JavaDoc i = config.getIncludeList().iterator() ; i.hasNext() ;) {
148                 Include incl = (Include)i.next();
149                 if ( !parsed_files_.contains(incl.getUrl()) ) {
150                     parsed_files_.add(incl.getUrl());
151                     parseFile(repository,readFile(incl.getUrl()));
152                 }
153             }
154         }
155
156         if (config.getClasspathList()!=null) {
157             for(Iterator JavaDoc i = config.getClasspathList().iterator() ; i.hasNext() ; ) {
158                 Classpath c = (Classpath)i.next();
159                 TraceSystem.get("parser").debug("Adding classpath "+c.getId()+" ...");
160                 repository.addClasspath(c.getId(),c);
161             }
162         }
163
164         if (config.getArgumentsList()!=null) {
165             for(Iterator JavaDoc i = config.getArgumentsList().iterator() ; i.hasNext() ; ) {
166                 Arguments a = (Arguments)i.next();
167                 TraceSystem.get("parser").debug("Adding argument "+a.getId()+" ...");
168                 repository.addArgument(a.getId(),a);
169             }
170         }
171
172         if (config.getPropertiesList()!=null) {
173             for(Iterator JavaDoc i = config.getPropertiesList().iterator() ; i.hasNext() ; ) {
174                 Properties p = (Properties)i.next();
175                 TraceSystem.get("parser").debug("Adding property "+p.getId()+" ...");
176                 repository.addProperty(p.getId(),p);
177             }
178         }
179
180         if (config.getRunList()!=null) {
181             for(Iterator JavaDoc i = config.getRunList().iterator() ; i.hasNext() ; ) {
182                 Run r = (Run)i.next();
183                 TraceSystem.get("parser").debug("Adding run "+r.getId()+" ...");
184                 repository.addRun(r.getId(), r);
185             }
186         }
187
188         if (config.getContextList()!=null) {
189             for(Iterator JavaDoc i = config.getContextList().iterator() ; i.hasNext() ; ) {
190                 Context c = (Context)i.next();
191                 TraceSystem.get("parser").debug("Adding context "+c.getId());
192                 repository.addContext(c.getId(), c);
193             }
194         }
195     }
196
197
198     /**
199      * Loads an XML file.
200      *
201      * @param url the url of the file to load.
202      */

203     public Repository load(String JavaDoc url) {
204         return parseFile(readFile(url));
205     }
206 }
207
Popular Tags