1 22 23 package org.xquark.mapper.storage; 24 25 import java.io.IOException ; 26 import java.io.StringReader ; 27 28 import javax.xml.parsers.SAXParserFactory ; 29 30 import org.w3c.dom.Document ; 31 import org.xml.sax.*; 32 import org.xml.sax.ext.LexicalHandler ; 33 import org.xquark.mapper.RepositoryException; 34 import org.xquark.util.DOMReader; 35 import org.xquark.util.HandlerDecorator; 36 import org.xquark.util.SAXConstants; 37 import org.xquark.xml.xdbc.XMLDBCException; 38 import org.xquark.xml.xdbc.XMLErrorHandler; 39 40 43 public class XDBCMapperAdapter extends HandlerDecorator 44 implements SAXConstants 45 { 46 private static final String RCSRevision = "$Revision: 1.1 $"; 47 private static final String RCSName = "$Name: $"; 48 private static SAXParserFactory parserFactory; 49 50 static 51 { 52 parserFactory = SAXParserFactory.newInstance(); 53 parserFactory.setNamespaceAware(true); 54 } 55 56 private SAXHandler handler; 57 private DOMReader DOMReader; 58 59 public XDBCMapperAdapter(ContentHandler contentHandler, LexicalHandler lexicalHandler, 60 ErrorHandler errorHandler, SAXHandler handler) 61 { 62 super(contentHandler, lexicalHandler); 63 super.setErrorHandler(errorHandler); 64 this.handler = handler; 65 } 66 67 public void flushBuffer() throws XMLDBCException 68 { 69 handler.flushBuffer(); 70 } 71 72 public void setErrorHandler(XMLErrorHandler handler) 73 { 74 this.handler.setErrorHandler(handler); 75 } 76 77 public XMLErrorHandler getErrorHandler() 78 { 79 return handler.getXMLErrorHandler(); 80 } 81 82 public void clearBuffer() throws XMLDBCException 83 { 84 handler.clearBuffer(); 85 } 86 87 public void close() throws XMLDBCException 88 { 89 handler.close(); 90 } 91 92 public boolean getAutoFlush() 93 { 94 return handler.getAutoFlush(); 95 } 96 97 public void setAutoFlush(boolean mode) throws XMLDBCException 98 { 99 handler.setAutoFlush(mode); 100 } 101 102 public void insertXMLDocument(org.xml.sax.InputSource input) 106 throws XMLDBCException, SAXException 107 { 108 insertXMLDocument(createXMLReader(), input); 109 } 110 111 public void insertXMLDocument(org.xml.sax.XMLReader parser, org.xml.sax.InputSource input) 112 throws XMLDBCException, SAXException 113 { 114 configureXMLReader(parser); 115 try 116 { 117 parser.parse(input); 118 } 119 catch (SAXException e) 120 { 121 if (e.getException() instanceof XMLDBCException) 122 throw (XMLDBCException)e.getException(); 123 else 124 throw e; 125 } 126 catch (IOException e) 127 { 128 throw new RepositoryException(RepositoryException.PARSER_ERROR, 129 "IO error during SAX parsing.", e); 130 } 131 } 132 133 public void insertXMLDocument(String doc) throws XMLDBCException, org.xml.sax.SAXException 134 { 135 int index = doc.indexOf("<?xml"); 137 if (index > 0) 138 doc = doc.substring(index); 139 insertXMLDocument(new InputSource(new StringReader (doc))); 140 } 141 142 public void insertXMLDocument(Document document) throws XMLDBCException 143 { 144 if (DOMReader == null) 145 DOMReader = new DOMReader(document); 146 else 147 DOMReader.setDocument(document); 148 XMLReader myReader = configureXMLReader(DOMReader); 149 try 150 { 151 myReader.parse("Fake"); 152 } 153 catch (IOException e) 154 { 155 throw new RepositoryException(RepositoryException.PARSER_ERROR, 156 "Error during internal SAX parsing.", e); 157 } 158 catch (SAXException e) 159 { 160 if (e.getException() instanceof XMLDBCException) 161 throw (XMLDBCException)e.getException(); 162 else 163 throw new RepositoryException(RepositoryException.PARSER_ERROR, 164 e.getMessage(), e.getException()); 165 } 166 } 167 168 protected XMLReader createXMLReader() throws RepositoryException 172 { 173 try 174 { 175 return parserFactory.newSAXParser().getXMLReader(); 176 } 177 catch (Exception e) 178 { 179 throw new RepositoryException(RepositoryException.XQUARK_CONF, "Could not instantiate an XMLReader using the property in Repository configuration.", e); 180 } 181 } 182 183 protected XMLReader configureXMLReader() throws RepositoryException 184 { 185 return configureXMLReader(createXMLReader()); 186 } 187 188 protected XMLReader configureXMLReader(XMLReader reader) 189 throws RepositoryException 190 { 191 try 192 { 193 reader.setContentHandler(this); 194 reader.setErrorHandler(this); 195 reader.setProperty(SAX_LEXICAL_PROPERTY, this); 196 } 197 catch(SAXException e) 198 { 199 throw new RepositoryException(RepositoryException.PARSER_ERROR, "The XML SAX Parser is not SAX 2.0 compliant"); 200 } 201 return reader; 202 } 203 204 } 205 | Popular Tags |