001/*
002 *  Copyright 2011 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 */
016package org.ametys.core.util.dom;
017
018import org.w3c.dom.DOMException;
019import org.w3c.dom.Element;
020import org.w3c.dom.Node;
021import org.w3c.dom.Text;
022
023/**
024 * Partial implementation of a read-only, non-namespace aware {@link Text} on top of an objects hierarchy.<br>
025 * 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>
026 */
027public class AmetysText extends AbstractAmetysNode implements Text
028{
029    private String _data;
030    private Element _parent;
031    
032    /**
033     * Constructor
034     * @param data The data value
035     * @param parent the parent {@link Element}.
036     */
037    public AmetysText (String data, Element parent)
038    {
039        super();
040        _data = data;
041        _parent = parent;
042    }
043    
044    @Override
045    public Node getParentNode()
046    {
047        return _parent;
048    }
049    
050    @Override
051    public String getData() throws DOMException
052    {
053        return getNodeValue();
054    }
055
056    @Override
057    public int getLength()
058    {
059        return _data != null ? _data.length() : 0;
060    }
061
062    @Override
063    public String substringData(int offset, int count) throws DOMException
064    {
065        if (_data == null)
066        {
067            return null;
068        }
069        
070        if (offset + count >= _data.length()) 
071        {
072            return _data.substring(offset);
073        }
074        else
075        {
076            return _data.substring(offset, offset + count);
077        }
078    }
079
080    @Override
081    public String getNodeValue() throws DOMException
082    {
083        return _data;
084    }
085
086    @Override
087    public String getNodeName()
088    {
089        return "#text";
090    }
091
092    @Override
093    public short getNodeType()
094    {
095        return Node.TEXT_NODE;
096    }
097    
098    @Override
099    public Node getNextSibling()
100    {
101        return null;
102    }
103
104    @Override
105    public String getTextContent() throws DOMException
106    {
107        return getNodeValue();
108    }
109
110    @Override
111    public void setData(String data) throws DOMException
112    {
113        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "setData");
114    }
115    
116    @Override
117    public boolean isElementContentWhitespace()
118    {
119        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "isElementContentWhitespace");
120    }
121
122    @Override
123    public String getWholeText()
124    {
125        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "getWholeText");
126    }
127
128    @Override
129    public void appendData(String arg) throws DOMException
130    {
131        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "appendData");
132    }
133
134    @Override
135    public void insertData(int offset, String arg) throws DOMException
136    {
137        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "insertData");
138    }
139
140    @Override
141    public void deleteData(int offset, int count) throws DOMException
142    {
143        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "deleteData");
144    }
145
146    @Override
147    public void replaceData(int offset, int count, String arg) throws DOMException
148    {
149        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "replaceData");
150    }
151    
152    @Override
153    public Text splitText(int offset) throws DOMException
154    {
155        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "splitText");
156    }
157    
158    @Override
159    public Text replaceWholeText(String content) throws DOMException
160    {
161        throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "replaceWholeText");
162    }
163
164    
165
166}