KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > filesystems > DefaultParser


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.filesystems;
21
22 import java.io.*;
23 import java.util.logging.Level JavaDoc;
24 import java.util.logging.Logger JavaDoc;
25
26 import org.xml.sax.*;
27 import org.xml.sax.helpers.*;
28 import org.openide.filesystems.*;
29 import org.openide.util.*;
30 import org.openide.xml.*;
31 import org.openide.*;
32
33
34 /**
35  * Implements default interruptible silent parser behaviour.
36  * Errors can be tested by quering parser state.
37  *
38  * @author Petr Kuzel
39  * @version 1.0
40  */

41 abstract class DefaultParser extends DefaultHandler {
42
43     protected FileObject fo;
44     private Locator locator = null;
45
46     protected short state = INIT;
47
48     protected static final short PARSED = 1000;
49     protected static final short ERROR = -1;
50     protected static final short INIT = 0;
51
52     protected DefaultParser() {
53     }
54     
55     protected DefaultParser(FileObject fo) {
56         this.fo = fo;
57     }
58
59     /**
60      * Preconfigure parser and return it.
61      */

62     protected XMLReader createXMLReader() throws IOException, SAXException {
63         return XMLUtil.createXMLReader(false);
64     }
65
66     /**
67      * Check if the given exception is one thrown from the handler
68      * for stopping the parser.
69      */

70     protected boolean isStopException(Exception JavaDoc e) {
71         return false;
72     }
73
74     /**
75      * @return current parser state
76      */

77     protected short getState() {
78         return state;
79     }
80
81     protected final Locator getLocator() {
82         return locator;
83     }
84     
85     /**
86      * Parser content workarounding known parser implementation
87      * problems.
88      */

89     protected void parse(FileObject fo) {
90         state = INIT; // #15672
91
InputStream is = null;
92         this.fo = fo;
93         try {
94             XMLReader parser = createXMLReader();
95             parser.setEntityResolver(this);
96             parser.setErrorHandler(this);
97             parser.setContentHandler(this);
98
99 // try {
100
// // do not read DTD
101
// parser.setFeature("http://xml.org/sax/features/external-parameter-entities", false); //NOI18N
102
// } catch (SAXException ignore) {
103
// // parsing may be slower :-(
104
// }
105

106             InputSource in = new InputSource();
107             is = fo.getInputStream();
108             in.setByteStream(is);
109             in.setSystemId(fo.getURL().toExternalForm());
110             customizeInputSource(in);
111             
112             parser.parse(in);
113
114         } catch (IOException io) {
115             if (!isStopException(io)) {
116                 if (fo.isValid() && fo.canRead()) {
117                     Exceptions.attachMessage(io, "While parsing: " + fo); // NOI18N
118
Logger.getLogger(DefaultParser.class.getName()).log(Level.INFO, null, io);
119                     state = ERROR;
120                 }
121             }
122         } catch (SAXException sex) {
123             if (!isStopException(sex)) {
124                 Exceptions.attachMessage(sex, "While parsing: " + fo); // NOI18N
125
Logger.getLogger(DefaultParser.class.getName()).log(Level.INFO, null, sex);
126                 state = ERROR;
127             }
128         } finally {
129             if (is != null) {
130                 try {
131                     is.close();
132                 } catch (IOException ex) {
133                     // already closed
134
}
135             }
136         }
137     }
138
139
140     protected void customizeInputSource(InputSource in) {
141     }
142     
143     /**
144      * Parser default file object
145      */

146     protected final void parse() {
147         if (fo == null) throw new NullPointerException JavaDoc();
148         parse(fo);
149     }
150
151     /** Report error occured during custom validation. */
152     protected void error() throws SAXException {
153         String JavaDoc reason = org.openide.util.NbBundle.getMessage(DefaultParser.class, "Invalid_XML_document");
154         error(reason);
155     }
156
157     /** Report error occured during custom validation. */
158     protected void error(String JavaDoc reason) throws SAXException {
159         StringBuffer JavaDoc buf = new StringBuffer JavaDoc (reason).append(": ").append(fo.toString());//NOI18N
160
if (locator != null) {
161             buf.append(" line: ").append(locator.getLineNumber());//NOI18N
162
buf.append(" column: ").append(locator.getColumnNumber());//NOI18N
163
}
164         String JavaDoc msg = buf.toString(); //NOI18N
165
SAXException sex = new SAXException(msg);
166         throw sex;
167     }
168
169     public void error(SAXParseException exception) throws SAXException {
170         throw exception;
171     }
172
173     public void fatalError(SAXParseException exception) throws SAXException {
174         throw exception;
175     }
176
177     public void endDocument() throws SAXException {
178         state = PARSED;
179     }
180
181     public void setDocumentLocator(Locator locator) {
182         this.locator = locator;
183     }
184
185     public InputSource resolveEntity (String JavaDoc publicID, String JavaDoc systemID) {
186         // Read nothing whatsoever.
187
return new InputSource (new ByteArrayInputStream (new byte[] { }));
188     }
189     
190 }
191
Popular Tags