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 sun.reflect.generics.reflectiveObjects.NotImplementedException; 24 25 import java.util.ArrayList; 26 import java.util.HashMap; 27 28 class MockNamedNodeMap implements NamedNodeMap { 29 30 /** map for access by namespace/name */ 31 private final HashMap<String, HashMap<String, Node>> mNodeMap = 32 new HashMap<String, HashMap<String, Node>>(); 33 34 /** list for access by index */ 35 private final ArrayList<Node> mNodeList = new ArrayList<Node>(); 36 addAttribute(String namespace, String localName, String value)37 public MockXmlNode addAttribute(String namespace, String localName, String value) { 38 MockXmlNode node = new MockXmlNode(namespace, localName, value); 39 40 if (namespace == null) { 41 namespace = ""; // no namespace 42 } 43 44 // get the map for the namespace 45 HashMap<String, Node> map = mNodeMap.get(namespace); 46 if (map == null) { 47 map = new HashMap<String, Node>(); 48 mNodeMap.put(namespace, map); 49 } 50 51 52 map.put(localName, node); 53 mNodeList.add(node); 54 55 return node; 56 } 57 58 // --------- NamedNodeMap ------- 59 getLength()60 public int getLength() { 61 return mNodeList.size(); 62 } 63 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 getNamedItemNS(String namespaceURI, String localName)73 public Node getNamedItemNS(String namespaceURI, String localName) throws DOMException { 74 if (namespaceURI == null) { 75 namespaceURI = ""; //no namespace 76 } 77 78 HashMap<String, Node> map = mNodeMap.get(namespaceURI); 79 if (map != null) { 80 return map.get(localName); 81 } 82 83 return null; 84 } 85 item(int index)86 public Node item(int index) { 87 return mNodeList.get(index); 88 } 89 removeNamedItem(String name)90 public Node removeNamedItem(String name) throws DOMException { 91 throw new NotImplementedException(); 92 } 93 removeNamedItemNS(String namespaceURI, String localName)94 public Node removeNamedItemNS(String namespaceURI, String localName) throws DOMException { 95 throw new NotImplementedException(); 96 } 97 setNamedItem(Node arg)98 public Node setNamedItem(Node arg) throws DOMException { 99 throw new NotImplementedException(); 100 } 101 setNamedItemNS(Node arg)102 public Node setNamedItemNS(Node arg) throws DOMException { 103 throw new NotImplementedException(); 104 } 105 106 } 107