KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > core > misc > XmlDocTypeFilter


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64 package com.jcorporate.expresso.core.misc;
65
66 import org.apache.log4j.helpers.LogLog;
67 import org.xml.sax.ErrorHandler JavaDoc;
68 import org.xml.sax.InputSource JavaDoc;
69 import org.xml.sax.SAXException JavaDoc;
70 import org.xml.sax.SAXParseException JavaDoc;
71 import org.xml.sax.ext.LexicalHandler JavaDoc;
72
73 import javax.xml.parsers.SAXParser JavaDoc;
74 import javax.xml.parsers.SAXParserFactory JavaDoc;
75
76
77 /**
78  * Given a directory, iterates over all xml files, and parses until it reaches
79  * the doctype parameter, at which point it checks against the desired doctype
80  * and prepares it for parsing if it is.
81  *
82  * @author Michael Rimov
83  * @since Expresso 5
84  */

85 public class XmlDocTypeFilter extends org.xml.sax.helpers.DefaultHandler JavaDoc
86         implements ErrorHandler JavaDoc, LexicalHandler JavaDoc {
87     /**
88      *
89      */

90     protected boolean result;
91     SAXParser JavaDoc parser = null;
92     String JavaDoc theDocType = null;
93
94     /**
95      * Creates a new XmlDocTypeFilter object.
96      */

97     public XmlDocTypeFilter() {
98     }
99
100     /**
101      * Return the SAXParser we will use to parse the input stream. If there is
102      * a problem creating the parser, return <code>null</code>.
103      *
104      * @return SAXParser instance
105      */

106     public SAXParser JavaDoc getParser() {
107         // Return the parser we already created (if any)
108
if (parser != null) {
109             return (parser);
110         }
111
112         // Create and return a new parser
113
try {
114             SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
115             factory.setNamespaceAware(false);
116             factory.setValidating(false);
117             parser = factory.newSAXParser();
118             parser.setProperty("http://xml.org/sax/properties/lexical-handler",
119                     this);
120
121             return (parser);
122         } catch (Exception JavaDoc e) {
123             LogLog.error("XmlDocTypeFilter.getParser(): ", e);
124
125             return (null);
126         }
127     }
128
129     /**
130      * Returns whether the InputStream specified is an xml document of the
131      * appropriate type.
132      *
133      * @param docType the doctype to filter on
134      * @param source the InputSource to check
135      * @return true if the doctype is the proper kind
136      */

137     public boolean isProperDocType(String JavaDoc docType, InputSource JavaDoc source) {
138         try {
139             result = false;
140             theDocType = docType;
141
142             SAXParser JavaDoc parser = this.getParser();
143             parser.parse(source, this);
144         } catch (SAXParseException JavaDoc ex) {
145             return result;
146         } catch (SAXException JavaDoc sax_ex) {
147             return result;
148         } catch (java.io.IOException JavaDoc ioe) {
149             LogLog.error("Io Exception in XmlDocTypeFilter.isProperDoctype", ioe);
150
151             return result;
152         }
153
154         return result;
155     }
156
157     /**
158      * @param ch
159      * @param start
160      * @param length
161      */

162     public void comment(char[] ch, int start, int length)
163             throws SAXException JavaDoc {
164     }
165
166     /**
167      * @throws SAXException
168      */

169     public void endCDATA() throws SAXException JavaDoc {
170     }
171
172     //
173
//No-op impementations of the Lexical Handler Interface
174
//
175
public void endDTD() throws SAXException JavaDoc {
176     }
177
178     /**
179      * @param name
180      */

181     public void endEntity(String JavaDoc name) throws SAXException JavaDoc {
182     }
183
184     /**
185      * Error. Used Internally for parsing only
186      *
187      * @param ex the SaxParseException
188      */

189     public void error(SAXParseException JavaDoc ex) throws SAXException JavaDoc {
190         LogLog.error(getLocationString(ex) + ": " + ex.getMessage());
191     }
192
193     /**
194      * Fatal error. Used Internally for parsing only
195      *
196      * @param ex the SaxParseException
197      */

198     public void fatalError(SAXParseException JavaDoc ex) throws SAXException JavaDoc {
199         LogLog.error(getLocationString(ex) + ": " + ex.getMessage());
200         throw ex;
201     }
202
203     /**
204      * @throws SAXException
205      */

206     public void startCDATA() throws SAXException JavaDoc {
207     }
208
209     //
210
//Document Handler Methods ------------------------------------
211
//
212
public void startDTD(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId)
213             throws SAXException JavaDoc {
214         if (name.equals(theDocType)) {
215             result = true;
216         }
217
218         throw new SAXException JavaDoc("Done");
219     }
220
221     /**
222      * @param name
223      */

224     public void startEntity(String JavaDoc name) throws SAXException JavaDoc {
225     }
226
227     //
228
//Error Handler Methods ------------------------------------
229
//
230

231     /**
232      * Warning handler
233      *
234      * @param ex the SAXParseException
235      */

236     public void warning(SAXParseException JavaDoc ex) {
237         LogLog.debug(getLocationString(ex) + ": " + ex.getMessage());
238     }
239
240     /**
241      * Returns a string of the location. Used Internally For Parsing Only
242      *
243      * @param ex the SaxParseException
244      * @return java.lang.String
245      */

246     private String JavaDoc getLocationString(SAXParseException JavaDoc ex) {
247         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
248         String JavaDoc systemId = ex.getSystemId();
249
250         if (systemId != null) {
251             int index = systemId.lastIndexOf('/');
252
253             if (index != -1) {
254                 systemId = systemId.substring(index + 1);
255             }
256
257             str.append(systemId);
258         }
259
260         str.append(':');
261         str.append(ex.getLineNumber());
262         str.append(':');
263         str.append(ex.getColumnNumber());
264
265         return str.toString();
266     }
267     // getLocationString(SAXParseException):String
268
}
269
Popular Tags