📄 xmlcontainer.java
字号:
// You can redistribute this software and/or modify it under the terms of// the Ozone Library License version 1 published by ozone-db.org.//// The original code and portions created by SMB are// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.//// $Id$package org.ozoneDB.xml.util;import java.io.IOException;import java.io.ObjectOutput;import java.io.ObjectInput;import java.io.Externalizable;import org.ozoneDB.*;import org.w3c.dom.Document;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.xml.sax.ContentHandler;import org.infozone.tools.xml.queries.XObject;import org.ozoneDB.xml.dom.DocumentProxy;/** * <p>This class is the central part of the ozone/XML API. Basically it provides a * persistent container for a XML document, which is stored in an ozone database.</p> * * <p><dl><dt>IMPORTANT:</dt><dd>Before calling one of the store or extract methods the * thread <b>must</b> have joined a transaction.</dd></dl> * </p> * * Usage of the SAX methods is recomended, because of the better performance. * * @version $Revision$ $Date$ * @author <a href="http://www.smb-tec.com">SMB</a> */public class XMLContainer implements Externalizable, SAXChunkProducerDelegate { // Class data public final static boolean debug = false; // Data private XMLContainerHelper helper; private transient OzoneInterface db; private transient Document doc; /** True if this object runs outside an ozone server. */ private transient boolean runsExternal; // Class methods /** * Creates a new container with the given name. * @param _db The database where the container will be stored. * @param _docName The name of the container (and the document). * @return the new container. */ public static XMLContainer newContainer(OzoneInterface _db, String _docName) throws Exception { XMLContainerHelper helper = (XMLContainerHelper) _db.createObject( "org.ozoneDB.xml.util.XMLContainerHelperImpl", OzoneInterface.Public, _docName); return new XMLContainer(_db, helper); } /** * Returns the XMLContainer representing the document with the given name. * @param _db The database where the container is stored. * @param _docName The name under which the container has been stored. * @return the container for the given name or null if the container * does not exist. */ public static XMLContainer forName(OzoneInterface _db, String _docName) throws Exception { XMLContainerHelper helper = (XMLContainerHelper) _db.objectForName(_docName); return helper != null ? new XMLContainer(_db, helper) : null; } /** * Returns the XMLContainer representing the document the given node * belongs to. * @param _db The database where the container is stored. * @param _pNode A node of the document the container represents. * @return the container for the given node or null if the node has not * been stored using the XMLContainer class. */ public static XMLContainer forNode(OzoneInterface _db, Node _pNode) throws Exception { if (!(_pNode instanceof OzoneProxy)) { throw new IllegalArgumentException("Not a persistent DOM node: " + _pNode.getClass().getName()); } DocumentProxy pDoc = _pNode instanceof Document ? (DocumentProxy) _pNode : (DocumentProxy) _pNode.getOwnerDocument(); XMLContainerHelper helper = pDoc.getContainer(); return helper != null ? new XMLContainer(_db, helper) : null; } // Constructors protected XMLContainer(OzoneInterface _db, XMLContainerHelper _helper) { db = _db; helper = _helper; runsExternal = (db instanceof ExternalDatabase); } // Methods /** * Changes the name of this container. * @param _name The new name of this container or null to remove the * current name. */ public void setName(String _name) throws Exception { if (helper == null) { throw new IllegalStateException("Document has already been deleted."); } db.nameObject(helper, _name); } /** * Deletes the container (and the associated document) from the database. */ public synchronized void delete() throws Exception { if (helper == null) { throw new IllegalStateException("Document has already been deleted."); } db.deleteObject(helper); helper = null; doc = null; } /** * Get the underlying persistent document that this container is working on. * @return The persistent document. */ public Document getPDocument() throws Exception { if (helper == null) { throw new IllegalStateException("Document has already been deleted."); } // not synchronized because it wouldn't make a difference here if (doc == null) { doc = helper.getDocument(); } return doc; } /** * Stores a transient DOM tree database. The newly created Nodes are * appended to the Document Node of this container. * * @see #storeDOM(Node, Node) */ public void storeDOM(Document _tNode) throws Exception { if (helper == null) throw new IllegalStateException("Document has already been deleted."); storeDOM(null, _tNode); } /** * Stores a transient node into the database. * * <p>Before calling this method the current thread <b>must</b> have joined an * {@link org.ozoneDB.ExternalTransaction explicit Transaction}. This is an * exception to the normal case, where an implicit transaction (handled by * Ozone) is more appropriate.</p> * <p><dl><dt>Note:</dt><dd>If ever possible the corresponding * {@link #storeSAX(Node) SAX method} should be used, because the performance * of SAX storage is much better.</dd></dl></p> * * @param _pNode The persistent node where the stored node will be appended to. * Null replaces the current document. * @param _tNode The transient node to be stored. * * @throws IllegalStateException if the underlying document has already been deleted. * @throws IllegalArgumentException if _tnode was null. * @throws IllegalStateException if the current thread has not joined a * {@link org.ozoneDB.ExternalTransaction transaction}. * @see org.ozoneDB.ExternalTransaction * @see org.ozoneDB.ExternalDatabase#newTransaction() */ public void storeDOM(Node _pNode, Node _tNode) throws Exception { long time = 0; long starttime = 0; if (helper == null) { throw new IllegalStateException("Document has already been deleted."); } if (_tNode == null) { throw new IllegalArgumentException("tNode == null."); } // thread must have joined a transaction if (runsExternal && ((ExternalDatabase) db).currentTransaction() == null) { throw new IllegalStateException("Thread must have joined a transaction!"); } if (_pNode == null) { System.out.println("replacing document content"); //clearDocument(); } SAXChunkConsumer consumer = helper.beginInputSequence(_pNode); ModifiableNodeList mnl = new ModifiableNodeList(1); mnl.addNode(_tNode); SAXChunkProducer producer = new SAXChunkProducer(mnl); ChunkOutputStream cos = producer.chunkStream(); do { cos.reset(); producer.createNextChunk(); if (XMLContainer.debug) { starttime = System.currentTimeMillis(); } consumer = helper.putChunk(cos.toByteArray(), consumer); if (XMLContainer.debug) { time += (System.currentTimeMillis() - starttime); } } while (!cos.getEndFlag()); if (XMLContainer.debug) { System.out.println("DOM store: store in db time: " + time + " ms"); } } /** * Stores a DOM tree represented by SAX events in the datasabase. The * newly created Nodes are appended to the Document Node of this container. * * @see #storeSAX(Node) */ public ContentHandler storeSAX() throws Exception { if (helper == null) { throw new IllegalStateException("Document has already been deleted."); } return storeSAX(null); } /** * Stores XML represented by SAX events into the database. * <p>The entire storage process <b>must</b> be enclosed by an * {@link org.ozoneDB.ExternalTransaction explicit Transaction}. This is an * exception to the normal case, where an implicit transaction (handled by * Ozone) is more appropriate. The storage process starts with the call to this * method and ends with the last event send to the content handler. The SAX * events must be properly terminated, i.e. there must be an end event for * for every start event, otherwise the correct storage can't be guaranteed.</p> * * @param _pNode The persistent node where the stored data will be appended to. *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -