1
/* Copyright 2002-2005 Elliotte Rusty Harold
3
This library is free software; you can redistribute it and/or modify
4
it under the terms of version 2.1 of the GNU Lesser General Public
5
License as published by the Free Software Foundation.
7
This library is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU Lesser General Public License for more details.
12
You should have received a copy of the GNU Lesser General Public
13
License along with this library; if not, write to the
14
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15
Boston, MA 02111-1307 USA
17
You can contact Elliotte Rusty Harold by sending e-mail to
18
elharo@metalab.unc.edu. Please include the word "XOM" in the
19
subject line. The XOM home page is located at http://www.xom.nu/
26
* This class represents a run of text.
27
* CDATA sections are not treated differently than
28
* normal text. <code>Text</code> objects may be adjacent to other
29
* <code>Text</code> objects.
32
* @author Elliotte Rusty Harold
36
public class Text extends Node {
44
* This constructor creates a new <code>Text</code> object.
45
* The data is checked for legality according to XML 1.0 rules.
46
* Characters that can be serialized by escaping them
47
* such as < and & are allowed. However, characters
48
* such as the form feed, null, vertical tab,
49
* unmatched halves of surrogate pairs,
50
* and 0xFFFE and 0xFFFF are not allowed.
53
* @param data the initial text of the object
55
* @throws IllegalCharacterDataException if data contains any
56
* characters which are illegal in well-formed XML 1.0 such as
57
* null, vertical tab, or unmatched halves of surrogate pairs
59
public Text(String data) {
66
* Creates a copy of the specified <code>Text</code> object.
69
* @param text the <code>Text</code> object to copy
71
public Text(Text text) {
72
// I'm relying here on the data being immutable.
73
// If this ever changes, e.g. by adding an append method,
74
// this method needs to change too.
75
this.data = text.data;
82
static Text build(String data) {
83
Text result = new Text();
91
* Sets the content of the <code>Text</code> object
92
* to the specified data. The data is checked for
93
* legality according to XML 1.0 rules. Characters that
94
* can be serialized such as < and & are allowed.
95
* However, characters such as the form feed, null,
96
* vertical tab, unmatched halves of surrogate pairs,
97
* and 0xFFFE and 0xFFFF are not allowed.
100
* @param data the text to install in the object
102
* @throws IllegalCharacterDataException if data contains any
103
* characters which are illegal in well-formed XML 1.0 such as
104
* null, vertical tab, or unmatched halves of surrogate pairs
106
public void setValue(String data) {
111
private void _setValue(String data) {
113
if (data == null) this.data = "";
115
// Interning all strings may be smaller than using UTF-8
116
// but what does this do to performance?
117
// data = data.intern();
118
Verifier.checkPCDATA(data);
126
* Returns the XPath 1.0 string-value of this <code>Text</code>
127
* node. The XPath string-value of a text node is the same as
128
* the text of the node.
131
* @return the content of the node
133
public final String getValue() {
140
* Throws <code>IndexOutOfBoundsException</code> because
141
* texts do not have children.
144
* @return never returns because texts do not have children;
145
* always throws an exception.
147
* @param position the index of the child node to return
149
* @throws IndexOutOfBoundsException because texts
150
* do not have children
152
public final Node getChild(int position) {
153
throw new IndexOutOfBoundsException(
154
"LeafNodes do not have children");
160
* Returns 0 because texts do not have children.
165
public final int getChildCount() {
172
* Returns a deep copy of this <code>Text</code> with no parent,
173
* that can be added to this document or a different one.
176
* @return a deep copy of this text node with no parent
180
if (isCDATASection()) {
181
return new CDATASection(this);
184
return new Text(this);
192
* Returns a string containing the XML serialization of this text
193
* node. Unlike <code>getValue</code>, this method escapes
194
* characters such as & and < using entity references such
195
* as <code>&amp;</code> and <code>&lt;</code>.
196
* It escapes the carriage return (\r) as <code>&#x0D;</code>.
199
* @return the string form of this text node
201
public final String toXML() {
213
* Returns a <code>String</code>
214
* representation of this <code>Text</code> suitable for
215
* debugging and diagnosis. This is <em>not</em>
216
* the XML representation of this <code>Text</code> node.
219
* @return a non-XML string representation of this node
221
public final String toString() {
223
return "[" + getClass().getName() + ": "
224
+ escapeLineBreaksAndTruncate(getValue()) + "]";
229
static String escapeLineBreaksAndTruncate(String s) {
231
int length = s.length();
232
boolean tooLong = length > 40;
235
s = s.substring(0, 35);
238
StringBuffer result = new StringBuffer(length);
239
for (int i = 0; i < length; i++) {
240
char c = s.charAt(i);
243
result.append("\\n");
246
result.append("\\r");
249
result.append("\\t");
255
if (tooLong) result.append("...");
257
return result.toString();
262
boolean isCDATASection() {
268
return this.data.length() == 0;
271
String escapeText() {
273
String s = getValue();
274
int length = s.length();
275
// Give the string buffer enough room for a couple of escaped characters
276
StringBuffer result = new StringBuffer(length+12);
277
for (int i = 0; i < length; i++) {
278
char c = s.charAt(i);
281
result.append("
");
356
result.append("&");
422
result.append("<");
428
result.append(">");
435
return result.toString();
b'\\ No newline at end of file'