001: package com.rimfaxe.xml.xmlreader;
002:
003: import java.io.IOException;
004: import java.io.Writer;
005: import java.util.Enumeration;
006:
007: /**
008: * A section of text in an element. In XML this can be either CDATA or not:
009: * the DOM model does not distinguish between the two encodings.
010:
011: <blockquote><small> Copyright (C) 2002 Hewlett-Packard Company.
012: This file is part of Sparta, an XML Parser, DOM, and XPath library.
013: This library is free software; you can redistribute it and/or
014: modify it under the terms of the GNU Lesser General Public License
015: as published by the Free Software Foundation; either version 2.1 of
016: the License, or (at your option) any later version. This library
017: is distributed in the hope that it will be useful, but WITHOUT ANY
018: WARRANTY; without even the implied warranty of MERCHANTABILITY or
019: FITNESS FOR A PARTICULAR PURPOSE. </small></blockquote>
020: @see <a "href="doc-files/LGPL.txt">GNU Lesser General Public License</a>
021: @version $Date: 2002/12/13 23:09:24 $ $Revision: 1.3 $
022: @author Eamonn O'Brien-Strain
023: * @see org.w3c.dom.Text
024: */
025:
026: public class Text extends Node {
027:
028: /**
029: * Create with an initial character as its data. This can be added to later.
030: */
031: public Text(String data) {
032: text_ = new StringBuffer(data);
033: }
034:
035: /**
036: * Create with an initial character as its data. This can be added to later.
037: */
038: public Text(char ch) {
039: text_ = new StringBuffer();
040: text_.append(ch);
041: }
042:
043: /** Deep clone: returns Text node with copy of this ones data. */
044: public Object clone() {
045: Text result = (Text) super .clone();
046: result.text_ = new StringBuffer(text_.toString());
047: return result;
048: }
049:
050: public void appendData(String s) {
051: text_.append(s);
052: notifyObservers();
053: }
054:
055: public void appendData(char ch) {
056: text_.append(ch);
057: notifyObservers();
058: }
059:
060: /**
061: * @param cbuf characters, some of which are to be appended to the text data
062: * @param offset the first index in cbuf to copy
063: * @param len the number of characters to copy
064: */
065: public void appendData(char[] cbuf, int offset, int len) {
066: text_.append(cbuf, offset, len);
067: notifyObservers();
068: }
069:
070: public String getData() {
071: return text_.toString();
072: }
073:
074: public void setData(String data) {
075: text_ = new StringBuffer(data);
076: notifyObservers();
077: }
078:
079: void toXml(Writer writer) throws IOException {
080: //System.out.println("Text.toXml "+text_.toString());
081: String s = text_.toString();
082: if (s.length() < 50)
083: //short
084: htmlEncode(writer, s);
085: else {
086: //long
087: writer.write("<![CDATA[");
088: writer.write(s);
089: writer.write("]]>");
090: }
091: }
092:
093: void toString(Writer writer) throws IOException {
094: writer.write(text_.toString());
095: }
096:
097: /** Not implemented. */
098: public Enumeration xpathSelectElements(String xpath) {
099: throw new Error("Sorry, not implemented");
100: }
101:
102: /** Not implemented */
103: public Enumeration xpathSelectStrings(String xpath) {
104: throw new Error("Sorry, not implemented");
105: }
106:
107: /** Not implemented. */
108: public Element xpathSelectElement(String xpath) {
109: throw new Error("Sorry, not implemented");
110: }
111:
112: /** Not implemented. */
113: public String xpathSelectString(String xpath) {
114: throw new Error("Sorry, not implemented");
115: }
116:
117: /** Text nodes can be equal even if they are in different documents,
118: * different parents, different siblings, or different annotations.
119: * They are equal IFF their string data is equal
120: * */
121: public boolean equals(Object thatO) {
122:
123: //Do cheap tests first
124: if (this == thatO)
125: return true;
126: if (!(thatO instanceof Text))
127: return false;
128: Text that = (Text) thatO;
129: return this .text_.toString().equals(that.text_.toString());
130: }
131:
132: private StringBuffer text_;
133: }
134:
135: // $Log: Text.java,v $
136: // Revision 1.3 2002/12/13 23:09:24 eobrain
137: // Fix javadoc.
138: //
139: // Revision 1.2 2002/11/06 02:57:59 eobrain
140: // Organize imputs to removed unused imports. Remove some unused local variables.
141: //
142: // Revision 1.1.1.1 2002/08/19 05:03:53 eobrain
143: // import from HP Labs internal CVS
144: //
145: // Revision 1.11 2002/08/18 04:22:24 eob
146: // Sparta no longer throws XPathException -- it throws ParseException
147: // instead. Remove deprecated constructors.
148: //
149: // Revision 1.10 2002/08/15 21:23:00 eob
150: // Constructor no longer needs document.
151: //
152: // Revision 1.9 2002/08/15 05:09:22 eob
153: // Notify observers. CDATA
154: //
155: // Revision 1.8 2002/08/01 23:29:17 sermarti
156: // Much faster Sparta parsing.
157: // Has debug features enabled by default. Currently toggled
158: // in ParseCharStream.java and recompiled.
159: //
160: // Revision 1.7 2002/06/14 19:37:51 eob
161: // Make toString of Node do the same as in XSLT -- recursive
162: // concatenation of all text in text nodes.
163: //
164: // Revision 1.6 2002/05/23 21:32:05 eob
165: // Add appendData method for efficiency. This optimization was done
166: // because of what performance profiling showed.
167: //
168: // Revision 1.5 2002/05/11 00:10:53 eob
169: // Add stubs for xpathSelect* methods.
170: //
171: // Revision 1.4 2002/05/10 21:38:06 eob
172: // equals added
173: //
174: // Revision 1.3 2002/03/28 01:23:18 jrowson
175: // fixed bugs related to client side caching
176: //
177: // Revision 1.2 2002/02/23 02:07:11 eob
178: // Add clone method. Tweak toXml API.
179: //
180: // Revision 1.1 2002/01/05 07:31:50 eob
181: // initial
|