KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bull > eclipse > jonas > utils > xml > 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
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.3 2003/11/24 13:26:19 coqp Exp $
24  * --------------------------------------------------------------------------
25  */

26 package com.bull.eclipse.jonas.utils.xml;
27
28 import java.io.IOException JavaDoc;
29 import java.io.Reader JavaDoc;
30
31 import org.apache.commons.digester.Digester;
32
33 import com.bull.eclipse.jonas.utils.xml.desc.DTDs;
34 import com.bull.eclipse.jonas.utils.xml.desc.Schemas;
35
36
37 /**
38  * This class defines a Digester for the xml parsing of
39  * deployment descriptors standard and specific
40  *
41  * @author Philippe Coq
42  */

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

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

123     public void parse(Reader JavaDoc reader,
124                       String JavaDoc fileName,
125                       TopLevelElement element)
126         throws DeploymentDescException {
127         try {
128             clear();
129             push(element);
130             parse(reader);
131         } catch (Exception JavaDoc ioe) {
132             throw new DeploymentDescException("Error when parsing XML document " + fileName, ioe);
133         } finally {
134             if (reader != null) {
135                 try {
136                     reader.close();
137                 } catch (IOException JavaDoc ignored) {
138                     ;
139                 }
140             }
141             push(null);
142         }
143     }
144     
145 }
146
Popular Tags