KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.instruct;
2 import net.sf.saxon.Controller;
3 import net.sf.saxon.event.Emitter;
4 import net.sf.saxon.event.TreeReceiver;
5 import net.sf.saxon.expr.*;
6 import net.sf.saxon.om.*;
7 import net.sf.saxon.pattern.NoNodeTest;
8 import net.sf.saxon.style.StandardNames;
9 import net.sf.saxon.trans.DynamicError;
10 import net.sf.saxon.trans.XPathException;
11 import net.sf.saxon.type.ItemType;
12
13 import javax.xml.transform.OutputKeys JavaDoc;
14 import java.io.OutputStreamWriter JavaDoc;
15 import java.io.PrintStream JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Properties JavaDoc;
19
20 /**
21 * An xsl:message element in the stylesheet.
22 */

23
24 public class Message extends Instruction {
25
26     // TODO: JAXP 1.3 specifies that xsl:message output is written to the ErrorListener
27

28     private Expression terminate;
29     private Expression select;
30
31     public Message(Expression select, Expression terminate) {
32         this.terminate = terminate;
33         this.select = select;
34         adoptChildExpression(terminate);
35         adoptChildExpression(select);
36     }
37
38     /**
39      * Simplify an expression. This performs any static optimization (by rewriting the expression
40      * as a different expression). The default implementation does nothing.
41      * @return the simplified expression
42      * @throws net.sf.saxon.trans.XPathException
43      * if an error is discovered during expression rewriting
44      */

45
46     public Expression simplify(StaticContext env) throws XPathException {
47         select = select.simplify(env);
48         if (terminate != null) {
49             terminate = terminate.simplify(env);
50         }
51         return this;
52     }
53
54     public Expression typeCheck(StaticContext env, ItemType contextItemType) throws XPathException {
55         select = select.typeCheck(env, contextItemType);
56         adoptChildExpression(select);
57         if (terminate != null) {
58             terminate = terminate.typeCheck(env, contextItemType);
59             adoptChildExpression(terminate);
60         }
61         return this;
62     }
63
64    public Expression optimize(Optimizer opt, StaticContext env, ItemType contextItemType) throws XPathException {
65         select = select.optimize(opt, env, contextItemType);
66         adoptChildExpression(select);
67         if (terminate != null) {
68             terminate = terminate.optimize(opt, env, contextItemType);
69             adoptChildExpression(terminate);
70         }
71         return this;
72     }
73
74     /**
75     * Get the name of this instruction for diagnostic and tracing purposes
76     */

77
78     public int getInstructionNameCode() {
79         return StandardNames.XSL_MESSAGE;
80     }
81
82     public ItemType getItemType() {
83         return NoNodeTest.getInstance();
84     }
85
86     public int getCardinality() {
87         return StaticProperty.EMPTY;
88     }
89
90     /**
91      * Determine whether this instruction creates new nodes.
92      * This implementation returns true.
93      */

94
95     public final boolean createsNewNodes() {
96         return true;
97     }
98     /**
99      * Handle promotion offers, that is, non-local tree rewrites.
100      * @param offer The type of rewrite being offered
101      * @throws XPathException
102      */

103
104     protected void promoteInst(PromotionOffer offer) throws XPathException {
105         if (select != null) {
106             select = doPromotion(select, offer);
107         }
108         if (terminate != null) {
109             terminate = doPromotion(terminate, offer);
110         }
111     }
112
113     /**
114      * Get all the XPath expressions associated with this instruction
115      * (in XSLT terms, the expression present on attributes of the instruction,
116      * as distinct from the child instructions in a sequence construction)
117      */

118
119     public Iterator JavaDoc iterateSubExpressions() {
120         ArrayList JavaDoc list = new ArrayList JavaDoc(2);
121         if (select != null) {
122             list.add(select);
123         }
124         if (terminate != null) {
125             list.add(terminate);
126         }
127         return list.iterator();
128     }
129
130     public TailCall processLeavingTail(XPathContext context) throws XPathException {
131         Controller controller = context.getController();
132         Emitter emitter = controller.getMessageEmitter();
133         if (emitter==null) {
134             emitter = controller.makeMessageEmitter();
135         }
136         if (emitter.getWriter()==null) {
137             emitter.setWriter(new OutputStreamWriter JavaDoc(System.err));
138         }
139
140         TreeReceiver rec = new TreeReceiver(emitter);
141
142         XPathContext c2 = context.newMinorContext();
143         c2.setOrigin(this);
144         Properties JavaDoc props = new Properties JavaDoc();
145         props.put(OutputKeys.OMIT_XML_DECLARATION, "yes");
146         emitter.setOutputProperties(props);
147         c2.changeOutputDestination(props, rec, false, Validation.PRESERVE, null);
148
149         if (select != null) {
150             SequenceIterator iter = select.iterate(c2);
151             while (true) {
152                 Item item = iter.next();
153                 if (item == null) {
154                     break;
155                 }
156                 rec.append(item, locationId, NodeInfo.ALL_NAMESPACES);
157             }
158         }
159         rec.close();
160
161         if (terminate != null) {
162             String JavaDoc term = terminate.evaluateAsString(context);
163             if (term.equals("no")) {
164                 // no action
165
} else if (term.equals("yes")) {
166                 throw new TerminationException("Processing terminated by xsl:message at line " + getLineNumber());
167             } else {
168                 DynamicError e = new DynamicError("The terminate attribute of xsl:message must be 'yes' or 'no'");
169                 e.setXPathContext(context);
170                 e.setErrorCode("XTDE0030");
171                 throw e;
172             }
173         }
174         return null;
175     }
176
177     /**
178      * Diagnostic print of expression structure. The expression is written to the System.err
179      * output stream
180      *
181      * @param level indentation level for this expression
182      * @param out
183      */

184
185     public void display(int level, NamePool pool, PrintStream JavaDoc out) {
186         out.println(ExpressionTool.indent(level) + "message");
187     }
188 }
189 //
190
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
191
// you may not use this file except in compliance with the License. You may obtain a copy of the
192
// License at http://www.mozilla.org/MPL/
193
//
194
// Software distributed under the License is distributed on an "AS IS" basis,
195
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
196
// See the License for the specific language governing rights and limitations under the License.
197
//
198
// The Original Code is: all this file.
199
//
200
// The Initial Developer of the Original Code is Michael H. Kay.
201
//
202
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
203
//
204
// Contributor(s): none.
205
//
206
Popular Tags