KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > tinytree > TinyAttributeImpl


1 package com.icl.saxon.tinytree;
2 import com.icl.saxon.om.*;
3 import com.icl.saxon.Context;
4 import com.icl.saxon.output.Outputter;
5
6 import org.w3c.dom.Node JavaDoc;
7 import org.w3c.dom.Attr 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 TinyAttributeImpl extends TinyNodeImpl implements Attr JavaDoc {
21
22     public TinyAttributeImpl(TinyDocumentImpl doc, int nodeNr) {
23         this.document = doc;
24         this.nodeNr = nodeNr;
25     }
26
27     /**
28     * Get the parent node
29     */

30
31     public NodeInfo getParent() {
32         return document.getNode(document.attParent[nodeNr]);
33     }
34
35     /**
36     * Get the node sequence number (in document order). Sequence numbers are monotonic but not
37     * consecutive. In the current implementation, parent nodes (elements and roots) have a zero
38     * least-significant word, while namespaces, attributes, text nodes, comments, and PIs have
39     * the top word the same as their owner and the bottom half reflecting their relative position.
40     */

41
42     protected long getSequenceNumber() {
43         // need the variable as workaround for a Java HotSpot problem, reported 11 Oct 2000
44
long z =
45             ((TinyNodeImpl)getParent()).getSequenceNumber()
46             + 0x8000 +
47             (nodeNr - document.offset[document.attParent[nodeNr]]);
48         return z;
49         // note the 0x8000 is to leave room for namespace nodes
50
}
51
52     /**
53     * Return the type of node.
54     * @return Node.ATTRIBUTE
55     */

56
57     public final short getNodeType() {
58         return ATTRIBUTE;
59     }
60
61     /**
62     * Return the character value of the node.
63     * @return the attribute value
64     */

65
66     public String JavaDoc getStringValue() {
67         return document.attValue[nodeNr];
68     }
69
70     /**
71     * Get the fingerprint of the node, used for matching names
72     */

73     
74     public int getFingerprint() {
75         return document.attCode[nodeNr] & 0xfffff;
76     }
77
78     /**
79     * Get the name code of the node, used for finding names in the name pool
80     */

81     
82     public int getNameCode() {
83         return document.attCode[nodeNr];
84     }
85
86     /**
87     * Get the prefix part of the name of this node. This is the name before the ":" if any.
88     * @return the prefix part of the name. For an unnamed node, return null.
89     */

90
91     public String JavaDoc getPrefix() {
92         int code = document.attCode[nodeNr];
93         if ((code>>20 & 0xff) == 0) return "";
94         return document.getNamePool().getPrefix(code);
95     }
96     
97     /**
98     * Get the display name of this node. For elements and attributes this is [prefix:]localname.
99     * For unnamed nodes, it is an empty string.
100     * @return The display name of this node.
101     * For a node with no name, return an empty string.
102     */

103
104     public String JavaDoc getDisplayName() {
105         return document.getNamePool().getDisplayName(document.attCode[nodeNr]);
106     }
107
108
109     /**
110     * Get the local name of this node.
111     * @return The local name of this node.
112     * For a node with no name, return an empty string.
113     */

114
115     public String JavaDoc getLocalName() {
116         return document.getNamePool().getLocalName(document.attCode[nodeNr]);
117     }
118
119     /**
120     * Get the URI part of the name of this node.
121     * @return The URI of the namespace of this node. For the default namespace, return an
122     * empty string
123     */

124
125     public final String JavaDoc getURI() {
126         return document.getNamePool().getURI(document.attCode[nodeNr]);
127     }
128
129     /**
130     * Generate id. Returns key of owning element with the attribute name as a suffix
131     */

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

140
141     public void copy(Outputter out) throws TransformerException JavaDoc {
142         int nameCode = document.attCode[nodeNr];
143         if ((nameCode>>20 & 0xff) != 0) { // non-null prefix
144
// check there is no conflict of namespaces
145
nameCode = out.checkAttributePrefix(nameCode);
146         }
147         out.writeAttribute(nameCode, getStringValue());
148     }
149    
150     /**
151     * Get the line number of the node within its source document entity
152     */

153
154     public int getLineNumber() {
155         return getParent().getLineNumber();
156     }
157
158 }
159
160 //
161
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
162
// you may not use this file except in compliance with the License. You may obtain a copy of the
163
// License at http://www.mozilla.org/MPL/
164
//
165
// Software distributed under the License is distributed on an "AS IS" basis,
166
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
167
// See the License for the specific language governing rights and limitations under the License.
168
//
169
// The Original Code is: all this file.
170
//
171
// The Initial Developer of the Original Code is
172
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
173
//
174
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
175
//
176
// Contributor(s): none.
177
//
178
Popular Tags