View Javadoc
1 /* 2 Copyright (C) 2003 Together 3 4 This library is free software; you can redistribute it and/or 5 modify it under the terms of the GNU Lesser General Public 6 License as published by the Free Software Foundation; either 7 version 2.1 of the License, or (at your option) any later version. 8 9 This library is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Lesser General Public License for more details. 13 14 You should have received a copy of the GNU Lesser General Public 15 License along with this library; if not, write to the Free Software 16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 17 */ 18 19 package org.enhydra.xml; 20 21 import org.w3c.dom.DOMException; 22 import org.w3c.dom.Node; 23 import org.w3c.dom.CharacterData; 24 25 /*** 26 * @author Tweety 27 * 28 * A class representing a node in a meta-data tree, which implements 29 * the <a href="../../../../api/org/w3c/dom/CharacterData.html"> 30 * 31 * @version 1.0 32 */ 33 public class CharacterDataImpl extends NodeImpl implements CharacterData { 34 35 36 /*** 37 * Constructs an empty <code>CharacterDataImpl</code>. 38 */ 39 public CharacterDataImpl() { 40 } 41 42 /*** 43 * Constructs a <code>CharacterDataImpl</code> from the 44 * given node as <code>Node</code>. 45 * 46 * @param node , as a <code>Node</code>. 47 */ 48 public CharacterDataImpl(Node node) { 49 super(node); 50 } 51 52 53 /*** 54 * Returns node value. 55 * 56 * @return node value, as <code>String</code>. 57 * 58 * @see org.w3c.dom.CharacterData#getData(). 59 * @throws DOMException 60 */ 61 public String getData() throws DOMException { 62 return nodeValue; 63 } 64 65 /*** 66 * Sets the new value of this node. 67 * 68 * @param data the new data 69 * 70 * @see org.w3c.dom.CharacterData#setData(String). 71 * @throws DOMException 72 */ 73 public void setData(String data) throws DOMException { 74 nodeValue = data; 75 } 76 77 78 /*** 79 * Returns the substring from the node's value. 80 * 81 * @param offset the begin index of the substring. 82 * @param count the number of characters. 83 * 84 * @return substring of the node's value. 85 * 86 * @see org.w3c.dom.CharacterData#substringData(int, int). 87 * @throws DOMException 88 */ 89 public String substringData(int offset, int count) throws DOMException { 90 int length = nodeValue.length(); 91 if (count < 0 || offset < 0 || offset > length - 1) 92 throw new DOMException(DOMException.INDEX_SIZE_ERR, "Index out of bounds"); 93 94 int tailIndex = length; 95 if(offset + count < length) 96 tailIndex = offset + count; 97 return nodeValue.substring(offset, tailIndex); 98 } 99 100 101 /*** 102 * Appends data to the node's value. 103 * 104 * @param arg the data to append to the node's value. 105 * 106 * @see org.w3c.dom.CharacterData#appendData(String). 107 */ 108 public void appendData(String arg) { 109 nodeValue += arg; 110 } 111 112 113 /*** 114 * Inserts substring into node's value string. 115 * 116 * @param offset the begin index of the substring. 117 * @param arg the <code>String</code> to insert. 118 * 119 * @see org.w3c.dom.CharacterData#insertData(int, String). 120 * @throws DOMException 121 */ 122 public void insertData(int offset, String arg) throws DOMException { 123 try { 124 nodeValue = new StringBuffer(nodeValue).insert(offset, arg).toString(); 125 } catch (StringIndexOutOfBoundsException e) { 126 throw new DOMException(DOMException.INDEX_SIZE_ERR, "Index out of bounds"); 127 } 128 } 129 130 131 /*** 132 * Deletes characters from the node's value string. 133 * 134 * @param offset the begin index of the substring. 135 * @param count the number of characters. 136 * 137 * @see org.w3c.dom.CharacterData#deleteData(int, int). 138 * @throws DOMException 139 */ 140 public void deleteData(int offset, int count) throws DOMException { 141 int tailLength = nodeValue.length() - count - offset; 142 if(nodeValue.length() - count - offset < 0) 143 tailLength = 0; 144 try { 145 nodeValue = nodeValue.substring(0, offset) + 146 (tailLength > 0 ? nodeValue.substring(offset + count, offset + count + tailLength) : ""); 147 } catch (StringIndexOutOfBoundsException e) { 148 throw new DOMException(DOMException.INDEX_SIZE_ERR, "Index out of bounds"); 149 } 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) throws DOMException { 164 deleteData(offset, count); 165 insertData(offset, arg); 166 } 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 181 }

This page was automatically generated by Maven