1 18 package org.apache.batik.dom; 19 20 import org.w3c.dom.CharacterData ; 21 import org.w3c.dom.DOMException ; 22 import org.w3c.dom.Node ; 23 24 30 public abstract class AbstractCharacterData 31 extends AbstractChildNode 32 implements CharacterData { 33 36 protected String nodeValue = ""; 37 38 42 public String getNodeValue() throws DOMException { 43 return nodeValue; 44 } 45 46 49 public void setNodeValue(String nodeValue) throws DOMException { 50 if (isReadonly()) { 51 throw createDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, 52 "readonly.node", 53 new Object [] { new Integer (getNodeType()), 54 getNodeName() }); 55 } 56 String val = this.nodeValue; 58 this.nodeValue = (nodeValue == null) ? "" : nodeValue; 59 60 fireDOMCharacterDataModifiedEvent(val, this.nodeValue); 62 if (getParentNode() != null) { 63 ((AbstractParentNode)getParentNode()). 64 fireDOMSubtreeModifiedEvent(); 65 } 66 } 67 68 72 public String getData() throws DOMException { 73 return getNodeValue(); 74 } 75 76 80 public void setData(String data) throws DOMException { 81 setNodeValue(data); 82 } 83 84 88 public int getLength() { 89 return nodeValue.length(); 90 } 91 92 96 public String substringData(int offset, int count) throws DOMException { 97 checkOffsetCount(offset, count); 98 99 String v = getNodeValue(); 100 return v.substring(offset, Math.min(v.length(), offset + count)); 101 } 102 103 107 public void appendData(String arg) throws DOMException { 108 if (isReadonly()) { 109 throw createDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, 110 "readonly.node", 111 new Object [] { new Integer (getNodeType()), 112 getNodeName() }); 113 } 114 setNodeValue(getNodeValue() + ((arg == null) ? "" : arg)); 115 } 116 117 121 public void insertData(int offset, String arg) throws DOMException { 122 if (isReadonly()) { 123 throw createDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, 124 "readonly.node", 125 new Object [] { new Integer (getNodeType()), 126 getNodeName() }); 127 } 128 if (offset < 0 || offset > getLength()) { 129 throw createDOMException(DOMException.INDEX_SIZE_ERR, 130 "offset", 131 new Object [] { new Integer (offset) }); 132 } 133 String v = getNodeValue(); 134 setNodeValue(v.substring(0, offset) + 135 arg + v.substring(offset, v.length())); 136 } 137 138 142 public void deleteData(int offset, int count) throws DOMException { 143 if (isReadonly()) { 144 throw createDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, 145 "readonly.node", 146 new Object [] { new Integer (getNodeType()), 147 getNodeName() }); 148 } 149 checkOffsetCount(offset, count); 150 151 String v = getNodeValue(); 152 setNodeValue(v.substring(0, offset) + 153 v.substring(Math.min(v.length(), offset + count), 154 v.length())); 155 } 156 157 161 public void replaceData(int offset, int count, String arg) 162 throws DOMException { 163 if (isReadonly()) { 164 throw createDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR, 165 "readonly.node", 166 new Object [] { new Integer (getNodeType()), 167 getNodeName() }); 168 } 169 checkOffsetCount(offset, count); 170 171 String v = getNodeValue(); 172 setNodeValue(v.substring(0, offset) + 173 arg + 174 v.substring(Math.min(v.length(), offset + count), 175 v.length())); 176 } 177 178 181 protected void checkOffsetCount(int offset, int count) 182 throws DOMException { 183 if (offset < 0 || offset >= getLength()) { 184 throw createDOMException(DOMException.INDEX_SIZE_ERR, 185 "offset", 186 new Object [] { new Integer (offset) }); 187 } 188 if (count < 0) { 189 throw createDOMException(DOMException.INDEX_SIZE_ERR, 190 "negative.count", 191 new Object [] { new Integer (count) }); 192 } 193 } 194 195 198 protected Node export(Node n, AbstractDocument d) { 199 super.export(n, d); 200 AbstractCharacterData cd = (AbstractCharacterData)n; 201 cd.nodeValue = nodeValue; 202 return n; 203 } 204 205 208 protected Node deepExport(Node n, AbstractDocument d) { 209 super.deepExport(n, d); 210 AbstractCharacterData cd = (AbstractCharacterData)n; 211 cd.nodeValue = nodeValue; 212 return n; 213 } 214 215 219 protected Node copyInto(Node n) { 220 super.copyInto(n); 221 AbstractCharacterData cd = (AbstractCharacterData)n; 222 cd.nodeValue = nodeValue; 223 return n; 224 } 225 226 230 protected Node deepCopyInto(Node n) { 231 super.deepCopyInto(n); 232 AbstractCharacterData cd = (AbstractCharacterData)n; 233 cd.nodeValue = nodeValue; 234 return n; 235 } 236 } 237 | Popular Tags |