KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ozoneDB > xml > util > XMLContainerHelperImpl


1 // You can redistribute this software and/or modify it under the terms of
2
// the Ozone Library License version 1 published by ozone-db.org.
3
//
4
// The original code and portions created by SMB are
5
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
6
//
7
// $Id$
8

9 package org.ozoneDB.xml.util;
10
11 import java.io.IOException JavaDoc;
12
13
14
15 import org.w3c.dom.Document JavaDoc;
16
17 import org.w3c.dom.Node JavaDoc;
18
19 import org.w3c.dom.NodeList JavaDoc;
20
21
22
23 import org.xml.sax.SAXException JavaDoc;
24
25
26
27 import org.ozoneDB.OzoneObject;
28
29 import org.ozoneDB.xml.dom.DocumentProxy;
30
31
32
33 import org.infozone.tools.xml.queries.XObject;
34
35 import org.infozone.tools.xml.queries.XPathQuery;
36
37 import org.infozone.tools.xml.queries.XUpdateQuery;
38
39 import org.infozone.tools.xml.queries.XPathQueryFactory;
40
41 import org.infozone.tools.xml.queries.XUpdateQueryFactory;
42
43
44 /**
45  * This class provides the server side part of the XMLContainer. It mainly
46  * handles storing and retrieving of parts of the underlying XML document.
47  *
48  * @version $Revision$ $Date$
49  * @author <a HREF="http://www.smb-tec.com">SMB</a>
50  */

