KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.instruct;
2
3 import net.sf.saxon.Controller;
4 import net.sf.saxon.event.Receiver;
5 import net.sf.saxon.event.SequenceReceiver;
6 import net.sf.saxon.expr.Expression;
7 import net.sf.saxon.expr.ExpressionTool;
8 import net.sf.saxon.expr.StaticContext;
9 import net.sf.saxon.expr.XPathContext;
10 import net.sf.saxon.om.*;
11 import net.sf.saxon.pattern.NodeKindTest;
12 import net.sf.saxon.pull.UnconstructedDocument;
13 import net.sf.saxon.style.StandardNames;
14 import net.sf.saxon.tinytree.TinyBuilder;
15 import net.sf.saxon.trans.DynamicError;
16 import net.sf.saxon.trans.XPathException;
17 import net.sf.saxon.type.ItemType;
18 import net.sf.saxon.value.TextFragmentValue;
19
20 import java.io.PrintStream JavaDoc;
21
22
23 /**
24  * An instruction to create a document node. This doesn't correspond directly
25  * to any XSLT instruction. It is used to support the document node constructor
26  * expression in XQuery, and is used as a sub-instruction within an xsl:variable
27  * that constructs a temporary tree.
28  *
29  * <p>Conceptually it represents an XSLT instruction xsl:document-node,
30  * with no attributes, whose content is a complex content constructor for the
31  * children of the document node.</p>
32  */

33
34 public class DocumentInstr extends ParentNodeConstructor {
35
36     private static final int[] treeSizeParameters = {50, 10, 5, 200};
37     // estimated size of a temporary tree: {nodes, attributes, namespaces, characters}
38

39     private boolean textOnly;
40     private String JavaDoc constantText;
41     private String JavaDoc baseURI;
42
43
44     public DocumentInstr(boolean textOnly,
45                          String JavaDoc constantText,
46                          String JavaDoc baseURI) {
47         this.textOnly = textOnly;
48         this.constantText = constantText;
49         this.baseURI = baseURI;
50     }
51
52     /**
53      * An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process().
54      * This method indicates which of these methods is prefered. For instructions this is the process() method.
55      */

56
57     public int getImplementationMethod() {
58         return Expression.EVALUATE_METHOD;
59     }
60
61
62     /**
63      * Set the validation action
64      */

65
66     public void setValidationAction(int action) {
67         validation = action;
68     }
69
70     /**
71      * Simplify an expression. This performs any static optimization (by rewriting the expression
72      * as a different expression). The default implementation does nothing.
73      *
74      * @return the simplified expression
75      * @throws net.sf.saxon.trans.XPathException
76      * if an error is discovered during expression rewriting
77      */

78
79     public Expression simplify(StaticContext env) throws XPathException {
80         setLazyConstruction(env.getConfiguration().isLazyConstructionMode());
81         return super.simplify(env);
82     }
83
84     public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException {
85         Expression res = super.typeCheck(env, contextItemType);
86         //verifyLazyConstruction();
87
return res;
88     }
89
90     public ItemType getItemType() {
91         return NodeKindTest.DOCUMENT;
92     }
93
94     public TailCall processLeavingTail(XPathContext context) throws XPathException {
95         Item item = evaluateItem(context);
96         if (item != null) {
97             SequenceReceiver out = context.getReceiver();
98             out.append(item, locationId, NodeInfo.ALL_NAMESPACES);
99         }
100         return null;
101     }
102
103     /**
104      * Evaluate as an expression.
105      */

106
107     public Item evaluateItem(XPathContext context) throws XPathException {
108         if (isLazyConstruction()) {
109             return new UnconstructedDocument(this, context);
110         } else {
111             Controller controller = context.getController();
112             DocumentInfo root;
113             if (textOnly) {
114                 CharSequence JavaDoc textValue;
115                 if (constantText != null) {
116                     textValue = constantText;
117                 } else {
118                     FastStringBuffer sb = new FastStringBuffer(100);
119                     SequenceIterator iter = content.iterate(context);
120                     if (iter instanceof AtomizableIterator) {
121                         ((AtomizableIterator)iter).setIsAtomizing(true);
122                     }
123                     while (true) {
124                         Item item = iter.next();
125                         if (item==null) break;
126                         sb.append(item.getStringValueCS());
127                     }
128                     textValue = sb.condense();
129                 }
130                 root = new TextFragmentValue(textValue, baseURI);
131                 ((TextFragmentValue)root).setConfiguration(controller.getConfiguration());
132             } else {
133                 try {
134                     XPathContext c2 = context.newMinorContext();
135                     c2.setOrigin(this);
136
137                     TinyBuilder builder = new TinyBuilder();
138                     builder.setSizeParameters(treeSizeParameters);
139                     builder.setLineNumbering(controller.getConfiguration().isLineNumbering());
140
141                     Receiver receiver = builder;
142                     receiver.setSystemId(baseURI);
143                     receiver.setPipelineConfiguration(controller.makePipelineConfiguration());
144
145                     c2.changeOutputDestination(null,
146                             receiver,
147                             false,
148                             validation,
149                             getSchemaType());
150                     Receiver out = c2.getReceiver();
151                     out.open();
152                     out.startDocument(0);
153
154                     content.process(c2);
155
156                     out.endDocument();
157                     out.close();
158
159                     root = (DocumentInfo)builder.getCurrentRoot();
160                 } catch (XPathException e) {
161                     if (e.getLocator() == null) {
162                         e.setLocator(this);
163                     }
164                     if (e instanceof DynamicError && ((DynamicError)e).getXPathContext() == null) {
165                         ((DynamicError)e).setXPathContext(context);
166                     }
167                     throw e;
168                 }
169             }
170             return root;
171         }
172     }
173
174
175     /**
176      * Get the name of this instruction for diagnostic and tracing purposes
177      * (the string "document-constructor")
178      */

179
180     public int getInstructionNameCode() {
181         return StandardNames.XSL_DOCUMENT;
182     }
183
184     /**
185      * Display this instruction as an expression, for diagnostics
186      */

187
188     public void display(int level, NamePool pool, PrintStream JavaDoc out) {
189         out.println(ExpressionTool.indent(level) + "document-constructor");
190         content.display(level+1, pool, out);
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 Michael H. Kay.
206
//
207
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
208
//
209
// Contributor(s): none.
210
//
211
Popular Tags