001: /*
002: * $Id: XMLContextImpl.java,v 1.4 2002/07/18 06:02:22 skavish Exp $
003: *
004: * ===========================================================================
005: *
006: * The JGenerator Software License, Version 1.0
007: *
008: * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
009: *
010: * Redistribution and use in source and binary forms, with or without
011: * modification, are permitted provided that the following conditions are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution, if
022: * any, must include the following acknowlegement:
023: * "This product includes software developed by Dmitry Skavish
024: * (skavish@usa.net, http://www.flashgap.com/)."
025: * Alternately, this acknowlegement may appear in the software itself,
026: * if and wherever such third-party acknowlegements normally appear.
027: *
028: * 4. The name "The JGenerator" must not be used to endorse or promote
029: * products derived from this software without prior written permission.
030: * For written permission, please contact skavish@usa.net.
031: *
032: * 5. Products derived from this software may not be called "The JGenerator"
033: * nor may "The JGenerator" appear in their names without prior written
034: * permission of Dmitry Skavish.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
040: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: *
049: */
050:
051: package org.openlaszlo.iv.flash.xml.apache;
052:
053: import org.openlaszlo.iv.flash.api.*;
054: import org.openlaszlo.iv.flash.util.*;
055:
056: import org.openlaszlo.iv.flash.xml.XMLHelper;
057: import org.openlaszlo.iv.flash.context.*;
058:
059: import java.util.*;
060:
061: import javax.xml.transform.TransformerException;
062:
063: import org.w3c.dom.*;
064: import org.w3c.dom.traversal.NodeIterator;
065:
066: import org.apache.xpath.*;
067: import org.apache.xpath.objects.XObject;
068:
069: /**
070: * Apache XML context
071: * <P>
072: * Represents one xml node
073: *
074: * @author Dmitry Skavish
075: */
076:
077: public class XMLContextImpl extends XMLContext {
078:
079: private XPathContext xpathContext;
080:
081: /**
082: * Creates xml context with specified parent and xml node.
083: *
084: * @param parent parent context
085: * @param node xml node represented by this context
086: */
087:
088: public XMLContextImpl(Context parent, Node node) {
089: super (parent, node);
090: this .xpathContext = new XPathContext();
091: }
092:
093: /**
094: * Evaluates the specified path ( as XPath ) in this context or nearest
095: * xml parent.
096: *
097: * @param string containing XPath expression
098: * @return string representation of result of xpath execution or empty string
099: */
100: public String getValue(String 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
106: .evalXPath(xpathContext, node, path);
107:
108: //System.out.println("xo class: "+xo.getClass().getName());
109: String res = XPathHelper.getXObjectData(xo); //xo.toString();
110: if (res != null) {
111: //System.out.println(" res='"+res+"'" );
112: return res;
113: }
114: } catch (TransformerException e) {
115: // Log.log(e);
116: // ignore this exception, it usually means that it is not an
117: // xpath, but just a variable
118: } catch (Exception e) {
119: Log.logRB(e);
120: }
121:
122: //System.out.println(" (node="+node.getNodeName()+") go to parent with "+path+"" );
123: String res = getValueFromParent(path);
124: //System.out.println( " res from parent='"+res+"'" );
125: return res;
126: }
127:
128: /**
129: * Evaluates the specified path ( as XPath ) in this context or nearest
130: * xml parent. If the path evaluates to a nodeset, a list of contexts for
131: * the nodes in that nodeset is returned, otherwise null
132: *
133: * @param string containing XPath expression
134: * @returns list of contexts or null
135: */
136: public List getValueList(String path) {
137: // Evaluate the path into an XObject
138:
139: //System.out.println( "XMLContext.getValueList("+path+")" );
140: try {
141: //NodeList nlist = XPathAPI.selectNodeList(node, path);
142: NodeList nlist = XPathHelper.selectNodeList(xpathContext,
143: node, path);
144:
145: int l = nlist.getLength();
146: if (l > 0) {
147: ArrayList list = new ArrayList(l);
148: //System.out.println(" items ("+l+"):");
149: for (int i = 0; i < l; i++) {
150: Node node = nlist.item(i);
151: //System.out.println(" node: "+node.getNodeName());
152: list.add(new XMLContextImpl(this , node));
153: }
154:
155: return list;
156: } else {
157: // The path either evaluated to nothing ( an unresolvable path ) or an
158: // atomic type. Neither of these suit our purposes ( I think ) so for
159: // now defer to the parent.
160:
161: return getValueListFromParent(path);
162: }
163: } catch (TransformerException e) {
164: // As usual, we don't want to bail just because an exception
165: // occurs in a single path evaluation, so log and defer to parent.
166:
167: Log.logRB(e);
168: return getValueListFromParent(path);
169: }
170: }
171:
172: /**
173: * Compiles given string into XPath and execute it in this context or its parent.
174: *
175: * @param expr supposedly XPath expression
176: * @return result of XPath execution
177: * @exception TransformerException
178: */
179: /* private XObject eval( String expr ) throws TransformerException {
180: //XObject xo = XPathAPI.eval(node, expr);
181: //System.out.println("XMLContext.eval("+node.getNodeName()+", "+expr+", type="+xo.getTypeString()+")=bebe");
182: XPath xpath = XPathHelper.getXPath(node, expr);
183: return eval( xpath );
184: }
185: */
186: /**
187: * Executes specified XPath in this context or nearest xml parent.
188: *
189: * @param xpath compiled XPath expression
190: * @return result of xpath execution
191: * @exception TransformerException
192: */
193: /* private XObject eval( XPath xpath ) throws TransformerException {
194: XObject xo = XPathHelper.evalXPath( xpathContext, node, xpath );
195:
196: return isUndefined( xo ) ? null : xo;
197: }
198: */
199: /**
200: * Check whether the specified XObject undefined or not.
201: *
202: * @param xo XObject, usually results of XPath execution
203: * @return true if specified xobject is undefined
204: * @exception TransformerException
205: */
206: /* private boolean isUndefined( XObject xo ) throws TransformerException {
207: if( xo == null ) return true;
208: int type = xo.getType();
209:
210: System.out.println("isUndefined(type="+xo.getTypeString()+")="+xo);
211: if( type == XObject.CLASS_NODESET )
212: System.out.println(" length="+XPathHelper.getNodeList(xo).getLength());
213: //return type == XObject.CLASS_UNKNOWN;
214: return ( type == XObject.CLASS_UNKNOWN ||
215: ( type == XObject.CLASS_NODESET
216: && XPathHelper.getNodeList(xo).getLength() == 0 ) );
217: return ( type == XObject.CLASS_UNKNOWN ||
218: ( type == XObject.CLASS_NODESET
219: && xo.nodeset().nextNode() == null ) );
220: }*/
221: }
|