1 9 package org.ozoneDB.xml.util; 10 11 import java.io.IOException ; 12 import java.io.ObjectOutput ; 13 import java.io.ObjectInput ; 14 import java.io.Externalizable ; 15 import org.ozoneDB.*; 16 import org.w3c.dom.Document ; 17 import org.w3c.dom.Node ; 18 import org.w3c.dom.NodeList ; 19 import org.xml.sax.ContentHandler ; 20 import org.infozone.tools.xml.queries.XObject; 21 import org.ozoneDB.xml.dom.DocumentProxy; 22 23 36 public class XMLContainer implements Externalizable , SAXChunkProducerDelegate { 37 38 public final static boolean debug = false; 40 41 private XMLContainerHelper helper; 43 private transient OzoneInterface db; 44 private transient Document doc; 45 46 private transient boolean runsExternal; 47 48 50 56 public static XMLContainer newContainer(OzoneInterface _db, String _docName) throws Exception { 57 XMLContainerHelper helper = (XMLContainerHelper) _db.createObject( 58 "org.ozoneDB.xml.util.XMLContainerHelperImpl", 59 OzoneInterface.Public, 60 _docName); 61 return new XMLContainer(_db, helper); 62 } 63 70 public static XMLContainer forName(OzoneInterface _db, String _docName) throws Exception { 71 XMLContainerHelper helper = (XMLContainerHelper) _db.objectForName(_docName); 72 return helper != null ? new XMLContainer(_db, helper) : null; 73 } 74 75 83 public static XMLContainer forNode(OzoneInterface _db, Node _pNode) throws Exception { 84 if (!(_pNode instanceof OzoneProxy)) { 85 throw new IllegalArgumentException ("Not a persistent DOM node: " + _pNode.getClass().getName()); 86 } 87 DocumentProxy pDoc = _pNode instanceof Document 88 ? (DocumentProxy) _pNode 89 : (DocumentProxy) _pNode.getOwnerDocument(); 90 XMLContainerHelper helper = pDoc.getContainer(); 91 return helper != null ? new XMLContainer(_db, helper) : null; 92 } 93 94 96 protected XMLContainer(OzoneInterface _db, XMLContainerHelper _helper) { 97 db = _db; 98 helper = _helper; 99 runsExternal = (db instanceof ExternalDatabase); 100 } 101 102 104 109 public void setName(String _name) throws Exception { 110 if (helper == null) { 111 throw new IllegalStateException ("Document has already been deleted."); 112 } 113 db.nameObject(helper, _name); 114 } 115 116 119 public synchronized void delete() throws Exception { 120 if (helper == null) { 121 throw new IllegalStateException ("Document has already been deleted."); 122 } 123 db.deleteObject(helper); 124 helper = null; 125 doc = null; 126 } 127 128 132 public Document getPDocument() throws Exception { 133 if (helper == null) { 134 throw new IllegalStateException ("Document has already been deleted."); 135 } 136 if (doc == null) { 138 doc = helper.getDocument(); 139 } 140 return doc; 141 } 142 143 149 public void storeDOM(Document _tNode) throws Exception { 150 if (helper == null) 151 throw new IllegalStateException ("Document has already been deleted."); 152 storeDOM(null, _tNode); 153 } 154 155 177 public void storeDOM(Node _pNode, Node _tNode) throws Exception { 178 long time = 0; 179 long starttime = 0; 180 if (helper == null) { 181 throw new IllegalStateException ("Document has already been deleted."); 182 } 183 if (_tNode == null) { 184 throw new IllegalArgumentException ("tNode == null."); 185 } 186 if (runsExternal && ((ExternalDatabase) db).currentTransaction() == null) { 188 throw new IllegalStateException ("Thread must have joined a transaction!"); 189 } 190 if (_pNode == null) { 191 System.out.println("replacing document content"); 192 } 194 SAXChunkConsumer consumer = helper.beginInputSequence(_pNode); 195 ModifiableNodeList mnl = new ModifiableNodeList(1); 196 mnl.addNode(_tNode); 197 SAXChunkProducer producer = new SAXChunkProducer(mnl); 198 ChunkOutputStream cos = producer.chunkStream(); 199 do { 200 cos.reset(); 201 producer.createNextChunk(); 202 if (XMLContainer.debug) { 203 starttime = System.currentTimeMillis(); 204 } 205 consumer = helper.putChunk(cos.toByteArray(), consumer); 206 if (XMLContainer.debug) { 207 time += (System.currentTimeMillis() - starttime); 208 } 209 } while (!cos.getEndFlag()); 210 if (XMLContainer.debug) { 211 System.out.println("DOM store: store in db time: " + time + " ms"); 212 } 213 } 214 215 221 public ContentHandler storeSAX() throws Exception { 222 if (helper == null) { 223 throw new IllegalStateException ("Document has already been deleted."); 224 } 225 return storeSAX(null); 226 } 227 228 249 public ContentHandler storeSAX(Node _pNode) throws Exception { 250 if (helper == null) { 251 throw new IllegalStateException ("Document has already been deleted."); 252 } 253 if (runsExternal && ((ExternalDatabase) db).currentTransaction() == null) { 255 throw new IllegalStateException ("Thread must have joined a transaction!"); 256 } 257 258 SAXChunkConsumer consumer = helper.beginInputSequence(_pNode); 260 SAXChunkProducer producer = new SAXChunkProducer(this); 261 producer.dbConsumer = consumer; 262 return producer; 263 } 264 265 268 public void processChunk(SAXChunkProducer _producer) throws Exception { 269 if (XMLContainer.debug) { 270 System.out.print("XMLContainer.processChunk()... "); 271 } 272 ChunkOutputStream cos = _producer.chunkStream(); 273 if (XMLContainer.debug) { 274 System.out.print("putChunk(" + cos.count + ")... "); 275 } 276 _producer.dbConsumer = helper.putChunk(cos.toByteArray(), _producer.dbConsumer); 277 } 278 279 282 public Document extractDOM(Document _domFactory) throws Exception { 283 return (Document ) extractDOM(_domFactory, (Node ) null, null, -1); 284 } 285 286 289 public Node extractDOM(Document _domFactory, Node _pNode, Node _appendTo) throws Exception { 290 return extractDOM(_domFactory, _pNode, _appendTo, -1); 291 } 292 293 312 public Node extractDOM(Document _domFactory, Node _pNode, Node _appendTo, int _depth) throws Exception { 313 ModifiableNodeList mnl = null; 314 if (_pNode != null) { 315 mnl = new ModifiableNodeList(1); 316 mnl.addNode(_pNode); 317 } 318 319 NodeList resultList = extractDOM(_domFactory, mnl, _appendTo, _depth); 320 return resultList.item(0); 321 322 340 } 341 342 public NodeList extractDOM(Document _domFactory, NodeList _pNodes, Node _appendTo, int _depth) throws Exception { 343 if (helper == null) { 344 throw new IllegalStateException ("Document has already been deleted."); 345 } 346 if (runsExternal && ((ExternalDatabase) db).currentTransaction() == null) { 348 throw new IllegalStateException ("Thread must have joined a transaction!"); 349 } 350 SAXChunkProducer producer = helper.beginOutputSequence(_pNodes, _depth); 351 SAXChunkConsumer consumer = new SAXChunkConsumer(_domFactory, _appendTo); 352 ChunkOutputStream cos; 353 do { 354 producer = helper.createNextChunk(producer); 355 cos = producer.chunkStream(); 356 consumer.processChunk(cos.toByteArray()); 357 cos.reset(); 358 } while (!cos.getEndFlag()); 359 return consumer.getResultNodeList(); 360 } 361 362 365 public void extractSAX(ContentHandler _contentHandler) throws Exception { 366 extractSAX(_contentHandler, null, -1); 367 } 368 369 372 public void extractSAX(ContentHandler _contentHandler, Node _pNode) throws Exception { 373 extractSAX(_contentHandler, _pNode, -1); 374 } 375 376 391 public void extractSAX(ContentHandler _contentHandler, Node _pNode, int _depth) throws Exception { 392 if (helper == null) { 393 throw new IllegalStateException ("Document has already been deleted."); 394 } 395 if (runsExternal && ((ExternalDatabase) db).currentTransaction() == null) { 397 throw new IllegalStateException ("Thread must have joined a transaction!"); 398 } 399 ModifiableNodeList mnl = null; 400 if (_pNode != null) { 401 mnl = new ModifiableNodeList(1); 402 mnl.addNode(_pNode); 403 } 404 SAXChunkProducer producer = helper.beginOutputSequence(mnl, _depth); 405 SAXChunkConsumer consumer = new SAXChunkConsumer(_contentHandler); 406 ChunkOutputStream cos; 407 do { 408 producer = helper.createNextChunk(producer); 409 cos = producer.chunkStream(); 410 consumer.processChunk(cos.toByteArray()); 411 cos.reset(); 412 } while (!cos.getEndFlag()); 413 } 414 415 423 public OzoneXUpdateQuery newXUpdateQuery() { 424 return new OzoneXUpdateQuery(this); 425 } 426 427 432 public OzoneXPathQuery newXPathQuery() { 433 return new OzoneXPathQuery(this); 434 } 435 436 protected void executeXUpdate(OzoneXUpdateQuery _query) throws Exception { 437 if (_query == null) { 438 throw new IllegalArgumentException ("_query == null."); 439 } 440 helper.executeXUpdate(_query); 441 } 442 443 protected XObject executeXPath(OzoneXPathQuery _query) throws Exception { 444 if (_query == null) { 445 throw new IllegalArgumentException ("_query == null."); 446 } 447 return helper.executeXPath(_query); 448 } 449 450 456 public String xpathForNode(Node _pnode) { 457 if (helper == null) { 458 throw new IllegalStateException ("Document has already been deleted."); 459 } 460 return helper.xpathForNode(_pnode); 461 } 462 463 public void writeExternal(ObjectOutput _out) throws IOException { 464 if (runsExternal) { 465 throw new IllegalStateException ("XMLContainer cannot be serialized outside an ozone server."); 466 } 467 _out.writeObject(helper); 468 } 469 470 public void readExternal(ObjectInput _in) throws IOException , ClassNotFoundException { 471 if (runsExternal) { 472 throw new IllegalStateException ("XMLContainer cannot be deserialized outside an ozone server."); 473 } 474 helper = (XMLContainerHelper) _in.readObject(); 475 db = org.ozoneDB.core.Env.currentEnv().database; 476 } 477 } 478 | Popular Tags |