KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > instruct > QuerySimpleContentConstructor


1 package net.sf.saxon.instruct;
2
3 import net.sf.saxon.expr.Atomizer;
4 import net.sf.saxon.expr.Expression;
5 import net.sf.saxon.expr.XPathContext;
6 import net.sf.saxon.expr.StaticProperty;
7 import net.sf.saxon.om.Item;
8 import net.sf.saxon.om.SequenceIterator;
9 import net.sf.saxon.om.FastStringBuffer;
10 import net.sf.saxon.trans.XPathException;
11 import net.sf.saxon.type.Type;
12 import net.sf.saxon.value.AtomicValue;
13 import net.sf.saxon.value.StringValue;
14
15 /**
16  * This class implements the rules for an XQuery simple content constructor, which are used in constructing
17  * the string value of an attribute node, text node, comment node, etc, from the value of the select
18  * expression or the contained sequence constructor. These differ slightly from the XSLT rules implemented
19  * in the superclass - specifically, the sequence is simply atomized, whereas XSLT takes special steps to
20  * concatenate adjacent text nodes before inserting separators.
21  */

22
23 public class QuerySimpleContentConstructor extends SimpleContentConstructor {
24
25     boolean noNodeIfEmpty;
26
27     public QuerySimpleContentConstructor(Expression select, Expression separator, boolean noNodeIfEmpty) {
28         super(select, separator);
29         this.noNodeIfEmpty = noNodeIfEmpty;
30     }
31
32     /**
33      * Compute the cardinality of the result of the expression.
34      * @return the cardinality, @link {StaticProperty.EXACTLY_ONE}
35      */

36
37     protected int computeCardinality() {
38         if (noNodeIfEmpty) {
39             return StaticProperty.ALLOWS_ZERO_OR_ONE;
40         } else {
41             return StaticProperty.EXACTLY_ONE;
42         }
43     }
44
45     /**
46     * Expand the stylesheet elements subordinate to this one, returning the result
47     * as a string. The expansion must not generate any element or attribute nodes.
48     * @param context The dynamic context for the transformation
49     */

50
51     public CharSequence JavaDoc expandChildren(XPathContext context) throws XPathException {
52         Item item = select.evaluateItem(context);
53         if (item==null) {
54             return (noNodeIfEmpty ? null : "");
55         } else {
56             return item.getStringValueCS();
57         }
58     }
59
60     /**
61      * Evaluate an expression as a single item. This always returns either a single Item or
62      * null (denoting the empty sequence). No conversion is done. This method should not be
63      * used unless the static type of the expression is a subtype of "item" or "item?": that is,
64      * it should not be called if the expression may return a sequence. There is no guarantee that
65      * this condition will be detected.
66      *
67      * @param context The context in which the expression is to be evaluated
68      * @return the node or atomic value that results from evaluating the
69      * expression; or null to indicate that the result is an empty
70      * sequence
71      * @throws net.sf.saxon.trans.XPathException
72      * if any dynamic error occurs evaluating the
73      * expression
74      */

75
76     public Item evaluateItem(XPathContext context) throws XPathException {
77
78         if (isSingleton && isAtomic) {
79             // optimize for this case
80
Item item = select.evaluateItem(context);
81             if (item == null) {
82                 if (noNodeIfEmpty) {
83                     return null;
84                 } else {
85                     return StringValue.EMPTY_STRING;
86                 }
87             }
88             if (item instanceof StringValue) {
89                 return item;
90             } else {
91                 return ((AtomicValue)item).convert(Type.STRING, context);
92             }
93         }
94         SequenceIterator iter = select.iterate(context);
95         if (!isAtomic) {
96             iter = Atomizer.AtomizingFunction.getAtomizingIterator(iter);
97         }
98         FastStringBuffer sb = new FastStringBuffer(1024);
99         boolean first = true;
100         String JavaDoc sep = " ";
101         while (true) {
102             Item item = iter.next();
103             if (item==null) {
104                 break;
105             }
106             if (!first) {
107                 sb.append(sep);
108             }
109             first = false;
110             sb.append(item.getStringValueCS());
111         }
112         if (first && noNodeIfEmpty) {
113             return null;
114         }
115         return StringValue.makeStringValue(sb.condense());
116     }
117
118 }
119
120
121 //
122
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
123
// you may not use this file except in compliance with the License. You may obtain a copy of the
124
// License at http://www.mozilla.org/MPL/
125
//
126
// Software distributed under the License is distributed on an "AS IS" basis,
127
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
128
// See the License for the specific language governing rights and limitations under the License.
129
//
130
// The Original Code is: all this file.
131
//
132
// The Initial Developer of the Original Code is Michael H. Kay.
133
//
134
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
135
//
136
// Contributor(s): none.
137
//
138
Popular Tags