KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > expr > ParentNodeExpression


1 package com.icl.saxon.expr;
2 import com.icl.saxon.Context;
3 import com.icl.saxon.om.NodeInfo;
4
5 /**
6 * Class ParentNodeExpression represents the XPath expression ".." or "parent::node()"
7 */

8
9 public class ParentNodeExpression extends SingletonExpression {
10
11     /**
12     * Return the node selected by this SingletonExpression
13     * @param context The context for the evaluation
14     * @return the parent of the current node defined by the context
15     */

16
17     public NodeInfo getNode(Context context) throws XPathException {
18         return context.getContextNodeInfo().getParent();
19     }
20
21     /**
22     * Determine which aspects of the context the expression depends on. The result is
23     * a bitwise-or'ed value composed from constants such as Context.VARIABLES and
24     * Context.CURRENT_NODE
25     */

26
27     public int getDependencies() {
28         return Context.CONTEXT_NODE;
29     }
30
31     /**
32     * Evaluate as a string. Returns the string value of the parent node
33     * @param context The context in which the expression is to be evaluated
34     * @return the value of the current node, identified by the context
35     */

36
37     public String JavaDoc evaluateAsString(Context context) throws XPathException {
38         NodeInfo parent = context.getContextNodeInfo().getParent();
39         if (parent==null) return "";
40         return parent.getStringValue();
41     }
42
43     /**
44     * Evaluate as a boolean. Returns true if there are any nodes
45     * selected by the NodeSetExpression.
46     * @param context The context in which the expression is to be evaluated
47     * @return true unless the current node is the Document node
48     */

49
50     public boolean evaluateAsBoolean(Context context) throws XPathException {
51         return (context.getContextNodeInfo().getParent()!=null);
52     }
53
54     /**
55     * Perform a partial evaluation of the expression, by eliminating specified dependencies
56     * on the context.
57     * @param dependencies The dependencies to be removed
58     * @param context The context to be used for the partial evaluation
59     * @return a new expression that does not have any of the specified
60     * dependencies
61     */

62
63     public Expression reduce(int dependencies, Context context) throws XPathException {
64         if ((dependencies & Context.CONTEXT_NODE) != 0 ) {
65             return new SingletonNodeSet(context.getContextNodeInfo().getParent());
66         } else {
67             return this;
68         }
69     }
70
71     /**
72     * Diagnostic print of expression structure
73     */

74     
75     public void display(int level) {
76         System.err.println(indent(level) + "..");
77     }
78
79 }
80
81 //
82
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
83
// you may not use this file except in compliance with the License. You may obtain a copy of the
84
// License at http://www.mozilla.org/MPL/
85
//
86
// Software distributed under the License is distributed on an "AS IS" basis,
87
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
88
// See the License for the specific language governing rights and limitations under the License.
89
//
90
// The Original Code is: all this file.
91
//
92
// The Initial Developer of the Original Code is
93
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
94
//
95
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
96
//
97
// Contributor(s): none.
98
//
99
Popular Tags