Home | History | Annotate | Download | only in mock
      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     @Override
    123     public Node appendChild(Node newChild) throws DOMException {
    124         mNodeList.getArrayList().add((MockXmlNode) newChild);
    125         return newChild;
    126     }
    127 
    128     @Override
    129     public NamedNodeMap getAttributes() {
    130         return mAttributes;
    131     }
    132 
    133     @Override
    134     public NodeList getChildNodes() {
    135         return mNodeList;
    136     }
    137 
    138     @Override
    139     public Node getFirstChild() {
    140         if (mNodeList.getLength() > 0) {
    141             return mNodeList.item(0);
    142         }
    143         return null;
    144     }
    145 
    146     @Override
    147     public Node getLastChild() {
    148         if (mNodeList.getLength() > 0) {
    149             return mNodeList.item(mNodeList.getLength() - 1);
    150         }
    151         return null;
    152     }
    153 
    154     @Override
    155     public Node getNextSibling() {
    156         return mNextSibling;
    157     }
    158 
    159     @Override
    160     public String getNodeName() {
    161         return mLocalName;
    162     }
    163 
    164     @Override
    165     public String getLocalName() {
    166         return mLocalName;
    167     }
    168 
    169     @Override
    170     public short getNodeType() {
    171         return mNodeType;
    172     }
    173 
    174     @Override
    175     public Node getParentNode() {
    176         return mParent;
    177     }
    178 
    179     @Override
    180     public Node getPreviousSibling() {
    181         return mPreviousSibling;
    182     }
    183 
    184     @Override
    185     public boolean hasChildNodes() {
    186         return mNodeList.getLength() > 0;
    187     }
    188 
    189     @Override
    190     public boolean hasAttributes() {
    191         throw new UnsupportedOperationException("Operation not implemented.");  //$NON-NLS-1$
    192     }
    193 
    194     @Override
    195     public boolean isSameNode(Node other) {
    196         return this == other;
    197     }
    198 
    199     @Override
    200     public String getNodeValue() throws DOMException {
    201         return mAttrValue;
    202     }
    203 
    204     @Override
    205     public String getPrefix() {
    206         return getPrefix(getNamespaceURI());
    207     }
    208 
    209     @Override
    210     public String getNamespaceURI() {
    211         return mNamespace;
    212     }
    213 
    214 
    215     // --- methods not implemented ---
    216 
    217     @Override
    218     public Node cloneNode(boolean deep) {
    219         throw new UnsupportedOperationException("Operation not implemented.");  //$NON-NLS-1$
    220     }
    221 
    222     @Override
    223     public short compareDocumentPosition(Node other) throws DOMException {
    224         throw new UnsupportedOperationException("Operation not implemented.");  //$NON-NLS-1$
    225     }
    226 
    227     @Override
    228     public String getBaseURI() {
    229         throw new UnsupportedOperationException("Operation not implemented.");  //$NON-NLS-1$
    230     }
    231 
    232     @Override
    233     public Object getFeature(String feature, String version) {
    234         throw new UnsupportedOperationException("Operation not implemented.");  //$NON-NLS-1$
    235     }
    236 
    237     @Override
    238     public Document getOwnerDocument() {
    239         throw new UnsupportedOperationException("Operation not implemented.");  //$NON-NLS-1$
    240     }
    241 
    242     @Override
    243     public String getTextContent() throws DOMException {
    244         throw new UnsupportedOperationException("Operation not implemented.");  //$NON-NLS-1$
    245     }
    246 
    247     @Override
    248     public Object getUserData(String key) {
    249         throw new UnsupportedOperationException("Operation not implemented.");  //$NON-NLS-1$
    250     }
    251 
    252     @Override
    253     public Node insertBefore(Node newChild, Node refChild)
    254             throws DOMException {
    255         throw new UnsupportedOperationException("Operation not implemented.");  //$NON-NLS-1$
    256     }
    257 
    258     @Override
    259     public boolean isDefaultNamespace(String namespaceURI) {
    260         throw new UnsupportedOperationException("Operation not implemented.");  //$NON-NLS-1$
    261     }
    262 
    263     @Override
    264     public boolean isEqualNode(Node arg) {
    265         throw new UnsupportedOperationException("Operation not implemented.");  //$NON-NLS-1$
    266     }
    267 
    268     @Override
    269     public boolean isSupported(String feature, String version) {
    270         throw new UnsupportedOperationException("Operation not implemented.");  //$NON-NLS-1$
    271     }
    272 
    273     @Override
    274     public String lookupNamespaceURI(String prefix) {
    275         throw new UnsupportedOperationException("Operation not implemented.");  //$NON-NLS-1$
    276     }
    277 
    278     @Override
    279     public String lookupPrefix(String namespaceURI) {
    280         throw new UnsupportedOperationException("Operation not implemented.");  //$NON-NLS-1$
    281     }
    282 
    283     @Override
    284     public void normalize() {
    285         throw new UnsupportedOperationException("Operation not implemented.");  //$NON-NLS-1$
    286     }
    287 
    288     @Override
    289     public Node removeChild(Node oldChild) throws DOMException {
    290         throw new UnsupportedOperationException("Operation not implemented.");  //$NON-NLS-1$
    291     }
    292 
    293     @Override
    294     public Node replaceChild(Node newChild, Node oldChild)
    295             throws DOMException {
    296         throw new UnsupportedOperationException("Operation not implemented.");  //$NON-NLS-1$
    297     }
    298 
    299     @Override
    300     public void setNodeValue(String nodeValue) throws DOMException {
    301         throw new UnsupportedOperationException("Operation not implemented.");  //$NON-NLS-1$
    302     }
    303 
    304     @Override
    305     public void setPrefix(String prefix) throws DOMException {
    306         throw new UnsupportedOperationException("Operation not implemented.");  //$NON-NLS-1$
    307     }
    308 
    309     @Override
    310     public void setTextContent(String textContent) throws DOMException {
    311         throw new UnsupportedOperationException("Operation not implemented.");  //$NON-NLS-1$
    312     }
    313 
    314     @Override
    315     public Object setUserData(String key, Object data,
    316             UserDataHandler handler) {
    317         throw new UnsupportedOperationException("Operation not implemented.");  //$NON-NLS-1$
    318     }
    319 }
    320