001: /*
002: * $Id: XmlParser.java 2366 2006-09-14 23:10:58Z xlv $
003: * $Name$
004: *
005: * Copyright 2001, 2002 by Bruno Lowagie.
006: *
007: * The contents of this file are subject to the Mozilla Public License Version 1.1
008: * (the "License"); you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the License.
014: *
015: * The Original Code is 'iText, a free JAVA-PDF library'.
016: *
017: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
018: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
019: * All Rights Reserved.
020: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
021: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
022: *
023: * Contributor(s): all the names of the contributors are added in the source code
024: * where applicable.
025: *
026: * Alternatively, the contents of this file may be used under the terms of the
027: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
028: * provisions of LGPL are applicable instead of those above. If you wish to
029: * allow use of your version of this file only under the terms of the LGPL
030: * License and not to allow others to use your version of this file under
031: * the MPL, indicate your decision by deleting the provisions above and
032: * replace them with the notice and other provisions required by the LGPL.
033: * If you do not delete the provisions above, a recipient may use your version
034: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
035: *
036: * This library is free software; you can redistribute it and/or modify it
037: * under the terms of the MPL as stated above or under the terms of the GNU
038: * Library General Public License as published by the Free Software Foundation;
039: * either version 2 of the License, or any later version.
040: *
041: * This library is distributed in the hope that it will be useful, but WITHOUT
042: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
043: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
044: * details.
045: *
046: * If you didn't download this code from the following link, you should check if
047: * you aren't using an obsolete version:
048: * http://www.lowagie.com/iText/
049: */
050:
051: package com.lowagie.text.xml;
052:
053: import java.io.IOException;
054: import java.io.InputStream;
055: import java.io.Reader;
056: import java.util.HashMap;
057:
058: import javax.xml.parsers.ParserConfigurationException;
059: import javax.xml.parsers.SAXParser;
060: import javax.xml.parsers.SAXParserFactory;
061:
062: import org.xml.sax.InputSource;
063: import org.xml.sax.SAXException;
064:
065: import com.lowagie.text.DocListener;
066: import com.lowagie.text.ExceptionConverter;
067:
068: /**
069: * This class can be used to parse an XML file.
070: */
071:
072: public class XmlParser {
073:
074: /** This is the instance of the parser. */
075: protected SAXParser parser;
076:
077: /**
078: * Constructs an XmlParser.
079: */
080:
081: public XmlParser() {
082: try {
083: parser = SAXParserFactory.newInstance().newSAXParser();
084: } catch (ParserConfigurationException pce) {
085: throw new ExceptionConverter(pce);
086: } catch (SAXException se) {
087: throw new ExceptionConverter(se);
088: }
089: }
090:
091: /**
092: * Parses a given file.
093: * @param document The document that will listen to the parser
094: * @param is The InputStream with the contents
095: */
096:
097: public void go(DocListener document, InputSource is) {
098: try {
099: parser.parse(is, new SAXiTextHandler(document));
100: } catch (SAXException se) {
101: throw new ExceptionConverter(se);
102: } catch (IOException ioe) {
103: throw new ExceptionConverter(ioe);
104: }
105: }
106:
107: /**
108: * Parses a given file.
109: * @param document The document that will listen to the parser
110: * @param is The inputsource with the content
111: * @param tagmap A userdefined tagmap
112: */
113:
114: public void go(DocListener document, InputSource is, String tagmap) {
115: try {
116: parser.parse(is, new SAXmyHandler(document, new TagMap(
117: tagmap)));
118: } catch (SAXException se) {
119: throw new ExceptionConverter(se);
120: } catch (IOException ioe) {
121: throw new ExceptionConverter(ioe);
122: }
123: }
124:
125: /**
126: * Parses a given file.
127: * @param document The document that will listen to the parser
128: * @param is the inputsource with the content
129: * @param tagmap an inputstream to a userdefined tagmap
130: */
131:
132: public void go(DocListener document, InputSource is,
133: InputStream tagmap) {
134: try {
135: parser.parse(is, new SAXmyHandler(document, new TagMap(
136: tagmap)));
137: } catch (SAXException se) {
138: throw new ExceptionConverter(se);
139: } catch (IOException ioe) {
140: throw new ExceptionConverter(ioe);
141: }
142: }
143:
144: /**
145: * Parses a given file.
146: * @param document The document that will listen to the parser
147: * @param is the inputsource with the content
148: * @param tagmap a userdefined tagmap
149: */
150:
151: public void go(DocListener document, InputSource is, HashMap tagmap) {
152: try {
153: parser.parse(is, new SAXmyHandler(document, tagmap));
154: } catch (SAXException se) {
155: throw new ExceptionConverter(se);
156: } catch (IOException ioe) {
157: throw new ExceptionConverter(ioe);
158: }
159: }
160:
161: /**
162: * Parses a given file.
163: * @param document The document that will listen to the parser
164: * @param file The path to a file with the content
165: */
166:
167: public void go(DocListener document, String file) {
168: try {
169: parser.parse(file, new SAXiTextHandler(document));
170: } catch (SAXException se) {
171: throw new ExceptionConverter(se);
172: } catch (IOException ioe) {
173: throw new ExceptionConverter(ioe);
174: }
175: }
176:
177: /**
178: * Parses a given file.
179: * @param document the document that will listen to the parser
180: * @param file the path to a file with the content
181: * @param tagmap a userdefined tagmap
182: */
183:
184: public void go(DocListener document, String file, String tagmap) {
185: try {
186: parser.parse(file, new SAXmyHandler(document, new TagMap(
187: tagmap)));
188: } catch (SAXException se) {
189: throw new ExceptionConverter(se);
190: } catch (IOException ioe) {
191: throw new ExceptionConverter(ioe);
192: }
193: }
194:
195: /**
196: * Parses a given file.
197: * @param document The document that will listen to the parser
198: * @param file the path to a file with the content
199: * @param tagmap a userdefined tagmap
200: */
201:
202: public void go(DocListener document, String file, HashMap tagmap) {
203: try {
204: parser.parse(file, new SAXmyHandler(document, tagmap));
205: } catch (SAXException se) {
206: throw new ExceptionConverter(se);
207: } catch (IOException ioe) {
208: throw new ExceptionConverter(ioe);
209: }
210: }
211:
212: /**
213: * Parses a given file that validates with the iText DTD and writes the content to a document.
214: * @param document The document that will listen to the parser
215: * @param is the inputsource with the content
216: */
217:
218: public static void parse(DocListener document, InputSource is) {
219: XmlParser p = new XmlParser();
220: p.go(document, is);
221: }
222:
223: /**
224: * Parses a given file that validates with the iText DTD and writes the content to a document.
225: * @param document The document that will listen to the parser
226: * @param is The inputsource with the content
227: * @param tagmap a userdefined tagmap
228: */
229:
230: public static void parse(DocListener document, InputSource is,
231: String tagmap) {
232: XmlParser p = new XmlParser();
233: p.go(document, is, tagmap);
234: }
235:
236: /**
237: * Parses a given file and writes the content to a document, using a certain tagmap.
238: * @param document The document that will listen to the parser
239: * @param is The inputsource with the content
240: * @param tagmap a userdefined tagmap
241: */
242:
243: public static void parse(DocListener document, InputSource is,
244: HashMap tagmap) {
245: XmlParser p = new XmlParser();
246: p.go(document, is, tagmap);
247: }
248:
249: /**
250: * Parses a given file that validates with the iText DTD and writes the content to a document.
251: * @param document The document that will listen to the parser
252: * @param file The path to a file with the content
253: */
254:
255: public static void parse(DocListener document, String file) {
256: XmlParser p = new XmlParser();
257: p.go(document, file);
258: }
259:
260: /**
261: * Parses a given file that validates with the iText DTD and writes the content to a document.
262: * @param document The document that will listen to the parser
263: * @param file The path to a file with the content
264: * @param tagmap A userdefined tagmap
265: */
266:
267: public static void parse(DocListener document, String file,
268: String tagmap) {
269: XmlParser p = new XmlParser();
270: p.go(document, file, tagmap);
271: }
272:
273: /**
274: * Parses a given file and writes the content to a document, using a certain tagmap.
275: * @param document The document that will listen to the parser
276: * @param file The path to a file with the content
277: * @param tagmap A userdefined tagmap
278: */
279:
280: public static void parse(DocListener document, String file,
281: HashMap tagmap) {
282: XmlParser p = new XmlParser();
283: p.go(document, file, tagmap);
284: }
285:
286: /**
287: * Parses a given file that validates with the iText DTD and writes the content to a document.
288: * @param document The document that will listen to the parser
289: * @param is The inputsource with the content
290: */
291:
292: public static void parse(DocListener document, InputStream is) {
293: XmlParser p = new XmlParser();
294: p.go(document, new InputSource(is));
295: }
296:
297: /**
298: * Parses a given file that validates with the iText DTD and writes the content to a document.
299: * @param document The document that will listen to the parser
300: * @param is The inputstream with the content
301: * @param tagmap A userdefined tagmap
302: */
303:
304: public static void parse(DocListener document, InputStream is,
305: String tagmap) {
306: XmlParser p = new XmlParser();
307: p.go(document, new InputSource(is), tagmap);
308: }
309:
310: /**
311: * Parses a given file and writes the content to a document, using a certain tagmap.
312: * @param document The document that will listen to the parser
313: * @param is The InputStream with the content
314: * @param tagmap A userdefined tagmap
315: */
316:
317: public static void parse(DocListener document, InputStream is,
318: HashMap tagmap) {
319: XmlParser p = new XmlParser();
320: p.go(document, new InputSource(is), tagmap);
321: }
322:
323: /**
324: * Parses a given file that validates with the iText DTD and writes the content to a document.
325: * @param document The document that will listen to the parser
326: * @param is The reader that reads the content
327: */
328:
329: public static void parse(DocListener document, Reader is) {
330: XmlParser p = new XmlParser();
331: p.go(document, new InputSource(is));
332: }
333:
334: /**
335: * Parses a given file that validates with the iText DTD and writes the content to a document.
336: * @param document The document that will listen to the parser
337: * @param is The reader that reads the content
338: * @param tagmap A userdefined tagmap
339: */
340:
341: public static void parse(DocListener document, Reader is,
342: String tagmap) {
343: XmlParser p = new XmlParser();
344: p.go(document, new InputSource(is), tagmap);
345: }
346:
347: /**
348: * Parses a given file and writes the content to a document, using a certain tagmap.
349: * @param document The document that will listen to the parser
350: * @param is The reader that reads the content
351: * @param tagmap A userdefined tagmap
352: */
353:
354: public static void parse(DocListener document, Reader is,
355: HashMap tagmap) {
356: XmlParser p = new XmlParser();
357: p.go(document, new InputSource(is), tagmap);
358: }
359: }
|