KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > message > Text


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.apache.axis.message;
18
19 import org.apache.axis.InternalException;
20 import org.w3c.dom.DOMException 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  */

30 public class Text extends NodeImpl implements javax.xml.soap.Text JavaDoc {
31
32     public Text(org.w3c.dom.CharacterData JavaDoc data) {
33         if ( data == null )
34         {
35            throw new IllegalArgumentException JavaDoc( "Text value may not be null." );
36         }
37         textRep = data;
38     }
39
40     public Text(String JavaDoc s) {
41         try {
42             org.w3c.dom.Document JavaDoc doc = org.apache.axis.utils.XMLUtils.newDocument();
43             textRep = doc.createTextNode(s);
44         } catch (javax.xml.parsers.ParserConfigurationException JavaDoc e) {
45             throw new InternalException(e);
46         }
47     }
48
49     public Text()
50     {
51         this((String JavaDoc)null);
52     }
53
54     /**
55      * Retrieves whether this <CODE>Text</CODE> object
56      * represents a comment.
57      * @return <CODE>true</CODE> if this <CODE>Text</CODE> object is
58      * a comment; <CODE>false</CODE> otherwise
59      */

60     public boolean isComment() {
61         String JavaDoc temp = textRep.getNodeValue().trim();
62         if(temp.startsWith("<!--") && temp.endsWith("-->"))
63             return true;
64         return false;
65     }
66
67     /**
68      * Implementation of DOM TEXT Interface
69      * *************************************************************
70      */

71
72     // Overriding the MessageElement Method, where it throws exeptions.
73
public String JavaDoc getNodeValue() throws DOMException JavaDoc {
74         return textRep.getNodeValue();
75     }
76
77     // Overriding the MessageElement Method, where it throws exeptions.
78
public void setNodeValue(String JavaDoc nodeValue) throws DOMException JavaDoc{
79         setDirty(true);
80         textRep.setNodeValue(nodeValue);
81     }
82
83     /**
84      * Use the textRep, and convert it to org.apache.axis.Text
85      * in order to keep the Axis SOAP strcture after operation
86      *
87      * This work would be easier if constructor, Text(org.w3c.dom.Text)
88      * is defined
89      *
90      * @since SAAJ 1.2
91      * @param offset
92      * @return
93      * @throws DOMException
94      */

95     public org.w3c.dom.Text JavaDoc splitText(int offset) throws DOMException JavaDoc
96     {
97         int length = textRep.getLength();
98         // take the first part, and save the second part for new Text
99
// length check and exception will be thrown here, no need to duplicated check
100
String JavaDoc tailData = textRep.substringData(offset,length);
101         textRep.deleteData(offset,length);
102
103         // insert the first part again as a new node
104
Text tailText = new Text(tailData);
105         org.w3c.dom.Node JavaDoc myParent = getParentNode();
106         if(myParent != null){
107             org.w3c.dom.NodeList JavaDoc brothers = myParent.getChildNodes();
108             for(int i = 0;i < brothers.getLength(); i++){
109                 if(brothers.item(i).equals(this)){
110                     myParent.insertBefore(tailText, this);
111                     return tailText;
112                 }
113             }
114         }
115         return tailText;
116     }
117
118     /**
119      * @since SAAJ 1.2
120      */

121     public String JavaDoc getData() throws DOMException JavaDoc {
122         return textRep.getData();
123     }
124
125     /**
126      * @since SAAJ 1.2
127      */

128     public void setData(String JavaDoc data) throws DOMException JavaDoc {
129         textRep.setData(data);
130     }
131
132     /**
133      * @since SAAJ 1.2
134      *
135      * @return
136      */

137     public int getLength(){
138         return textRep.getLength();
139     }
140
141     /**
142      * @since SAAJ 1.2
143      * @param offset
144      * @param count
145      * @return
146      * @throws DOMException
147      */

148     public String JavaDoc substringData(int offset, int count)throws DOMException JavaDoc {
149         return textRep.substringData(offset,count);
150     }
151
152     /**
153      *
154      * @since SAAJ 1.2
155      * @param arg
156      * @throws DOMException
157      */

158     public void appendData(String JavaDoc arg) throws DOMException JavaDoc {
159         textRep.appendData(arg);
160     }
161
162     /**
163      * @since SAAJ 1.2
164      * @param offset
165      * @param arg
166      * @throws DOMException
167      */

168     public void insertData(int offset, String JavaDoc arg)throws DOMException JavaDoc {
169         textRep.insertData(offset, arg);
170     }
171
172     /**
173      * @since SAAJ 1.2
174      * @param offset
175      * @param count
176      * @param arg
177      * @throws DOMException
178      */

179     public void replaceData(int offset, int count, String JavaDoc arg) throws DOMException JavaDoc {
180         textRep.replaceData(offset, count, arg);
181     }
182
183     /**
184      * @since SAAJ 1.2
185      * @param offset
186      * @param count
187      * @throws DOMException
188      */

189     public void deleteData(int offset, int count) throws DOMException JavaDoc {
190         textRep.deleteData(offset, count);
191     }
192
193     public String JavaDoc toString()
194     {
195         return textRep.getNodeValue();
196     }
197
198     public boolean equals( Object JavaDoc obj )
199     {
200         if ( !( obj instanceof Text ) )
201         {
202             return false;
203         }
204         return this == obj || hashCode() == obj.hashCode();
205     }
206
207     public int hashCode()
208     {
209         if ( textRep == null )
210         {
211            return -1;
212         }
213         return ( textRep.getData() != null ? textRep.getData().hashCode() : 0 );
214     }
215
216 }
217
Popular Tags