KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.instruct;
2
3 import net.sf.saxon.expr.*;
4 import net.sf.saxon.trans.XPathException;
5 import net.sf.saxon.type.ItemType;
6 import net.sf.saxon.type.SchemaType;
7 import net.sf.saxon.type.SimpleType;
8 import net.sf.saxon.om.Validation;
9
10 import java.util.Iterator JavaDoc;
11
12 /**
13  * An abstract class to act as a common parent for instructions that create element nodes
14  * and document nodes.
15  */

16
17 public abstract class ParentNodeConstructor extends Instruction {
18
19     protected Expression content;
20     private boolean lazyConstruction = false;
21     private boolean namespaceSensitiveType;
22     int validation = Validation.PRESERVE;
23     private SchemaType schemaType;
24
25
26
27     public ParentNodeConstructor() {}
28
29     /**
30      * Indicate that lazy construction should (or should not) be used
31      * @param lazy set to true if lazy construction should be used
32      */

33
34     public void setLazyConstruction(boolean lazy) {
35         lazyConstruction = lazy;
36     }
37
38     /**
39      * Establish whether lazy construction is to be used
40      */

41
42     public final boolean isLazyConstruction() {
43         return lazyConstruction;
44     }
45
46     /**
47      * Set the schema type to be used for validation
48      */

49
50     public void setSchemaType(SchemaType type) {
51         schemaType = type;
52         namespaceSensitiveType = (type instanceof SimpleType) && ((SimpleType)type).isNamespaceSensitive();
53     }
54
55     /**
56      * Get the schema type chosen for validation; null if not defined
57      */

58
59     public SchemaType getSchemaType() {
60         return schemaType;
61     }
62
63     /**
64      * Determine whether the schema type is namespace sensitive. The result is undefined if schemaType is null.
65      */

66
67     public boolean isNamespaceSensitive() {
68         return namespaceSensitiveType;
69     }
70
71     /**
72      * Get the validation mode for this instruction
73      * @return the validation mode, for example {@link Validation#STRICT} or {@link Validation#PRESERVE}
74      */

75     public int getValidationAction() {
76         return validation;
77     }
78
79     /**
80      * Set the expression that constructs the content of the element
81      */

82
83     public void setContentExpression(Expression content) {
84         this.content = content;
85         adoptChildExpression(content);
86     }
87
88     /**
89      * Get the expression that constructs the content of the element
90      */

91
92     public Expression getContentExpression() {
93         return content;
94     }
95
96
97     /**
98      * Simplify an expression. This performs any static optimization (by rewriting the expression
99      * as a different expression). The default implementation does nothing.
100      * @return the simplified expression
101      * @throws net.sf.saxon.trans.XPathException
102      * if an error is discovered during expression rewriting
103      */

104
105     public Expression simplify(StaticContext env) throws XPathException {
106         content = content.simplify(env);
107         return this;
108     }
109
110     public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException {
111         content = content.typeCheck(env, contextItemType);
112         adoptChildExpression(content);
113         verifyLazyConstruction();
114         return this;
115     }
116
117     public Expression optimize(Optimizer opt, StaticContext env, ItemType contextItemType) throws XPathException {
118         content = content.optimize(opt, env, contextItemType);
119         adoptChildExpression(content);
120         return this;
121     }
122
123
124     /**
125      * Handle promotion offers, that is, non-local tree rewrites.
126      * @param offer The type of rewrite being offered
127      * @throws net.sf.saxon.trans.XPathException
128      */

129
130     protected void promoteInst(PromotionOffer offer) throws XPathException {
131         content = doPromotion(content, offer);
132     }
133
134     /**
135       * Get the immediate sub-expressions of this expression.
136       * @return an iterator containing the sub-expressions of this expression
137       */

138
139     public Iterator JavaDoc iterateSubExpressions() {
140         return new MonoIterator(content);
141     }
142
143     /**
144      * Determine whether this instruction creates new nodes.
145      * This implementation returns true.
146      */

147
148     public final boolean createsNewNodes() {
149         return true;
150     }
151
152     public int getCardinality() {
153         return StaticProperty.EXACTLY_ONE;
154     }
155
156     /**
157      * Check that lazy construction is possible for this element
158      */

159
160     void verifyLazyConstruction() {
161         if (!isLazyConstruction()) {
162             return;
163         }
164         // Lazy construction is not possible if the expression depends on the values of position() or last(),
165
// as we can't save these.
166
if ((getDependencies() & (StaticProperty.DEPENDS_ON_POSITION | StaticProperty.DEPENDS_ON_LAST)) != 0) {
167             setLazyConstruction(false);
168         }
169         // Lazy construction is not possible if validation is required
170
if (validation == Validation.STRICT || validation == Validation.LAX
171                 || schemaType != null) {
172             setLazyConstruction(false);
173         }
174     }
175 }
176
177
178 //
179
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
180
// you may not use this file except in compliance with the License. You may obtain a copy of the
181
// License at http://www.mozilla.org/MPL/
182
//
183
// Software distributed under the License is distributed on an "AS IS" basis,
184
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
185
// See the License for the specific language governing rights and limitations under the License.
186
//
187
// The Original Code is: all this file.
188
//
189
// The Initial Developer of the Original Code is Michael H. Kay.
190
//
191
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
192
//
193
// Contributor(s): none.
194
//
195

196
Popular Tags