001: package com.rimfaxe.xml.xmlreader;
002:
003: import java.io.*;
004:
005: /**
006: * Provides a simple interface to the XML parser.
007: * This is an example of the Facade design pattern.
008: *
009: * There are two types of parse methods: DOM methods which parse into a DOM tree
010: * and SAX methods in which the parser calls a client-provided hander. The XML can be proved
011: * either as characters (in a String, character array, or Reader) or as bytes
012: * (in a File, byte array, or InputStream). In the latter case the Unicode encoding is
013: * first guessed by looking at the first few characters and then possibly confirmed
014: * by reading an encoding declaration on the first line of the XML. In some cases the declared
015: * encoding may be different to the guessed encoding and the parser will have to re-start reading
016: * the bytes.
017:
018: <blockquote><small> Copyright (C) 2002 Hewlett-Packard Company.
019: This file is part of Sparta, an XML Parser, DOM, and XPath library.
020: This library is free software; you can redistribute it and/or
021: modify it under the terms of the GNU Lesser General Public License
022: as published by the Free Software Foundation; either version 2.1 of
023: the License, or (at your option) any later version. This library
024: is distributed in the hope that it will be useful, but WITHOUT ANY
025: WARRANTY; without even the implied warranty of MERCHANTABILITY or
026: FITNESS FOR A PARTICULAR PURPOSE. </small></blockquote>
027: @see <a "href="doc-files/LGPL.txt">GNU Lesser General Public License</a>
028: @version $Date: 2002/12/13 23:09:24 $ $Revision: 1.2 $
029: @author Eamonn O'Brien-Strain
030: @author Sergio Marti
031: */
032:
033: public class Parser {
034:
035: /* DOM parse calls */
036:
037: /** DOM parsing of XML in a character stream, using a default log. **/
038: static public Document parse(String systemId, Reader reader)
039: throws ParseException, IOException {
040: BuildDocument bd = new BuildDocument();
041: new ParseCharStream(systemId, reader, null, null, bd);
042: return bd.getDocument();
043: }
044:
045: /** DOM parsing of XML in a character stream. **/
046: static public Document parse(String systemId, Reader reader,
047: ParseLog log) throws ParseException, IOException {
048: BuildDocument bd = new BuildDocument();
049: new ParseCharStream(systemId, reader, log, null, bd);
050: return bd.getDocument();
051: }
052:
053: /** DOM parsing of XML in a file. **/
054: static public Document parse(File file, ParseLog log)
055: throws ParseException, IOException {
056: BuildDocument bd = new BuildDocument();
057: new ParseCharStream(toString(file), new FileReader(file), log,
058: null, bd);
059: return bd.getDocument();
060: }
061:
062: static private String toString(File file) {
063: String absPath = file.getAbsolutePath();
064: if (File.separatorChar != '/')
065: absPath = absPath.replace(File.separatorChar, '/');
066: if (file.isDirectory() && !absPath.endsWith("/"))
067: absPath = absPath + "/";
068: return "file:/" + absPath;
069: }
070:
071: /** DOM parsing of XML in a file, using a default log. **/
072: static public Document parse(File file) throws ParseException,
073: IOException {
074: BuildDocument bd = new BuildDocument();
075: new ParseCharStream(toString(file), new FileReader(file), null,
076: null, bd);
077: return bd.getDocument();
078: }
079:
080: /** DOM parsing of XML in a String. **/
081: static public Document parse(String xml) throws ParseException,
082: IOException {
083: BuildDocument bd = new BuildDocument();
084: new ParseCharStream("file:anonymous-string", new StringReader(
085: xml), null, null, bd);
086: return bd.getDocument();
087: }
088:
089: /** DOM parsing of XML in a character array (this is the fastest parse method). **/
090: static public Document parse(char[] xml) throws ParseException,
091: IOException {
092: BuildDocument bd = new BuildDocument();
093: new ParseCharStream("file:anonymous-string", xml, null, null,
094: bd);
095: return bd.getDocument();
096: }
097:
098: /**
099: * Parse XML to DOM, figuring out the encoding using the first few characters and possibly
100: * an encoding declaration on the first line of the XML.
101: * @param xml stored in an array of bytes in some Unicode encoding.
102: * @return the DOM Document resulting from the parsing
103: * @throws ParseException on parse error
104: */
105: static public Document parse(byte[] xml) throws ParseException,
106: IOException {
107: BuildDocument bd = new BuildDocument();
108: new ParseByteStream("file:anonymous-string",
109: new ByteArrayInputStream(xml), null, null, bd);
110: return bd.getDocument();
111:
112: }
113:
114: /** DOM parsing of XML in a character stream, specifying the Unicode encoding. */
115: static public Document parse(String systemId, Reader reader,
116: ParseLog log, String encoding) throws ParseException,
117: EncodingMismatchException, IOException {
118: BuildDocument bd = new BuildDocument();
119: new ParseCharStream(systemId, reader, log, encoding, bd);
120: return bd.getDocument();
121: }
122:
123: /** DOM parsing of XML encoded in a byte stream. */
124: static public Document parse(String systemId, InputStream istream,
125: ParseLog log) throws ParseException, IOException {
126: BuildDocument bd = new BuildDocument();
127: new ParseByteStream(systemId, istream, log, null, bd);
128: return bd.getDocument();
129: }
130:
131: /** DOM parsing of XML encoded in a byte stream, using a default log. */
132: static public Document parse(String systemId, InputStream istream)
133: throws ParseException, IOException {
134: BuildDocument bd = new BuildDocument();
135: new ParseByteStream(systemId, istream, null, null, bd);
136: return bd.getDocument();
137: }
138:
139: /** DOM parsing of XML encoded in a character stream, specifying the Unicode encoding. */
140: static public Document parse(String systemId, InputStream istream,
141: ParseLog log, String guessedEncoding)
142: throws ParseException, IOException {
143: BuildDocument bd = new BuildDocument();
144: new ParseByteStream(systemId, istream, log, guessedEncoding, bd);
145: return bd.getDocument();
146: }
147:
148: /* SAX parse calls. Must give own ParseHandler */
149:
150: /** SAX parsing of XML in character stream, using default log. */
151: static public void parse(String systemId, Reader reader,
152: ParseHandler ph) throws ParseException, IOException {
153: new ParseCharStream(systemId, reader, null, null, ph);
154: }
155:
156: /** SAX parsing of XML in character stream. */
157: static public void parse(String systemId, Reader reader,
158: ParseLog log, ParseHandler ph) throws ParseException,
159: IOException {
160: new ParseCharStream(systemId, reader, log, null, ph);
161: }
162:
163: /** SAX parsing of XML in a file. */
164: static public void parse(File file, ParseLog log, ParseHandler ph)
165: throws ParseException, IOException {
166: new ParseCharStream(toString(file), new FileReader(file), log,
167: null, ph);
168: }
169:
170: /** SAX parsing of XML in a file, using default log. */
171: static public void parse(File file, ParseHandler ph)
172: throws ParseException, IOException {
173: new ParseCharStream(toString(file), new FileReader(file), null,
174: null, ph);
175: }
176:
177: /** SAX parsing of XML in a string. */
178: static public void parse(String xml, ParseHandler ph)
179: throws ParseException, IOException {
180: new ParseCharStream("file:anonymous-string", new StringReader(
181: xml), null, null, ph);
182: }
183:
184: /** SAX parsing of XML in a character array. */
185: static public void parse(char[] xml, ParseHandler ph)
186: throws ParseException, IOException {
187: new ParseCharStream("file:anonymous-string", xml, null, null,
188: ph);
189: }
190:
191: /** SAX parsing of XML encoded in a byte array. */
192: static public void parse(byte[] xml, ParseHandler ph)
193: throws ParseException, IOException {
194: new ParseByteStream("file:anonymous-string",
195: new ByteArrayInputStream(xml), null, null, ph);
196: }
197:
198: /** SAX parsing of XML encoded in a byte stream, using default log. */
199: static public void parse(String systemId, InputStream istream,
200: ParseLog log, ParseHandler ph) throws ParseException,
201: IOException {
202: new ParseByteStream(systemId, istream, log, null, ph);
203: }
204:
205: /** SAX parsing of XML encoded in a byte stream. */
206: static public void parse(String systemId, InputStream istream,
207: ParseHandler ph) throws ParseException, IOException {
208: new ParseByteStream(systemId, istream, null, null, ph);
209: }
210:
211: /** SAX parsing of XML encoded in a byte stream, specifying the Unicode encoding. */
212: static public void parse(String systemId, InputStream istream,
213: ParseLog log, String guessedEncoding, ParseHandler ph)
214: throws ParseException, IOException {
215: new ParseByteStream(systemId, istream, log, guessedEncoding, ph);
216: }
217:
218: /** SAX parsing of XML encoded in a character stream, specifying the Unicode encoding. */
219: static public void parse(String systemId, Reader reader,
220: ParseLog log, String encoding, ParseHandler ph)
221: throws ParseException, EncodingMismatchException,
222: IOException {
223: new ParseCharStream(systemId, reader, log, encoding, ph);
224: }
225: }
226:
227: // $Log: Parser.java,v $
228: // Revision 1.2 2002/12/13 23:09:24 eobrain
229: // Fix javadoc.
230: //
231: // Revision 1.1.1.1 2002/08/19 05:03:57 eobrain
232: // import from HP Labs internal CVS
233: //
234: // Revision 1.8 2002/08/18 04:42:25 eob
235: // Add copyright and other formatting and commenting in preparation for
236: // release to SourceForge.
237: //
238: // Revision 1.7 2002/08/17 00:54:14 sermarti
239: //
240: // Revision 1.6 2002/08/15 23:40:22 sermarti
241: //
242: // Revision 1.5 2002/08/05 20:04:32 sermarti
243: //
244: // Revision 1.4 2002/07/25 21:10:15 sermarti
245: // Adding files that mysteriously weren't added from Sparta before.
246: //
247: // Revision 1.3 2002/07/11 21:06:41 eob
248: // Add parse(String) and parse(byte[])
249: //
250: // Revision 1.2 2002/06/13 19:14:44 eob
251: // Add a convenience constructor.
252: //
253: // Revision 1.1 2002/03/21 23:48:54 eob
254: // initial
|