1 22 23 package org.xquark.xquery.xdbc; 24 25 import java.io.StringWriter ; 26 import java.io.Writer ; 27 28 import javax.xml.parsers.DocumentBuilder ; 29 import javax.xml.parsers.DocumentBuilderFactory ; 30 import javax.xml.parsers.ParserConfigurationException ; 31 32 import org.w3c.dom.Document ; 33 import org.w3c.dom.Element ; 34 import org.xml.sax.*; 35 import org.xml.sax.ext.LexicalHandler ; 36 import org.xquark.serialize.XMLSerializer; 37 import org.xquark.util.DOMBuilder; 38 import org.xquark.util.SAXConstants; 39 import org.xquark.xml.xdbc.*; 40 41 42 49 public abstract class AbstractXMLDocument implements XMLDocument 50 { 51 private static final String RCSRevision = "$Revision: 1.1 $"; 52 private static final String RCSName = "$Name: $"; 53 54 private static DocumentBuilder DOMCreator; 55 56 59 private XMLCollection xmlCollection = null ; 60 61 62 65 private String identifier = null; 66 private boolean readOnly = false; 67 68 protected ContentHandler contentHandler ; 69 protected ErrorHandler errorHandler ; 70 protected LexicalHandler lexicalHandler ; 71 72 protected AbstractXMLDocument() 73 { 74 } 75 76 protected AbstractXMLDocument(boolean readOnly) 77 { 78 setReadOnly(readOnly); 79 } 80 81 protected AbstractXMLDocument(String Id) 82 { 83 this(Id, null, false); 84 } 85 86 protected AbstractXMLDocument(String Id, boolean readOnly) 87 { 88 this(Id, null, readOnly); 89 } 90 91 protected AbstractXMLDocument(String Id, XMLCollection collection) 92 { 93 this(Id, collection, false); 94 } 95 96 protected AbstractXMLDocument(String Id, XMLCollection collection, boolean readOnly) 97 { 98 identifier = Id; 99 xmlCollection = collection; 100 setReadOnly(readOnly); 101 } 102 103 protected void setReadOnly(boolean readOnly) 104 { 105 this.readOnly = readOnly; 106 } 107 108 113 public String getIdentifier() 114 { 115 return identifier ; 116 } 117 118 128 public void setIdentifier(String identifier) throws XMLDBCException, XMLDBCNotSupportedException 129 { 130 if (readOnly) 131 throw new XMLDBCNotSupportedException("This document is read-only."); 132 if (xmlCollection != null) 133 xmlCollection.renameDocument(this.identifier, identifier); 134 this.identifier = identifier; 135 } 136 137 142 public XMLCollection getCollection() 143 { 144 return xmlCollection ; 145 } 146 147 148 156 public void remove() throws XMLDBCException, XMLDBCNotSupportedException 157 { 158 if (readOnly) 159 throw new XMLDBCNotSupportedException("This document is read-only."); 160 if (xmlCollection != null) 161 { 162 xmlCollection.removeDocument(getIdentifier()); 163 xmlCollection = null; 164 } 165 else 166 throw new XMLDBCNotSupportedException("Document is not a stored object"); 167 } 168 169 175 public void setContentHandler(ContentHandler handler) 176 { 177 this.contentHandler = handler; 178 } 179 180 186 public void setLexicalHandler(LexicalHandler handler) 187 { 188 this.lexicalHandler = handler ; 189 } 190 191 197 public void setErrorHandler(ErrorHandler handler) 198 { 199 this.errorHandler = handler ; 200 } 201 202 207 public LexicalHandler getLexicalHandler() 208 { 209 return lexicalHandler; 210 } 211 212 217 public ContentHandler getContentHandler() 218 { 219 return contentHandler; 220 } 221 222 227 public ErrorHandler getErrorHandler() 228 { 229 return errorHandler; 230 } 231 232 237 public Document getAsDocument() throws XMLDBCException 238 { 239 createDOMBuilder(); 240 Document doc = DOMCreator.newDocument(); 241 DOMBuilder builder = new DOMBuilder(doc); 242 getDocument(builder, builder); 243 return doc; 244 } 245 246 public Document getAsDOM() throws XMLDBCException 247 { 248 return getAsDocument(); 249 } 250 251 public void getAsDOM(Element parent) throws XMLDBCException 252 { 253 DOMBuilder builder = new DOMBuilder(parent); 254 getDocument(builder, builder); 255 } 256 257 262 public String getAsString() throws XMLDBCException 263 { 264 StringWriter strWriter = new StringWriter () ; 265 getAsStream(strWriter); 266 return strWriter.toString() ; 267 } 268 269 public void getAsStream(Writer out) throws XMLDBCException 270 { 271 XMLSerializer serializer = new XMLSerializer(out); 272 getDocument(serializer, serializer); 273 } 274 275 private void getDocument(ContentHandler contentHandler, LexicalHandler lexicalHandler) 279 throws XMLDBCException 280 { 281 ContentHandler initialContentHandler = getContentHandler(); 283 LexicalHandler initialLexicalHandler = getLexicalHandler(); 284 285 setContentHandler(contentHandler); 286 setLexicalHandler(lexicalHandler); 287 try 288 { 289 getAsSAX(); 290 } 291 catch (SAXException e) 292 { 293 throw new XMLDBCException(e.getMessage(), e.getException()); 294 } 295 finally 296 { 297 if (initialContentHandler != null) 299 setContentHandler(initialContentHandler); 300 if (initialLexicalHandler != null) 301 setLexicalHandler(initialLexicalHandler); 302 } 303 } 304 305 protected void plugHandlers(XMLReader reader) 306 throws XMLDBCException 307 { 308 try 309 { 310 if (contentHandler != null) 311 reader.setContentHandler(contentHandler); 312 if (errorHandler != null) 313 reader.setErrorHandler(errorHandler); 314 if (lexicalHandler != null) 315 reader.setProperty(SAXConstants.SAX_LEXICAL_PROPERTY, lexicalHandler); 316 } 317 catch (SAXException e) 318 { 319 throw new XMLDBCException("Could not plug handlers on repository XMLReader.", e); 320 } 321 } 322 323 private synchronized void createDOMBuilder() 324 { 325 if (DOMCreator == null) 326 { 327 DocumentBuilderFactory DOMFactory = DocumentBuilderFactory.newInstance(); 328 DOMFactory.setNamespaceAware(true); 329 try 330 { 331 DOMCreator = DOMFactory.newDocumentBuilder(); 332 } 333 catch (ParserConfigurationException pce) 334 { 335 throw new RuntimeException ("Could not create an empty DOM level 2 document using JAXP. Check a JAXP DOM implementation is in your classpath.") ; 336 } 337 } 338 } 339 } 340 | Popular Tags |