51 public final class XMLContainerHelperImpl
52              extends OzoneObject
53              implements XMLContainerHelper {
54
55     private static final long serialVersionUID = 4L;
56
57     private static final boolean debug = XMLContainer.debug;
58
59     private static XPathQueryFactory xpathQueryFactory;
60
61     private static XUpdateQueryFactory xupdateQueryFactory;
62
63     /** The underlying XML document. */
64     private Document JavaDoc document;
65
66     static {
67         // use Xt and Lexus for XPath and XUpdate
68
if (System.getProperties().get( "org.infozone.tools.xml.queries.XPathQueryFactory" ) == null) {
69             System.getProperties().put( "org.infozone.tools.xml.queries.XPathQueryFactory",
70                                         "org.infozone.tools.xml.queries.xt.XPathQueryFactoryImpl");
71         }
72         if (System.getProperties().get( "org.infozone.tools.xml.queries.XUpdateQueryFactory" ) == null ) {
73             System.getProperties().put( "org.infozone.tools.xml.queries.XUpdateQueryFactory",
74                                         "org.infozone.lexus.XUpdateQueryFactoryImpl");
75         }
76
77         xpathQueryFactory = XPathQueryFactory.newInstance();
78         xupdateQueryFactory = XUpdateQueryFactory.newInstance();
79     }
80
81     public XMLContainerHelperImpl() {
82         if (debug) {
83             System.out.println( "helper: ctor..." );
84         }
85     }
86
87     public final void onCreate() {
88         if (debug) {
89             System.out.println( "helper: onCreate()..." );
90         }
91         document = (Document JavaDoc)database().createObject( org.ozoneDB.xml.dom.DocumentImpl.class.getName() );
92         ((DocumentProxy)document).setContainer( this );
93     }
94
95     public final void onDelete() {
96         if (debug) {
97             System.out.println( "helper: onDelete()..." );
98         }
99         clearDocument();
100     }
101
102     public void clearDocument() {
103         // FIXME: this is what we should do but that does not work
104
// for some strange reason
105
//Node pNode = getDocument();
106
//((DocumentProxy)pNode).clearDocument();
107

108         // HACK: just create a new Document instead
109
if (document != null) {
110             database().deleteObject( (DocumentProxy)document );
111         }
112         document = (Document JavaDoc)database().createObject( org.ozoneDB.xml.dom.DocumentImpl.class.getName() );
113     }
114
115     public final void setDocument( Document JavaDoc _pdoc ) {
116         if (debug) {
117             System.out.println( "helper: setDocument..." );
118         }
119
120         if (document != null) {
121             throw new IllegalStateException JavaDoc( "Container helper is already assigned to a document." );
122         }
123
124         document = _pdoc;
125         ((DocumentProxy)document).setContainer( this );
126     }
127
128     public final Document JavaDoc getDocument() {
129         if (debug) {
130             System.out.println( "helper: getDocument..." );
131         }
132         return document;
133     }
134
135     public final SAXChunkConsumer beginInputSequence( Node JavaDoc _pNode ) throws Exception JavaDoc {
136         if (debug) {
137             System.out.println("helper: beginInputSequence...");
138         }
139         if (_pNode == null) {
140             clearDocument();
141         }
142         SAXChunkConsumer consumer = new SAXChunkConsumer( getDocument(), _pNode );
143         return consumer;
144     }
145
146     public final SAXChunkConsumer putChunk( byte[] _chunkData, SAXChunkConsumer _consumer )
147             throws SAXException JavaDoc, IOException JavaDoc {
148         _consumer.processChunk( _chunkData );
149         return _consumer;
150     }
151
152     public final void endInputSequence() throws Exception JavaDoc {
153         if (debug) {
154             System.out.println( "helper: endInputSequence..." );
155         }
156     }
157
158 /* public final SAXChunkProducer beginOutputSequence( Node _pnode, int _depth ) throws Exception {
159         if (_pnode == null) {
160             _pnode = getDocument();
161         }
162         ModifiableNodeList mnl = new ModifiableNodeList(1);
163         mnl.addNode(_pnode);
164         return new SAXChunkProducer( mnl, _depth );
165     }
166 */

167     public final SAXChunkProducer beginOutputSequence( NodeList JavaDoc _pnodes, int _depth ) throws Exception JavaDoc {
168         if (_pnodes == null) {
169             _pnodes = new ModifiableNodeList(1);
170             ((ModifiableNodeList)_pnodes).addNode(getDocument());
171         }
172         return new SAXChunkProducer( _pnodes, _depth );
173     }
174
175     public final SAXChunkProducer createNextChunk( SAXChunkProducer producer ) throws SAXException JavaDoc {
176         producer.createNextChunk();
177         return producer;
178     }
179
180     public final void endOutputSequence() throws Exception JavaDoc {
181         if (debug) {
182             System.out.println( "helper: endOutputSequence..." );
183         }
184     }
185
186     public final XObject executeXPath( OzoneXPathQuery _query ) throws Exception JavaDoc {
187         if (_query.rootNode == null) {
188             _query.rootNode = getDocument();
189         }
190         XPathQuery query = xpathQueryFactory.newXPathQuery();
191         query.setQString( _query.qstring );
192         if (_query.filter != null) {
193             query.setNodeFilter( _query.filter );
194         }
195         if (_query.namespace != null) {
196             query.setNamespace( _query.namespace );
197         }
198         return query.execute( _query.rootNode );
199     }
200
201     public final void executeXUpdate( OzoneXUpdateQuery _query ) throws Exception JavaDoc {
202         if (_query.rootNode == null) {
203             _query.rootNode = getDocument();
204         }
205         XUpdateQuery query = xupdateQueryFactory.newXUpdateQuery();
206         query.setQString( _query.qstring );
207         if (_query.filter != null) {
208             query.setNodeFilter( _query.filter );
209         }
210         if (_query.namespace != null) {
211             query.setNamespace( _query.namespace );
212         }
213         System.out.println( query.getClass().getName() );
214         System.out.println( _query.qstring );
215         System.out.println( _query.rootNode );
216         query.execute( _query.rootNode );
217     }
218
219     /**
220      * Determines the absolute XPath for the given node.
221      * @param _pnode The W3C DOM node whose XPath is to determine.
222      * @return The string representing the absolute XPath for this node.
223      */

224     public final String JavaDoc xpathForNode( Node JavaDoc _pnode ) {
225         if (_pnode == null) {
226             throw new IllegalArgumentException JavaDoc( "_pnode == null." );
227         }
228         StringBuffer JavaDoc xpath = new StringBuffer JavaDoc();
229         xpath = iterateBack( _pnode, xpath );
230         return xpath.toString();
231     }
232
233     // internal stuff
234

235     private final StringBuffer JavaDoc iterateBack( Node JavaDoc node, StringBuffer JavaDoc buffer ) {
236         int nthChild = 1;
237         String JavaDoc nodeName = node.getNodeName();
238         Node JavaDoc prevNode = node;
239         while ((prevNode = prevNode.getPreviousSibling()) != null) {
240             if (prevNode.getNodeName().equals( nodeName ) && 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 + (nodeName.length() > 0 ? "[" + nthChild + "]" : "") );
254         node = node.getParentNode();
255         if (node != null && node.getNodeType() != Node.DOCUMENT_NODE) {
256             buffer = iterateBack( node, buffer );
257         }
258         return buffer;
259     }
260 }
Popular Tags