| StringSAXEventProvider.java |
1 /**
2 * Distribution License:
3 * JSword is free software; you can redistribute it and/or modify it under
4 * the terms of the GNU Lesser General Public License, version 2.1 or later
5 * as published by the Free Software Foundation. This program is distributed
6 * in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
7 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
8 * See the GNU Lesser General Public License for more details.
9 *
10 * The License is available on the internet at:
11 * http://www.gnu.org/copyleft/lgpl.html
12 * or by writing to:
13 * Free Software Foundation, Inc.
14 * 59 Temple Place - Suite 330
15 * Boston, MA 02111-1307, USA
16 *
17 * © CrossWire Bible Society, 2005 - 2016
18 *
19 */
20 package org.crosswire.common.xml;
21
22 import java.io.IOException;
23 import java.io.StringReader;
24
25 import javax.xml.parsers.ParserConfigurationException;
26 import javax.xml.parsers.SAXParser;
27 import javax.xml.parsers.SAXParserFactory;
28
29 import org.xml.sax.ContentHandler;
30 import org.xml.sax.InputSource;
31 import org.xml.sax.SAXException;
32 import org.xml.sax.XMLReader;
33
34 /**
35 * A SAXEventProvider that provides SAX events from a String.
36 *
37 * @see gnu.lgpl.License The GNU Lesser General Public License for details.
38 * @author Joe Walker
39 */
40 public class StringSAXEventProvider implements SAXEventProvider {
41 /**
42 * Simple ctor
43 *
44 * @param xmlstr the xml as a string
45 * @throws ParserConfigurationException when there is a parser configuration problem
46 * @throws SAXException when there is a SAX problem
47 */
48 public StringSAXEventProvider(String xmlstr) throws ParserConfigurationException, SAXException {
49 this.xmlstr = xmlstr;
50
51 SAXParserFactory fact = SAXParserFactory.newInstance();
52 SAXParser parser = fact.newSAXParser();
53
54 reader = parser.getXMLReader();
55 }
56
57 /* (non-Javadoc)
58 * @see org.crosswire.common.xml.SAXEventProvider#provideSAXEvents(org.xml.sax.ContentHandler)
59 */
60 public void provideSAXEvents(ContentHandler handler) throws SAXException {
61 try {
62 StringReader sr = new StringReader(xmlstr);
63 InputSource is = new InputSource(sr);
64
65 reader.setContentHandler(handler);
66 reader.parse(is);
67 } catch (IOException ex) {
68 throw new SAXException(ex);
69 }
70 }
71
72 /**
73 * The SAX parser
74 */
75 private XMLReader reader;
76
77 /**
78 * The source of XML data
79 */
80 private String xmlstr;
81 }
82