KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > functions > Id


1 package com.icl.saxon.functions;
2 import com.icl.saxon.*;
3 import com.icl.saxon.om.*;
4 import com.icl.saxon.expr.*;
5 import com.icl.saxon.sort.LocalOrderComparer;
6
7 import java.util.*;
8 import java.lang.Math;
9 import java.text.*;
10
11
12
13 public class Id extends Function {
14
15     private DocumentInfo boundDocument = null;
16
17     public String getName() {
18         return "id";
19     };
20
21     /**
22     * Determine the data type of the expression
23     * @return Value.NODESET
24     */

25
26     public int getDataType() {
27         return Value.NODESET;
28     }
29
30     /**
31     * Determine, in the case of an expression whose data type is Value.NODESET,
32     * whether all the nodes in the node-set are guaranteed to come from the same
33     * document as the context node. Used for optimization.
34     */

35     
36     public boolean isContextDocumentNodeSet() {
37         return true;
38     }
39
40     /**
41     * Simplify and validate
42     */

43
44     public Expression simplify() throws XPathException {
45         checkArgumentCount(1, 1);
46         argument[0] = argument[0].simplify();
47         return this;
48     }
49
50     /**
51     * Evaluate in a context where a node-set is required
52     */

53
54     public NodeSetValue evaluateAsNodeSet(Context context) throws XPathException {
55         return findId(argument[0].evaluate(context), context);
56     }
57
58     /**
59     * Evaluate in a general context
60     */

61
62     public Value evaluate(Context context) throws XPathException {
63         return evaluateAsNodeSet(context);
64     }
65
66     /**
67     * Determine which aspects of the context the expression depends on. The result is
68     * a bitwise-or'ed value composed from constants such as Context.VARIABLES and
69     * Context.CURRENT_NODE
70     */

71
72     public int getDependencies() {
73         int dep = argument[0].getDependencies();
74         if (boundDocument != null) {
75             return dep;
76         } else {
77             return dep | Context.CONTEXT_NODE | Context.CONTEXT_DOCUMENT;
78         }
79     }
80
81     /**
82     * Remove specified dependencies.
83     */

84
85     public Expression reduce(int dependencies, Context context)
86             throws XPathException {
87
88         Id id = new Id();
89         id.addArgument(argument[0].reduce(dependencies, context));
90         id.setStaticContext(getStaticContext());
91         id.boundDocument = boundDocument;
92
93         if (boundDocument==null &&
94                 ((dependencies & (Context.CONTEXT_NODE | Context.CONTEXT_DOCUMENT)) != 0)) {
95             id.boundDocument = (context.getContextNodeInfo()).getDocumentRoot();
96         }
97         return id;
98     }
99
100     /**
101     * This method actually evaluates the function
102     */

103
104     private NodeSetValue findId(Value arg0, Context context) throws XPathException {
105         Vector idrefresult = null;
106         DocumentInfo doc;
107         if (boundDocument==null) {
108             doc = (context.getContextNodeInfo()).getDocumentRoot();
109         } else {
110             doc = boundDocument;
111         }
112
113         if ((arg0 instanceof NodeSetValue) &&
114                 !(arg0 instanceof FragmentValue || arg0 instanceof FragmentValue)) {
115             
116             NodeEnumeration enum = ((NodeSetValue)arg0).enumerate();
117             while (enum.hasMoreElements()) {
118                 NodeInfo node = enum.nextElement();
119                 String s = node.getStringValue();
120                 StringTokenizer st = new StringTokenizer(s);
121                 while (st.hasMoreTokens()) {
122                     NodeInfo el = doc.selectID(st.nextToken());
123                     if (el!=null) {
124                         if (idrefresult==null) {
125                             idrefresult = new Vector(2);
126                         }
127                         idrefresult.addElement(el);
128                     }
129                 }
130             }
131
132         } else {
133
134             String s = arg0.asString();
135             StringTokenizer st = new StringTokenizer(s);
136             while (st.hasMoreTokens()) {
137                 NodeInfo el = doc.selectID(st.nextToken());
138                 if (el!=null) {
139                     if (idrefresult==null) {
140                         idrefresult = new Vector(2);
141                     }
142                     idrefresult.addElement(el);
143                 }
144             }
145         }
146
147         if (idrefresult==null) {
148             return new EmptyNodeSet();
149         }
150         if (idrefresult.size() == 1) {
151             return new SingletonNodeSet((NodeInfo)idrefresult.elementAt(0));
152         }
153         return new NodeSetExtent(idrefresult, LocalOrderComparer.getInstance());
154
155     }
156
157 }
158
159
160
161
162 //
163
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
164
// you may not use this file except in compliance with the License. You may obtain a copy of the
165
// License at http://www.mozilla.org/MPL/
166
//
167
// Software distributed under the License is distributed on an "AS IS" basis,
168
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
169
// See the License for the specific language governing rights and limitations under the License.
170
//
171
// The Original Code is: all this file.
172
//
173
// The Initial Developer of the Original Code is
174
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
175
//
176
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
177
//
178
// Contributor(s): none.
179
//
180
Popular Tags