001: package net.sf.saxon.expr;
002:
003: import net.sf.saxon.om.DocumentInfo;
004: import net.sf.saxon.om.Item;
005: import net.sf.saxon.om.NamePool;
006: import net.sf.saxon.om.NodeInfo;
007: import net.sf.saxon.trans.StaticError;
008: import net.sf.saxon.trans.XPathException;
009: import net.sf.saxon.type.ItemType;
010: import net.sf.saxon.type.TypeHierarchy;
011: import net.sf.saxon.pattern.NodeKindTest;
012:
013: import java.io.PrintStream;
014:
015: /**
016: * An expression whose value is always a set of nodes containing a single node,
017: * the document root. This corresponds to the XPath Expression "/", including the implicit
018: * "/" at the start of a path expression with a leading "/".
019: */
020:
021: public class RootExpression extends SingleNodeExpression {
022:
023: /**
024: * Simplify an expression
025: * @return the simplified expression
026: */
027:
028: public Expression simplify(StaticContext env) throws StaticError {
029: return this ;
030: }
031:
032: /**
033: * Is this expression the same as another expression?
034: */
035:
036: public boolean equals(Object other) {
037: return (other instanceof RootExpression);
038: }
039:
040: /**
041: * Specify that the expression returns a singleton
042: */
043:
044: public final int computeCardinality() {
045: return StaticProperty.EXACTLY_ONE;
046: }
047:
048: /**
049: * Determine the data type of the items returned by this expression
050: *
051: * @return Type.NODE
052: * @param th
053: */
054:
055: public ItemType getItemType(TypeHierarchy th) {
056: return NodeKindTest.DOCUMENT;
057: }
058:
059: /**
060: * get HashCode for comparing two expressions
061: */
062:
063: public int hashCode() {
064: return "RootExpression".hashCode();
065: }
066:
067: /**
068: * Return the first element selected by this Expression
069: * @param context The evaluation context
070: * @return the NodeInfo of the first selected element, or null if no element
071: * is selected
072: */
073:
074: public NodeInfo getNode(XPathContext context) throws XPathException {
075: Item current = context.getContextItem();
076: if (current == null) {
077: dynamicError(
078: "Finding root of tree: the context item is undefined",
079: "XPDY0002", context);
080: }
081: if (current instanceof NodeInfo) {
082: DocumentInfo doc = ((NodeInfo) current).getDocumentRoot();
083: if (doc == null) {
084: dynamicError(
085: "The root of the tree containing the context item is not a document node",
086: "XPDY0050", context);
087: }
088: return doc;
089: }
090: typeError(
091: "Finding root of tree: the context item is not a node",
092: "XPTY0020", context);
093: // dummy return; we never get here
094: return null;
095: }
096:
097: /**
098: * Determine which aspects of the context the expression depends on. The result is
099: * a bitwise-or'ed value composed from constants such as StaticProperty.VARIABLES and
100: * StaticProperty.CURRENT_NODE
101: */
102:
103: public int getIntrinsicDependencies() {
104: return StaticProperty.DEPENDS_ON_CONTEXT_DOCUMENT
105: | StaticProperty.SINGLE_DOCUMENT_NODESET
106: | StaticProperty.CONTEXT_DOCUMENT_NODESET;
107: }
108:
109: /**
110: * Diagnostic print of expression structure
111: */
112:
113: public void display(int level, NamePool pool, PrintStream out) {
114: out.println(ExpressionTool.indent(level) + '/');
115: }
116:
117: }
118:
119: //
120: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
121: // you may not use this file except in compliance with the License. You may obtain a copy of the
122: // License at http://www.mozilla.org/MPL/
123: //
124: // Software distributed under the License is distributed on an "AS IS" basis,
125: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
126: // See the License for the specific language governing rights and limitations under the License.
127: //
128: // The Original Code is: all this file.
129: //
130: // The Initial Developer of the Original Code is Michael H. Kay.
131: //
132: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
133: //
134: // Contributor(s): none.
135: //
|