KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.instruct;
2 import net.sf.saxon.Controller;
3 import net.sf.saxon.event.*;
4 import net.sf.saxon.expr.ExpressionTool;
5 import net.sf.saxon.expr.XPathContext;
6 import net.sf.saxon.om.Item;
7 import net.sf.saxon.om.NamePool;
8 import net.sf.saxon.om.NodeInfo;
9 import net.sf.saxon.style.StandardNames;
10 import net.sf.saxon.trans.DynamicError;
11 import net.sf.saxon.trans.XPathException;
12 import net.sf.saxon.type.AnyItemType;
13 import net.sf.saxon.type.ItemType;
14 import net.sf.saxon.type.SchemaType;
15 import net.sf.saxon.type.Type;
16
17 import java.io.PrintStream JavaDoc;
18
19 /**
20 * Handler for xsl:copy elements in stylesheet.
21 */

22
23 public class Copy extends ElementCreator {
24
25     private boolean copyNamespaces;
26     private ItemType contextItemType;
27
28     public Copy(boolean copyNamespaces,
29                 boolean inheritNamespaces,
30                 SchemaType schemaType,
31                 int validation) {
32         this.copyNamespaces = copyNamespaces;
33         this.inheritNamespaces = inheritNamespaces;
34         setSchemaType(schemaType);
35         this.validation = validation;
36         if (copyNamespaces) {
37             setLazyConstruction(false);
38             // can't do lazy construction at present in cases where namespaces need to be copied from the
39
// source document.
40
}
41     }
42
43     /**
44     * Get the name of this instruction for diagnostic and tracing purposes
45     */

46
47     public int getInstructionNameCode() {
48         return StandardNames.XSL_COPY;
49     }
50
51     /**
52      * Get the item type of the result of this instruction.
53      * @return The context item type.
54      */

55
56     public ItemType getItemType() {
57         if (contextItemType == null) {
58             return AnyItemType.getInstance();
59         } else {
60             return contextItemType;
61         }
62     }
63
64     /**
65      * Callback from ElementCreator when constructing an element
66      * @param context
67      * @return the namecode of the element to be constructed
68      * @throws XPathException
69      */

70
71     public int getNameCode(XPathContext context)
72             throws XPathException {
73         return ((NodeInfo)context.getContextItem()).getNameCode();
74     }
75
76     /**
77      * Callback to output namespace nodes for the new element.
78      * @param context The execution context
79      * @param receiver the Receiver where the namespace nodes are to be written
80      * @throws XPathException
81      */

82
83     protected void outputNamespaceNodes(XPathContext context, Receiver receiver)
84     throws XPathException {
85         if (copyNamespaces) {
86             NodeInfo element = (NodeInfo)context.getContextItem();
87             element.sendNamespaceDeclarations(receiver, true);
88         }
89     }
90
91     /**
92      * Callback to get a list of the intrinsic namespaces that need to be generated for the element.
93      * The result is an array of namespace codes, the codes either occupy the whole array or are
94      * terminated by a -1 entry. A result of null is equivalent to a zero-length array.
95      */

96
97     public int[] getActiveNamespaces() throws XPathException {
98         if (copyNamespaces) {
99             // we should have disabled lazy construction, so this shouldn't be called.
100
throw new UnsupportedOperationException JavaDoc();
101         } else {
102             return null;
103         }
104     }
105
106     public TailCall processLeavingTail(XPathContext context) throws XPathException {
107         Controller controller = context.getController();
108         XPathContext c2 = context.newMinorContext();
109         c2.setOrigin(this);
110         SequenceReceiver out = c2.getReceiver();
111         Item item = context.getContextItem();
112         if (!(item instanceof NodeInfo)) {
113             out.append(item, locationId, NodeInfo.ALL_NAMESPACES);
114             return null;
115         }
116         NodeInfo source = (NodeInfo)item;
117
118         // Processing depends on the node kind.
119

120         switch(source.getNodeKind()) {
121
122         case Type.ELEMENT:
123             // use the generic code for creating new elements
124
return super.processLeavingTail(c2);
125
126         case Type.ATTRIBUTE:
127             try {
128                 CopyOf.copyAttribute(source, getSchemaType(), validation, this, c2, false);
129             } catch (NoOpenStartTagException err) {
130                 err.setXPathContext(context);
131                 throw dynamicError(this, err, c2);
132             }
133             break;
134
135         case Type.TEXT:
136             out.characters(source.getStringValueCS(), locationId, 0);
137             break;
138
139         case Type.PROCESSING_INSTRUCTION:
140             out.processingInstruction(source.getDisplayName(), source.getStringValueCS(), locationId, 0);
141             break;
142
143         case Type.COMMENT:
144             out.comment(source.getStringValueCS(), locationId, 0);
145             break;
146
147         case Type.NAMESPACE:
148             try {
149                 source.copy(out, NodeInfo.NO_NAMESPACES, false, locationId);
150             } catch (NoOpenStartTagException err) {
151                 DynamicError e = new DynamicError(err.getMessage());
152                 e.setXPathContext(context);
153                 e.setErrorCode(err.getErrorCodeLocalPart());
154                 throw dynamicError(this, e, context);
155             }
156             break;
157
158         case Type.DOCUMENT:
159             Receiver val = controller.getConfiguration().
160                     getDocumentValidator(out,
161                                          source.getBaseURI(),
162                                          controller.getNamePool(),
163                                          validation, getSchemaType());
164             if (val != out) {
165                 SequenceReceiver sr = new TreeReceiver(val);
166                 sr.setPipelineConfiguration(out.getPipelineConfiguration());
167                 c2.setReceiver(sr);
168                 val = sr;
169             }
170             val.startDocument(0);
171             content.process(c2);
172             val.endDocument();
173             break;
174
175         default:
176             throw new IllegalArgumentException JavaDoc("Unknown node kind " + source.getNodeKind());
177
178         }
179         return null;
180     }
181
182     /**
183      * Evaluate as an expression. We rely on the fact that when these instructions
184      * are generated by XQuery, there will always be a valueExpression to evaluate
185      * the content
186      */

187
188     public Item evaluateItem(XPathContext context) throws XPathException {
189         Controller controller = context.getController();
190         XPathContext c2 = context.newMinorContext();
191         c2.setOrigin(this);
192         SequenceOutputter seq = new SequenceOutputter(1);
193         seq.setPipelineConfiguration(controller.makePipelineConfiguration());
194         c2.setTemporaryReceiver(seq);
195         process(c2);
196         seq.close();
197         return seq.getFirstItem();
198     }
199
200     /**
201      * Diagnostic print of expression structure. The expression is written to the System.err
202      * output stream
203      *
204      * @param level indentation level for this expression
205      * @param out
206      */

207
208     public void display(int level, NamePool pool, PrintStream JavaDoc out) {
209         out.println(ExpressionTool.indent(level) + "copy");
210     }
211
212
213 }
214
215 //
216
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
217
// you may not use this file except in compliance with the License. You may obtain a copy of the
218
// License at http://www.mozilla.org/MPL/
219
//
220
// Software distributed under the License is distributed on an "AS IS" basis,
221
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
222
// See the License for the specific language governing rights and limitations under the License.
223
//
224
// The Original Code is: all this file.
225
//
226
// The Initial Developer of the Original Code is Michael H. Kay.
227
//
228
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
229
//
230
// Contributor(s): none.
231
//
232
Popular Tags