001: /*
002: Copyright (C) 2003 Together
003:
004: This library is free software; you can redistribute it and/or
005: modify it under the terms of the GNU Lesser General Public
006: License as published by the Free Software Foundation; either
007: version 2.1 of the License, or (at your option) any later version.
008:
009: This library is distributed in the hope that it will be useful,
010: but WITHOUT ANY WARRANTY; without even the implied warranty of
011: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: Lesser General Public License for more details.
013:
014: You should have received a copy of the GNU Lesser General Public
015: License along with this library; if not, write to the Free Software
016: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package org.enhydra.xml;
020:
021: import org.w3c.dom.DOMException;
022: import org.w3c.dom.Node;
023: import org.w3c.dom.CharacterData;
024:
025: /**
026: * @author Tweety
027: *
028: * A class representing a node in a meta-data tree, which implements
029: * the <a href="../../../../api/org/w3c/dom/CharacterData.html">
030: *
031: * @version 1.0
032: */
033: public class CharacterDataImpl extends NodeImpl implements
034: CharacterData {
035:
036: /**
037: * Constructs an empty <code>CharacterDataImpl</code>.
038: */
039: public CharacterDataImpl() {
040: }
041:
042: /**
043: * Constructs a <code>CharacterDataImpl</code> from the
044: * given node as <code>Node</code>.
045: *
046: * @param node , as a <code>Node</code>.
047: */
048: public CharacterDataImpl(Node node) {
049: super (node);
050: }
051:
052: /**
053: * Returns node value.
054: *
055: * @return node value, as <code>String</code>.
056: *
057: * @see org.w3c.dom.CharacterData#getData().
058: * @throws DOMException
059: */
060: public String getData() throws DOMException {
061: return nodeValue;
062: }
063:
064: /**
065: * Sets the new value of this node.
066: *
067: * @param data the new data
068: *
069: * @see org.w3c.dom.CharacterData#setData(String).
070: * @throws DOMException
071: */
072: public void setData(String data) throws DOMException {
073: nodeValue = data;
074: }
075:
076: /**
077: * Returns the substring from the node's value.
078: *
079: * @param offset the begin index of the substring.
080: * @param count the number of characters.
081: *
082: * @return substring of the node's value.
083: *
084: * @see org.w3c.dom.CharacterData#substringData(int, int).
085: * @throws DOMException
086: */
087: public String substringData(int offset, int count)
088: throws DOMException {
089: int length = nodeValue.length();
090: if (count < 0 || offset < 0 || offset > length - 1)
091: throw new DOMException(DOMException.INDEX_SIZE_ERR,
092: "Index out of bounds");
093:
094: int tailIndex = length;
095: if (offset + count < length)
096: tailIndex = offset + count;
097: return nodeValue.substring(offset, tailIndex);
098: }
099:
100: /**
101: * Appends data to the node's value.
102: *
103: * @param arg the data to append to the node's value.
104: *
105: * @see org.w3c.dom.CharacterData#appendData(String).
106: */
107: public void appendData(String arg) {
108: nodeValue += arg;
109: }
110:
111: /**
112: * Inserts substring into node's value string.
113: *
114: * @param offset the begin index of the substring.
115: * @param arg the <code>String</code> to insert.
116: *
117: * @see org.w3c.dom.CharacterData#insertData(int, String).
118: * @throws DOMException
119: */
120: public void insertData(int offset, String arg) throws DOMException {
121: try {
122: nodeValue = new StringBuffer(nodeValue).insert(offset, arg)
123: .toString();
124: } catch (StringIndexOutOfBoundsException e) {
125: throw new DOMException(DOMException.INDEX_SIZE_ERR,
126: "Index out of bounds");
127: }
128: }
129:
130: /**
131: * Deletes characters from the node's value string.
132: *
133: * @param offset the begin index of the substring.
134: * @param count the number of characters.
135: *
136: * @see org.w3c.dom.CharacterData#deleteData(int, int).
137: * @throws DOMException
138: */
139: public void deleteData(int offset, int count) throws DOMException {
140: int tailLength = nodeValue.length() - count - offset;
141: if (nodeValue.length() - count - offset < 0)
142: tailLength = 0;
143: try {
144: nodeValue = nodeValue.substring(0, offset)
145: + (tailLength > 0 ? nodeValue.substring(offset
146: + count, offset + count + tailLength) : "");
147: } catch (StringIndexOutOfBoundsException e) {
148: throw new DOMException(DOMException.INDEX_SIZE_ERR,
149: "Index out of bounds");
150: }
151: }
152:
153: /**
154: * Replaces characters in the node's value string.
155: *
156: * @param offset the begin index of the substring.
157: * @param count the number of characters.
158: * @param arg the <code>String</code> to insert.
159: *
160: * @see org.w3c.dom.CharacterData#replaceData(int, int, String).
161: * @throws DOMException
162: */
163: public void replaceData(int offset, int count, String arg)
164: throws DOMException {
165: deleteData(offset, count);
166: insertData(offset, arg);
167: }
168:
169: /**
170: * Returns the namespace of the node.
171: *
172: * @return the namespace of the node.
173: *
174: * @see org.w3c.dom.Node#getNamespaceURI().
175: */
176: public String getNamespaceURI() {
177: return super.getNamespaceURI();
178: }
179:
180: }
|