KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > tree > AttributeImpl


1 package net.sf.saxon.tree;
2 import net.sf.saxon.event.Receiver;
3 import net.sf.saxon.om.AttributeCollection;
4 import net.sf.saxon.om.NodeInfo;
5 import net.sf.saxon.trans.XPathException;
6 import net.sf.saxon.type.Type;
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 AttributeImpl extends NodeImpl {
15
16     private int nameCode;
17     private int typeCode;
18     private String JavaDoc value;
19
20     /**
21     * Construct an Attribute node for the n'th attribute of a given element
22     * @param element The element containing the relevant attribute
23     * @param index The index position of the attribute starting at zero
24     */

25
26     public AttributeImpl(ElementImpl element, int index) {
27         parent = element;
28         this.index = index;
29         AttributeCollection atts = element.getAttributeList();
30         this.nameCode = atts.getNameCode(index);
31         this.value = atts.getValue(index);
32         this.typeCode = atts.getTypeAnnotation(index);
33     }
34
35     /**
36     * Get the name code, which enables the name to be located in the name pool
37     */

38
39     public int getNameCode() {
40         return nameCode;
41     }
42
43     /**
44      * Get the type annotation of this node, if any
45      */

46
47     public int getTypeAnnotation() {
48         return typeCode;
49     }
50
51     /**
52     * Determine whether this is the same node as another node
53     * @return true if this Node object and the supplied Node object represent the
54     * same node in the tree.
55     */

56
57     public boolean isSameNodeInfo(NodeInfo other) {
58         if (!(other instanceof AttributeImpl)) return false;
59         if (this==other) return true;
60         AttributeImpl otherAtt = (AttributeImpl)other;
61         return (parent.isSameNodeInfo(otherAtt.parent) &&
62                  ((nameCode&0xfffff)==(otherAtt.nameCode&0xfffff)));
63     }
64
65     /**
66     * Get the node sequence number (in document order). Sequence numbers are monotonic but not
67     * consecutive. In the current implementation, parent nodes (elements and roots) have a zero
68     * least-significant word, while namespaces, attributes, text nodes, comments, and PIs have
69     * the top word the same as their owner and the bottom half reflecting their relative position.
70     */

71
72     protected long getSequenceNumber() {
73         return parent.getSequenceNumber() + 0x8000 + index;
74         // note the 0x8000 is to leave room for namespace nodes
75
}
76
77     /**
78     * Return the type of node.
79     * @return Node.ATTRIBUTE
80     */

81
82     public final int getNodeKind() {
83         return Type.ATTRIBUTE;
84     }
85
86     /**
87     * Return the character value of the node.
88     * @return the attribute value
89     */

90
91     public String JavaDoc getStringValue() {
92         return value;
93     }
94
95     /**
96     * Get next sibling - not defined for attributes
97     */

98
99     public NodeInfo getNextSibling() {
100         return null;
101     }
102
103     /**
104     * Get previous sibling - not defined for attributes
105     */

106
107     public NodeInfo getPreviousSibling() {
108         return null;
109     }
110
111     /**
112     * Get the previous node in document order (skipping attributes)
113     */

114
115     public NodeImpl getPreviousInDocument() {
116         return (NodeImpl)getParent();
117     }
118
119     /**
120     * Get the next node in document order (skipping attributes)
121     */

122
123     public NodeImpl getNextInDocument(NodeImpl anchor) {
124         if (anchor==this) return null;
125         return ((NodeImpl)getParent()).getNextInDocument(anchor);
126     }
127
128     /**
129     * Get sequential key. Returns key of owning element with the attribute index as a suffix
130     */

131
132     public String JavaDoc generateId() {
133         return parent.generateId() + 'a' + index;
134     }
135
136     /**
137     * Copy this node to a given outputter
138     */

139
140     public void copy(Receiver out, int whichNamespaces, boolean copyAnnotations, int locationId) throws XPathException {
141         int nameCode = getNameCode();
142         int typeCode = (copyAnnotations ? getTypeAnnotation() : -1);
143         out.attribute(nameCode, typeCode, getStringValue(), locationId, 0);
144     }
145
146 }
147
148 //
149
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
150
// you may not use this file except in compliance with the License. You may obtain a copy of the
151
// License at http://www.mozilla.org/MPL/
152
//
153
// Software distributed under the License is distributed on an "AS IS" basis,
154
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
155
// See the License for the specific language governing rights and limitations under the License.
156
//
157
// The Original Code is: all this file.
158
//
159
// The Initial Developer of the Original Code is Michael H. Kay.
160
//
161
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
162
//
163
// Contributor(s): none.
164
//
165
Popular Tags