• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 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.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;
25 
26 import java.util.HashMap;
27 
28 import sun.reflect.generics.reflectiveObjects.NotImplementedException;
29 
30 
31 /**
32  * A mock XML node with only a minimal set of information.
33  */
34 public class MockXmlNode implements Node {
35 
36     MockNodeList mNodeList;
37     private String mLocalName;
38     private String mNamespace;
39     private short mNodeType;
40     private MockXmlNode mParent;
41     private MockXmlNode mPreviousSibling;
42     private MockXmlNode mNextSibling;
43     private String mAttrValue;
44     private MockNamedNodeMap mAttributes;
45 
46     // namespace stuff only set in the root node
47     /** map from namespace to prefix. */
48     private HashMap<String, String> mNsMap = null;
49 
50     /**
51      * Constructs a node from a given children list.
52      *
53      * @param namespace The namespace of the node or null if none
54      * @param localName The XML local node name.
55      * @param node_type One of Node.xxx_NODE constants, e.g. Node.ELEMENT_NODE
56      * @param children The children list. Can be null.
57      */
MockXmlNode(String namespace, String localName, short node_type, MockXmlNode[] children)58     public MockXmlNode(String namespace, String localName, short node_type,
59             MockXmlNode[] children) {
60         mLocalName = localName;
61         mNamespace = namespace;
62         mNodeType = node_type;
63         mNodeList = new MockNodeList(children);
64         fixNavigation();
65     }
66 
67     /**
68      * Constructs an attribute node
69      *
70      * @param namespace The namespace of the node or null if none
71      * @param localName The XML local node name.
72      * @param value the value of the attribute
73      */
MockXmlNode(String namespace, String localName, String value)74     public MockXmlNode(String namespace, String localName, String value) {
75         mLocalName = localName;
76         mNamespace = namespace;
77         mAttrValue = value;
78         mNodeType = Node.ATTRIBUTE_NODE;
79         mNodeList = new MockNodeList(new MockXmlNode[0]);
80         fixNavigation();
81     }
82 
fixNavigation()83     private void fixNavigation() {
84         MockXmlNode prev = null;
85         for (MockXmlNode n : mNodeList.getArrayList()) {
86             n.mParent = this;
87             n.mPreviousSibling = prev;
88             if (prev != null) {
89                 prev.mNextSibling = n;
90             }
91             n.fixNavigation();
92             prev = n;
93         }
94     }
95 
addAttributes(String namespaceURI, String localName, String value)96     public void addAttributes(String namespaceURI, String localName, String value) {
97         if (mAttributes == null) {
98             mAttributes = new MockNamedNodeMap();
99         }
100 
101         MockXmlNode node = mAttributes.addAttribute(namespaceURI, localName, value);
102         node.mParent = this;
103     }
104 
setPrefix(String namespace, String prefix)105     public void setPrefix(String namespace, String prefix) {
106         if (mNsMap == null) {
107             mNsMap = new HashMap<String, String>();
108         }
109 
110         mNsMap.put(namespace, prefix);
111     }
112 
getPrefix(String namespace)113     public String getPrefix(String namespace) {
114         if (mNsMap != null) {
115             return mNsMap.get(namespace);
116         }
117 
118         return mParent.getPrefix(namespace);
119     }
120 
121 
122     // ----------- Node methods
123 
appendChild(Node newChild)124     public Node appendChild(Node newChild) throws DOMException {
125         mNodeList.getArrayList().add((MockXmlNode) newChild);
126         return newChild;
127     }
128 
getAttributes()129     public NamedNodeMap getAttributes() {
130         return mAttributes;
131     }
132 
getChildNodes()133     public NodeList getChildNodes() {
134         return mNodeList;
135     }
136 
getFirstChild()137     public Node getFirstChild() {
138         if (mNodeList.getLength() > 0) {
139             return mNodeList.item(0);
140         }
141         return null;
142     }
143 
getLastChild()144     public Node getLastChild() {
145         if (mNodeList.getLength() > 0) {
146             return mNodeList.item(mNodeList.getLength() - 1);
147         }
148         return null;
149     }
150 
getNextSibling()151     public Node getNextSibling() {
152         return mNextSibling;
153     }
154 
getNodeName()155     public String getNodeName() {
156         return mLocalName;
157     }
158 
getLocalName()159     public String getLocalName() {
160         return mLocalName;
161     }
162 
getNodeType()163     public short getNodeType() {
164         return mNodeType;
165     }
166 
getParentNode()167     public Node getParentNode() {
168         return mParent;
169     }
170 
getPreviousSibling()171     public Node getPreviousSibling() {
172         return mPreviousSibling;
173     }
174 
hasChildNodes()175     public boolean hasChildNodes() {
176         return mNodeList.getLength() > 0;
177     }
178 
hasAttributes()179     public boolean hasAttributes() {
180         // TODO Auto-generated method stub
181         throw new NotImplementedException();
182         //return false;
183     }
184 
isSameNode(Node other)185     public boolean isSameNode(Node other) {
186         return this == other;
187     }
188 
getNodeValue()189     public String getNodeValue() throws DOMException {
190         return mAttrValue;
191     }
192 
getPrefix()193     public String getPrefix() {
194         return getPrefix(getNamespaceURI());
195     }
196 
getNamespaceURI()197     public String getNamespaceURI() {
198         return mNamespace;
199     }
200 
201 
202     // --- methods not implemented ---
203 
cloneNode(boolean deep)204     public Node cloneNode(boolean deep) {
205         throw new NotImplementedException();
206     }
207 
compareDocumentPosition(Node other)208     public short compareDocumentPosition(Node other) throws DOMException {
209         throw new NotImplementedException();
210     }
211 
getBaseURI()212     public String getBaseURI() {
213         throw new NotImplementedException();
214     }
215 
getFeature(String feature, String version)216     public Object getFeature(String feature, String version) {
217         throw new NotImplementedException();
218     }
219 
getOwnerDocument()220     public Document getOwnerDocument() {
221         throw new NotImplementedException();
222     }
223 
getTextContent()224     public String getTextContent() throws DOMException {
225         throw new NotImplementedException();
226     }
227 
getUserData(String key)228     public Object getUserData(String key) {
229         throw new NotImplementedException();
230     }
231 
insertBefore(Node newChild, Node refChild)232     public Node insertBefore(Node newChild, Node refChild)
233             throws DOMException {
234         throw new NotImplementedException();
235     }
236 
isDefaultNamespace(String namespaceURI)237     public boolean isDefaultNamespace(String namespaceURI) {
238         throw new NotImplementedException();
239     }
240 
isEqualNode(Node arg)241     public boolean isEqualNode(Node arg) {
242         throw new NotImplementedException();
243     }
244 
isSupported(String feature, String version)245     public boolean isSupported(String feature, String version) {
246         throw new NotImplementedException();
247     }
248 
lookupNamespaceURI(String prefix)249     public String lookupNamespaceURI(String prefix) {
250         throw new NotImplementedException();
251     }
252 
lookupPrefix(String namespaceURI)253     public String lookupPrefix(String namespaceURI) {
254         throw new NotImplementedException();
255     }
256 
normalize()257     public void normalize() {
258         throw new NotImplementedException();
259     }
260 
removeChild(Node oldChild)261     public Node removeChild(Node oldChild) throws DOMException {
262         throw new NotImplementedException();
263     }
264 
replaceChild(Node newChild, Node oldChild)265     public Node replaceChild(Node newChild, Node oldChild)
266             throws DOMException {
267         throw new NotImplementedException();
268     }
269 
setNodeValue(String nodeValue)270     public void setNodeValue(String nodeValue) throws DOMException {
271         throw new NotImplementedException();
272     }
273 
setPrefix(String prefix)274     public void setPrefix(String prefix) throws DOMException {
275         throw new NotImplementedException();
276     }
277 
setTextContent(String textContent)278     public void setTextContent(String textContent) throws DOMException {
279         throw new NotImplementedException();
280     }
281 
setUserData(String key, Object data, UserDataHandler handler)282     public Object setUserData(String key, Object data,
283             UserDataHandler handler) {
284         throw new NotImplementedException();
285     }
286 }
287