KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejen > EjenTask


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;
22
23 import org.apache.tools.ant.Task;
24 import org.apache.tools.ant.BuildException;
25
26 /**
27  * Ejen ant task class (wrapes an {@link org.ejen.EjenRootNode EjenRootNode}).
28  * <p>
29  * <table class="usage">
30  * <tr><th class="usage">Usage (ant build file)</th></tr>
31  * <tr><td class="usage"><pre><code>
32  * &lt;?xml version="1.0" encoding="UTF-8"?&gt;
33  *
34  * &lt;project name="generate" default="build"&gt;
35  *
36  * <b>&lt;taskdef name="ejen" classname="org.ejen.EjenTask"/&gt;</b>
37  *
38  * &lt;target name="build"&gt;
39  * <b>&lt;ejen [{@link #setStacktrace(boolean) stacktrace}="(true|false)"]&gt;</b>
40  * ...
41  * &lt;{@link org.ejen.EjenSourceNode source} .../&gt;
42  * &lt;{@link org.ejen.EjenMergeNode merge} .../&gt;
43  * &lt;{@link org.ejen.EjenSaveNode save} .../&gt;
44  * &lt;{@link org.ejen.EjenFilterNode filter} .../&gt;
45  * &lt;{@link org.ejen.EjenTemplateNode template} .../&gt;
46  * ...
47  * <b>&lt;/ejen&gt;</b>
48  * &lt;/target&gt;
49  *
50  * &lt;/project&gt;
51  * </code></pre></td></tr></table>
52  * <p>
53  * <b>Parent nodes</b>:
54  * <ul>
55  * <li>Ant <code>target</code> node.
56  * </ul>
57  * <p>
58  * @author F. Wolff
59  * @version 1.0
60  */

61 public class EjenTask extends Task {
62     private EjenRootNode _ern = null;
63     private boolean _stacktrace = false;
64
65     /**
66      * Constructor (creates a new EjenRootNode and register an
67      * {@link org.ejen.EjenListener EjenListener} for message/error
68      * reporting.
69      */

70     public EjenTask() {
71         _ern = new EjenRootNode();
72         _ern.setListener(new EjenListener() {
73             public void stateChanged(EjenEvent ev) {
74                 log("(" + ev.getMessage() + ") " + ev.getSource().toString(),
75                         ev.getLevel());
76             }
77
78             public void nodeMessageSent(EjenEvent ev) {
79                 log(ev.getMessage(), ev.getLevel());
80             }
81
82             public void xslMessageSent(EjenEvent ev) {
83                 log(ev.getMessage(), ev.getLevel());
84             }
85         });
86     }
87
88     /**
89      * <b>[optional]</b> - sets the stacktrace option for error reporting (print
90      * or not a stack trace). Default is <code>false</code>.
91      * @param stacktrace a <code>String</code> equals to "true" or "false",
92      * automatically converted to a <code>boolean</code> by Ant.
93      */

94     public void setStacktrace(boolean stacktrace) {
95         _stacktrace = stacktrace;
96     }
97
98     /**
99      * Creates a new filter node.
100      * @return the new filter node.
101      */

102     public EjenFilterNode createFilter() {
103         EjenFilterNode f = new EjenFilterNode();
104
105         _ern.appendChildNode(f);
106         return f;
107     }
108
109     /**
110      * Creates a new template node.
111      * @return the new template node.
112      */

113     public EjenTemplateNode createTemplate() {
114         EjenTemplateNode t = new EjenTemplateNode();
115
116         _ern.appendChildNode(t);
117         return t;
118     }
119
120     /**
121      * Creates a new source node.
122      * @return the new source node.
123      */

124     public EjenSourceNode createSource() {
125         EjenSourceNode s = new EjenSourceNode();
126
127         _ern.appendChildNode(s);
128         return s;
129     }
130
131     /**
132      * Creates a new save node.
133      * @return the new save node.
134      */

135     public EjenSaveNode createSave() {
136         EjenSaveNode s = new EjenSaveNode();
137
138         _ern.appendChildNode(s);
139         return s;
140     }
141
142     /**
143      * Creates a new merge node.
144      * @return the new merge node.
145      */

146     public EjenMergeNode createMerge() {
147         EjenMergeNode s = new EjenMergeNode();
148
149         _ern.appendChildNode(s);
150         return s;
151     }
152
153     /**
154      * Executes the generation, according to child nodes.
155      * @throws org.apache.tools.ant.BuildException if goes wrong.
156      */

157     public void execute() throws BuildException {
158         try {
159             _ern.check();
160             _ern.beforeProcess();
161             _ern.process();
162             _ern.afterProcess();
163             _ern.idle();
164         } catch (Exception JavaDoc e) {
165             throw new BuildException("\n\n"
166                     + EjenErrors.toString(null, null, e, _stacktrace));
167         }
168     }
169 }
170
Popular Tags