1 16 package org.apache.cocoon.xml; 17 18 import org.apache.excalibur.xml.sax.XMLizable; 19 import org.xml.sax.ContentHandler ; 20 import org.xml.sax.SAXException ; 21 import org.xml.sax.InputSource ; 22 23 import javax.xml.parsers.SAXParserFactory ; 24 import javax.xml.parsers.SAXParser ; 25 import javax.xml.parsers.ParserConfigurationException ; 26 import java.io.IOException ; 27 import java.io.StringReader ; 28 29 35 public class StringXMLizable implements XMLizable { 36 private String data; 37 38 public StringXMLizable(String data) { 39 this.data = data; 40 } 41 42 public void toSAX(ContentHandler contentHandler) throws SAXException { 43 SAXParserFactory parserFactory = SAXParserFactory.newInstance(); 44 parserFactory.setNamespaceAware(true); 45 SAXParser parser = null; 46 try { 47 parser = parserFactory.newSAXParser(); 48 } catch (ParserConfigurationException e) { 49 throw new SAXException ("Error creating SAX parser.", e); 50 } 51 parser.getXMLReader().setContentHandler(contentHandler); 52 InputSource is = new InputSource (new StringReader (data)); 53 try { 54 parser.getXMLReader().parse(is); 55 } catch (IOException e) { 56 throw new SAXException (e); 57 } 58 } 59 } 60 | Popular Tags |