001: package org.enhydra.xml;
002:
003: import org.w3c.dom.CharacterData;
004: import org.w3c.dom.DOMException;
005: import org.w3c.dom.Node;
006:
007: /**
008: * @author Tweety
009: *
010: * A class representing a node in a meta-data tree, which implements
011: * the <a href="../../../../api/org/w3c/dom/CharacterData.html">
012: *
013: * @version 1.0
014: */
015: public class CharacterDataImpl extends NodeImpl implements
016: CharacterData {
017:
018: /**
019: * Constructs an empty <code>CharacterDataImpl</code>.
020: */
021: public CharacterDataImpl() {
022: }
023:
024: /**
025: * Constructs a <code>CharacterDataImpl</code> from the
026: * given node as <code>Node</code>.
027: *
028: * @param node, as a <code>Node</code>.
029: */
030: public CharacterDataImpl(Node node) {
031: super (node);
032: }
033:
034: /**
035: * Returns node value.
036: *
037: * @return node value, as <code>String</code>.
038: *
039: * @see org.w3c.dom.CharacterData#getData().
040: */
041: public String getData() throws DOMException {
042: return nodeValue;
043: }
044:
045: /**
046: * Sets the new value of this node.
047: *
048: * @param data the new data
049: *
050: * @see org.w3c.dom.CharacterData#setData(String).
051: */
052: public void setData(String data) throws DOMException {
053: nodeValue = data;
054: }
055:
056: /**
057: * Returns the substring from the node's value.
058: *
059: * @param offset the begin index of the substring.
060: * @param count the number of characters.
061: *
062: * @return substring of the node's value.
063: *
064: * @see org.w3c.dom.CharacterData#substringData(int, int).
065: */
066: public String substringData(int offset, int count)
067: throws DOMException {
068: int length = nodeValue.length();
069: if (count < 0 || offset < 0 || offset > length - 1)
070: throw new DOMException(DOMException.INDEX_SIZE_ERR,
071: "Index out of bounds");
072:
073: int tailIndex = length;
074: if (offset + count < length)
075: tailIndex = offset + count;
076: return nodeValue.substring(offset, tailIndex);
077: }
078:
079: /**
080: * Appends data to the node's value.
081: *
082: * @param arg the data to append to the node's value.
083: *
084: * @see org.w3c.dom.CharacterData#appendData(String).
085: */
086: public void appendData(String arg) {
087: nodeValue += arg;
088: }
089:
090: /**
091: * Inserts substring into node's value string.
092: *
093: * @param offset the begin index of the substring.
094: * @param arg the <code>String</code> to insert.
095: *
096: * @see org.w3c.dom.CharacterData#insertData(int, String).
097: */
098: public void insertData(int offset, String arg) throws DOMException {
099: try {
100: nodeValue = new StringBuffer(nodeValue).insert(offset, arg)
101: .toString();
102: } catch (StringIndexOutOfBoundsException e) {
103: throw new DOMException(DOMException.INDEX_SIZE_ERR,
104: "Index out of bounds");
105: }
106: }
107:
108: /**
109: * Deletes characters from the node's value string.
110: *
111: * @param offset the begin index of the substring.
112: * @param count the number of characters.
113: *
114: * @see org.w3c.dom.CharacterData#deleteData(int, int).
115: */
116: public void deleteData(int offset, int count) throws DOMException {
117: int tailLength = nodeValue.length() - count - offset;
118: if (nodeValue.length() - count - offset < 0)
119: tailLength = 0;
120: try {
121: nodeValue = nodeValue.substring(0, offset)
122: + (tailLength > 0 ? nodeValue.substring(offset
123: + count, offset + count + tailLength) : "");
124: } catch (StringIndexOutOfBoundsException e) {
125: throw new DOMException(DOMException.INDEX_SIZE_ERR,
126: "Index out of bounds");
127: }
128: }
129:
130: /**
131: * Replaces characters in the node's value string.
132: *
133: * @param offset the begin index of the substring.
134: * @param count the number of characters.
135: * @param arg the <code>String</code> to insert.
136: *
137: * @see org.w3c.dom.CharacterData#replaceData(int, int, String).
138: */
139: public void replaceData(int offset, int count, String arg)
140: throws DOMException {
141: deleteData(offset, count);
142: insertData(offset, arg);
143: }
144:
145: /**
146: * Returns the namespace of the node.
147: *
148: * @return the namespace of the node.
149: *
150: * @see org.w3c.dom.Node#getNamespaceURI().
151: */
152: public String getNamespaceURI() {
153: return super.getNamespaceURI();
154: }
155:
156: }
|