KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xml > 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.xml;
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 public abstract class QAttributedNode extends QNode {
37   QAttr _firstAttribute;
38
39   /**
40    * Returns a map of the attributes.
41    */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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