KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > style > XSLMessage


1 package com.icl.saxon.style;
2 import com.icl.saxon.tree.AttributeCollection;
3 import com.icl.saxon.*;
4 import com.icl.saxon.expr.*;
5 import com.icl.saxon.output.*;
6 import javax.xml.transform.*;
7 import javax.xml.transform.stream.StreamResult JavaDoc;
8
9 import java.io.*;
10 import java.util.*;
11
12 /**
13 * An xsl:message element in the stylesheet.<BR>
14 */

15
16 public class XSLMessage extends StyleElement {
17
18     boolean terminate = false;
19
20     /**
21     * Determine whether this node is an instruction.
22     * @return true - it is an instruction
23     */

24
25     public boolean isInstruction() {
26         return true;
27     }
28
29     /**
30     * Determine whether this type of element is allowed to contain a template-body
31     * @return true: yes, it may contain a template-body
32     */

33
34     public boolean mayContainTemplateBody() {
35         return true;
36     }
37
38     public void prepareAttributes() throws TransformerConfigurationException {
39
40         String JavaDoc terminateAtt = null;
41         StandardNames sn = getStandardNames();
42         AttributeCollection atts = getAttributeList();
43         
44         for (int a=0; a<atts.getLength(); a++) {
45             int nc = atts.getNameCode(a);
46             int f = nc & 0xfffff;
47             if (f==sn.TERMINATE) {
48                 terminateAtt = atts.getValue(a);
49             } else {
50                 checkUnknownAttribute(nc);
51             }
52         }
53
54         if (terminateAtt!=null) {
55             if (terminateAtt.equals("yes")) {
56                 terminate = true;
57             } else if (terminateAtt.equals("no")) {
58                 terminate = false;
59             } else {
60                 styleError("terminate must be \"yes\" or \"no\"");
61             }
62         }
63     }
64
65     public void validate() throws TransformerConfigurationException {
66         checkWithinTemplate();
67     }
68
69     public void process(Context context) throws TransformerException
70     {
71         Controller c = context.getController();
72         Emitter emitter = c.getMessageEmitter();
73         if (emitter==null) {
74             emitter = c.makeMessageEmitter();
75         }
76         if (emitter.getWriter()==null) {
77             emitter.setWriter(new OutputStreamWriter(System.err));
78         }
79         
80         Outputter old = c.getOutputter();
81         Properties props = new Properties();
82         props.put(OutputKeys.OMIT_XML_DECLARATION, "yes");
83         c.changeOutputDestination(props, emitter);
84
85         processChildren(context);
86
87         c.resetOutputDestination(old);
88                 
89         if (terminate) {
90             throw new TerminationException("Processing terminated by xsl:message at line " + getLineNumber());
91         }
92     }
93
94 }
95 //
96
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
97
// you may not use this file except in compliance with the License. You may obtain a copy of the
98
// License at http://www.mozilla.org/MPL/
99
//
100
// Software distributed under the License is distributed on an "AS IS" basis,
101
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
102
// See the License for the specific language governing rights and limitations under the License.
103
//
104
// The Original Code is: all this file.
105
//
106
// The Initial Developer of the Original Code is
107
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
108
//
109
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
110
//
111
// Contributor(s): none.
112
//
113
Popular Tags