|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
CharacterDataImpl.java | 66.7% | 51.9% | 50% | 53.8% |
|
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | * | |
17 | * $Id: CharacterDataImpl.java 541508 2007-05-25 01:54:12Z vgritsenko $ | |
18 | */ | |
19 | ||
20 | package org.apache.xindice.xml.dom; | |
21 | ||
22 | import org.apache.commons.logging.Log; | |
23 | import org.apache.commons.logging.LogFactory; | |
24 | import org.apache.xindice.util.ByteArrayInput; | |
25 | import org.apache.xindice.xml.SymbolTable; | |
26 | import org.apache.xindice.xml.XMLCompressedInput; | |
27 | ||
28 | import org.w3c.dom.CharacterData; | |
29 | import org.w3c.dom.DOMException; | |
30 | ||
31 | import java.io.IOException; | |
32 | ||
33 | /** | |
34 | * CharacterDataImpl | |
35 | * | |
36 | * @version $Revision: 541508 $, $Date: 2007-05-24 18:54:12 -0700 (Thu, 24 May 2007) $ | |
37 | */ | |
38 | public abstract class CharacterDataImpl extends NodeImpl implements CharacterData { | |
39 | ||
40 | private static final Log log = LogFactory.getLog(CharacterDataImpl.class); | |
41 | ||
42 | ||
43 | 0 | public CharacterDataImpl() { |
44 | } | |
45 | ||
46 | 173188 | public CharacterDataImpl(NodeImpl parentNode, byte[] data, int pos, int len) { |
47 | 173188 | super(parentNode, data, pos, len); |
48 | } | |
49 | ||
50 | 218009 | public CharacterDataImpl(NodeImpl parentNode, boolean dirty) { |
51 | 218009 | super(parentNode, dirty); |
52 | } | |
53 | ||
54 | 121219 | public CharacterDataImpl(NodeImpl parentNode, String nodeValue) { |
55 | 121219 | super(parentNode, true); |
56 | 121219 | if (nodeValue == null) { |
57 | 0 | nodeValue = ""; |
58 | } | |
59 | 121219 | this.nodeValue = nodeValue; |
60 | } | |
61 | ||
62 | 894171 | protected final void checkLoaded() { |
63 | 894171 | if (loaded) { |
64 | 459229 | return; |
65 | } | |
66 | ||
67 | 434940 | loaded = true; |
68 | 434940 | try { |
69 | 434940 | if (data != null) { |
70 | 173110 | DocumentImpl doc = (DocumentImpl) getOwnerDocument(); |
71 | 173110 | SymbolTable st = doc.getSymbols(); |
72 | ||
73 | 173110 | ByteArrayInput bis = new ByteArrayInput(data, pos, len); |
74 | 173110 | XMLCompressedInput xci = new XMLCompressedInput(bis, st); |
75 | ||
76 | 173110 | xci.readSignature(); // Skip The Signature |
77 | 173110 | if (getNodeType() == TEXT_NODE) { |
78 | 173064 | xci.readContentSize(); |
79 | } else { | |
80 | 46 | xci.readInt(); |
81 | } | |
82 | ||
83 | 173110 | byte[] buf = new byte[bis.available()]; |
84 | 173110 | xci.read(buf); |
85 | 173110 | nodeValue = new String(buf, "UTF-8"); |
86 | } | |
87 | } catch (IOException e) { | |
88 | 0 | if (log.isWarnEnabled()) { |
89 | 0 | log.warn("ignored exception", e); |
90 | } | |
91 | } | |
92 | } | |
93 | ||
94 | 163017 | public final void setNodeValue(String nodeValue) throws DOMException { |
95 | 163017 | checkLoaded(); |
96 | 163016 | checkReadOnly(); |
97 | 163017 | if (nodeValue == null) { |
98 | 0 | nodeValue = ""; |
99 | } | |
100 | 163017 | this.nodeValue = nodeValue; |
101 | 163017 | setDirty(); |
102 | } | |
103 | ||
104 | /** | |
105 | * Extracts a range of data from the node. | |
106 | * @param offset Start offset of substring to extract. | |
107 | * @param count The number of characters to extract. | |
108 | * @return The specified substring. If the sum of <code>offset</code> and | |
109 | * <code>count</code> exceeds the <code>length</code>, then all | |
110 | * characters to the end of the data are returned. | |
111 | * @exception DOMException | |
112 | * INDEX_SIZE_ERR: Raised if the specified offset is negative or greater | |
113 | * than the number of characters in <code>data</code>, or if the | |
114 | * specified <code>count</code> is negative. | |
115 | * <br>DOMSTRING_SIZE_ERR: Raised if the specified range of text does not | |
116 | * fit into a <code>DOMString</code>. | |
117 | */ | |
118 | 0 | public final String substringData(int offset, int count) throws DOMException { |
119 | 0 | try { |
120 | 0 | return nodeValue.substring(offset, offset + count); |
121 | } catch (Exception e) { | |
122 | 0 | throw EX_INDEX_SIZE; |
123 | } | |
124 | } | |
125 | ||
126 | /** | |
127 | * The character data of the node that implements this interface. The DOM | |
128 | * implementation may not put arbitrary limits on the amount of data that | |
129 | * may be stored in a <code>CharacterData</code> node. However, | |
130 | * implementation limits may mean that the entirety of a node's data may | |
131 | * not fit into a single <code>DOMString</code>. In such cases, the user | |
132 | * may call <code>substringData</code> to retrieve the data in | |
133 | * appropriately sized pieces. | |
134 | * @exception DOMException | |
135 | * NO_MODIFICATION_ALLOWED_ERR: Raised when the node is readonly. | |
136 | * @exception DOMException | |
137 | * DOMSTRING_SIZE_ERR: Raised when it would return more characters than | |
138 | * fit in a <code>DOMString</code> variable on the implementation | |
139 | * platform. | |
140 | */ | |
141 | 164534 | public final String getData() throws DOMException { |
142 | 164533 | return getNodeValue(); |
143 | } | |
144 | ||
145 | 163017 | public final void setData(String data) throws DOMException { |
146 | 163017 | setNodeValue(data); |
147 | } | |
148 | ||
149 | /** | |
150 | * Insert a string at the specified character offset. | |
151 | * @param offset The character offset at which to insert. | |
152 | * @param arg The <code>DOMString</code> to insert. | |
153 | * @exception DOMException | |
154 | * INDEX_SIZE_ERR: Raised if the specified offset is negative or greater | |
155 | * than the number of characters in <code>data</code>. | |
156 | * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly. | |
157 | */ | |
158 | 0 | public final void insertData(int offset, String arg) throws DOMException { |
159 | 0 | String value = getNodeValue(); |
160 | 0 | checkReadOnly(); |
161 | 0 | try { |
162 | 0 | setNodeValue(value.substring(0, offset) + arg + value.substring(offset + 1)); |
163 | } catch (Exception e) { | |
164 | 0 | throw EX_INDEX_SIZE; |
165 | } | |
166 | } | |
167 | ||
168 | /** | |
169 | * The number of characters that are available through <code>data</code> and | |
170 | * the <code>substringData</code> method below. This may have the value | |
171 | * zero, i.e., <code>CharacterData</code> nodes may be empty. | |
172 | */ | |
173 | 0 | public final int getLength() { |
174 | 0 | return getNodeValue().length(); |
175 | } | |
176 | ||
177 | /** | |
178 | * Replace the characters starting at the specified character offset with | |
179 | * the specified string. | |
180 | * @param offset The offset from which to start replacing. | |
181 | * @param count The number of characters to replace. If the sum of | |
182 | * <code>offset</code> and <code>count</code> exceeds <code>length</code> | |
183 | * , then all characters to the end of the data are replaced (i.e., the | |
184 | * effect is the same as a <code>remove</code> method call with the same | |
185 | * range, followed by an <code>append</code> method invocation). | |
186 | * @param arg The <code>DOMString</code> with which the range must be | |
187 | * replaced. | |
188 | * @exception DOMException | |
189 | * INDEX_SIZE_ERR: Raised if the specified offset is negative or greater | |
190 | * than the number of characters in <code>data</code>, or if the | |
191 | * specified <code>count</code> is negative. | |
192 | * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly. | |
193 | */ | |
194 | 0 | public final void replaceData(int offset, int count, String arg) throws DOMException { |
195 | 0 | String value = getNodeValue(); |
196 | 0 | checkReadOnly(); |
197 | 0 | try { |
198 | 0 | setNodeValue(value.substring(0, offset) + arg + value.substring(offset + count)); |
199 | } catch (Exception e) { | |
200 | 0 | throw EX_INDEX_SIZE; |
201 | } | |
202 | } | |
203 | ||
204 | /** | |
205 | * Append the string to the end of the character data of the node. Upon | |
206 | * success, <code>data</code> provides access to the concatenation of | |
207 | * <code>data</code> and the <code>DOMString</code> specified. | |
208 | * @param arg The <code>DOMString</code> to append. | |
209 | * @exception DOMException | |
210 | * NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly. | |
211 | */ | |
212 | 0 | public final void appendData(String arg) throws DOMException { |
213 | 0 | String value = getNodeValue(); |
214 | 0 | checkReadOnly(); |
215 | 0 | setNodeValue(value + arg); |
216 | } | |
217 | ||
218 | /** | |
219 | * Remove a range of characters from the node. Upon success, | |
220 | * <code>data</code> and <code>length</code> reflect the change. | |
221 | * @param offset The offset from which to remove characters. | |
222 | * @param count The number of characters to delete. If the sum of | |
223 | * <code>offset</code> and <code>count</code> exceeds <code>length</code> | |
224 | * then all characters from <code>offset</code> to the end of the data | |
225 | * are deleted. | |
226 | * @exception DOMException | |
227 | * INDEX_SIZE_ERR: Raised if the specified offset is negative or greater | |
228 | * than the number of characters in <code>data</code>, or if the | |
229 | * specified <code>count</code> is negative. | |
230 | * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly. | |
231 | */ | |
232 | 0 | public final void deleteData(int offset, int count) throws DOMException { |
233 | 0 | String value = getNodeValue(); |
234 | 0 | checkReadOnly(); |
235 | 0 | try { |
236 | 0 | setNodeValue(value.substring(0, offset) + value.substring(offset + count)); |
237 | } catch (Exception e) { | |
238 | 0 | throw EX_INDEX_SIZE; |
239 | } | |
240 | } | |
241 | } |
|