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 as published by
5    * the Free Software Foundation. This program is distributed in the hope
6    * that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * 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   * Copyright: 2005
18   *     The copyright to this program is held by it's authors.
19   *
20   * ID: $Id: StringSAXEventProvider.java 1966 2009-10-30 01:15:14Z dmsmith $
21   */
22  package org.crosswire.common.xml;
23  
24  import java.io.IOException;
25  import java.io.StringReader;
26  
27  import javax.xml.parsers.ParserConfigurationException;
28  import javax.xml.parsers.SAXParser;
29  import javax.xml.parsers.SAXParserFactory;
30  
31  import org.xml.sax.ContentHandler;
32  import org.xml.sax.InputSource;
33  import org.xml.sax.SAXException;
34  import org.xml.sax.XMLReader;
35  
36  /**
37   * A SAXEventProvider that provides SAX events from a String.
38   * 
39   * @see gnu.lgpl.License for license details.<br>
40   *      The copyright to this program is held by it's authors.
41   * @author Joe Walker [joe at eireneh dot com]
42   */
43  public class StringSAXEventProvider implements SAXEventProvider {
44      /**
45       * Simple ctor
46       */
47      public StringSAXEventProvider(String xmlstr) throws ParserConfigurationException, SAXException {
48          this.xmlstr = xmlstr;
49  
50          SAXParserFactory fact = SAXParserFactory.newInstance();
51          SAXParser parser = fact.newSAXParser();
52  
53          reader = parser.getXMLReader();
54      }
55  
56      /*
57       * (non-Javadoc)
58       * 
59       * @see
60       * org.crosswire.common.xml.SAXEventProvider#provideSAXEvents(org.xml.sax
61       * .ContentHandler)
62       */
63      public void provideSAXEvents(ContentHandler handler) throws SAXException {
64          try {
65              StringReader sr = new StringReader(xmlstr);
66              InputSource is = new InputSource(sr);
67  
68              reader.setContentHandler(handler);
69              reader.parse(is);
70          } catch (IOException ex) {
71              throw new SAXException(ex);
72          }
73      }
74  
75      /**
76       * The SAX parser
77       */
78      private XMLReader reader;
79  
80      /**
81       * The source of XML data
82       */
83      private String xmlstr;
84  }
85