KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > message > TextImpl


1 /*
2  * Copyright 2002-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.jboss.axis.message;
18
19 import org.w3c.dom.DOMException JavaDoc;
20 import org.w3c.dom.Text JavaDoc;
21
22 /**
23  * A representation of a node whose value is text. A <CODE>
24  * Text</CODE> object may represent text that is content or text
25  * that is a comment.
26  *
27  * @author Davanum Srinivas (dims@yahoo.com)
28  * @author Heejune Ahn (cityboy@tmax.co.kr)
29  * @author Thomas Diesler (thomas.diesler@jboss.org)
30  */

31 public class TextImpl extends NodeImpl implements javax.xml.soap.Text JavaDoc
32 {
33
34    public TextImpl(org.w3c.dom.Node JavaDoc node)
35    {
36       super(node);
37    }
38
39    /**
40     * Retrieves whether this object represents a comment.
41     */

42    public boolean isComment()
43    {
44       String JavaDoc value = getNodeValue().trim();
45       return value.startsWith("<!--") && value.endsWith("-->");
46    }
47
48    public String JavaDoc getValue()
49    {
50       return getNodeValue();
51    }
52
53    public void setValue(String JavaDoc value)
54    {
55       setNodeValue(value);
56    }
57
58    /**
59     * Breaks this node into two nodes at the specified <code>offset</code>, keeping both in the tree as siblings.
60     * <p/>
61     * After being split, this node will contain all the content up to the <code>offset</code> point. A
62     * new node of the same type, which contains all the content at and after the <code>offset</code> point, is returned.
63     * If the original node had a parent node, the new node is inserted as the next sibling of the original node.
64     * When the <code>offset</code> is equal to the length of this node, the new node has no data.
65     *
66     * @param offset The 16-bit unit offset at which to split, starting from <code>0</code>.
67     * @return The new node, of the same type as this node.
68     * @throws DOMException INDEX_SIZE_ERR: Raised if the specified offset is negative or greater
69     * than the number of 16-bit units in <code>data</code>.
70     * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
71     */

72    public org.w3c.dom.Text JavaDoc splitText(int offset) throws DOMException JavaDoc
73    {
74
75       if (offset < 0 || offset > getNodeValue().length())
76          throw new IllegalArgumentException JavaDoc("Invalid offset [" + offset + "] for '" + getNodeValue() + "'");
77
78       String JavaDoc before = getNodeValue().substring(0, offset + 1);
79       setNodeValue(before);
80
81       String JavaDoc after = getNodeValue().substring(offset + 1);
82       TextImpl txtNode = new TextImpl(domNode.getOwnerDocument().createTextNode(after));
83
84       org.w3c.dom.Node JavaDoc parent = getParentNode();
85       if (parent != null)
86       {
87          org.w3c.dom.Node JavaDoc sibling = getNextSibling();
88          if (sibling == null)
89             parent.appendChild(txtNode);
90          else
91             parent.insertBefore(txtNode, sibling);
92       }
93
94       return txtNode;
95    }
96
97    // org.w3c.dom.CharacterData ***************************************************************************************
98

99    /**
100     * The number of 16-bit units that are available through <code>data</code>
101     * and the <code>substringData</code> method below. This may have the
102     * value zero, i.e., <code>CharacterData</code> nodes may be empty.
103     */

104    public int getLength()
105    {
106       return getNodeValue().length();
107    }
108
109    /**
110     * Remove a range of 16-bit units from the node. Upon success,
111     * <code>data</code> and <code>length</code> reflect the change.
112     *
113     * @param offset The offset from which to start removing.
114     * @param count The number of 16-bit units to delete. If the sum of
115     * <code>offset</code> and <code>count</code> exceeds
116     * <code>length</code> then all 16-bit units from <code>offset</code>
117     * to the end of the data are deleted.
118     * @throws org.w3c.dom.DOMException INDEX_SIZE_ERR: Raised if the specified <code>offset</code> is
119     * negative or greater than the number of 16-bit units in
120     * <code>data</code>, or if the specified <code>count</code> is
121     * negative.
122     * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
123     */

124    public void deleteData(int offset, int count) throws DOMException JavaDoc
125    {
126       String JavaDoc value = getNodeValue().substring(0, offset + 1);
127       setNodeValue(value);
128    }
129
130    /**
131     * The character data of the node that implements this interface. The DOM
132     * implementation may not put arbitrary limits on the amount of data
133     * that may be stored in a <code>CharacterData</code> node. However,
134     * implementation limits may mean that the entirety of a node's data may
135     * not fit into a single <code>DOMString</code>. In such cases, the user
136     * may call <code>substringData</code> to retrieve the data in
137     * appropriately sized pieces.
138     *
139     * @throws org.w3c.dom.DOMException NO_MODIFICATION_ALLOWED_ERR: Raised when the node is readonly.
140     * @throws org.w3c.dom.DOMException DOMSTRING_SIZE_ERR: Raised when it would return more characters than
141     * fit in a <code>DOMString</code> variable on the implementation
142     * platform.
143     */

144    public String JavaDoc getData() throws DOMException JavaDoc
145    {
146       return getNodeValue();
147    }
148
149    /**
150     * Extracts a range of data from the node.
151     *
152     * @param offset Start offset of substring to extract.
153     * @param count The number of 16-bit units to extract.
154     * @return The specified substring. If the sum of <code>offset</code> and
155     * <code>count</code> exceeds the <code>length</code>, then all 16-bit
156     * units to the end of the data are returned.
157     * @throws org.w3c.dom.DOMException INDEX_SIZE_ERR: Raised if the specified <code>offset</code> is
158     * negative or greater than the number of 16-bit units in
159     * <code>data</code>, or if the specified <code>count</code> is
160     * negative.
161     * <br>DOMSTRING_SIZE_ERR: Raised if the specified range of text does
162     * not fit into a <code>DOMString</code>.
163     */

164    public String JavaDoc substringData(int offset, int count) throws DOMException JavaDoc
165    {
166       return getNodeValue().substring(offset, offset + count);
167    }
168
169    /**
170     * Replace the characters starting at the specified 16-bit unit offset
171     * with the specified string.
172     *
173     * @param offset The offset from which to start replacing.
174     * @param count The number of 16-bit units to replace. If the sum of
175     * <code>offset</code> and <code>count</code> exceeds
176     * <code>length</code>, then all 16-bit units to the end of the data
177     * are replaced; (i.e., the effect is the same as a <code>remove</code>
178     * method call with the same range, followed by an <code>append</code>
179     * method invocation).
180     * @param arg The <code>DOMString</code> with which the range must be
181     * replaced.
182     * @throws org.w3c.dom.DOMException INDEX_SIZE_ERR: Raised if the specified <code>offset</code> is
183     * negative or greater than the number of 16-bit units in
184     * <code>data</code>, or if the specified <code>count</code> is
185     * negative.
186     * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
187     */

188    public void replaceData(int offset, int count, String JavaDoc arg) throws DOMException JavaDoc
189    {
190       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(getNodeValue());
191       buffer.replace(offset, offset + count, arg);
192       setNodeValue(buffer.toString());
193    }
194
195    /**
196     * Insert a string at the specified 16-bit unit offset.
197     *
198     * @param offset The character offset at which to insert.
199     * @param arg The <code>DOMString</code> to insert.
200     * @throws org.w3c.dom.DOMException INDEX_SIZE_ERR: Raised if the specified <code>offset</code> is
201     * negative or greater than the number of 16-bit units in
202     * <code>data</code>.
203     * <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
204     */

205    public void insertData(int offset, String JavaDoc arg) throws DOMException JavaDoc
206    {
207       StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(getNodeValue());
208       buffer.insert(offset, arg);
209       setNodeValue(buffer.toString());
210    }
211
212    /**
213     * Append the string to the end of the character data of the node. Upon
214     * success, <code>data</code> provides access to the concatenation of
215     * <code>data</code> and the <code>DOMString</code> specified.
216     *
217     * @param arg The <code>DOMString</code> to append.
218     * @throws org.w3c.dom.DOMException NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
219     */

220    public void appendData(String JavaDoc arg) throws DOMException JavaDoc
221    {
222       setNodeValue(getNodeValue() + arg);
223    }
224
225    /**
226     * The character data of the node that implements this interface. The DOM
227     * implementation may not put arbitrary limits on the amount of data
228     * that may be stored in a <code>CharacterData</code> node. However,
229     * implementation limits may mean that the entirety of a node's data may
230     * not fit into a single <code>DOMString</code>. In such cases, the user
231     * may call <code>substringData</code> to retrieve the data in
232     * appropriately sized pieces.
233     *
234     * @throws org.w3c.dom.DOMException NO_MODIFICATION_ALLOWED_ERR: Raised when the node is readonly.
235     * @throws org.w3c.dom.DOMException DOMSTRING_SIZE_ERR: Raised when it would return more characters than
236     * fit in a <code>DOMString</code> variable on the implementation
237     * platform.
238     */

239    public void setData(String JavaDoc data) throws DOMException JavaDoc
240    {
241       setNodeValue(data);
242    }
243    
244    // Stubbed out org.w3c.dom.Text methods **************************
245

246    /**
247     * TODO - complete the implementation
248     *
249     * Returns whether this text node contains <a HREF='http://www.w3.org/TR/2004/REC-xml-infoset-20040204#infoitem.character'>
250     * element content whitespace</a>, often abusively called "ignorable whitespace". The text node is
251     * determined to contain whitespace in element content during the load
252     * of the document or if validation occurs while using
253     * <code>Document.normalizeDocument()</code>.
254     * @since DOM Level 3
255     */

256    public boolean isElementContentWhitespace()
257    {
258       return false;
259    }
260
261    /**
262     * TODO - complete the implementation
263     *
264     * Returns all text of <code>Text</code> nodes logically-adjacent text
265     * nodes to this node, concatenated in document order.
266     * <br>For instance, in the example below <code>wholeText</code> on the
267     * <code>Text</code> node that contains "bar" returns "barfoo", while on
268     * the <code>Text</code> node that contains "foo" it returns "barfoo".
269     * @since DOM Level 3
270     */

271    public String JavaDoc getWholeText()
272    {
273       return null;
274    }
275
276    /**
277     * TODO - complete the implementation
278     *
279     * Replaces the text of the current node and all logically-adjacent text
280     * nodes with the specified text. All logically-adjacent text nodes are
281     * removed including the current node unless it was the recipient of the
282     * replacement text.
283     * <br>This method returns the node which received the replacement text.
284     * The returned node is:
285     * <ul>
286     * <li><code>null</code>, when the replacement text is
287     * the empty string;
288     * </li>
289     * <li>the current node, except when the current node is
290     * read-only;
291     * </li>
292     * <li> a new <code>Text</code> node of the same type (
293     * <code>Text</code> or <code>CDATASection</code>) as the current node
294     * inserted at the location of the replacement.
295     * </li>
296     * </ul>
297     * <br>For instance, in the above example calling
298     * <code>replaceWholeText</code> on the <code>Text</code> node that
299     * contains "bar" with "yo" in argument results in the following:
300     * <br>Where the nodes to be removed are read-only descendants of an
301     * <code>EntityReference</code>, the <code>EntityReference</code> must
302     * be removed instead of the read-only nodes. If any
303     * <code>EntityReference</code> to be removed has descendants that are
304     * not <code>EntityReference</code>, <code>Text</code>, or
305     * <code>CDATASection</code> nodes, the <code>replaceWholeText</code>
306     * method must fail before performing any modification of the document,
307     * raising a <code>DOMException</code> with the code
308     * <code>NO_MODIFICATION_ALLOWED_ERR</code>.
309     * <br>For instance, in the example below calling
310     * <code>replaceWholeText</code> on the <code>Text</code> node that
311     * contains "bar" fails, because the <code>EntityReference</code> node
312     * "ent" contains an <code>Element</code> node which cannot be removed.
313     * @param content The content of the replacing <code>Text</code> node.
314     * @return The <code>Text</code> node created with the specified content.
315     * @exception DOMException
316     * NO_MODIFICATION_ALLOWED_ERR: Raised if one of the <code>Text</code>
317     * nodes being replaced is readonly.
318     * @since DOM Level 3
319     */

320    public Text JavaDoc replaceWholeText(String JavaDoc content)
321            throws DOMException JavaDoc
322    {
323       return null;
324    }
325
326 }
327
Popular Tags