KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > html > domimpl > NamedNodeMapImpl


1 /*
2     GNU LESSER GENERAL PUBLIC LICENSE
3     Copyright (C) 2006 The Lobo Project
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19     Contact info: xamjadmin@users.sourceforge.net
20 */

21 /*
22  * Created on Sep 3, 2005
23  */

24 package org.lobobrowser.html.domimpl;
25
26 import org.lobobrowser.js.*;
27 import org.w3c.dom.*;
28 import java.util.*;
29
30 public class NamedNodeMapImpl extends AbstractScriptableDelegate implements NamedNodeMap {
31     //Note: class must be public for reflection to work.
32
private final Map attributes = new HashMap();
33     private final ArrayList attributeList = new ArrayList();
34     
35     public NamedNodeMapImpl(Element owner, Map attribs) {
36         Iterator i = attribs.entrySet().iterator();
37         while(i.hasNext()) {
38             Map.Entry entry = (Map.Entry) i.next();
39             String JavaDoc name = (String JavaDoc) entry.getKey();
40             String JavaDoc value = (String JavaDoc) entry.getValue();
41             //TODO: "specified" attributes
42
Attr attr = new AttrImpl(name, value, true, owner, "ID".equals(name));
43             this.attributes.put(name, attr);
44             this.attributeList.add(attr);
45         }
46     }
47
48     public int getLength() {
49         return this.attributeList.size();
50     }
51
52     public Node getNamedItem(String JavaDoc name) {
53         return (Node) this.attributes.get(name);
54     }
55
56     /**
57      * @param name
58      */

59     public Node namedItem(String JavaDoc name) {
60         // Method needed for Javascript indexing.
61
return this.getNamedItem(name);
62     }
63     
64     public Node getNamedItemNS(String JavaDoc namespaceURI,
65             String JavaDoc localName) throws DOMException {
66         throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "No namespace support");
67     }
68
69     public Node item(int index) {
70         try {
71             return (Node) this.attributeList.get(index);
72         } catch(IndexOutOfBoundsException JavaDoc iob) {
73             return null;
74         }
75     }
76
77     public Node removeNamedItem(String JavaDoc name) throws DOMException {
78         return (Node) this.attributes.remove(name);
79     }
80
81     public Node removeNamedItemNS(String JavaDoc namespaceURI,
82             String JavaDoc localName) throws DOMException {
83         throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "No namespace support");
84     }
85
86     public Node setNamedItem(Node arg) throws DOMException {
87         Object JavaDoc prevValue = this.attributes.put(arg.getNodeName(), arg);
88         if(prevValue != null) {
89             this.attributeList.remove(prevValue);
90         }
91         this.attributeList.add(arg);
92         return arg;
93     }
94
95     public Node setNamedItemNS(Node arg) throws DOMException {
96         throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "No namespace support");
97     }
98 }
99
Popular Tags