KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > iv > flash > xml > apache > XMLContextImpl


1 /*
2  * $Id: XMLContextImpl.java,v 1.4 2002/07/18 06:02:22 skavish Exp $
3  *
4  * ===========================================================================
5  *
6  * The JGenerator Software License, Version 1.0
7  *
8  * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowlegement:
23  * "This product includes software developed by Dmitry Skavish
24  * (skavish@usa.net, http://www.flashgap.com/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The name "The JGenerator" must not be used to endorse or promote
29  * products derived from this software without prior written permission.
30  * For written permission, please contact skavish@usa.net.
31  *
32  * 5. Products derived from this software may not be called "The JGenerator"
33  * nor may "The JGenerator" appear in their names without prior written
34  * permission of Dmitry Skavish.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
40  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  *
49  */

50
51 package org.openlaszlo.iv.flash.xml.apache;
52
53 import org.openlaszlo.iv.flash.api.*;
54 import org.openlaszlo.iv.flash.util.*;
55
56 import org.openlaszlo.iv.flash.xml.XMLHelper;
57 import org.openlaszlo.iv.flash.context.*;
58
59 import java.util.*;
60
61 import javax.xml.transform.TransformerException JavaDoc;
62
63 import org.w3c.dom.*;
64 import org.w3c.dom.traversal.NodeIterator;
65
66 import org.apache.xpath.*;
67 import org.apache.xpath.objects.XObject;
68
69 /**
70  * Apache XML context
71  * <P>
72  * Represents one xml node
73  *
74  * @author Dmitry Skavish
75  */

76
77 public class XMLContextImpl extends XMLContext {
78
79     private XPathContext xpathContext;
80
81     /**
82      * Creates xml context with specified parent and xml node.
83      *
84      * @param parent parent context
85      * @param node xml node represented by this context
86      */

87
88     public XMLContextImpl( Context parent, Node node ) {
89         super(parent, node);
90         this.xpathContext = new XPathContext();
91     }
92
93     /**
94      * Evaluates the specified path ( as XPath ) in this context or nearest
95      * xml parent.
96      *
97      * @param string containing XPath expression
98      * @return string representation of result of xpath execution or empty string
99      */

100     public String JavaDoc getValue( String JavaDoc path ) {
101         // Evaluate the XPath in this context
102

103         //System.out.println( "XMLContext(node="+node.getNodeName()+").getValue("+path+")" );
104
try {
105             XObject xo = XPathHelper.evalXPath(xpathContext, node, path);
106
107             //System.out.println("xo class: "+xo.getClass().getName());
108
String JavaDoc res = XPathHelper.getXObjectData(xo); //xo.toString();
109
if( res != null ) {
110                 //System.out.println(" res='"+res+"'" );
111
return res;
112             }
113         } catch( TransformerException JavaDoc e ) {
114             // Log.log(e);
115
// ignore this exception, it usually means that it is not an
116
// xpath, but just a variable
117
} catch( Exception JavaDoc e ) {
118             Log.logRB(e);
119         }
120
121         //System.out.println(" (node="+node.getNodeName()+") go to parent with "+path+"" );
122
String JavaDoc res = getValueFromParent( path );
123         //System.out.println( " res from parent='"+res+"'" );
124
return res;
125     }
126
127     /**
128      * Evaluates the specified path ( as XPath ) in this context or nearest
129      * xml parent. If the path evaluates to a nodeset, a list of contexts for
130      * the nodes in that nodeset is returned, otherwise null
131      *
132      * @param string containing XPath expression
133      * @returns list of contexts or null
134      */

135     public List getValueList( String JavaDoc path ) {
136         // Evaluate the path into an XObject
137

138         //System.out.println( "XMLContext.getValueList("+path+")" );
139
try {
140             //NodeList nlist = XPathAPI.selectNodeList(node, path);
141
NodeList nlist = XPathHelper.selectNodeList(xpathContext, node, path);
142
143             int l = nlist.getLength();
144             if( l > 0 ) {
145                 ArrayList list = new ArrayList(l);
146                 //System.out.println(" items ("+l+"):");
147
for( int i=0; i<l; i++ ) {
148                     Node node = nlist.item(i);
149                     //System.out.println(" node: "+node.getNodeName());
150
list.add( new XMLContextImpl( this, node ) );
151                 }
152
153                 return list;
154             } else {
155                 // The path either evaluated to nothing ( an unresolvable path ) or an
156
// atomic type. Neither of these suit our purposes ( I think ) so for
157
// now defer to the parent.
158

159                 return getValueListFromParent( path );
160             }
161         } catch ( TransformerException JavaDoc e ) {
162             // As usual, we don't want to bail just because an exception
163
// occurs in a single path evaluation, so log and defer to parent.
164

165             Log.logRB( e );
166             return getValueListFromParent( path );
167         }
168     }
169
170     /**
171      * Compiles given string into XPath and execute it in this context or its parent.
172      *
173      * @param expr supposedly XPath expression
174      * @return result of XPath execution
175      * @exception TransformerException
176      */

177 /* private XObject eval( String expr ) throws TransformerException {
178         //XObject xo = XPathAPI.eval(node, expr);
179         //System.out.println("XMLContext.eval("+node.getNodeName()+", "+expr+", type="+xo.getTypeString()+")=bebe");
180         XPath xpath = XPathHelper.getXPath(node, expr);
181         return eval( xpath );
182     }
183 */

184     /**
185      * Executes specified XPath in this context or nearest xml parent.
186      *
187      * @param xpath compiled XPath expression
188      * @return result of xpath execution
189      * @exception TransformerException
190      */

191 /* private XObject eval( XPath xpath ) throws TransformerException {
192         XObject xo = XPathHelper.evalXPath( xpathContext, node, xpath );
193
194         return isUndefined( xo ) ? null : xo;
195     }
196 */

197     /**
198      * Check whether the specified XObject undefined or not.
199      *
200      * @param xo XObject, usually results of XPath execution
201      * @return true if specified xobject is undefined
202      * @exception TransformerException
203      */

204 /* private boolean isUndefined( XObject xo ) throws TransformerException {
205         if( xo == null ) return true;
206         int type = xo.getType();
207
208         System.out.println("isUndefined(type="+xo.getTypeString()+")="+xo);
209         if( type == XObject.CLASS_NODESET )
210             System.out.println(" length="+XPathHelper.getNodeList(xo).getLength());
211         //return type == XObject.CLASS_UNKNOWN;
212         return ( type == XObject.CLASS_UNKNOWN ||
213                  ( type == XObject.CLASS_NODESET
214                    && XPathHelper.getNodeList(xo).getLength() == 0 ) );
215         return ( type == XObject.CLASS_UNKNOWN ||
216                  ( type == XObject.CLASS_NODESET
217                    && xo.nodeset().nextNode() == null ) );
218     }*/

219 }
220
Popular Tags