• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php
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 com.android.ide.eclipse.adt.internal.editors.mock;
18 
19 import org.w3c.dom.DOMException;
20 import org.w3c.dom.NamedNodeMap;
21 import org.w3c.dom.Node;
22 
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 
26 class MockNamedNodeMap implements NamedNodeMap {
27 
28     /** map for access by namespace/name */
29     private final HashMap<String, HashMap<String, Node>> mNodeMap =
30         new HashMap<String, HashMap<String, Node>>();
31 
32     /** list for access by index */
33     private final ArrayList<Node> mNodeList = new ArrayList<Node>();
34 
addAttribute(String namespace, String localName, String value)35     public MockXmlNode addAttribute(String namespace, String localName, String value) {
36         MockXmlNode node = new MockXmlNode(namespace, localName, value);
37 
38         if (namespace == null) {
39             namespace = ""; // no namespace
40         }
41 
42         // get the map for the namespace
43         HashMap<String, Node> map = mNodeMap.get(namespace);
44         if (map == null) {
45             map = new HashMap<String, Node>();
46             mNodeMap.put(namespace, map);
47         }
48 
49 
50         map.put(localName, node);
51         mNodeList.add(node);
52 
53         return node;
54     }
55 
56     // --------- NamedNodeMap -------
57 
58     @Override
getLength()59     public int getLength() {
60         return mNodeList.size();
61     }
62 
63     @Override
getNamedItem(String name)64     public Node getNamedItem(String name) {
65         HashMap<String, Node> map = mNodeMap.get(""); // no namespace
66         if (map != null) {
67             return map.get(name);
68         }
69 
70         return null;
71     }
72 
73     @Override
getNamedItemNS(String namespaceURI, String localName)74     public Node getNamedItemNS(String namespaceURI, String localName) throws DOMException {
75         if (namespaceURI == null) {
76             namespaceURI = ""; //no namespace
77         }
78 
79         HashMap<String, Node> map = mNodeMap.get(namespaceURI);
80         if (map != null) {
81             return map.get(localName);
82         }
83 
84         return null;
85     }
86 
87     @Override
item(int index)88     public Node item(int index) {
89         return mNodeList.get(index);
90     }
91 
92     @Override
removeNamedItem(String name)93     public Node removeNamedItem(String name) throws DOMException {
94         throw new UnsupportedOperationException("Operation not implemented.");  //$NON-NLS-1$
95     }
96 
97     @Override
removeNamedItemNS(String namespaceURI, String localName)98     public Node removeNamedItemNS(String namespaceURI, String localName) throws DOMException {
99         throw new UnsupportedOperationException("Operation not implemented.");  //$NON-NLS-1$
100     }
101 
102     @Override
setNamedItem(Node arg)103     public Node setNamedItem(Node arg) throws DOMException {
104         throw new UnsupportedOperationException("Operation not implemented.");  //$NON-NLS-1$
105     }
106 
107     @Override
setNamedItemNS(Node arg)108     public Node setNamedItemNS(Node arg) throws DOMException {
109         throw new UnsupportedOperationException("Operation not implemented.");  //$NON-NLS-1$
110     }
111 
112 }
113