1 22 23 package org.xquark.xquery.xdbc; 24 25 import java.io.StringReader ; 26 27 import javax.xml.parsers.ParserConfigurationException ; 28 import javax.xml.parsers.SAXParserFactory ; 29 30 import org.xml.sax.InputSource ; 31 import org.xml.sax.SAXException ; 32 import org.xml.sax.XMLReader ; 33 import org.xquark.xml.xdbc.XMLDBCException; 34 35 public class StringDocument extends AbstractXMLDocument { 36 private static final String RCSRevision = "$Revision: 1.1 $"; 37 private static final String RCSName = "$Name: $"; 38 39 private static SAXParserFactory spf; 40 static { 41 spf = SAXParserFactory.newInstance(); 42 spf.setNamespaceAware(true); 43 } 44 45 private String xml; 46 47 public StringDocument(String xml) { 48 super(); 49 this.xml = xml; 50 } 51 public StringDocument(String xml, boolean readOnly) { 52 super(readOnly); 53 this.xml = xml; 54 } 55 56 public String getAsString() throws XMLDBCException { 57 return xml; 58 } 59 60 public void getAsSAX() throws XMLDBCException, SAXException { 61 if (xml == null) 62 { 63 throw new XMLDBCException ("StringDocument.getAsSAX : Could not create SAX handler, xml string is null.") ; 64 } 65 StringReader sr = new StringReader (xml); 66 try { 67 InputSource source = new InputSource (sr); 68 XMLReader reader = spf.newSAXParser().getXMLReader(); 69 plugHandlers(reader); 70 reader.parse(source); 71 } catch (java.io.IOException ioex) { 72 throw new XMLDBCException(ioex.getMessage(), ioex); 73 } catch (ParserConfigurationException pce) { 74 throw new XMLDBCException("Could not create SAX parser using JAXP. Check a JAXP DOM implementation is in your classpath.", pce) ; 75 } finally { 76 sr.close(); 77 } 78 } 79 } 80 | Popular Tags |