1 22 23 28 package org.xquark.mapper.storage; 29 30 import java.io.IOException ; 31 import java.sql.SQLException ; 32 33 import javax.xml.parsers.DocumentBuilderFactory ; 34 import javax.xml.parsers.ParserConfigurationException ; 35 36 import org.w3c.dom.Document ; 37 import org.xml.sax.*; 38 import org.xml.sax.ext.LexicalHandler ; 39 import org.xquark.mapper.RepositoryConnection; 40 import org.xquark.mapper.RepositoryException; 41 import org.xquark.mapper.metadata.PathNode; 42 import org.xquark.mapper.metadata.PathSet; 43 import org.xquark.util.DOMBuilder; 44 import org.xquark.xml.xdbc.XMLDBCException; 45 46 53 public class DocumentProducer extends BaseXMLReaderImpl 54 implements Locator 55 { 56 private static final String RCSRevision = "$Revision: 1.2 $"; 57 private static final String RCSName = "$Name: $"; 58 59 public static DocumentBuilderFactory DOMFactory; 60 static 61 { 62 DOMFactory = DocumentBuilderFactory.newInstance(); 63 DOMFactory.setNamespaceAware(true); 64 } 65 66 private final static String DEFAULT_PREFIX = "ns"; 67 68 private DocumentExplorer docTuplePool; 69 70 private PathSet pathSet; 71 private String Id; 72 73 74 public DocumentProducer(_RepositoryCollection collection) throws XMLDBCException 75 { 76 super(collection); 77 try 78 { 79 80 tuplePool = docTuplePool = new DocumentExplorer(collection); 81 } 82 catch (SQLException e) 83 { 84 throw new RepositoryException(RepositoryException.DB_ERROR, 85 "JDBC error while initializing reader."); 86 } 87 generateDBPrefix = !collection.getFeature(RepositoryConnection.USE_SCHEMA_PREFIXES_FEATURE); 88 prefixProvider = new PrefixProvider(); 89 pathSet = collection.getMetadata().getPathSet(); 90 try { 91 docTuplePool.initDocReading(collection.getRepositoryConnection().getConnection()); 92 } 93 catch (SQLException e) { 94 throw new RepositoryException(RepositoryException.DB_ERROR, 95 "JDBC error while initializing prepared statements", e); 96 } 97 } 98 99 103 114 public void parse(String systemId) 115 throws IOException , SAXException 116 { 117 if (systemId == null) 118 throw new IOException ("A string ID must be provided in order to retrieve the document."); 119 120 Id = systemId.trim(); 122 try 123 { 124 try 125 { 126 docTuplePool.initDocumentExploration(Id); 127 128 if (!docTuplePool.lookup()) 130 throw new RepositoryException(RepositoryException.NOT_EXISTS, 131 "Document with ID " + Id + " not found in this collection"); 132 133 contentHandler.setDocumentLocator(this); 134 135 contentHandler.startDocument(); 139 140 browseBranch 141 ( 142 0, 143 Long.MAX_VALUE, 144 -1, 145 -1, 146 pathSet.createIterator 147 ( 148 (PathNode)pathSet.getRoot() 149 ), 150 0, 151 false 152 ); 153 154 contentHandler.endDocument(); 158 reset(); 159 } 160 catch(SQLException e) 161 { 162 throw new SAXException("JDBC error raised while generating SAX events.", e); 163 } 164 catch(XMLDBCException e) 165 { 166 throw new SAXException("Repository error raised while generating SAX events.", e); 167 } 168 } 169 catch (SAXException e) 170 { 171 if (getErrorHandler() != null) 172 getErrorHandler().fatalError( 173 new SAXParseException(e.getMessage(), this, e.getException())); 174 try { 175 reset(); 176 } 177 catch (SQLException ex) { 178 }catch (RepositoryException ex) { 179 } 181 throw e; 182 } 183 } 184 185 192 public Document getDocument(String ID) throws RepositoryException 193 { 194 Document document = null; 195 try { 196 document = DOMFactory.newDocumentBuilder().newDocument(); 197 } 198 catch (ParserConfigurationException e) { 199 throw new RepositoryException(RepositoryException.PARSER_ERROR, "Could not create an empty DOM level 2 document using JAXP. Check a JAXP DOM implementation is in your classpath."); 200 } 201 202 DOMBuilder builder = new DOMBuilder(document); 204 205 ContentHandler initialContentHandler = getContentHandler(); 207 LexicalHandler initialLexicalHandler; 208 try { 209 initialLexicalHandler = (LexicalHandler )getProperty(SAX_LEXICAL_PROPERTY); 210 } 211 catch (SAXException e) { 212 throw new RepositoryException(RepositoryException.INTERNAL_ERROR, "Couldn't access the lexical handler in XMLReaderImpl.",e); 213 } 214 215 setContentHandler(builder); 216 try { 217 setProperty(SAX_LEXICAL_PROPERTY, builder); 218 } 219 catch (SAXException e) { 220 throw new RepositoryException(RepositoryException.INTERNAL_ERROR, "Couldn't access the lexical handler in XMLReaderImpl.",e); 221 } 222 223 try { 224 parse(ID); 225 } 226 catch (IOException e) { 227 throw new RepositoryException(RepositoryException.INTERNAL_ERROR, "Error while processing internal SAX parsing.",e); 228 } 229 catch (SAXException e) { 230 throw new RepositoryException(RepositoryException.INTERNAL_ERROR, "Error while processing internal SAX parsing.",e); 231 } 232 233 setContentHandler(initialContentHandler); 235 try { 236 setProperty(SAX_LEXICAL_PROPERTY, initialLexicalHandler); 237 } 238 catch (SAXException e) { 239 throw new RepositoryException(RepositoryException.INTERNAL_ERROR, "Couldn't access the lexical handler in XMLReaderImpl.",e); 240 } 241 242 return document; 243 } 244 245 public void reset() throws SQLException , RepositoryException 246 { 247 Id = null; 248 super.reset(); 249 } 250 251 public java.lang.String getSystemId(){return Id;} 252 public java.lang.String getPublicId(){return null;} 253 public int getLineNumber() {return -1;} 254 public int getColumnNumber() {return -1;} 255 256 } 257
| Popular Tags
|