KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejen > ext > Messenger


1 //
2
// Ejen (code generation system)
3
// Copyright (C) 2001, 2002 François Wolff (ejen@noos.fr).
4
//
5
// This file is part of Ejen.
6
//
7
// Ejen is free software; you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License as published by
9
// the Free Software Foundation; either version 2 of the License, or
10
// (at your option) any later version.
11
//
12
// Ejen is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU General Public License for more details.
16
//
17
// You should have received a copy of the GNU General Public License
18
// along with Ejen; if not, write to the Free Software
19
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
//
21
package org.ejen.ext;
22
23 import org.ejen.util.XSLUtil;
24 import org.ejen.EjenChildNode;
25 import java.sql.SQLException JavaDoc;
26 import org.apache.xalan.extensions.XSLProcessorContext;
27 import org.apache.xalan.templates.ElemExtensionCall;
28 import org.apache.xml.utils.WrappedRuntimeException;
29
30 /**
31  * Messages utility (static methods).
32  * <p>
33  * <table class="usage">
34  * <tr><th class="usage">Usage (XSL stylesheet)</th></tr>
35  * <tr><td class="usage"><pre>
36  * &lt;?xml version="1.0" encoding="iso-8859-1"?&gt;
37  *
38  * &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
39  * ...
40  * <b>xmlns:msg="org.ejen.ext.Messenger"
41  * extension-element-prefixes="msg ..."
42  * exclude-result-prefixes="msg ..."</b>
43  * version="1.0"&gt;
44  *
45  * &lt;xsl:output method="text" encoding="iso-8859-1"/&gt;
46  *
47  * &lt;xsl:template match="ejen"&gt;
48  *
49  * &lt;msg:{@link #send(XSLProcessorContext, ElemExtensionCall) send} message="Oop!"/&gt;
50  * &lt;msg:{@link #throwSQLException(XSLProcessorContext, ElemExtensionCall) throwSQLException} message="Table not found" code="0"/&gt;
51  * &lt;msg:{@link #throwRuntimeException(XSLProcessorContext, ElemExtensionCall) throwRuntimeException} message="Bug!"/&gt;
52  *
53  * &lt;/xsl:template&gt;
54  *
55  * &lt;/xsl:stylesheet&gt;
56  * </pre></td></tr></table>
57  * @author F. Wolff
58  * @version 1.0
59  */

60 public class Messenger {
61
62     /**
63      * Protected constructor (prevents instanciation).
64      */

65     protected Messenger() {}
66
67     /**
68      * Sends a message to the current {@link org.ejen.EjenListener}.
69      * <p>
70      * <table class="usage"><tr><td class="usage"><pre>
71      *
72      * &lt;msg:send message="Hello {people[$i]/@name} !"/&gt;
73      * </pre></td></tr></table>
74      * <p>
75      * <dd><dl><dt><b>XSLT Attributes:</b>
76      * <dd>message <b>[Mandatory/AVT]</b> - message to be sent.
77      * </dl></dd>
78      * <p>
79      * @param context automatically passed by the xalan extension mechanism.
80      * @param elem automatically passed by the xalan extension mechanism.
81      * @throws org.apache.xml.utils.WrappedRuntimeException with a XSL Exception
82      * if something goes wrong.
83      */

84     public static void send(XSLProcessorContext context, ElemExtensionCall elem) {
85         EjenChildNode.sendXSLMessageEvent(XSLUtil.getAttribute(context, elem,
86                 "message", true));
87     }
88
89     /**
90      * Throws a {@link java.sql.SQLException}.
91      * <p>
92      * <table class="usage"><tr><td class="usage"><pre>
93      *
94      * &lt;msg:throwSQLException message="Table not found" code="2"/&gt;
95      * </pre></td></tr></table>
96      * <p>
97      * <dd><dl><dt><b>XSLT Attributes:</b>
98      * <dd>message <b>[Mandatory/AVT]</b> - SQLException message.
99      * <dd>code <b>[Mandatory/AVT]</b> - SQLException code.
100      * </dl></dd>
101      * <p>
102      * @param context automatically passed by the xalan extension mechanism.
103      * @param elem automatically passed by the xalan extension mechanism.
104      * @throws java.sql.SQLException with supplied message/code.
105      * @throws org.apache.xml.utils.WrappedRuntimeException
106      * if one of 'message' or 'code' attributes is missing, or if 'code'
107      * cannot be parsed as an integer.
108      */

109     public static void throwSQLException(XSLProcessorContext context, ElemExtensionCall elem)
110         throws SQLException JavaDoc {
111         String JavaDoc message = XSLUtil.getAttribute(context, elem, "message", true);
112         String JavaDoc code = XSLUtil.getAttribute(context, elem, "code", true);
113         int iCode;
114
115         try {
116             iCode = Integer.parseInt(code);
117         } catch (NumberFormatException JavaDoc e) {
118             throw new WrappedRuntimeException("Invalid 'code' attribute in 'throwSQLException' node: "
119                     + code + " (message: " + message + ")",
120                     e);
121         }
122         throw new SQLException JavaDoc(message, null, iCode);
123     }
124
125     /**
126      * Throws a {@link java.lang.RuntimeException}.
127      * <p>
128      * <table class="usage"><tr><td class="usage"><pre>
129      *
130      * &lt;msg:throwRuntimeException message="Didn't want to work anymore"/&gt;
131      * </pre></td></tr></table>
132      * <p>
133      * <dd><dl><dt><b>XSLT Attributes:</b>
134      * <dd>message <b>[Mandatory/AVT]</b> - RuntimeException message.
135      * </dl></dd>
136      * <p>
137      * @param context automatically passed by the xalan extension mechanism.
138      * @param elem automatically passed by the xalan extension mechanism.
139      * @throws java.lang.RuntimeException with supplied message.
140      * @throws org.apache.xml.utils.WrappedRuntimeException
141      * if the 'message' attribute is missing.
142      */

143     public static void throwRuntimeException(XSLProcessorContext context, ElemExtensionCall elem) {
144         context.getTransformer().stopTransformation();
145         throw new RuntimeException JavaDoc(XSLUtil.getAttribute(context, elem, "message",
146                 true));
147     }
148 }
149
Popular Tags