001: // You can redistribute this software and/or modify it under the terms of
002: // the Ozone Library License version 1 published by ozone-db.org.
003: //
004: // The original code and portions created by SMB are
005: // Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
006: //
007: // $Id: XMLContainerHelperImpl.java,v 1.1 2001/12/18 11:03:24 per_nyfelt Exp $
008:
009: package org.ozoneDB.xml.util;
010:
011: import java.io.IOException;
012:
013: import org.w3c.dom.Document;
014: import org.w3c.dom.Node;
015: import org.w3c.dom.NodeList;
016:
017: import org.xml.sax.SAXException;
018:
019: import org.ozoneDB.OzoneObject;
020: import org.ozoneDB.xml.dom.DocumentProxy;
021:
022: import org.infozone.tools.xml.queries.XObject;
023: import org.infozone.tools.xml.queries.XPathQuery;
024: import org.infozone.tools.xml.queries.XUpdateQuery;
025: import org.infozone.tools.xml.queries.XPathQueryFactory;
026: import org.infozone.tools.xml.queries.XUpdateQueryFactory;
027:
028: /**
029: * This class provides the server side part of the XMLContainer. It mainly
030: * handles storing and retrieving of parts of the underlying XML document.
031: *
032: * @version $Revision: 1.1 $ $Date: 2001/12/18 11:03:24 $
033: * @author <a href="http://www.smb-tec.com">SMB</a>
034: */
035: public final class XMLContainerHelperImpl extends OzoneObject implements
036: XMLContainerHelper {
037:
038: private final static long serialVersionUID = 4L;
039:
040: private final static boolean debug = XMLContainer.debug;
041:
042: private static XPathQueryFactory xpathQueryFactory;
043:
044: private static XUpdateQueryFactory xupdateQueryFactory;
045:
046: /** The underlying XML document. */
047: private Document document;
048:
049: static {
050: // use Xt and Lexus for XPath and XUpdate
051: if (System.getProperties().get(
052: "org.infozone.tools.xml.queries.XPathQueryFactory") == null) {
053: System
054: .getProperties()
055: .put(
056: "org.infozone.tools.xml.queries.XPathQueryFactory",
057: "org.infozone.tools.xml.queries.xt.XPathQueryFactoryImpl");
058: }
059:
060: if (System.getProperties().get(
061: "org.infozone.tools.xml.queries.XUpdateQueryFactory") == null) {
062: System
063: .getProperties()
064: .put(
065: "org.infozone.tools.xml.queries.XUpdateQueryFactory",
066: "org.infozone.lexus.XUpdateQueryFactoryImpl");
067: }
068:
069: xpathQueryFactory = XPathQueryFactory.newInstance();
070: xupdateQueryFactory = XUpdateQueryFactory.newInstance();
071: }
072:
073: public XMLContainerHelperImpl() {
074: if (debug) {
075: System.out.println("helper: ctor...");
076: }
077: }
078:
079: public final void onCreate() throws Exception {
080: if (debug) {
081: System.out.println("helper: onCreate()...");
082: }
083: document = (Document) database().createObject(
084: org.ozoneDB.xml.dom.DocumentImpl.class.getName());
085: ((DocumentProxy) document).setContainer(this );
086: }
087:
088: public final void onDelete() throws Exception {
089: if (debug) {
090: System.out.println("helper: onDelete()...");
091: }
092: if (document != null) {
093: database().deleteObject((DocumentProxy) document);
094: }
095: }
096:
097: public final void setDocument(Document _pdoc) {
098: if (debug) {
099: System.out.println("helper: setDocument...");
100: }
101:
102: if (document != null) {
103: throw new IllegalStateException(
104: "Container helper is already assigned to a document.");
105: }
106:
107: document = _pdoc;
108: ((DocumentProxy) document).setContainer(this );
109: }
110:
111: public final Document getDocument() {
112: if (debug) {
113: System.out.println("helper: getDocument...");
114: }
115:
116: return document;
117: }
118:
119: public final SAXChunkConsumer beginInputSequence(Node _pNode)
120: throws Exception {
121: if (debug) {
122: System.out.println("helper: beginInputSequence...");
123: }
124:
125: if (_pNode == null) {
126: _pNode = getDocument();
127: ((DocumentProxy) _pNode).clearDocument();
128: }
129:
130: SAXChunkConsumer consumer = new SAXChunkConsumer(getDocument(),
131: _pNode);
132: return consumer;
133: }
134:
135: public final SAXChunkConsumer putChunk(byte[] _chunkData,
136: SAXChunkConsumer _consumer) throws SAXException,
137: IOException {
138: _consumer.processChunk(_chunkData);
139: return _consumer;
140: }
141:
142: public final void endInputSequence() throws Exception {
143: if (debug) {
144: System.out.println("helper: endInputSequence...");
145: }
146: }
147:
148: /* public final SAXChunkProducer beginOutputSequence( Node _pnode, int _depth ) throws Exception {
149: if (_pnode == null) {
150: _pnode = getDocument();
151: }
152:
153: ModifiableNodeList mnl = new ModifiableNodeList(1);
154: mnl.addNode(_pnode);
155:
156: return new SAXChunkProducer( mnl, _depth );
157: }
158: */
159:
160: public final SAXChunkProducer beginOutputSequence(NodeList _pnodes,
161: int _depth) throws Exception {
162: if (_pnodes == null) {
163: _pnodes = new ModifiableNodeList(1);
164: ((ModifiableNodeList) _pnodes).addNode(getDocument());
165: }
166: return new SAXChunkProducer(_pnodes, _depth);
167: }
168:
169: public final SAXChunkProducer createNextChunk(
170: SAXChunkProducer producer) throws SAXException {
171: producer.createNextChunk();
172: return producer;
173: }
174:
175: public final void endOutputSequence() throws Exception {
176: if (debug) {
177: System.out.println("helper: endOutputSequence...");
178: }
179: }
180:
181: public final XObject executeXPath(OzoneXPathQuery _query)
182: throws Exception {
183: if (_query.rootNode == null) {
184: _query.rootNode = getDocument();
185: }
186: XPathQuery query = xpathQueryFactory.newXPathQuery();
187: query.setQString(_query.qstring);
188: if (_query.filter != null) {
189: query.setNodeFilter(_query.filter);
190: }
191: if (_query.namespace != null) {
192: query.setNamespace(_query.namespace);
193: }
194: return query.execute(_query.rootNode);
195: }
196:
197: public final void executeXUpdate(OzoneXUpdateQuery _query)
198: throws Exception {
199: if (_query.rootNode == null) {
200: _query.rootNode = getDocument();
201: }
202: XUpdateQuery query = xupdateQueryFactory.newXUpdateQuery();
203: query.setQString(_query.qstring);
204: if (_query.filter != null) {
205: query.setNodeFilter(_query.filter);
206: }
207: if (_query.namespace != null) {
208: query.setNamespace(_query.namespace);
209: }
210: System.out.println(query.getClass().getName());
211: System.out.println(_query.qstring);
212: System.out.println(_query.rootNode);
213: query.execute(_query.rootNode);
214: }
215:
216: /**
217: * Determines the absolute XPath for the given node.
218: * @param node The W3C DOM node whose XPath is to determine.
219: * @return The string representing the absolute XPath for this node.
220: */
221: public final String xpathForNode(Node _pnode) {
222: if (_pnode == null) {
223: throw new IllegalArgumentException("_pnode == null.");
224: }
225:
226: StringBuffer xpath = new StringBuffer();
227: xpath = iterateBack(_pnode, xpath);
228: return xpath.toString();
229: }
230:
231: // internal stuff
232:
233: private final StringBuffer iterateBack(Node node,
234: StringBuffer buffer) {
235: int nthChild = 1;
236: String nodeName = node.getNodeName();
237: Node prevNode = node;
238: while ((prevNode = prevNode.getPreviousSibling()) != null) {
239: if (prevNode.getNodeName().equals(nodeName)
240: && prevNode.getNodeType() == node.getNodeType()) {
241: nthChild++;
242: }
243: }
244: if (node.getNodeType() == Node.TEXT_NODE) {
245: nodeName = "text()";
246: } else if (node.getNodeType() == Node.COMMENT_NODE) {
247: nodeName = "comment()";
248: } else {
249: if (node.getNodeType() == Node.DOCUMENT_NODE) {
250: nodeName = "";
251: }
252: }
253: buffer.insert(0, "/" + nodeName
254: + (nodeName.length() > 0 ? "[" + nthChild + "]" : ""));
255: node = node.getParentNode();
256: if (node != null && node.getNodeType() != Node.DOCUMENT_NODE) {
257: buffer = iterateBack(node, buffer);
258: }
259: return buffer;
260: }
261:
262: }
|