KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_lib > deployment > digester > JDigester


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or 1any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer: Philippe Coq
22  * --------------------------------------------------------------------------
23  * $Id: JDigester.java,v 1.1 2004/05/25 14:26:34 sauthieg Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas_lib.deployment.digester;
28
29 import java.io.IOException JavaDoc;
30 import java.io.Reader JavaDoc;
31
32 import org.apache.commons.digester.Digester;
33
34 import org.objectweb.jonas_lib.deployment.api.DTDs;
35 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
36 import org.objectweb.jonas_lib.deployment.api.Schemas;
37 import org.objectweb.jonas_lib.deployment.rules.JRuleSetBase;
38 import org.objectweb.jonas_lib.deployment.xml.TopLevelElement;
39
40
41 /**
42  * This class defines a Digester for the xml parsing of
43  * deployment descriptors standard and specific
44  *
45  * @author Philippe Coq
46  */

47
48 public class JDigester extends Digester {
49
50     /**
51      * Construct an instance of a JDigester which is a Digester
52      * that is configured for parsing the deployment descriptors standards
53      * and specifics used in JOnAS application.
54      * the Digester contains the rules for the xml parsing
55      * By default the created digester is set with:
56      * useContextClassLoader = true
57      * @param ruleSet an object that extends JRuleSetBase
58      * @param parsingWithValidation flag for xmlvalidation
59      * @param namespaceAware must be true when schema is used
60      * @param dtds mapping between publicId and local Urls of DTDs
61      * @param schemas local urls for the schemas
62      * @throws DeploymentDescException if the deployment descriptors are corrupted.
63      */

64
65     public JDigester(JRuleSetBase ruleSet,
66                      boolean parsingWithValidation,
67                      boolean namespaceAware,
68                      DTDs dtds,
69                      Schemas schemas)
70         throws DeploymentDescException {
71         super();
72
73
74         String JavaDoc packageName = ruleSet.getClass().getPackage().getName();
75         String JavaDoc rootPackageName = packageName.substring(0, packageName.lastIndexOf('.'));
76
77
78         // Set the validation process
79
setNamespaceAware(namespaceAware);
80         setValidating(parsingWithValidation);
81
82         // Define an error handler
83
setErrorHandler(new JErrorHandler());
84
85         // Register all Sun dtds/Schemas
86
JEntityResolver jEntityResolver = new JEntityResolver(this);
87         jEntityResolver.addDtds(dtds);
88         jEntityResolver.addSchemas(schemas);
89         setEntityResolver(jEntityResolver);
90
91
92         // Set the schema that Digester must use
93
if (parsingWithValidation) {
94             try {
95                 setFeature("http://apache.org/xml/features/validation/schema",
96                                              true);
97             } catch (Exception JavaDoc ee) {
98                 throw new DeploymentDescException("Error setting feature", ee);
99             }
100         }
101
102         org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(rootPackageName + ".digester");
103         setLogger(log);
104
105         // Set the encoding feature
106
try {
107             setFeature("http://apache.org/xml/features/allow-java-encodings",
108                                          true);
109         } catch (Exception JavaDoc ee) {
110             throw new DeploymentDescException("Error setting feature", ee);
111         }
112
113         // Use Thread classloader by default
114
setUseContextClassLoader(true);
115
116         // Add rules
117
addRuleSet(ruleSet);
118     }
119
120
121     /**
122      * Parse the deployment descrptor
123      * @param reader the Reader of the XML file.
124      * @param fileName the name of the file.
125      * @param element top level xml element
126      * which is a structure containing the result of the xml parsing.
127      * @throws DeploymentDescException if the deployment descriptor
128      * is corrupted.
129      */

130     public void parse(Reader JavaDoc reader,
131                       String JavaDoc fileName,
132                       TopLevelElement element)
133         throws DeploymentDescException {
134         try {
135             clear();
136             push(element);
137             parse(reader);
138         } catch (Exception JavaDoc ioe) {
139             throw new DeploymentDescException("Error when parsing XML document " + fileName, ioe);
140         } finally {
141             if (reader != null) {
142                 try {
143                     reader.close();
144                 } catch (IOException JavaDoc ignored) {
145                     getLogger().warn("Can't close '" + fileName + "'");
146                 }
147             }
148             push(null);
149         }
150     }
151
152 }
153
Popular Tags