KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > expr > RootExpression


1 package net.sf.saxon.expr;
2 import net.sf.saxon.om.DocumentInfo;
3 import net.sf.saxon.om.Item;
4 import net.sf.saxon.om.NamePool;
5 import net.sf.saxon.om.NodeInfo;
6 import net.sf.saxon.trans.StaticError;
7 import net.sf.saxon.trans.XPathException;
8 import net.sf.saxon.type.ItemType;
9 import net.sf.saxon.pattern.NodeKindTest;
10
11 import java.io.PrintStream JavaDoc;
12
13
14 /**
15  * An expression whose value is always a set of nodes containing a single node,
16  * the document root. This corresponds to the XPath Expression "/", including the implicit
17  * "/" at the start of a path expression with a leading "/".
18 */

19
20 public class RootExpression extends SingleNodeExpression {
21
22
23     /**
24     * Simplify an expression
25     * @return the simplified expression
26     */

27
28      public Expression simplify(StaticContext env) throws StaticError {
29         return this;
30     }
31
32     /**
33     * Is this expression the same as another expression?
34     */

35
36     public boolean equals(Object JavaDoc other) {
37         return (other instanceof RootExpression);
38     }
39
40     /**
41     * Specify that the expression returns a singleton
42     */

43
44     public final int computeCardinality() {
45         return StaticProperty.EXACTLY_ONE;
46     }
47
48     /**
49      * Determine the data type of the items returned by this expression
50      *
51      * @return Type.NODE
52      */

53
54     public ItemType getItemType() {
55         return NodeKindTest.DOCUMENT;
56     }
57
58     /**
59     * get HashCode for comparing two expressions
60     */

61
62     public int hashCode() {
63         return "RootExpression".hashCode();
64     }
65
66     /**
67     * Return the first element selected by this Expression
68     * @param context The evaluation context
69     * @return the NodeInfo of the first selected element, or null if no element
70     * is selected
71     */

72
73     public NodeInfo getNode(XPathContext context) throws XPathException {
74         Item current = context.getContextItem();
75         if (current==null) {
76             dynamicError("Finding root of tree: the context item is undefined", "XPDY0002", context);
77         }
78         if (current instanceof NodeInfo) {
79             DocumentInfo doc = ((NodeInfo)current).getDocumentRoot();
80             if (doc==null) {
81                 dynamicError("The root of the tree containing the context item is not a document node", "XPDY0050", context);
82             }
83             return doc;
84         }
85         typeError("Finding root of tree: the context item is not a node", "XPTY0020", context);
86         // dummy return; we never get here
87
return null;
88     }
89
90     /**
91     * Determine which aspects of the context the expression depends on. The result is
92     * a bitwise-or'ed value composed from constants such as StaticProperty.VARIABLES and
93     * StaticProperty.CURRENT_NODE
94     */

95
96     public int getIntrinsicDependencies() {
97         return StaticProperty.DEPENDS_ON_CONTEXT_DOCUMENT |
98                 StaticProperty.SINGLE_DOCUMENT_NODESET |
99                 StaticProperty.CONTEXT_DOCUMENT_NODESET;
100     }
101
102     /**
103     * Diagnostic print of expression structure
104     */

105
106     public void display(int level, NamePool pool, PrintStream JavaDoc out) {
107         out.println(ExpressionTool.indent(level) + '/');
108     }
109
110 }
111
112 //
113
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
114
// you may not use this file except in compliance with the License. You may obtain a copy of the
115
// License at http://www.mozilla.org/MPL/
116
//
117
// Software distributed under the License is distributed on an "AS IS" basis,
118
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
119
// See the License for the specific language governing rights and limitations under the License.
120
//
121
// The Original Code is: all this file.
122
//
123
// The Initial Developer of the Original Code is Michael H. Kay.
124
//
125
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
126
//
127
// Contributor(s): none.
128
//
129
Popular Tags