KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > tree > AttributeImpl


1 package com.icl.saxon.tree;
2 import com.icl.saxon.om.NodeInfo;
3
4 import com.icl.saxon.output.Outputter;
5
6 import org.w3c.dom.Attr JavaDoc;
7 import org.w3c.dom.Node JavaDoc;
8 import org.w3c.dom.Element JavaDoc;
9 import org.w3c.dom.DOMException JavaDoc;
10
11 import javax.xml.transform.TransformerException JavaDoc;
12
13
14 /**
15   * A node in the XML parse tree representing an attribute. Note that this is
16   * generated only "on demand", when the attribute is selected by a select pattern.<P>
17   * @author <A HREF="mailto:mhkay@iclway.co.uk>Michael H. Kay</A>
18   */

19
20 final class AttributeImpl extends NodeImpl implements Attr JavaDoc {
21
22     private int nameCode;
23     private String JavaDoc value;
24
25     /**
26     * Construct an Attribute node for the n'th attribute of a given element
27     * @param element The element containing the relevant attribute
28     * @param index The index position of the attribute starting at zero
29     * @param sequenceBase The sequence number to be allocated to the first attribute node. Note
30     * that attributes appear in document order AFTER namespace nodes, so we can't deduce
31     * this from the index alone.
32     */

33
34     public AttributeImpl(ElementImpl element, int index) {
35         parent = element;
36         this.index = index;
37         AttributeCollection atts = (AttributeCollection)element.getAttributeList();
38         this.nameCode = atts.getNameCode(index);
39         this.value = atts.getValue(index);
40     }
41
42     /**
43     * Get the name code, which enables the name to be located in the name pool
44     */

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

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

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

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

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

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

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

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

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

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

138
139     public void copy(Outputter out) throws TransformerException JavaDoc {
140         int nameCode = getNameCode();
141         if ((nameCode>>20 & 0xff) != 0) { // non-null prefix
142
// check there is no conflict of namespaces
143
nameCode = out.checkAttributePrefix(nameCode);
144         }
145         out.writeAttribute(nameCode, getStringValue());
146     }
147     
148 }
149
150 //
151
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
152
// you may not use this file except in compliance with the License. You may obtain a copy of the
153
// License at http://www.mozilla.org/MPL/
154
//
155
// Software distributed under the License is distributed on an "AS IS" basis,
156
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
157
// See the License for the specific language governing rights and limitations under the License.
158
//
159
// The Original Code is: all this file.
160
//
161
// The Initial Developer of the Original Code is
162
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
163
//
164
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
165
//
166
// Contributor(s): none.
167
//
168
Popular Tags