KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis2 > saaj > TextImpl


1 /*
2  * Copyright 2004,2005 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 package org.apache.axis2.saaj;
17
18 import org.apache.axis2.om.OMElement;
19 import org.apache.axis2.om.OMText;
20 import org.w3c.dom.DOMException JavaDoc;
21
22 import javax.xml.soap.SOAPElement JavaDoc;
23 import javax.xml.soap.SOAPException JavaDoc;
24 import javax.xml.soap.Text JavaDoc;
25
26 /**
27  * @author Ashutosh Shahi ashutosh.shahi@gmail.com
28  *
29  * TODO To change the template for this generated type comment go to
30  * Window - Preferences - Java - Code Style - Code Templates
31  */

32 public class TextImpl extends NodeImpl implements Text {
33
34     private OMText omText;
35
36     public TextImpl(String JavaDoc s){
37         //super();
38
omNode = omText = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createText(s);
39     }
40
41     public TextImpl(SOAPElementImpl parent, String JavaDoc s) throws SOAPException JavaDoc{
42         //super();
43
//super.setParentElement(parent);
44
OMElement par = parent.getOMElement();
45         omNode = omText = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createText(par, s);
46     }
47
48     public TextImpl(org.w3c.dom.CharacterData JavaDoc data){
49         if ( data == null ){
50            throw new IllegalArgumentException JavaDoc( "Text value may not be null." );
51         }
52         omText = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createText(data.getData());
53     }
54
55     /*Overridden Method*/
56     public SOAPElement JavaDoc getParentElement() {
57         OMElement parent = (OMElement)omText.getParent();
58         return new SOAPElementImpl(parent);
59     }
60
61     /*Overridden Method*/
62     public void setParentElement(SOAPElement JavaDoc parent) throws SOAPException JavaDoc {
63         OMElement omParent = ((SOAPElementImpl)parent).getOMElement();
64         omText.setParent(omParent);
65     }
66
67     /*Overridden Method*/
68     public String JavaDoc getValue() {
69         return omText.getText();
70     }
71
72     public boolean isComment() {
73
74         String JavaDoc temp = omText.getText();
75         if(temp.startsWith("<!--") && temp.endsWith("-->"))
76             return true;
77         return false;
78     }
79
80     /**
81      * Implementation of DOM TEXT Interface
82      * *************************************************************
83      */

84
85
86     public org.w3c.dom.Text JavaDoc splitText(int offset) throws DOMException JavaDoc {
87
88         String JavaDoc temp = omText.getText();
89         int length = temp.length();
90         String JavaDoc tailData = temp.substring(offset);
91         temp = temp.substring(0, offset);
92         omText = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createText(temp);
93         TextImpl tailText = new TextImpl(tailData);
94         org.w3c.dom.Node JavaDoc myParent = getParentNode();
95         if(myParent != null){
96             org.w3c.dom.NodeList JavaDoc brothers = myParent.getChildNodes();
97             for(int i = 0;i < brothers.getLength(); i++){
98                 if(brothers.item(i).equals(this)){
99                     myParent.insertBefore(tailText, this);
100                     return tailText;
101                 }
102             }
103         }
104         return tailText;
105     }
106
107
108     public int getLength() {
109
110         return omText.getText().length();
111     }
112
113
114     public void deleteData(int offset, int count) throws DOMException JavaDoc {
115
116         String JavaDoc temp = omText.getText();
117         StringBuffer JavaDoc subString = new StringBuffer JavaDoc(temp.substring(0,offset));
118         if(temp.length() - offset >= count - offset)
119             subString = subString.append(temp.substring(offset+count));
120         temp = subString.toString();
121         omText = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createText(temp);
122
123     }
124
125
126     public String JavaDoc getData() throws DOMException JavaDoc {
127
128         return omText.getText();
129     }
130
131
132     public String JavaDoc substringData(int offset, int count) throws DOMException JavaDoc {
133
134         String JavaDoc temp = omText.getText();
135         if(temp.length() - offset >= count - offset)
136             return temp.substring(offset, count);
137         else
138             return temp.substring(offset);
139     }
140
141
142     public void replaceData(int offset, int count, String JavaDoc arg)
143             throws DOMException JavaDoc {
144
145         deleteData(offset, count);
146         StringBuffer JavaDoc temp = new StringBuffer JavaDoc(omText.getText());
147         temp.append(arg);
148         omText = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createText(temp.toString());
149     }
150
151
152     public void insertData(int offset, String JavaDoc arg) throws DOMException JavaDoc {
153
154         if(offset < 0 || offset > omText.getText().length())
155             throw new DOMException JavaDoc(DOMException.INDEX_SIZE_ERR, "");
156         StringBuffer JavaDoc temp = new StringBuffer JavaDoc(omText.getText().substring(0, offset));
157         temp = temp.append(arg);
158         omText = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createText(temp.toString());
159     }
160
161
162     public void appendData(String JavaDoc arg) throws DOMException JavaDoc {
163
164         StringBuffer JavaDoc temp = new StringBuffer JavaDoc(omText.getText());
165         temp = temp.append(arg);
166         omText = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createText(temp.toString());
167
168     }
169
170
171     public void setData(String JavaDoc arg) throws DOMException JavaDoc {
172
173         omText = org.apache.axis2.om.OMAbstractFactory.getOMFactory().createText(arg);
174     }
175
176 }
177
Popular Tags