KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > xml > client > impl > TextImpl


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.xml.client.impl;
17
18 import com.google.gwt.core.client.JavaScriptException;
19 import com.google.gwt.core.client.JavaScriptObject;
20 import com.google.gwt.xml.client.DOMException;
21 import com.google.gwt.xml.client.Text;
22
23 /**
24  * This class is the implementation of the XML DOM Text interface.
25  */

26 class TextImpl extends CharacterDataImpl implements Text {
27
28   protected TextImpl(JavaScriptObject o) {
29     super(o);
30   }
31
32   /**
33    * This function delegates to the native method <code>splitText</code> in
34    * XMLParserImpl.
35    */

36   public Text splitText(int offset) {
37     try {
38       return (Text) NodeImpl.build(XMLParserImpl.splitText(this.getJsObject(),
39         offset));
40     } catch (JavaScriptException e) {
41       throw new DOMNodeException(DOMException.INVALID_MODIFICATION_ERR, e, this);
42     }
43   }
44
45   public String JavaDoc toString() {
46     StringBuffer JavaDoc b = new StringBuffer JavaDoc();
47     String JavaDoc[] x = getData().split("(?=[;&<>\'\"])", -1);
48     for (int i = 0; i < x.length; i++) {
49       if (x[i].startsWith(";")) {
50         b.append("&semi;");
51         b.append(x[i].substring(1));
52       } else if (x[i].startsWith("&")) {
53         b.append("&amp;");
54         b.append(x[i].substring(1));
55       } else if (x[i].startsWith("\"")) {
56         b.append("&quot;");
57         b.append(x[i].substring(1));
58       } else if (x[i].startsWith("'")) {
59         b.append("&apos;");
60         b.append(x[i].substring(1));
61       } else if (x[i].startsWith("<")) {
62         b.append("&lt;");
63         b.append(x[i].substring(1));
64       } else if (x[i].startsWith(">")) {
65         b.append("&gt;");
66         b.append(x[i].substring(1));
67       } else {
68         b.append(x[i]);
69       }
70     }
71     return b.toString();
72   }
73 }
74
Popular Tags