KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > xml > XmlParser


1 /*
2  * $Id: XmlParser.java 2366 2006-09-14 23:10:58Z xlv $
3  * $Name$
4  *
5  * Copyright 2001, 2002 by Bruno Lowagie.
6  *
7  * The contents of this file are subject to the Mozilla Public License Version 1.1
8  * (the "License"); you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the License.
14  *
15  * The Original Code is 'iText, a free JAVA-PDF library'.
16  *
17  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19  * All Rights Reserved.
20  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
22  *
23  * Contributor(s): all the names of the contributors are added in the source code
24  * where applicable.
25  *
26  * Alternatively, the contents of this file may be used under the terms of the
27  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
28  * provisions of LGPL are applicable instead of those above. If you wish to
29  * allow use of your version of this file only under the terms of the LGPL
30  * License and not to allow others to use your version of this file under
31  * the MPL, indicate your decision by deleting the provisions above and
32  * replace them with the notice and other provisions required by the LGPL.
33  * If you do not delete the provisions above, a recipient may use your version
34  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Library General Public License as published by the Free Software Foundation;
39  * either version 2 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
44  * details.
45  *
46  * If you didn't download this code from the following link, you should check if
47  * you aren't using an obsolete version:
48  * http://www.lowagie.com/iText/
49  */

50
51 package com.lowagie.text.xml;
52
53 import java.io.IOException JavaDoc;
54 import java.io.InputStream JavaDoc;
55 import java.io.Reader JavaDoc;
56 import java.util.HashMap JavaDoc;
57
58 import javax.xml.parsers.ParserConfigurationException JavaDoc;
59 import javax.xml.parsers.SAXParser JavaDoc;
60 import javax.xml.parsers.SAXParserFactory JavaDoc;
61
62 import org.xml.sax.InputSource JavaDoc;
63 import org.xml.sax.SAXException JavaDoc;
64
65 import com.lowagie.text.DocListener;
66 import com.lowagie.text.ExceptionConverter;
67
68 /**
69  * This class can be used to parse an XML file.
70  */

