KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejen > EjenParamNode


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 java.util.Properties JavaDoc;
24 import javax.xml.transform.dom.DOMSource JavaDoc;
25 import org.apache.xpath.XPathAPI;
26 import org.apache.xalan.transformer.TransformerImpl;
27
28 /**
29  * Param node class.
30  * <p>
31  * A param node allows to pass a parameter to a stylesheet (filter, template,
32  * import or include).
33  * <p>
34  * <table class="usage">
35  * <tr><th class="usage">Usage (ant build file)</th></tr>
36  * <tr><td class="usage"><pre><code>
37  * &lt;?xml version="1.0" encoding="UTF-8"?&gt;
38  *
39  * &lt;project name="generate" default="build"&gt;
40  *
41  * &lt;taskdef name="ejen" classname="org.ejen.EjenTask"/&gt;
42  *
43  * &lt;target name="build"&gt;
44  * &lt;{@link org.ejen.EjenTask ejen} ...&gt;
45  * ...
46  * &lt;{@link org.ejen.EjenFilterNode filter} ...&gt;
47  * ...
48  * <b>&lt;param {@link #setName(String) name}="param1"
49  * <i>either</i>
50  * {@link #setSelect(String) select}="/ejen/entity-bean"
51  * <i>or</i>
52  * {@link #setLiteral(String) literal}="value"
53  * /&gt;</b>
54  * ...
55  * &lt;/filter&gt;
56  * ...
57  * &lt;/ejen&gt;
58  * &lt;/target&gt;
59  *
60  * &lt;/project&gt;
61  * </code></pre></td></tr></table>
62  * <p>
63  * <b>Parent nodes</b>:
64  * <ul>
65  * <li>{@link org.ejen.EjenFilterNode filter}
66  * <li>{@link org.ejen.EjenTemplateNode template}
67  * <li>{@link org.ejen.EjenImportNode import}
68  * <li>{@link org.ejen.EjenIncludeNode include}
69  * </ul>
70  * @author F. Wolff
71  * @version 1.0
72  */

73 public class EjenParamNode extends EjenChildNode {
74     protected String JavaDoc _name = null;
75     protected String JavaDoc _select = null;
76     protected String JavaDoc _literal = null;
77
78     /**
79      * Returns the name of this EjenParamNode (always "param").
80      * @return the name of this EjenParamNode.
81      */

82     public String JavaDoc nodeName() {
83         return "param";
84     }
85
86     /**
87      * Returns all non null attributes of this EjenParamNode.
88      * @return non null attributes of this EjenParamNode.
89      */

90     public Properties JavaDoc getAttributes() {
91         Properties JavaDoc attrs = super.getAttributes();
92
93         if (_name != null) {
94             attrs.setProperty("name", _name);
95         }
96         if (_select != null) {
97             attrs.setProperty("select", _select);
98         }
99         if (_literal != null) {
100             attrs.setProperty("literal", _literal);
101         }
102         return attrs;
103     }
104
105     /**
106      * <b>[mandatory/AVT]</b> - sets the name attribute.
107      * @param name name of the parameter to be passed.
108      */

109     public void setName(String JavaDoc name) {
110         _name = name;
111     }
112
113     /**
114      * <b>[mandatory if literal is not set/AVT]</b> - sets the select attribute.
115      * @param select an XPATH select expression that gives the value of the parameter
116      * to be passed, relative to the current in memory DOM tree.
117      */

118     public void setSelect(String JavaDoc select) {
119         _select = select;
120     }
121
122     /**
123      * <b>[mandatory if select is not set]</b> - sets the literal attribute.
124      * @param literal a String that will be passed as the parameter value
125      * without interpretation.
126      */

127     public void setLiteral(String JavaDoc literal) {
128         _literal = literal;
129     }
130
131     /**
132      * Checks this EjenParamNode for mandatory attributes.
133      * @throws org.ejen.EjenException if name attribute is not set or
134      * if both select and literal are set or not set together.
135      */

136     public void check() {
137         super.check();
138         if (_name == null) {
139             throw new EjenException(this, "No 'name' attribute");
140         }
141         if (_select == null && _literal == null) {
142             throw new EjenException(this,
143                     "Neither 'select' nor 'literal' attribute");
144         }
145         if (_select != null && _literal != null) {
146             throw new EjenException(this,
147                     "Cannot choose between 'select' and 'literal' attributes");
148         }
149     }
150     
151     /**
152      * Executes this EjenParamNode.
153      * @throws org.ejen.EjenException if something goes wrong...
154      */

155     public void process() {
156         super.process();
157         try {
158             TransformerImpl ti = (TransformerImpl) (getFromContext(CTX_TRANSFORMER_IMPL));
159
160             if (ti == null) {
161                 throw new EjenException(this,
162                         "no '" + CTX_TRANSFORMER_IMPL + "' in context");
163             }
164             String JavaDoc name = evaluateAVT(_name);
165
166             if (_select != null) {
167                 DOMSource JavaDoc src = (DOMSource JavaDoc) (getFromGlobalContext(CTX_DOM_SOURCE));
168
169                 if (src == null) {
170                     throw new EjenException(this,
171                             "no '" + CTX_DOM_SOURCE + "' in global context");
172                 }
173                 ti.setParameter(name,
174                         XPathAPI.selectNodeList(src.getNode(),
175                         evaluateAVT(ti, _select)));
176             } else {
177                 ti.setParameter(name, _literal);
178             }
179         } catch (EjenException e) {
180             throw e;
181         } catch (Exception JavaDoc e) {
182             throw new EjenException(this, "bad param", e);
183         }
184     }
185 }
186
Popular Tags