001/*
002 *  Copyright 2012 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016
017package org.ametys.core.util.dom;
018
019import java.util.Collections;
020
021import org.w3c.dom.DOMException;
022import org.w3c.dom.Document;
023import org.w3c.dom.NamedNodeMap;
024import org.w3c.dom.Node;
025import org.w3c.dom.NodeList;
026import org.w3c.dom.UserDataHandler;
027
028/**
029 * VERY partial implementation of a read-only, non-namespace aware {@link Node} on top of an objects hierarchy.<br>
030 * It is NOT intended to be used as a full-featured DOM implementation, but it aims to provide a thin DOM layer over objects usable e.g. in XPath expressions and XSL stylesheets.<br>
031 * There's no text nodes, nor entities, documents, ...
032 */
033public abstract class AbstractAmetysNode implements Node
034{
035    private NodeList _childNodes;
036    
037    @Override
038    public String getNodeValue() throws DOMException
039    {
040        return null;
041    }
042
043    @Override
044    public Node getParentNode()
045    {
046        return null;
047    }
048
049    @Override
050    public NamedNodeMap getAttributes()
051    {
052        return null;
053    }
054    
055    @Override
056    public NodeList getChildNodes()
057    {
058        if (_childNodes == null)
059        {
060            _childNodes = _getChildNodes();
061        }
062        
063        return _childNodes;
064    }
065    
066    /**
067     * Actual processing of child nodes.
068     * Sublclasses should override this method and not getChildNodes().
069     * @return a NodeList containing all children.
070     */
071    protected NodeList _getChildNodes()
072    {
073        return new AmetysNodeList(Collections.<Node>emptyList());
074    }
075
076    @Override
077    public boolean hasChildNodes()
078    {
079        return getFirstChild() != null;
080    }
081
082    @Override
083    public String getNamespaceURI()
084    {
085        return null;
086    }
087
088    @Override
089    public String getPrefix()
090    {
091        return null;
092    }
093    
094    @Override
095    public String getLocalName()
096    {
097        return null;
098    }
099
100    @Override
101    public boolean hasAttributes()
102    {
103        return false;
104    }
105
106    @Override
107    public String getBaseURI()
108    {
109        return null;
110    }
111
112    @Override
113    public Node getFirstChild()
114    {
115        return null;
116    }
117    
118    @Override
119    public Node getNextSibling()
120    {
121        Node parent = getParentNode();
122        if (parent == null)
123        {
124            return null;
125        }
126        
127        NodeList siblings = parent.getChildNodes();
128        
129        boolean isNext = false;
130        Node nextSibling = null;
131        int i = 0;
132        
133        while (nextSibling == null && i < siblings.getLength())
134        {
135            Node child = siblings.item(i++);
136            
137            if (isNext)
138            {
139                nextSibling = child;
140            }
141            else if (child == this)
142            {
143                isNext = true;
144            }
145        }
146        
147        return nextSibling == null ? null : nextSibling;
148    }
149    
150    // Unsupported methods
151
152    @Override
153    public Node getLastChild()
154    {
155        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "getLastChild");
156    }
157
158    @Override
159    public Node getPreviousSibling()
160    {
161        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "getPreviousSibling");
162    }
163
164    @Override
165    public Document getOwnerDocument()
166    {
167        //throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "getOwnerDocument");
168        return null;
169    }
170
171    @Override
172    public void setNodeValue(String nodeValue) throws DOMException
173    {
174        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "setNodeValue");
175    }
176
177    @Override
178    public Node insertBefore(Node newChild, Node refChild) throws DOMException
179    {
180        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "insertBefore");
181    }
182
183    @Override
184    public Node replaceChild(Node newChild, Node oldChild) throws DOMException
185    {
186        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "replaceChild");
187    }
188
189    @Override
190    public Node removeChild(Node oldChild) throws DOMException
191    {
192        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "removeChild");
193    }
194
195    @Override
196    public Node appendChild(Node newChild) throws DOMException
197    {
198        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "appendChild");
199    }
200
201    @Override
202    public Node cloneNode(boolean deep)
203    {
204        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "cloneNode");
205    }
206
207    @Override
208    public void normalize()
209    {
210        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "normalize");
211    }
212
213    @Override
214    public boolean isSupported(String feature, String version)
215    {
216        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "isSupported");
217    }
218
219    @Override
220    public void setPrefix(String prefix) throws DOMException
221    {
222        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "setPrefix");
223    }
224
225    @Override
226    public short compareDocumentPosition(Node other) throws DOMException
227    {
228        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "compareDocumentPosition");
229    }
230
231    @Override
232    public void setTextContent(String textContent) throws DOMException
233    {
234        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "setTextContent");
235    }
236
237    @Override
238    public boolean isSameNode(Node other)
239    {
240        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "isSameNode");
241    }
242
243    @Override
244    public String lookupPrefix(String namespaceURI)
245    {
246        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "lookupPrefix");
247    }
248
249    @Override
250    public boolean isDefaultNamespace(String namespaceURI)
251    {
252        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "isDefaultNamespace");
253    }
254
255    @Override
256    public String lookupNamespaceURI(String prefix)
257    {
258        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "lookupNamespaceURI");
259    }
260
261    @Override
262    public boolean isEqualNode(Node arg)
263    {
264        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "isEqualNode");
265    }
266
267    @Override
268    public Object getFeature(String feature, String version)
269    {
270        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "getFeature");
271    }
272
273    @Override
274    public Object setUserData(String key, Object data, UserDataHandler handler)
275    {
276        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "setUserData");
277    }
278
279    @Override
280    public Object getUserData(String key)
281    {
282        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "getUserData");
283    }
284}