KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xml2 > QAttributedNode


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.xml2;
30
31 import org.w3c.dom.Attr JavaDoc;
32 import org.w3c.dom.DOMException JavaDoc;
33 import org.w3c.dom.NamedNodeMap JavaDoc;
34 import org.w3c.dom.Node JavaDoc;
35
36 import javax.xml.namespace.QName JavaDoc;
37
38 public abstract class QAttributedNode extends QNode {
39   QAttr _firstAttribute;
40
41   /**
42    * Returns a map of the attributes.
43    */

44   public NamedNodeMap JavaDoc getAttributes()
45   {
46     return new QAttributeMap(this);
47   }
48
49   /**
50    * Returns true if the element has attributes.
51    */

52   public boolean hasAttributes()
53   {
54     return _firstAttribute != null;
55   }
56
57   /**
58    * Returns the first attribute in the attribute list.
59    */

60   public Attr JavaDoc getFirstAttribute()
61   {
62     return _firstAttribute;
63   }
64
65   /**
66    * Returns the named attribute.
67    */

68   public String JavaDoc getAttribute(String JavaDoc name)
69   {
70     for (QAbstractNode attr = _firstAttribute;
71          attr != null;
72          attr = attr._next) {
73       if (name.equals(attr.getNodeName()))
74         return attr.getNodeValue();
75     }
76
77     return "";
78   }
79
80   /**
81    * Returns the attribute specified by a namespace.
82    */

83   public String JavaDoc getAttributeNS(String JavaDoc namespaceURI, String JavaDoc local)
84   {
85     for (QAbstractNode attr = _firstAttribute;
86          attr != null;
87          attr = attr._next) {
88       String JavaDoc attrURI = attr.getNamespaceURI();
89       
90       if (attr.getLocalName().equals(local) &&
91           (attrURI == namespaceURI ||
92        attrURI != null && attrURI.equals(namespaceURI)))
93         return attr.getNodeValue();
94     }
95
96     return "";
97   }
98
99   public boolean hasAttribute(String JavaDoc name)
100   {
101     for (QAbstractNode attr = _firstAttribute;
102          attr != null;
103          attr = attr._next) {
104       if (attr.getNodeName().equals(name))
105         return true;
106     }
107
108     return false;
109   }
110
111   public boolean hasAttributeNS(String JavaDoc uri, String JavaDoc local)
112   {
113     for (QAbstractNode attr = _firstAttribute;
114          attr != null;
115          attr = attr._next) {
116       String JavaDoc attrURI = attr.getNamespaceURI();
117       
118       if (attr.getLocalName().equals(local) &&
119       (attrURI == uri || attrURI != null && attrURI.equals(uri)))
120         return true;
121     }
122
123     return false;
124   }
125
126   /**
127    * Returns the attribute specified by the name.
128    */

129   public Attr JavaDoc getAttributeNode(String JavaDoc name)
130   {
131     for (QAbstractNode attr = _firstAttribute;
132          attr != null;
133          attr = attr._next) {
134       if (attr.getNodeName().equals(name))
135         return (Attr JavaDoc) attr;
136     }
137
138     return null;
139   }
140
141   public Attr JavaDoc getAttributeNodeNS(String JavaDoc uri, String JavaDoc local)
142   {
143     for (QAbstractNode attr = _firstAttribute;
144          attr != null;
145          attr = attr._next) {
146       String JavaDoc attrURI = attr.getNamespaceURI();
147       
148       if (attr.getLocalName().equals(local) &&
149           (attrURI == uri ||
150        attrURI != null && attrURI.equals(uri)))
151         return (Attr JavaDoc) attr;
152     }
153
154     return null;
155   }
156
157   public void setAttribute(String JavaDoc name, String JavaDoc value)
158     throws DOMException JavaDoc
159   {
160     if (! isNameValid(name))
161       throw new QDOMException(DOMException.INVALID_CHARACTER_ERR,
162                   "illegal attribute `" + name + "'");
163
164     setAttributeNode(_owner.createAttribute(name, value));
165   }
166
167   public void setAttributeNS(String JavaDoc uri, String JavaDoc local, String JavaDoc value)
168   {
169     Attr JavaDoc attr = _owner.createAttributeNS(uri, local);
170     attr.setNodeValue(value);
171     
172     setAttributeNodeNS(attr);
173   }
174
175   void setAttribute(QName JavaDoc name, String JavaDoc value)
176     throws DOMException JavaDoc
177   {
178     setAttributeNode(_owner.createAttribute(name, value));
179   }
180
181   /**
182    * Sets an attribute, specified by the object.
183    */

184   public void setIdAttribute(String JavaDoc name, boolean isId)
185     throws DOMException JavaDoc
186   {
187   }
188
189   /**
190    * Sets an attribute, specified by the object.
191    */

192   public void setIdAttributeNS(String JavaDoc namespaceURI, String JavaDoc localName,
193                    boolean isId)
194     throws DOMException JavaDoc
195   {
196   }
197
198   /**
199    * Sets an attribute, specified by the object.
200    */

201   public void setIdAttributeNode(Attr JavaDoc attr, boolean isId)
202     throws DOMException JavaDoc
203   {
204   }
205
206   /**
207    * Sets an attribute, specified by the object.
208    */

209   public Attr JavaDoc setAttributeNode(Attr JavaDoc attr)
210     throws DOMException JavaDoc
211   {
212     QAttr qAttr = (QAttr) attr;
213
214     if (qAttr._owner == null)
215       qAttr._owner = _owner;
216     else if (qAttr._owner != _owner)
217       throw new QDOMException(DOMException.WRONG_DOCUMENT_ERR,
218                   "attribute from wrong document");
219
220     if (qAttr._parent != null)
221       throw new QDOMException(DOMException.INUSE_ATTRIBUTE_ERR,
222                   "attribute `" + attr.getNodeName() +
223                   "' is in use");
224
225     qAttr._parent = this;
226
227     // remove any matching old attribute
228
QAttr old = unlink(attr.getNodeName());
229
230     QAttr ptr = _firstAttribute;
231
232     if (ptr == null) {
233       _firstAttribute = qAttr;
234     }
235     else {
236       for (; ptr._next != null; ptr = (QAttr) ptr._next) {
237       }
238
239       ptr._next = qAttr;
240     }
241     
242     return old;
243   }
244   
245   public Attr JavaDoc setAttributeNodeNS(Attr JavaDoc attr)
246     throws DOMException JavaDoc
247   {
248     QAttr qAttr = (QAttr) attr;
249
250     if (qAttr._owner != _owner)
251       throw new QDOMException(DOMException.WRONG_DOCUMENT_ERR,
252                   "attribute from wrong document");
253
254     if (qAttr._parent != null)
255       throw new QDOMException(DOMException.INUSE_ATTRIBUTE_ERR,
256                   "attribute `" + attr.getNodeName() +
257                   "' is in use");
258
259     // remove any matching old attribute
260
QAttr old = unlink(qAttr.getNamespaceURI(), qAttr.getLocalName());
261     
262     qAttr._parent = this;
263
264     qAttr._next = _firstAttribute;
265     _firstAttribute = qAttr;
266
267     return old;
268   }
269
270   /**
271    * Removes the named attribute.
272    */

273   public void removeAttribute(String JavaDoc name)
274   {
275     if (! isNameValid(name))
276       throw new QDOMException(DOMException.INVALID_CHARACTER_ERR,
277                   "illegal attribute `" + name + "'");
278     
279     unlink(name);
280   }
281
282   /**
283    * Removes the attribute specified by the localname and namespace.
284    */

285   public void removeAttributeNS(String JavaDoc uri, String JavaDoc name)
286   {
287     unlink(uri, name);
288   }
289
290   /**
291    * Removes the matching attribute.
292    */

293   public Attr JavaDoc removeAttributeNode(Attr JavaDoc attr)
294   {
295     return unlink(attr.getNodeName());
296   }
297
298   /**
299    * Removes the matching attribute.
300    */

301   public Attr JavaDoc removeAttributeNodeNS(Attr JavaDoc attr)
302   {
303     return unlink(attr.getNamespaceURI(), attr.getLocalName());
304   }
305
306   /**
307    * Unlinks an attribute, returning it.
308    */

309   QAttr unlink(String JavaDoc name)
310   {
311     QAttr prev = null;
312     QAttr ptr;
313
314     for (ptr = _firstAttribute;
315          ptr != null && ! ptr.getNodeName().equals(name);
316          ptr = (QAttr) ptr._next) {
317       prev = ptr;
318     }
319
320     if (ptr == null)
321       return null;
322
323     if (prev == null)
324       _firstAttribute = (QAttr) ptr._next;
325     else
326       prev._next = ptr._next;
327
328     ptr._next = null;
329
330     return ptr;
331   }
332
333   /**
334    * Removes the attribute named by the URI and local name.
335    */

336   public QAttr unlink(String JavaDoc uri, String JavaDoc local)
337   {
338     if (local == null || uri == null)
339       return null;
340     
341     QAttr prev = null;
342     QAttr ptr;
343
344     for (ptr = (QAttr) _firstAttribute;
345          ptr != null && (! local.equals(ptr.getLocalName()) ||
346                          ! uri.equals(ptr.getNamespaceURI()));
347          ptr = (QAttr) ptr._next) {
348       prev = ptr;
349     }
350
351     if (ptr == null)
352       return null;
353
354     if (prev == null)
355       _firstAttribute = (QAttr) ptr._next;
356     else
357       prev._next = ptr._next;
358
359     ptr._next = null;
360
361     return ptr;
362   }
363
364   static class QAttributeMap implements NamedNodeMap JavaDoc {
365     QAttributedNode _elt;
366     int _i;
367     QAttr _attr;
368
369     QAttributeMap(QAttributedNode elt)
370     {
371       _elt = elt;
372     }
373   
374     public Node JavaDoc getNamedItem(String JavaDoc name)
375     {
376       return _elt.getAttributeNode(name);
377     }
378   
379     public Node JavaDoc getNamedItemNS(String JavaDoc uri, String JavaDoc localName)
380     {
381       return _elt.getAttributeNodeNS(uri, localName);
382     }
383
384     public Node JavaDoc setNamedItem(Node JavaDoc arg) throws DOMException JavaDoc
385     {
386       return _elt.setAttributeNode((Attr JavaDoc) arg);
387     }
388   
389     public Node JavaDoc setNamedItemNS(Node JavaDoc arg)
390     {
391       return _elt.setAttributeNodeNS((Attr JavaDoc) arg);
392     }
393
394     public Node JavaDoc removeNamedItem(String JavaDoc name) throws DOMException JavaDoc
395     {
396       return _elt.unlink(name);
397     }
398   
399     public Node JavaDoc removeNamedItemNS(String JavaDoc uri, String JavaDoc localName)
400     {
401       return _elt.getAttributeNodeNS(uri, localName);
402     }
403
404     public Node JavaDoc item(int index)
405     {
406       QAbstractNode attr = _elt._firstAttribute;
407
408       while (index > 0 && attr != null) {
409         attr = attr._next;
410         index--;
411       }
412
413       return attr;
414     }
415
416     public int getLength()
417     {
418       int length = 0;
419
420       for (QAbstractNode attr = _elt._firstAttribute;
421            attr != null;
422            attr = attr._next)
423         length++;
424
425       return length;
426     }
427   }
428 }
429
Popular Tags