OSDN Git Service

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