71
72 public class XmlParser {
73     
74 /** This is the instance of the parser. */
75     protected SAXParser JavaDoc parser;
76     
77 /**
78  * Constructs an XmlParser.
79  */

80     
81     public XmlParser() {
82         try {
83             parser = SAXParserFactory.newInstance().newSAXParser();
84         }
85         catch(ParserConfigurationException JavaDoc pce) {
86             throw new ExceptionConverter(pce);
87         }
88         catch(SAXException JavaDoc se) {
89             throw new ExceptionConverter(se);
90         }
91     }
92     
93 /**
94  * Parses a given file.
95  * @param document The document that will listen to the parser
96  * @param is The InputStream with the contents
97  */

98     
99     public void go(DocListener document, InputSource JavaDoc is) {
100         try {
101             parser.parse(is, new SAXiTextHandler(document));
102         }
103         catch(SAXException JavaDoc se) {
104             throw new ExceptionConverter(se);
105         }
106         catch(IOException JavaDoc ioe) {
107             throw new ExceptionConverter(ioe);
108         }
109     }
110     
111 /**
112  * Parses a given file.
113  * @param document The document that will listen to the parser
114  * @param is The inputsource with the content
115  * @param tagmap A userdefined tagmap
116  */

117     
118     public void go(DocListener document, InputSource JavaDoc is, String JavaDoc tagmap) {
119         try {
120             parser.parse(is, new SAXmyHandler(document, new TagMap(tagmap)));
121         }
122         catch(SAXException JavaDoc se) {
123             throw new ExceptionConverter(se);
124         }
125         catch(IOException JavaDoc ioe) {
126             throw new ExceptionConverter(ioe);
127         }
128     }
129     
130     /**
131      * Parses a given file.
132      * @param document The document that will listen to the parser
133      * @param is the inputsource with the content
134      * @param tagmap an inputstream to a userdefined tagmap
135      */

136         
137         public void go(DocListener document, InputSource JavaDoc is, InputStream JavaDoc tagmap) {
138             try {
139                 parser.parse(is, new SAXmyHandler(document, new TagMap(tagmap)));
140             }
141             catch(SAXException JavaDoc se) {
142                 throw new ExceptionConverter(se);
143             }
144             catch(IOException JavaDoc ioe) {
145                 throw new ExceptionConverter(ioe);
146             }
147         }
148     
149 /**
150  * Parses a given file.
151  * @param document The document that will listen to the parser
152  * @param is the inputsource with the content
153  * @param tagmap a userdefined tagmap
154  */

155     
156     public void go(DocListener document, InputSource JavaDoc is, HashMap JavaDoc tagmap) {
157         try {
158             parser.parse(is, new SAXmyHandler(document, tagmap));
159         }
160         catch(SAXException JavaDoc se) {
161             throw new ExceptionConverter(se);
162         }
163         catch(IOException JavaDoc ioe) {
164             throw new ExceptionConverter(ioe);
165         }
166     }
167     
168 /**
169  * Parses a given file.
170  * @param document The document that will listen to the parser
171  * @param file The path to a file with the content
172  */

173     
174     public void go(DocListener document, String JavaDoc file) {
175         try {
176             parser.parse(file, new SAXiTextHandler(document));
177         }
178         catch(SAXException JavaDoc se) {
179             throw new ExceptionConverter(se);
180         }
181         catch(IOException JavaDoc ioe) {
182             throw new ExceptionConverter(ioe);
183         }
184     }
185     
186 /**
187  * Parses a given file.
188  * @param document the document that will listen to the parser
189  * @param file the path to a file with the content
190  * @param tagmap a userdefined tagmap
191  */

192     
193     public void go(DocListener document, String JavaDoc file, String JavaDoc tagmap) {
194         try {
195             parser.parse(file, new SAXmyHandler(document, new TagMap(tagmap)));
196         }
197         catch(SAXException JavaDoc se) {
198             throw new ExceptionConverter(se);
199         }
200         catch(IOException JavaDoc ioe) {
201             throw new ExceptionConverter(ioe);
202         }
203     }
204     
205 /**
206  * Parses a given file.
207  * @param document The document that will listen to the parser
208  * @param file the path to a file with the content
209  * @param tagmap a userdefined tagmap
210  */

211     
212     public void go(DocListener document, String JavaDoc file, HashMap JavaDoc tagmap) {
213         try {
214             parser.parse(file, new SAXmyHandler(document, tagmap));
215         }
216         catch(SAXException JavaDoc se) {
217             throw new ExceptionConverter(se);
218         }
219         catch(IOException JavaDoc ioe) {
220             throw new ExceptionConverter(ioe);
221         }
222     }
223     
224 /**
225  * Parses a given file that validates with the iText DTD and writes the content to a document.
226  * @param document The document that will listen to the parser
227  * @param is the inputsource with the content
228  */

229     
230     public static void parse(DocListener document, InputSource JavaDoc is) {
231         XmlParser p = new XmlParser();
232         p.go(document, is);
233     }
234     
235 /**
236  * Parses a given file that validates with the iText DTD and writes the content to a document.
237  * @param document The document that will listen to the parser
238  * @param is The inputsource with the content
239  * @param tagmap a userdefined tagmap
240  */

241     
242     public static void parse(DocListener document, InputSource JavaDoc is, String JavaDoc tagmap) {
243         XmlParser p = new XmlParser();
244         p.go(document, is, tagmap);
245     }
246     
247 /**
248  * Parses a given file and writes the content to a document, using a certain tagmap.
249  * @param document The document that will listen to the parser
250  * @param is The inputsource with the content
251  * @param tagmap a userdefined tagmap
252  */

253     
254     public static void parse(DocListener document, InputSource JavaDoc is, HashMap JavaDoc tagmap) {
255         XmlParser p = new XmlParser();
256         p.go(document, is, tagmap);
257     }
258     
259 /**
260  * Parses a given file that validates with the iText DTD and writes the content to a document.
261  * @param document The document that will listen to the parser
262  * @param file The path to a file with the content
263  */

264     
265     public static void parse(DocListener document, String JavaDoc file) {
266         XmlParser p = new XmlParser();
267         p.go(document, file);
268     }
269     
270 /**
271  * Parses a given file that validates with the iText DTD and writes the content to a document.
272  * @param document The document that will listen to the parser
273  * @param file The path to a file with the content
274  * @param tagmap A userdefined tagmap
275  */

276     
277     public static void parse(DocListener document, String JavaDoc file, String JavaDoc tagmap) {
278         XmlParser p = new XmlParser();
279         p.go(document, file, tagmap);
280     }
281     
282 /**
283  * Parses a given file and writes the content to a document, using a certain tagmap.
284  * @param document The document that will listen to the parser
285  * @param file The path to a file with the content
286  * @param tagmap A userdefined tagmap
287  */

288     
289     public static void parse(DocListener document, String JavaDoc file, HashMap JavaDoc tagmap) {
290         XmlParser p = new XmlParser();
291         p.go(document, file, tagmap);
292     }
293     
294 /**
295  * Parses a given file that validates with the iText DTD and writes the content to a document.
296  * @param document The document that will listen to the parser
297  * @param is The inputsource with the content
298  */

299     
300     public static void parse(DocListener document, InputStream JavaDoc is) {
301         XmlParser p = new XmlParser();
302         p.go(document, new InputSource JavaDoc(is));
303     }
304     
305 /**
306  * Parses a given file that validates with the iText DTD and writes the content to a document.
307  * @param document The document that will listen to the parser
308  * @param is The inputstream with the content
309  * @param tagmap A userdefined tagmap
310  */

311     
312     public static void parse(DocListener document, InputStream JavaDoc is, String JavaDoc tagmap) {
313         XmlParser p = new XmlParser();
314         p.go(document, new InputSource JavaDoc(is), tagmap);
315     }
316     
317 /**
318  * Parses a given file and writes the content to a document, using a certain tagmap.
319  * @param document The document that will listen to the parser
320  * @param is The InputStream with the content
321  * @param tagmap A userdefined tagmap
322  */

323     
324     public static void parse(DocListener document, InputStream JavaDoc is, HashMap JavaDoc tagmap) {
325         XmlParser p = new XmlParser();
326         p.go(document, new InputSource JavaDoc(is), tagmap);
327     }
328     
329 /**
330  * Parses a given file that validates with the iText DTD and writes the content to a document.
331  * @param document The document that will listen to the parser
332  * @param is The reader that reads the content
333  */

334     
335     public static void parse(DocListener document, Reader JavaDoc is) {
336         XmlParser p = new XmlParser();
337         p.go(document, new InputSource JavaDoc(is));
338     }
339     
340 /**
341  * Parses a given file that validates with the iText DTD and writes the content to a document.
342  * @param document The document that will listen to the parser
343  * @param is The reader that reads the content
344  * @param tagmap A userdefined tagmap
345  */

346     
347     public static void parse(DocListener document, Reader JavaDoc is, String JavaDoc tagmap) {
348         XmlParser p = new XmlParser();
349         p.go(document, new InputSource JavaDoc(is), tagmap);
350     }
351     
352 /**
353  * Parses a given file and writes the content to a document, using a certain tagmap.
354  * @param document The document that will listen to the parser
355  * @param is The reader that reads the content
356  * @param tagmap A userdefined tagmap
357  */

358     
359     public static void parse(DocListener document, Reader JavaDoc is, HashMap JavaDoc tagmap) {
360         XmlParser p = new XmlParser();
361         p.go(document, new InputSource JavaDoc(is), tagmap);
362     }
363 }
Popular Tags