View Javadoc

1   /**********************************************
2    * Copyright (C) 2010 Lukas Laag
3    * This file is part of lib-gwt-svg.
4    * 
5    * libgwtsvg is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU Lesser General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    * 
10   * libgwtsvg is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU Lesser General Public License for more details.
14   * 
15   * You should have received a copy of the GNU Lesser General Public License
16   * along with libgwtsvg.  If not, see http://www.gnu.org/licenses/
17   **********************************************/
18  /*
19   * Copyright (c) 2004 World Wide Web Consortium,
20   *
21   * (Massachusetts Institute of Technology, European Research Consortium for
22   * Informatics and Mathematics, Keio University). All Rights Reserved. This
23   * work is distributed under the W3C(r) Software License [1] in the hope that
24   * it will be useful, but WITHOUT ANY WARRANTY; without even the implied
25   * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
26   *
27   * [1] http://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
28   */
29  package org.vectomatic.dom.svg;
30  
31  import org.vectomatic.dom.svg.utils.DOMHelper;
32  import org.w3c.dom.DOMException;
33  
34  import com.google.gwt.core.client.JavaScriptException;
35  import com.google.gwt.dom.client.Document;
36  import com.google.gwt.dom.client.Text;
37  
38  /**
39   * Wrapper class for DOM Text
40   * @author laaglu
41   */
42  public class OMText extends OMNode {
43  	/**
44  	 * Constructor
45  	 * @param data The text data
46  	 */
47  	public OMText(String data) {
48  		this(Document.get().createTextNode(data));
49  	}
50  	protected OMText(Text ot) {
51  		super(ot);
52  	}
53  	
54  	/**
55  	 * Returns the wrapped {@link com.google.gwt.dom.client.Text}
56  	 * @return the wrapped {@link com.google.gwt.dom.client.Text}
57  	 */
58  	public Text getText() {
59  		return ot.cast();
60  	}
61  
62  	// Implementation of the dom::CharacterData W3C IDL interface
63      /**
64       * The character data of the node that implements this interface. The DOM 
65       * implementation may not put arbitrary limits on the amount of data 
66       * that may be stored in a <code>CharacterData</code> node. However, 
67       * implementation limits may mean that the entirety of a node's data may 
68       * not fit into a single <code>DOMString</code>. In such cases, the user 
69       * may call <code>substringData</code> to retrieve the data in 
70       * appropriately sized pieces.
71       * @exception DOMException
72       *   DOMSTRING_SIZE_ERR: Raised when it would return more characters than 
73       *   fit in a <code>DOMString</code> variable on the implementation 
74       *   platform.
75       */
76  	public final String getData() throws JavaScriptException {
77  		return ((Text)ot.cast()).getData();
78  	};
79  
80      /**
81       * The character data of the node that implements this interface. The DOM 
82       * implementation may not put arbitrary limits on the amount of data 
83       * that may be stored in a <code>CharacterData</code> node. However, 
84       * implementation limits may mean that the entirety of a node's data may 
85       * not fit into a single <code>DOMString</code>. In such cases, the user 
86       * may call <code>substringData</code> to retrieve the data in 
87       * appropriately sized pieces.
88       * @exception DOMException
89       *   NO_MODIFICATION_ALLOWED_ERR: Raised when the node is readonly.
90       */
91  	public final void setData(String value) throws JavaScriptException {
92  		((Text)ot.cast()).setData(value);
93  	};
94  
95      /**
96       * The number of 16-bit units that are available through <code>data</code> 
97       * and the <code>substringData</code> method below. This may have the 
98       * value zero, i.e., <code>CharacterData</code> nodes may be empty.
99       */
100 	public final int getLength() {
101 		return ((Text)ot.cast()).getLength();
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 16-bit units 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 16-bit 
110      *   units to the end of the data are returned.
111      * @exception DOMException
112      *   INDEX_SIZE_ERR: Raised if the specified <code>offset</code> is 
113      *   negative or greater than the number of 16-bit units in 
114      *   <code>data</code>, or if the specified <code>count</code> is 
115      *   negative.
116      *   <br>DOMSTRING_SIZE_ERR: Raised if the specified range of text does 
117      *   not fit into a <code>DOMString</code>.
118      */
119 	public final String substringData(int offset, int count) throws JavaScriptException {
120 		return DOMHelper.substringData((Text)ot.cast(), offset, count);
121 	};
122 
123     /**
124      * Append the string to the end of the character data of the node. Upon 
125      * success, <code>data</code> provides access to the concatenation of 
126      * <code>data</code> and the <code>DOMString</code> specified.
127      * @param arg The <code>DOMString</code> to append.
128      * @exception DOMException
129      *   NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
130      */
131 	public final void appendData(String arg) throws JavaScriptException {
132 		DOMHelper.appendData((Text)ot.cast(), arg);
133 	};
134 
135     /**
136      * Insert a string at the specified 16-bit unit offset.
137      * @param offset The character offset at which to insert.
138      * @param arg The <code>DOMString</code> to insert.
139      * @exception DOMException
140      *   INDEX_SIZE_ERR: Raised if the specified <code>offset</code> is 
141      *   negative or greater than the number of 16-bit units in 
142      *   <code>data</code>.
143      *   <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
144      */
145 	public final void insertData(int offset, String arg) throws JavaScriptException {
146 		((Text)ot.cast()).insertData(offset, arg);
147 	};
148 
149     /**
150      * Remove a range of 16-bit units from the node. Upon success, 
151      * <code>data</code> and <code>length</code> reflect the change.
152      * @param offset The offset from which to start removing.
153      * @param count The number of 16-bit units to delete. If the sum of 
154      *   <code>offset</code> and <code>count</code> exceeds 
155      *   <code>length</code> then all 16-bit units from <code>offset</code> 
156      *   to the end of the data are deleted.
157      * @exception DOMException
158      *   INDEX_SIZE_ERR: Raised if the specified <code>offset</code> is 
159      *   negative or greater than the number of 16-bit units in 
160      *   <code>data</code>, or if the specified <code>count</code> is 
161      *   negative.
162      *   <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
163      */
164 	public final void deleteData(int offset, int count) throws JavaScriptException {
165 		((Text)ot.cast()).deleteData(offset, count);
166 	};
167 
168     /**
169      * Replace the characters starting at the specified 16-bit unit offset 
170      * with the specified string.
171      * @param offset The offset from which to start replacing.
172      * @param count The number of 16-bit units to replace. If the sum of 
173      *   <code>offset</code> and <code>count</code> exceeds 
174      *   <code>length</code>, then all 16-bit units to the end of the data 
175      *   are replaced; (i.e., the effect is the same as a <code>remove</code>
176      *    method call with the same range, followed by an <code>append</code>
177      *    method invocation).
178      * @param arg The <code>DOMString</code> with which the range must be 
179      *   replaced.
180      * @exception DOMException
181      *   INDEX_SIZE_ERR: Raised if the specified <code>offset</code> is 
182      *   negative or greater than the number of 16-bit units in 
183      *   <code>data</code>, or if the specified <code>count</code> is 
184      *   negative.
185      *   <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
186      */
187 	public final void replaceData(int offset, int count, String arg) throws JavaScriptException {
188 		((Text)ot.cast()).replaceData(offset, count, arg);
189 	};
190 
191 	// Implementation of the dom::Text W3C IDL interface
192     /**
193      * Breaks this node into two nodes at the specified <code>offset</code>, 
194      * keeping both in the tree as siblings. After being split, this node 
195      * will contain all the content up to the <code>offset</code> point. A 
196      * new node of the same type, which contains all the content at and 
197      * after the <code>offset</code> point, is returned. If the original 
198      * node had a parent node, the new node is inserted as the next sibling 
199      * of the original node. When the <code>offset</code> is equal to the 
200      * length of this node, the new node has no data.
201      * @param offset The 16-bit unit offset at which to split, starting from 
202      *   <code>0</code>.
203      * @return The new node, of the same type as this node.
204      * @exception DOMException
205      *   INDEX_SIZE_ERR: Raised if the specified offset is negative or greater 
206      *   than the number of 16-bit units in <code>data</code>.
207      *   <br>NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
208      */
209 	public final OMText splitText(int offset) throws JavaScriptException {
210 		return new OMText(((Text)ot.cast()).splitText(offset));
211 	};
212 
213 }