InnerNodeImpl.java revision f6c387128427e121477c1b32ad35cdcaa5101ba3
1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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
17package org.apache.harmony.xml.dom;
18
19import java.util.ArrayList;
20import java.util.List;
21
22import org.w3c.dom.DOMException;
23import org.w3c.dom.Node;
24import org.w3c.dom.NodeList;
25import org.w3c.dom.Text;
26
27/**
28 * Provides a straightforward implementation of the corresponding W3C DOM
29 * interface. The class is used internally only, thus only notable members that
30 * are not in the original interface are documented (the W3C docs are quite
31 * extensive). Hope that's ok.
32 * <p>
33 * Some of the fields may have package visibility, so other classes belonging to
34 * the DOM implementation can easily access them while maintaining the DOM tree
35 * structure.
36 * <p>
37 * This class represents a Node that has a parent Node as well as (potentially)
38 * a number of children.
39 */
40public abstract class InnerNodeImpl extends LeafNodeImpl {
41
42    // Maintained by LeafNodeImpl and ElementImpl.
43    List<LeafNodeImpl> children = new ArrayList<LeafNodeImpl>();
44
45    public InnerNodeImpl(DocumentImpl document) {
46        super(document);
47    }
48
49    public Node appendChild(Node newChild) throws DOMException {
50        return insertChildAt(newChild, children.size());
51    }
52
53    public NodeList getChildNodes() {
54        NodeListImpl list = new NodeListImpl();
55
56        for (NodeImpl node : children) {
57            list.add(node);
58        }
59
60        return list;
61    }
62
63    public Node getFirstChild() {
64        return (!children.isEmpty() ? children.get(0) : null);
65    }
66
67    public Node getLastChild() {
68        return (!children.isEmpty() ? children.get(children.size() - 1) : null);
69    }
70
71    public Node getNextSibling() {
72        if (parent == null || index >= parent.children.size()) {
73            return null;
74        }
75
76        return parent.children.get(index + 1);
77    }
78
79    public boolean hasChildNodes() {
80        return children.size() != 0;
81    }
82
83    public Node insertBefore(Node newChild, Node refChild) throws DOMException {
84        LeafNodeImpl refChildImpl = (LeafNodeImpl) refChild;
85
86        if (refChildImpl.document != document) {
87            throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, null);
88        }
89
90        if (refChildImpl.parent != this) {
91            throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, null);
92        }
93
94        return insertChildAt(newChild, refChildImpl.index);
95    }
96
97    /**
98     * Inserts a new child node into this node at a given position. If the new
99     * node is already child of another node, it is first removed from there.
100     * This method is the generalization of the appendChild() and insertBefore()
101     * methods.
102     *
103     * @param newChild The new child node to add.
104     * @param index The index at which to insert the new child node.
105     *
106     * @return The node added.
107     *
108     * @throws DOMException If the attempted operation violates the XML/DOM
109     *         well-formedness rules.
110     */
111    public Node insertChildAt(Node newChild, int index) throws DOMException {
112        LeafNodeImpl newChildImpl = (LeafNodeImpl) newChild;
113
114        if (document != null && newChildImpl.document != null && newChildImpl.document != document) {
115            throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, null);
116        }
117
118        if (newChildImpl.isParentOf(this)) {
119            throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, null);
120        }
121
122        if (newChildImpl.parent != null) {
123            int oldIndex = newChildImpl.index;
124            newChildImpl.parent.children.remove(oldIndex);
125            newChildImpl.parent.refreshIndices(oldIndex);
126        }
127
128        children.add(index, newChildImpl);
129        newChildImpl.parent = this;
130        refreshIndices(index);
131
132        return newChild;
133    }
134
135    public boolean isParentOf(Node node) {
136        LeafNodeImpl nodeImpl = (LeafNodeImpl) node;
137
138        while (nodeImpl != null) {
139            if (nodeImpl == this) {
140                return true;
141            }
142
143            nodeImpl = nodeImpl.parent;
144        }
145
146        return false;
147    }
148
149    @Override
150    public void normalize() {
151        Node nextNode = null;
152
153        for (int i = children.size() - 1; i >= 0; i--) {
154            Node thisNode = children.get(i);
155
156            thisNode.normalize();
157
158            if (thisNode.getNodeType() == Node.TEXT_NODE) {
159                if (nextNode != null && nextNode.getNodeType() == Node.TEXT_NODE) {
160                    ((Text)thisNode).setData(thisNode.getNodeValue() + nextNode.getNodeValue());
161                    removeChild(nextNode);
162                }
163
164                if ("".equals(thisNode.getNodeValue())) {
165                    removeChild(thisNode);
166                    nextNode = null;
167                } else {
168                    nextNode = thisNode;
169                }
170            }
171        }
172    }
173
174    private void refreshIndices(int fromIndex) {
175        for (int i = fromIndex; i < children.size(); i++) {
176            children.get(i).index = i;
177        }
178    }
179
180    public Node removeChild(Node oldChild) throws DOMException {
181        LeafNodeImpl oldChildImpl = (LeafNodeImpl) oldChild;
182
183        if (oldChildImpl.document != document) {
184            throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, null);
185        }
186
187        if (oldChildImpl.parent != this) {
188            throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, null);
189        }
190
191        int index = oldChildImpl.index;
192        children.remove(index);
193        oldChildImpl.parent = null;
194        refreshIndices(index);
195
196        return oldChild;
197    }
198
199    public Node replaceChild(Node newChild, Node oldChild) throws DOMException {
200        LeafNodeImpl oldChildImpl = (LeafNodeImpl) oldChild;
201        LeafNodeImpl newChildImpl = (LeafNodeImpl) newChild;
202
203        if (oldChildImpl.document != document
204                || newChildImpl.document != document) {
205            throw new DOMException(DOMException.WRONG_DOCUMENT_ERR, null);
206        }
207
208        if (oldChildImpl.parent != this || newChildImpl.isParentOf(this)) {
209            throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR, null);
210        }
211
212        int index = oldChildImpl.index;
213        children.set(index, newChildImpl);
214        oldChildImpl.parent = null;
215        newChildImpl.parent = this;
216        refreshIndices(index);
217
218        return oldChildImpl;
219    }
220
221}
222