View Javadoc
1 package org.enhydra.xml; 2 3 import org.w3c.dom.DOMException; 4 import org.w3c.dom.Node; 5 import org.w3c.dom.CharacterData; 6 7 /*** 8 * @author Tweety 9 * 10 * A class representing a node in a meta-data tree, which implements 11 * the <a href="../../../../api/org/w3c/dom/CharacterData.html"> 12 * 13 * @version 1.0 14 */ 15 public class CharacterDataImpl extends NodeImpl implements CharacterData { 16 17 18 /*** 19 * Constructs an empty <code>CharacterDataImpl</code>. 20 */ 21 public CharacterDataImpl() { 22 } 23 24 /*** 25 * Constructs a <code>CharacterDataImpl</code> from the 26 * given node as <code>Node</code>. 27 * 28 * @param node, as a <code>Node</code>. 29 */ 30 public CharacterDataImpl(Node node) { 31 super(node); 32 } 33 34 35 /*** 36 * Returns node value. 37 * 38 * @return node value, as <code>String</code>. 39 * 40 * @see org.w3c.dom.CharacterData#getData(). 41 */ 42 public String getData() throws DOMException { 43 return nodeValue; 44 } 45 46 /*** 47 * Sets the new value of this node. 48 * 49 * @param data the new data 50 * 51 * @see org.w3c.dom.CharacterData#setData(String). 52 */ 53 public void setData(String data) throws DOMException { 54 nodeValue = data; 55 } 56 57 58 /*** 59 * Returns the substring from the node's value. 60 * 61 * @param offset the begin index of the substring. 62 * @param count the number of characters. 63 * 64 * @return substring of the node's value. 65 * 66 * @see org.w3c.dom.CharacterData#substringData(int, int). 67 */ 68 public String substringData(int offset, int count) throws DOMException { 69 int length = nodeValue.length(); 70 if (count < 0 || offset < 0 || offset > length - 1) 71 throw new DOMException(DOMException.INDEX_SIZE_ERR, "Index out of bounds"); 72 73 int tailIndex = length; 74 if(offset + count < length) 75 tailIndex = offset + count; 76 return nodeValue.substring(offset, tailIndex); 77 } 78 79 80 /*** 81 * Appends data to the node's value. 82 * 83 * @param arg the data to append to the node's value. 84 * 85 * @see org.w3c.dom.CharacterData#appendData(String). 86 */ 87 public void appendData(String arg) { 88 nodeValue += arg; 89 } 90 91 92 /*** 93 * Inserts substring into node's value string. 94 * 95 * @param offset the begin index of the substring. 96 * @param arg the <code>String</code> to insert. 97 * 98 * @see org.w3c.dom.CharacterData#insertData(int, String). 99 */ 100 public void insertData(int offset, String arg) throws DOMException { 101 try { 102 nodeValue = new StringBuffer(nodeValue).insert(offset, arg).toString(); 103 } catch (StringIndexOutOfBoundsException e) { 104 throw new DOMException(DOMException.INDEX_SIZE_ERR, "Index out of bounds"); 105 } 106 } 107 108 109 /*** 110 * Deletes characters from the node's value string. 111 * 112 * @param offset the begin index of the substring. 113 * @param count the number of characters. 114 * 115 * @see org.w3c.dom.CharacterData#deleteData(int, int). 116 */ 117 public void deleteData(int offset, int count) throws DOMException { 118 int tailLength = nodeValue.length() - count - offset; 119 if(nodeValue.length() - count - offset < 0) 120 tailLength = 0; 121 try { 122 nodeValue = nodeValue.substring(0, offset) + 123 (tailLength > 0 ? nodeValue.substring(offset + count, offset + count + tailLength) : ""); 124 } catch (StringIndexOutOfBoundsException e) { 125 throw new DOMException(DOMException.INDEX_SIZE_ERR, "Index out of bounds"); 126 } 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) throws DOMException { 140 deleteData(offset, count); 141 insertData(offset, arg); 142 } 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 157 }

This page automatically generated by Maven