01: /*
02: * selectNodeList.java
03: *
04: * Copyright (c) 2005 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.xml;
10:
11: import com.sun.org.apache.xpath.internal.XPathAPI;
12: import pnuts.lang.PnutsFunction;
13: import pnuts.lang.Context;
14: import pnuts.lang.PnutsException;
15: import org.w3c.dom.Node;
16: import javax.xml.transform.TransformerException;
17: import javax.xml.xpath.XPath;
18: import javax.xml.xpath.XPathConstants;
19: import javax.xml.xpath.XPathFactory;
20:
21: public class selectNodeList extends PnutsFunction {
22:
23: public selectNodeList() {
24: super ("selectNodeList");
25: }
26:
27: public boolean defined(int nargs) {
28: return nargs == 2 || nargs == 3;
29: }
30:
31: protected Object exec(Object[] args, Context context) {
32: try {
33: int nargs = args.length;
34: if (nargs == 2) {
35: Object target = args[0];
36: String expr = (String) args[1];
37: XPath xpath = XPathFactory.newInstance().newXPath();
38: if (target instanceof Node) {
39: return xpath.evaluate(expr, target,
40: XPathConstants.NODESET);
41: } else {
42: return xpath.evaluate(expr, Util.inputSource(
43: target, context), XPathConstants.NODESET);
44: }
45: } else if (nargs == 3) {
46: Object target = args[0];
47: String expr = (String) args[1];
48: XPath xpath = XPathFactory.newInstance().newXPath();
49: if (target instanceof Node) {
50: return xpath.evaluate(expr, target,
51: XPathConstants.NODESET);
52: } else {
53: return xpath.evaluate(expr, Util.inputSource(
54: target, context), XPathConstants.NODESET);
55: }
56: } else {
57: undefined(args, context);
58: return null;
59: }
60: } catch (Exception e) {
61: throw new PnutsException(e, context);
62: }
63: }
64:
65: public String toString() {
66: return "function selectNodeList(Node, xpathExpression)";
67: }
68: }
|