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 getLength()58 public int getLength() { 59 return mNodeList.size(); 60 } 61 getNamedItem(String name)62 public Node getNamedItem(String name) { 63 HashMap<String, Node> map = mNodeMap.get(""); // no namespace 64 if (map != null) { 65 return map.get(name); 66 } 67 68 return null; 69 } 70 getNamedItemNS(String namespaceURI, String localName)71 public Node getNamedItemNS(String namespaceURI, String localName) throws DOMException { 72 if (namespaceURI == null) { 73 namespaceURI = ""; //no namespace 74 } 75 76 HashMap<String, Node> map = mNodeMap.get(namespaceURI); 77 if (map != null) { 78 return map.get(localName); 79 } 80 81 return null; 82 } 83 item(int index)84 public Node item(int index) { 85 return mNodeList.get(index); 86 } 87 removeNamedItem(String name)88 public Node removeNamedItem(String name) throws DOMException { 89 throw new UnsupportedOperationException("Operation not implemented."); //$NON-NLS-1$ 90 } 91 removeNamedItemNS(String namespaceURI, String localName)92 public Node removeNamedItemNS(String namespaceURI, String localName) throws DOMException { 93 throw new UnsupportedOperationException("Operation not implemented."); //$NON-NLS-1$ 94 } 95 setNamedItem(Node arg)96 public Node setNamedItem(Node arg) throws DOMException { 97 throw new UnsupportedOperationException("Operation not implemented."); //$NON-NLS-1$ 98 } 99 setNamedItemNS(Node arg)100 public Node setNamedItemNS(Node arg) throws DOMException { 101 throw new UnsupportedOperationException("Operation not implemented."); //$NON-NLS-1$ 102 } 103 104 } 105