KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.instruct;
2 import net.sf.saxon.expr.*;
3 import net.sf.saxon.om.*;
4 import net.sf.saxon.trans.DynamicError;
5 import net.sf.saxon.trans.StaticError;
6 import net.sf.saxon.trans.XPathException;
7 import net.sf.saxon.type.ItemType;
8 import net.sf.saxon.type.Type;
9
10 import java.io.PrintStream JavaDoc;
11 import java.util.Iterator JavaDoc;
12
13 /**
14  * Common superclass for XSLT instructions whose content template produces a text
15  * value: xsl:attribute, xsl:comment, xsl:processing-instruction, xsl:namespace,
16  * and xsl:text
17  */

18
19 public abstract class SimpleNodeConstructor extends Instruction {
20
21     protected Expression select = null;
22
23     public SimpleNodeConstructor() {
24     }
25
26     public void setSelect(Expression select) throws StaticError {
27         this.select = select;
28         adoptChildExpression(select);
29     }
30
31     /**
32      * Determine whether this instruction creates new nodes.
33      * This implementation returns true.
34      */

35
36     public final boolean createsNewNodes() {
37         return true;
38     }
39
40     public Expression simplify(StaticContext env) throws XPathException {
41         if (select != null) {
42             select = select.simplify(env);
43         }
44         return this;
45     }
46
47     /**
48      * Get the static properties of this expression (other than its type). The result is
49      * bit-signficant. These properties are used for optimizations. In general, if
50      * property bit is set, it is true, but if it is unset, the value is unknown.
51      *
52      * @return a set of flags indicating static properties of this expression
53      */

54
55     public int computeSpecialProperties() {
56         return super.computeSpecialProperties() |
57                 StaticProperty.SINGLE_DOCUMENT_NODESET;
58     }
59
60     public abstract void localTypeCheck(StaticContext env, ItemType contextItemType) throws XPathException;
61
62     /**
63      * The analyze() method is called in XQuery, where node constructors
64      * are implemented as Expressions. In this case the required type for the
65      * select expression is a single string.
66      * @param env The static context for the query
67      * @return the rewritten expression
68      * @throws XPathException if any static errors are found in this expression
69      * or any of its children
70      */

71
72     public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException {
73         localTypeCheck(env, contextItemType);
74
75         if (select != null) {
76             select = select.typeCheck(env, contextItemType);
77             if (!Type.isSubType(select.getItemType(), Type.ANY_ATOMIC_TYPE)) {
78                 select = new Atomizer(select, env.getConfiguration());
79             }
80             if (!Type.isSubType(select.getItemType(), Type.STRING_TYPE)) {
81                 select = new AtomicSequenceConverter(select, Type.STRING_TYPE);
82             }
83             adoptChildExpression(select);
84         }
85         return this;
86     }
87
88     public Expression optimize(Optimizer opt, StaticContext env, ItemType contextItemType) throws XPathException {
89         if (select != null) {
90             select = select.optimize(opt, env, contextItemType);
91             adoptChildExpression(select);
92         }
93         return this;
94     }
95
96     public Iterator JavaDoc iterateSubExpressions() {
97         return new MonoIterator(select);
98     }
99
100     /**
101     * Expand the stylesheet elements subordinate to this one, returning the result
102     * as a string. The expansion must not generate any element or attribute nodes.
103     * @param context The dynamic context for the transformation
104     */

105
106     public CharSequence JavaDoc expandChildren(XPathContext context) throws XPathException {
107         Item item = select.evaluateItem(context);
108         if (item==null) {
109             return "";
110         } else {
111             return item.getStringValueCS();
112         }
113     }
114
115     /**
116      * Evaluate as an expression. We rely on the fact that when these instructions
117      * are generated by XQuery, there will always be a valueExpression to evaluate
118      * the content
119      */

120
121     public Item evaluateItem(XPathContext context) throws XPathException {
122         String JavaDoc content = (select==null ?
123                     "" :
124                     select.evaluateAsString(context));
125         content = checkContent(content, context);
126         try {
127             Orphan o = new Orphan(context.getController().getConfiguration());
128             o.setNodeKind((short)getItemType().getPrimitiveType());
129             o.setStringValue(content);
130             o.setNameCode(evaluateNameCode(context));
131             return o;
132         } catch (SkipInstructionException skip) {
133             // error recovery path
134
return null;
135         }
136     }
137
138     /**
139      * Check the content of the node, and adjust it if necessary. The checks depend on the node kind.
140      * @param data the supplied content
141      * @param context the dynamic context
142      * @return the original content, unless adjustments are needed
143      * @throws DynamicError if the content is invalid
144      */

145
146     protected String JavaDoc checkContent(String JavaDoc data, XPathContext context) throws DynamicError {
147         return data;
148     }
149
150     protected int evaluateNameCode(XPathContext context)
151     throws XPathException, XPathException {
152         return -1;
153     }
154
155     public SequenceIterator iterate(XPathContext context) throws XPathException {
156         return SingletonIterator.makeIterator(evaluateItem(context));
157     }
158
159     /**
160      * Display this instruction as an expression, for diagnostics
161      */

162
163     public void display(int level, NamePool pool, PrintStream JavaDoc out) {
164         if (select != null) {
165             select.display(level, pool, out);
166         }
167     }
168
169     /**
170       * Offer promotion for subexpressions. The offer will be accepted if the subexpression
171       * is not dependent on the factors (e.g. the context item) identified in the PromotionOffer.
172       * By default the offer is not accepted - this is appropriate in the case of simple expressions
173       * such as constant values and variable references where promotion would give no performance
174       * advantage. This method is always called at compile time.
175       *
176       * @param offer details of the offer, for example the offer to move
177       * expressions that don't depend on the context to an outer level in
178       * the containing expression
179       * @exception XPathException if any error is detected
180       */

181
182      protected void promoteInst(PromotionOffer offer) throws XPathException {
183          if (select != null) {
184              select = doPromotion(select, offer);
185          }
186          super.promoteInst(offer);
187      }
188
189
190 }
191
192 //
193
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
194
// you may not use this file except in compliance with the License. You may obtain a copy of the
195
// License at http://www.mozilla.org/MPL/
196
//
197
// Software distributed under the License is distributed on an "AS IS" basis,
198
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
199
// See the License for the specific language governing rights and limitations under the License.
200
//
201
// The Original Code is: all this file.
202
//
203
// The Initial Developer of the Original Code is Michael H. Kay.
204
//
205
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
206
//
207
// Contributor(s): none.
208
//
209
Popular Tags