KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > expr > TextFragmentValue


1 package com.icl.saxon.expr;
2 import com.icl.saxon.*;
3 import com.icl.saxon.om.*;
4 import com.icl.saxon.tinytree.TinyBuilder;
5 import com.icl.saxon.output.*;
6 import javax.xml.transform.TransformerException JavaDoc;
7
8
9 /**
10 * This class represents a Value of type result tree fragment, specifically,
11 * an RTF whose root owns a single text node. <BR>
12 */

13
14 public final class TextFragmentValue extends SingletonNodeSet {
15
16     private String JavaDoc text;
17     private String JavaDoc baseURI;
18     private Controller controller;
19
20     /**
21     * Constructor: create a result tree fragment containing a single text node
22     * @param value: a String containing the value
23     */

24
25     public TextFragmentValue(String JavaDoc value, String JavaDoc systemId, Controller controller) {
26         this.text = value;
27         this.node = null;
28         this.baseURI = systemId;
29         this.controller = controller;
30         generalUseAllowed = false;
31     }
32
33     /**
34     * Convert the result tree fragment to a string.
35     */

36
37     public String JavaDoc asString() {
38         return text;
39     }
40
41     /**
42     * Evaluate an expression as a String and write the result to the
43     * specified outputter.<br>
44     * @param out The required outputter
45     * @param context The context in which the expression is to be evaluated
46     */

47
48     public void outputStringValue(Outputter out, Context context) throws TransformerException JavaDoc {
49         out.writeContent(text);
50     }
51
52     /**
53     * Convert the result tree fragment to a number
54     */

55
56     public double asNumber() {
57         return Value.stringToNumber(text);
58     }
59
60     /**
61     * Convert the result tree fragment to a boolean
62     */

63
64     public boolean asBoolean() {
65         return true;
66     }
67
68     /**
69     * Count the nodes in the node-set.
70     */

71
72     public int getCount() {
73         return 1;
74     }
75   
76     /**
77     * Simplify the expression
78     */

79
80     public Expression simplify() {
81         // overrides method on superclass
82
return this;
83     }
84
85     /**
86     * Get the first node in the nodeset (in document order)
87     * @return the first node
88     */

89
90     public NodeInfo getFirst() {
91         return getRootNode();
92     }
93
94     /**
95     * Return an enumeration of this nodeset value.
96     */

97
98     public NodeEnumeration enumerate() throws XPathException {
99         if (!generalUseAllowed) {
100             throw new XPathException("Cannot process a result tree fragment as a node-set under XSLT 1.0");
101         }
102         return new SingletonEnumeration(getRootNode());
103     }
104
105     /**
106     * Test whether a nodeset "equals" another Value
107     */

108
109     public boolean equals(Value other) throws XPathException {
110         if (other instanceof StringValue) { // short cut for common case
111
return text.equals(other.asString());
112         }
113         return new StringValue(text).equals(other);
114     }
115
116     /**
117     * Test whether a nodeset "not-equals" another Value
118     */

119
120     public boolean notEquals(Value other) throws XPathException {
121         return new StringValue(text).notEquals(other);
122     }
123
124     /**
125     * Test how a FragmentValue compares to another Value under a relational comparison.
126     */

127
128     public boolean compare(int operator, Value other) throws XPathException {
129         return new StringValue(text).compare(operator, other);
130     }
131
132     /**
133     * Return the type of the value
134     * @return Value.NODESET (always)
135     */

136
137     public int getType() {
138         return Value.NODESET;
139     }
140     
141     /**
142     * Determine the data type of the expression, if possible
143     * @return Value.NODESET
144     */

145
146     public int getDataType() {
147         return Value.NODESET;
148     }
149
150     /**
151     * Get the root (document) node
152     */

153
154     public DocumentInfo getRootNode() {
155         if (node!=null) { // only do it once
156
return (DocumentInfo)node;
157         }
158         try {
159             int len = text.length();
160             char[] chars = new char[len];
161             text.getChars(0, len, chars, 0);
162             Builder builder = new TinyBuilder();
163             builder.setSystemId(baseURI);
164             builder.setNamePool(controller.getNamePool()); // not used
165
builder.startDocument();
166             builder.characters(chars, 0, len);
167             builder.endDocument();
168             node = builder.getCurrentDocument();
169             controller.getDocumentPool().add((DocumentInfo)node, null);
170             return (DocumentInfo)node;
171         } catch (TransformerException JavaDoc err) {
172             throw new InternalSaxonError("Error building temporary tree: " + err.getMessage());
173         }
174     }
175
176     /**
177     * Copy the result tree fragment value to a given Outputter
178     */

179
180     public void copy(Outputter out) throws TransformerException JavaDoc {
181         out.writeContent(text);
182     }
183
184     /**
185     * Diagnostic print of expression structure
186     */

187     
188     public void display(int level) {
189         System.err.println(indent(level) + "** result tree fragment ** (" + text + ")");
190     }
191
192 }
193
194 //
195
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
196
// you may not use this file except in compliance with the License. You may obtain a copy of the
197
// License at http://www.mozilla.org/MPL/
198
//
199
// Software distributed under the License is distributed on an "AS IS" basis,
200
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
201
// See the License for the specific language governing rights and limitations under the License.
202
//
203
// The Original Code is: all this file.
204
//
205
// The Initial Developer of the Original Code is
206
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
207
//
208
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
209
//
210
// Contributor(s): none.
211
//
212

213
Popular Tags