KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdf > arp > SingleThreadedParser


1 /*
2  * (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP
3  * [See end of file]
4  */

5
6 package com.hp.hpl.jena.rdf.arp;
7
8 import com.hp.hpl.jena.util.CharEncoding;
9
10 import java.io.IOException JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import java.io.InputStreamReader JavaDoc;
13 import java.io.Reader JavaDoc;
14 import java.io.UTFDataFormatException JavaDoc;
15 import java.nio.charset.Charset JavaDoc;
16
17 import org.apache.xerces.parsers.SAXParser;
18 import org.apache.xerces.parsers.StandardParserConfiguration;
19 import org.apache.xerces.util.EncodingMap;
20 import org.apache.xerces.xni.Augmentations;
21 import org.apache.xerces.xni.parser.XMLInputSource;
22 import org.apache.xerces.xni.parser.XMLPullParserConfiguration;
23 import org.xml.sax.*;
24
25 /**
26  *
27  * This parser uses the Xerces pull parser configuration, and runs in a single
28  * thread. Hence it is preferred over the SAX2RDF parser, which needs two
29  * threads.
30  *
31  * @author Jeremy J. Carroll
32  *
33  */

34 class SingleThreadedParser extends XMLHandler {
35
36     private XMLPullParserConfiguration pullParser;
37
38     private SAXParser saxParser;
39
40     private String JavaDoc readerXMLEncoding = null;
41
42     private String JavaDoc xmlEncoding = null;
43
44     private SingleThreadedParser(SAXParser rdr,
45             XMLPullParserConfiguration config) {
46         super();
47         pullParser = config;
48         saxParser = rdr;
49         try {
50             SAX2RDF.installHandlers(rdr, this);
51         } catch (SAXException e) {
52             throw new RuntimeException JavaDoc("Supposedly impossible:", e);
53         }
54         // setErrorHandler(new DefaultErrorHandler());
55
}
56
57     SAXParser getSAXParser() {
58         return saxParser;
59     }
60
61     static private class MySAXParser extends SAXParser {
62         MySAXParser(StandardParserConfiguration c) {
63             super(c);
64             try {
65                 setFeature("http://xml.org/sax/features/string-interning",
66                         false);
67             } catch (SAXException e) {
68                 // Not supported - aggh
69
// TODO ask on xerces list why not?
70
// e.printStackTrace();
71
}
72         }
73
74         SingleThreadedParser a;
75
76         public void xmlDecl(String JavaDoc version, String JavaDoc encoding, String JavaDoc standalone,
77                 Augmentations augs) {
78             a.setEncoding(encoding == null ? "UTF" : encoding);
79             super.xmlDecl(version, encoding, standalone, augs);
80         }
81         /*
82          * public void startDocument(XMLLocator locator, java.lang.String
83          * encoding, NamespaceContext namespaceContext, Augmentations augs) {
84          * a.setEncoding(encoding);
85          * super.startDocument(locator,encoding,namespaceContext,augs); }
86          *
87          */

88     }
89
90     static SingleThreadedParser create() {
91         StandardParserConfiguration c = new StandardParserConfiguration();
92         MySAXParser msp = new MySAXParser(c);
93         SingleThreadedParser a = new SingleThreadedParser(msp, c);
94         msp.a = a;
95         return a;
96     }
97
98     boolean parseSome() {
99         try {
100             return pullParser.parse(false);
101         } catch (UTFDataFormatException JavaDoc e) {
102             try {
103                 generalError(ERR_UTF_ENCODING, e);
104             } catch (SAXParseException e1) {
105                 // e1.printStackTrace();
106
}
107             return false;
108         } catch (IOException JavaDoc e) {
109             try {
110                 generalError(ERR_GENERIC_IO, e);
111             } catch (SAXParseException e1) {
112                 // e1.printStackTrace();
113
}
114             return false;
115         } catch (FatalParsingErrorException e) {
116             return false;
117         }
118     }
119
120     RDFParser rdfParser;
121
122     synchronized public void parse(InputSource input) throws IOException JavaDoc,
123             SAXException {
124         parse(input, input.getSystemId());
125     }
126
127     synchronized public void parse(InputSource input, String JavaDoc base)
128             throws IOException JavaDoc, SAXException {
129         // Make sure we have a sane state for
130
// Namespace processing.
131
initParse(base);
132         // Start the RDFParser
133
pipe = new PullingTokenPipe(this);
134         pullParser.setInputSource(convert(input));
135
136         SAX2RDF.installHandlers(saxParser, this);
137         saxParser.reset();
138
139         // initEncodingChecks();
140
try {
141             try {
142                 rdfParser = new RDFParser(pipe, SingleThreadedParser.this);
143                 if (getOptions().getEmbedding())
144                     rdfParser.embeddedFile(documentContext);
145                 else
146                     rdfParser.rdfFile(documentContext);
147             } catch (WrappedException wrapped) {
148                 wrapped.throwMe();
149             } catch (ParseException parse) {
150                 // This has not been reported???
151
// TODO more work on error reporting
152

153                 userError(parse);
154                 // Don't overdo it.
155
//throw parse.rootCause();
156

157             }
158         } finally {
159             endBnodeScope();
160         }
161
162     }
163
164     XMLInputSource convert(InputSource in) {
165         Reader JavaDoc rdr = in.getCharacterStream();
166         InputStream JavaDoc str = in.getByteStream();
167         String JavaDoc publicID = in.getPublicId();
168         String JavaDoc systemID = in.getSystemId();
169         readerXMLEncoding = null;
170         encodingProblems = false;
171         if (rdr == null && str == null) {
172             return new XMLInputSource(publicID, systemID, systemID);
173         } else if (rdr == null) {
174             return new XMLInputSource(publicID, systemID, systemID, str, null);
175         } else if (str == null) {
176             if (rdr instanceof InputStreamReader JavaDoc) {
177                 String JavaDoc javaEnc = ((InputStreamReader JavaDoc) rdr).getEncoding();
178                 readerXMLEncoding = CharEncoding.create(javaEnc).name();
179             }
180             return new XMLInputSource(publicID, systemID, systemID, rdr, null);
181         }
182         return null;
183     }
184
185     void setEncoding(String JavaDoc original) {
186
187         CharEncoding encodingInfo = CharEncoding.create(original);
188         String JavaDoc e = encodingInfo.name();
189         // System.err.println("xmlEncoding = " + e);
190
if (xmlEncoding == null) {
191             // special case UTF-8 or UTF-16?
192
if (e.equals("UTF") && readerXMLEncoding != null
193                     && readerXMLEncoding.startsWith("UTF")) {
194                 xmlEncoding = readerXMLEncoding;
195                 return;
196             }
197             xmlEncoding = e;
198             try {
199
200                 if (readerXMLEncoding != null
201                         && !readerXMLEncoding.equalsIgnoreCase(e)) {
202                     putWarning(
203                             WARN_ENCODING_MISMATCH,
204                             new Location(locator),
205                             "Encoding on InputStreamReader or FileReader does not match that of XML document. Use FileInputStream. ["
206                                     + readerXMLEncoding + " != " + e + "]");
207                     encodingProblems = true;
208                 }
209
210                 if (e.equals("UTF"))
211                     return;
212                 
213                 if (!encodingInfo.isIANA()) {
214                     putWarning(
215                             encodingInfo.isInNIO() ? WARN_NON_IANA_ENCODING
216                                     : WARN_UNSUPPORTED_ENCODING, new Location(
217                                     locator), encodingInfo.warningMessage());
218                 } else if (!original.equalsIgnoreCase(e)) {
219                     putWarning(
220                             WARN_NONCANONICAL_IANA_NAME,
221                             new Location(locator),
222                             "The encoding \""
223                                     + original
224                                     + "\" is not the canonical name at IANA, suggest \""
225                                     + e
226                                     + "\" would give more interoperability.");
227
228                 }
229             } catch (SAXParseException e1) {
230                 //e1.printStackTrace();
231
}
232
233         }
234     }
235
236 }
237
238 /*
239  * (c) Copyright 2004, 2005 Hewlett-Packard Development Company, LP All rights
240  * reserved.
241  *
242  * Redistribution and use in source and binary forms, with or without
243  * modification, are permitted provided that the following conditions are met:
244  * 1. Redistributions of source code must retain the above copyright notice,
245  * this list of conditions and the following disclaimer. 2. Redistributions in
246  * binary form must reproduce the above copyright notice, this list of
247  * conditions and the following disclaimer in the documentation and/or other
248  * materials provided with the distribution. 3. The name of the author may not
249  * be used to endorse or promote products derived from this software without
250  * specific prior written permission.
251  *
252  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
253  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
254  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
255  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
256  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
257  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
258  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
259  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
260  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
261  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
262  */

263
264
Popular Tags