KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > tinytree > TinyAttributeImpl


1 package net.sf.saxon.tinytree;
2 import net.sf.saxon.event.Receiver;
3 import net.sf.saxon.om.NodeInfo;
4 import net.sf.saxon.trans.XPathException;
5 import net.sf.saxon.type.Type;
6
7
8 /**
9   * A node in the XML parse tree representing an attribute. Note that this is
10   * generated only "on demand", when the attribute is selected by a select pattern.<P>
11   * @author Michael H. Kay
12   */

13
14 final class TinyAttributeImpl extends TinyNodeImpl {
15
16     public TinyAttributeImpl(TinyTree tree, int nodeNr) {
17         this.tree = tree;
18         this.nodeNr = nodeNr;
19     }
20
21     /**
22     * Get the parent node
23     */

24
25     public NodeInfo getParent() {
26         return tree.getNode(tree.attParent[nodeNr]);
27     }
28
29     /**
30      * Get the root node of the tree (not necessarily a document node)
31      *
32      * @return the NodeInfo representing the root of this tree
33      */

34
35     public NodeInfo getRoot() {
36         NodeInfo parent = getParent();
37         if (parent == null) {
38             return this; // doesn't happen - parentless attributes are represented by the Orphan class
39
} else {
40             return parent.getRoot();
41         }
42     }
43
44     /**
45     * Get the node sequence number (in document order). Sequence numbers are monotonic but not
46     * consecutive. In this implementation, elements have a zero
47     * least-significant word, while attributes and namespaces use the same value in the top word as
48     * the containing element, and use the bottom word to hold
49     * a sequence number, which numbers namespaces first and then attributes.
50     */

51
52     protected long getSequenceNumber() {
53         // need the variable as workaround for a Java HotSpot problem, reported 11 Oct 2000
54
long z =
55             ((TinyNodeImpl)getParent()).getSequenceNumber()
56             + 0x8000 +
57             (nodeNr - tree.alpha[tree.attParent[nodeNr]]);
58         return z;
59         // note the 0x8000 is to leave room for namespace nodes
60
}
61
62     /**
63     * Return the type of node.
64     * @return Node.ATTRIBUTE
65     */

66
67     public final int getNodeKind() {
68         return Type.ATTRIBUTE;
69     }
70
71     /**
72     * Return the string value of the node.
73     * @return the attribute value
74     */

75
76     public CharSequence JavaDoc getStringValueCS() {
77         return tree.attValue[nodeNr];
78     }
79
80     /**
81     * Return the string value of the node.
82     * @return the attribute value
83     */

84
85     public String JavaDoc getStringValue() {
86         return tree.attValue[nodeNr].toString();
87     }
88
89     /**
90     * Get the fingerprint of the node, used for matching names
91     */

92
93     public int getFingerprint() {
94         return tree.attCode[nodeNr] & 0xfffff;
95     }
96
97     /**
98     * Get the name code of the node, used for finding names in the name pool
99     */

100
101     public int getNameCode() {
102         return tree.attCode[nodeNr];
103     }
104
105     /**
106     * Get the prefix part of the name of this node. This is the name before the ":" if any.
107     * @return the prefix part of the name. For an unnamed node, return null.
108     */

109
110     public String JavaDoc getPrefix() {
111         int code = tree.attCode[nodeNr];
112         if ((code>>20 & 0xff) == 0) return "";
113         return tree.getNamePool().getPrefix(code);
114     }
115
116     /**
117     * Get the display name of this node. For elements and attributes this is [prefix:]localname.
118     * For unnamed nodes, it is an empty string.
119     * @return The display name of this node.
120     * For a node with no name, return an empty string.
121     */

122
123     public String JavaDoc getDisplayName() {
124         return tree.getNamePool().getDisplayName(tree.attCode[nodeNr]);
125     }
126
127
128     /**
129     * Get the local name of this node.
130     * @return The local name of this node.
131     * For a node with no name, return an empty string.
132     */

133
134     public String JavaDoc getLocalPart() {
135         return tree.getNamePool().getLocalName(tree.attCode[nodeNr]);
136     }
137
138     /**
139     * Get the URI part of the name of this node.
140     * @return The URI of the namespace of this node. For the default namespace, return an
141     * empty string
142     */

143
144     public final String JavaDoc getURI() {
145         return tree.getNamePool().getURI(tree.attCode[nodeNr]);
146     }
147
148     /**
149     * Get the type annotation of this node, if any
150     * The bit {@link NodeInfo#IS_DTD_TYPE} (1<<30) will be set in the case of an attribute node if the type annotation
151     * is one of ID, IDREF, or IDREFS and this is derived from DTD rather than schema validation.
152     * Returns UNTYPED_ATOMIC if there is no type annotation
153     */

154
155     public int getTypeAnnotation() {
156         return tree.getAttributeAnnotation(nodeNr);
157     }
158
159     /**
160     * Generate id. Returns key of owning element with the attribute namecode as a suffix
161     */

162
163     public String JavaDoc generateId() {
164         return (getParent()).generateId() + 'a' + tree.attCode[nodeNr];
165         // we previously used the attribute name. But this breaks the requirement
166
// that the result of generate-id consists entirely of alphanumeric ASCII
167
// characters
168
}
169
170     /**
171     * Copy this node to a given outputter
172     */

173
174     public void copy(Receiver out, int whichNamespaces, boolean copyAnnotations, int locationId) throws XPathException {
175         int nameCode = tree.attCode[nodeNr];
176         int typeCode = (copyAnnotations ? getTypeAnnotation() : -1);
177         out.attribute(nameCode, typeCode, getStringValue(), locationId, 0);
178     }
179
180     /**
181     * Get the line number of the node within its source document entity
182     */

183
184     public int getLineNumber() {
185         return getParent().getLineNumber();
186     }
187
188 }
189
190 //
191
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
192
// you may not use this file except in compliance with the License. You may obtain a copy of the
193
// License at http://www.mozilla.org/MPL/
194
//
195
// Software distributed under the License is distributed on an "AS IS" basis,
196
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
197
// See the License for the specific language governing rights and limitations under the License.
198
//
199
// The Original Code is: all this file.
200
//
201
// The Initial Developer of the Original Code is Michael H. Kay.
202
//
203
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
204
//
205
// Contributor(s): none.
206
//
207
Popular Tags