2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.eclipse.org/org/documents/epl-v10.php
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.
17 package com.android.ide.eclipse.adt.internal.editors.mock;
19 import org.w3c.dom.DOMException;
20 import org.w3c.dom.Document;
21 import org.w3c.dom.NamedNodeMap;
22 import org.w3c.dom.Node;
23 import org.w3c.dom.NodeList;
24 import org.w3c.dom.UserDataHandler;
26 import java.util.HashMap;
30 * A mock XML node with only a minimal set of information.
32 public class MockXmlNode implements Node {
34 MockNodeList mNodeList;
35 private String mLocalName;
36 private String mNamespace;
37 private short mNodeType;
38 private MockXmlNode mParent;
39 private MockXmlNode mPreviousSibling;
40 private MockXmlNode mNextSibling;
41 private String mAttrValue;
42 private MockNamedNodeMap mAttributes;
44 // namespace stuff only set in the root node
45 /** map from namespace to prefix. */
46 private HashMap<String, String> mNsMap = null;
49 * Constructs a node from a given children list.
51 * @param namespace The namespace of the node or null if none
52 * @param localName The XML local node name.
53 * @param node_type One of Node.xxx_NODE constants, e.g. Node.ELEMENT_NODE
54 * @param children The children list. Can be null.
56 public MockXmlNode(String namespace, String localName, short node_type,
57 MockXmlNode[] children) {
58 mLocalName = localName;
59 mNamespace = namespace;
60 mNodeType = node_type;
61 mNodeList = new MockNodeList(children);
66 * Constructs an attribute node
68 * @param namespace The namespace of the node or null if none
69 * @param localName The XML local node name.
70 * @param value the value of the attribute
72 public MockXmlNode(String namespace, String localName, String value) {
73 mLocalName = localName;
74 mNamespace = namespace;
76 mNodeType = Node.ATTRIBUTE_NODE;
77 mNodeList = new MockNodeList(new MockXmlNode[0]);
81 private void fixNavigation() {
82 MockXmlNode prev = null;
83 for (MockXmlNode n : mNodeList.getArrayList()) {
85 n.mPreviousSibling = prev;
87 prev.mNextSibling = n;
94 public void addAttributes(String namespaceURI, String localName, String value) {
95 if (mAttributes == null) {
96 mAttributes = new MockNamedNodeMap();
99 MockXmlNode node = mAttributes.addAttribute(namespaceURI, localName, value);
103 public void setPrefix(String namespace, String prefix) {
104 if (mNsMap == null) {
105 mNsMap = new HashMap<String, String>();
108 mNsMap.put(namespace, prefix);
111 public String getPrefix(String namespace) {
112 if (mNsMap != null) {
113 return mNsMap.get(namespace);
116 return mParent.getPrefix(namespace);
120 // ----------- Node methods
122 public Node appendChild(Node newChild) throws DOMException {
123 mNodeList.getArrayList().add((MockXmlNode) newChild);
127 public NamedNodeMap getAttributes() {
131 public NodeList getChildNodes() {
135 public Node getFirstChild() {
136 if (mNodeList.getLength() > 0) {
137 return mNodeList.item(0);
142 public Node getLastChild() {
143 if (mNodeList.getLength() > 0) {
144 return mNodeList.item(mNodeList.getLength() - 1);
149 public Node getNextSibling() {
153 public String getNodeName() {
157 public String getLocalName() {
161 public short getNodeType() {
165 public Node getParentNode() {
169 public Node getPreviousSibling() {
170 return mPreviousSibling;
173 public boolean hasChildNodes() {
174 return mNodeList.getLength() > 0;
177 public boolean hasAttributes() {
178 throw new UnsupportedOperationException("Operation not implemented."); //$NON-NLS-1$
181 public boolean isSameNode(Node other) {
182 return this == other;
185 public String getNodeValue() throws DOMException {
189 public String getPrefix() {
190 return getPrefix(getNamespaceURI());
193 public String getNamespaceURI() {
198 // --- methods not implemented ---
200 public Node cloneNode(boolean deep) {
201 throw new UnsupportedOperationException("Operation not implemented."); //$NON-NLS-1$
204 public short compareDocumentPosition(Node other) throws DOMException {
205 throw new UnsupportedOperationException("Operation not implemented."); //$NON-NLS-1$
208 public String getBaseURI() {
209 throw new UnsupportedOperationException("Operation not implemented."); //$NON-NLS-1$
212 public Object getFeature(String feature, String version) {
213 throw new UnsupportedOperationException("Operation not implemented."); //$NON-NLS-1$
216 public Document getOwnerDocument() {
217 throw new UnsupportedOperationException("Operation not implemented."); //$NON-NLS-1$
220 public String getTextContent() throws DOMException {
221 throw new UnsupportedOperationException("Operation not implemented."); //$NON-NLS-1$
224 public Object getUserData(String key) {
225 throw new UnsupportedOperationException("Operation not implemented."); //$NON-NLS-1$
228 public Node insertBefore(Node newChild, Node refChild)
229 throws DOMException {
230 throw new UnsupportedOperationException("Operation not implemented."); //$NON-NLS-1$
233 public boolean isDefaultNamespace(String namespaceURI) {
234 throw new UnsupportedOperationException("Operation not implemented."); //$NON-NLS-1$
237 public boolean isEqualNode(Node arg) {
238 throw new UnsupportedOperationException("Operation not implemented."); //$NON-NLS-1$
241 public boolean isSupported(String feature, String version) {
242 throw new UnsupportedOperationException("Operation not implemented."); //$NON-NLS-1$
245 public String lookupNamespaceURI(String prefix) {
246 throw new UnsupportedOperationException("Operation not implemented."); //$NON-NLS-1$
249 public String lookupPrefix(String namespaceURI) {
250 throw new UnsupportedOperationException("Operation not implemented."); //$NON-NLS-1$
253 public void normalize() {
254 throw new UnsupportedOperationException("Operation not implemented."); //$NON-NLS-1$
257 public Node removeChild(Node oldChild) throws DOMException {
258 throw new UnsupportedOperationException("Operation not implemented."); //$NON-NLS-1$
261 public Node replaceChild(Node newChild, Node oldChild)
262 throws DOMException {
263 throw new UnsupportedOperationException("Operation not implemented."); //$NON-NLS-1$
266 public void setNodeValue(String nodeValue) throws DOMException {
267 throw new UnsupportedOperationException("Operation not implemented."); //$NON-NLS-1$
270 public void setPrefix(String prefix) throws DOMException {
271 throw new UnsupportedOperationException("Operation not implemented."); //$NON-NLS-1$
274 public void setTextContent(String textContent) throws DOMException {
275 throw new UnsupportedOperationException("Operation not implemented."); //$NON-NLS-1$
278 public Object setUserData(String key, Object data,
279 UserDataHandler handler) {
280 throw new UnsupportedOperationException("Operation not implemented."); //$NON-NLS-1$