KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.icl.saxon.expr;
2 import com.icl.saxon.*;
3 import com.icl.saxon.om.*;
4 import com.icl.saxon.pattern.NameTest;
5 import com.icl.saxon.om.Axis;
6 import java.util.*;
7
8 /**
9 * An expression that represents a reference to a named attribute
10 */

11
12 class AttributeReference extends SingletonExpression {
13
14     private int fingerprint;
15     private NodeInfo boundParentNode = null; // null implies use the context node
16

17     // private Constructor
18

19     private AttributeReference() {}
20
21     /**
22     * Constructor
23     */

24
25     public AttributeReference(int fingerprint) {
26         this.fingerprint = fingerprint;
27     }
28
29     /**
30     * Bind the reference to a particular node
31     */

32
33     public void bindParentNode(NodeInfo node) {
34         boundParentNode = node;
35     }
36
37     /**
38     * Get the parent node
39     */

40
41     private NodeInfo getParentNode(Context context) {
42         if (boundParentNode == null) {
43             return context.getContextNodeInfo();
44         } else {
45             return boundParentNode;
46         }
47     }
48
49     /**
50     * Return the relevant attribute node
51     * @param context the evaluation context
52     */

53
54     public NodeInfo getNode(Context context) throws XPathException {
55         NodeInfo node = getParentNode(context);
56         if (node.getNodeType()==NodeInfo.ELEMENT) {
57             NameTest test = new NameTest(NodeInfo.ATTRIBUTE, fingerprint);
58             NodeEnumeration enum = node.getEnumeration(Axis.ATTRIBUTE, test);
59             if (enum.hasMoreElements()) {
60                 return enum.nextElement();
61             }
62             return null;
63         }
64         return null;
65     }
66
67     /**
68     * Evaluate as a boolean. Returns true if there are any nodes
69     * selected by the NodeSetExpression
70     * @param context The context in which the expression is to be evaluated
71     * @return true if there are any nodes selected by the NodeSetExpression
72     */

73
74     public boolean evaluateAsBoolean(Context context) throws XPathException {
75         NodeInfo node = getParentNode(context);
76         if (node.getNodeType()==NodeInfo.ELEMENT) {
77             return node.getAttributeValue(fingerprint)!=null;
78         }
79         return false;
80     }
81
82     /**
83     * Evaluate as a string. Returns the string value of the attribute if it exists
84     * @param context The context in which the expression is to be evaluated
85     * @return true if there are any nodes selected by the NodeSetExpression
86     */

87
88     public String evaluateAsString(Context context) throws XPathException {
89         NodeInfo node = getParentNode(context);
90         if (node.getNodeType()==NodeInfo.ELEMENT) {
91             String s = node.getAttributeValue(fingerprint);
92             if (s==null) return "";
93             return s;
94         }
95         return "";
96     }
97
98
99     /**
100     * Determine which aspects of the context the expression depends on. The result is
101     * a bitwise-or'ed value composed from constants such as Context.VARIABLES and
102     * Context.CURRENT_NODE
103     */

104
105     public int getDependencies() {
106         if (boundParentNode==null) {
107             return Context.CONTEXT_NODE;
108         } else {
109             return 0;
110         }
111     }
112
113     /**
114     * Perform a partial evaluation of the expression, by eliminating specified dependencies
115     * on the context.
116     * @param dependencies The dependencies to be removed
117     * @param context The context to be used for the partial evaluation
118     * @return a new expression that does not have any of the specified
119     * dependencies
120     */

121
122     public Expression reduce(int dependencies, Context context) throws XPathException {
123         if (boundParentNode==null && ((dependencies & Context.CONTEXT_NODE) != 0)) {
124             AttributeReference a = new AttributeReference();
125             a.fingerprint = fingerprint;
126             a.bindParentNode(context.getContextNodeInfo());
127             a.setStaticContext(getStaticContext());
128             return a;
129         } else {
130             return this;
131         }
132     }
133
134     /**
135     * Diagnostic print of expression structure
136     */

137     
138     public void display(int level) {
139         System.err.println(indent(level) + "@" + fingerprint);
140     }
141
142 }
143
144
145
146 //
147
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
148
// you may not use this file except in compliance with the License. You may obtain a copy of the
149
// License at http://www.mozilla.org/MPL/
150
//
151
// Software distributed under the License is distributed on an "AS IS" basis,
152
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
153
// See the License for the specific language governing rights and limitations under the License.
154
//
155
// The Original Code is: all this file.
156
//
157
// The Initial Developer of the Original Code is
158
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
159
//
160
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
161
//
162
// Contributor(s): none.
163
//
164
Popular Tags