KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > xs > opti > ElementImpl


1 /*
2  * Copyright 2001, 2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.impl.xs.opti;
18
19 import org.w3c.dom.Attr JavaDoc;
20 import org.w3c.dom.Document JavaDoc;
21 import org.w3c.dom.NamedNodeMap JavaDoc;
22 import org.w3c.dom.Node JavaDoc;
23
24 /**
25  * @xerces.internal
26  *
27  * @author Rahul Srivastava, Sun Microsystems Inc.
28  * @author Sandy Gao, IBM
29  *
30  * @version $Id: ElementImpl.java,v 1.9 2004/12/16 16:45:20 ankitp Exp $
31  */

32 public class ElementImpl extends DefaultElement {
33     
34     SchemaDOM schemaDOM;
35     Attr JavaDoc[] attrs;
36     int row;
37     int col;
38     int parentRow;
39     
40     int line;
41     int column;
42     int charOffset;
43     String JavaDoc fSyntheticAnnotation;
44     
45     public ElementImpl(int line, int column, int offset) {
46         row = -1;
47         col = -1;
48         parentRow = -1;
49         nodeType = Node.ELEMENT_NODE;
50         
51         this.line = line;
52         this.column = column;
53         charOffset = offset;
54     }
55     
56     public ElementImpl(int line, int column) {
57         this(line, column, -1);
58     }
59     
60     
61     public ElementImpl(String JavaDoc prefix, String JavaDoc localpart, String JavaDoc rawname,
62             String JavaDoc uri, int line, int column, int offset) {
63         super(prefix, localpart, rawname, uri, Node.ELEMENT_NODE);
64         row = -1;
65         col = -1;
66         parentRow = -1;
67         
68         this.line = line;
69         this.column = column;
70         charOffset = offset;
71     }
72     
73     public ElementImpl(String JavaDoc prefix, String JavaDoc localpart, String JavaDoc rawname,
74             String JavaDoc uri, int line, int column) {
75         this(prefix, localpart, rawname, uri, line, column, -1);
76     }
77     
78     
79     //
80
// org.w3c.dom.Node methods
81
//
82

83     public Document JavaDoc getOwnerDocument() {
84         return schemaDOM;
85     }
86     
87     
88     public Node JavaDoc getParentNode() {
89         return schemaDOM.relations[row][0];
90     }
91     
92     
93     public boolean hasChildNodes() {
94         if (parentRow == -1) {
95             return false;
96         }
97         else {
98             return true;
99         }
100     }
101     
102     
103     public Node JavaDoc getFirstChild() {
104         if (parentRow == -1) {
105             return null;
106         }
107         return schemaDOM.relations[parentRow][1];
108     }
109     
110     
111     public Node JavaDoc getLastChild() {
112         if (parentRow == -1) {
113             return null;
114         }
115         int i=1;
116         for (; i<schemaDOM.relations[parentRow].length; i++) {
117             if (schemaDOM.relations[parentRow][i] == null) {
118                 return schemaDOM.relations[parentRow][i-1];
119             }
120         }
121         if (i ==1) {
122             i++;
123         }
124         return schemaDOM.relations[parentRow][i-1];
125     }
126     
127     
128     public Node JavaDoc getPreviousSibling() {
129         if (col == 1) {
130             return null;
131         }
132         return schemaDOM.relations[row][col-1];
133     }
134     
135     
136     public Node JavaDoc getNextSibling() {
137         if (col == schemaDOM.relations[row].length-1) {
138             return null;
139         }
140         return schemaDOM.relations[row][col+1];
141     }
142     
143     
144     public NamedNodeMap JavaDoc getAttributes() {
145         return new NamedNodeMapImpl(attrs);
146     }
147     
148     
149     public boolean hasAttributes() {
150         return (attrs.length == 0 ? false : true);
151     }
152     
153     
154     
155     //
156
// org.w3c.dom.Element methods
157
//
158

159     public String JavaDoc getTagName() {
160         return rawname;
161     }
162     
163     
164     public String JavaDoc getAttribute(String JavaDoc name) {
165         
166         for (int i=0; i<attrs.length; i++) {
167             if (attrs[i].getName().equals(name)) {
168                 return attrs[i].getValue();
169             }
170         }
171         return "";
172     }
173     
174     
175     public Attr JavaDoc getAttributeNode(String JavaDoc name) {
176         for (int i=0; i<attrs.length; i++) {
177             if (attrs[i].getName().equals(name)) {
178                 return attrs[i];
179             }
180         }
181         return null;
182     }
183     
184     
185     public String JavaDoc getAttributeNS(String JavaDoc namespaceURI, String JavaDoc localName) {
186         for (int i=0; i<attrs.length; i++) {
187             if (attrs[i].getLocalName().equals(localName) && attrs[i].getNamespaceURI().equals(namespaceURI)) {
188                 return attrs[i].getValue();
189             }
190         }
191         return "";
192     }
193     
194     
195     public Attr JavaDoc getAttributeNodeNS(String JavaDoc namespaceURI, String JavaDoc localName) {
196         for (int i=0; i<attrs.length; i++) {
197             if (attrs[i].getName().equals(localName) && attrs[i].getNamespaceURI().equals(namespaceURI)) {
198                 return attrs[i];
199             }
200         }
201         return null;
202     }
203     
204     
205     public boolean hasAttribute(String JavaDoc name) {
206         for (int i=0; i<attrs.length; i++) {
207             if (attrs[i].getName().equals(name)) {
208                 return true;
209             }
210         }
211         return false;
212     }
213     
214     
215     public boolean hasAttributeNS(String JavaDoc namespaceURI, String JavaDoc localName) {
216         for (int i=0; i<attrs.length; i++) {
217             if (attrs[i].getName().equals(localName) && attrs[i].getNamespaceURI().equals(namespaceURI)) {
218                 return true;
219             }
220         }
221         return false;
222     }
223     
224     
225     public void setAttribute(String JavaDoc name, String JavaDoc value) {
226         for (int i=0; i<attrs.length; i++) {
227             if (attrs[i].getName().equals(name)) {
228                 attrs[i].setValue(value);
229                 return;
230             }
231         }
232     }
233     
234     /** Returns the line number. */
235     public int getLineNumber() {
236         return line;
237     }
238     
239     /** Returns the column number. */
240     public int getColumnNumber() {
241         return column;
242     }
243     
244     /** Returns the character offset. */
245     public int getCharacterOffset() {
246         return charOffset;
247     }
248     
249     public String JavaDoc getSyntheticAnnotation() {
250         return fSyntheticAnnotation;
251     }
252 }
253
Popular Tags