KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejen > EjenFilterNode


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 javax.xml.transform.dom.DOMResult JavaDoc;
26 import org.w3c.dom.Document JavaDoc;
27 import org.w3c.dom.Node JavaDoc;
28 import org.w3c.dom.NodeList JavaDoc;
29 import org.apache.xpath.XPathAPI;
30 import org.apache.xalan.transformer.TransformerImpl;
31
32 /**
33  * Filter node class.
34  * <p>
35  * A filter node tranforms the current in memory DOM tree.
36  * <p>
37  * <table class="usage">
38  * <tr><th class="usage">Usage (ant build file)</th></tr>
39  * <tr><td class="usage"><pre><code>
40  * &lt;?xml version="1.0" encoding="UTF-8"?&gt;
41  *
42  * &lt;project name="generate" default="build"&gt;
43  *
44  * &lt;taskdef name="ejen" classname="org.ejen.EjenTask"/&gt;
45  *
46  * &lt;target name="build"&gt;
47  * &lt;{@link org.ejen.EjenTask ejen} ...&gt;
48  * ...
49  * <b>&lt;filter {@link org.ejen.EjenStylesheetNode#setFile(String) file}="filter.xml"
50  * [{@link #setForeach(String) foreach}="/ejen/entity-bean"]&gt;</b>
51  * ...
52  * [&lt;{@link org.ejen.EjenIncludeNode include} .../&gt;]
53  * [&lt;{@link org.ejen.EjenImportNode import} .../&gt;]
54  * [&lt;{@link org.ejen.EjenParamNode param} .../&gt;]
55  * ...
56  * <b>&lt;/filter&gt;</b>
57  * ...
58  * &lt;/ejen&gt;
59  * &lt;/target&gt;
60  *
61  * &lt;/project&gt;
62  * </code></pre></td></tr></table>
63  * <p>
64  * <b>Parent nodes</b>:
65  * <ul>
66  * <li>{@link org.ejen.EjenTask ejen}
67  * </ul>
68  * @author F. Wolff
69  * @version 1.0
70  */

71 public class EjenFilterNode extends EjenStylesheetNode {
72     protected String JavaDoc _foreach = null;
73
74     /**
75      * Returns the name of this EjenFilterNode (always "filter").
76      * @return the name of this EjenFilterNode.
77      */

78     public String JavaDoc nodeName() {
79         return "filter";
80     }
81
82     /**
83      * Returns all non null attributes of this EjenFilterNode.
84      * @return non null attributes of this EjenFilterNode.
85      */

86     public Properties JavaDoc getAttributes() {
87         Properties JavaDoc attrs = super.getAttributes();
88
89         if (_foreach != null) {
90             attrs.setProperty("foreach", _foreach);
91         }
92         return attrs;
93     }
94
95     /**
96      * <b>[optional/AVT]</b> - sets the foreach attribute. This attribute
97      * allows iterative applications of this filter stylesheet to a sub-nodes
98      * set of the current in memory DOM tree.
99      * <p>
100      * Suppose you have the following DOM tree in memory:
101      * <table class="usage">
102      * <tr><td class="usage"><pre><code>
103      * &lt;?xml version="1.0" encoding="iso-8859-1"?&gt;
104      * &lt;ejen&gt;
105      * &lt;name&gt;Name1&lt;/name&gt;
106      * &lt;name&gt;Name2&lt;/name&gt;
107      * &lt;name&gt;Name3&lt;/name&gt;
108      * ...
109      * &lt;/ejen&gt;
110      * </code></pre></td></tr></table>
111      * You want to transform it into this DOM tree:
112      * <table class="usage">
113      * <tr><td class="usage"><pre><code>
114      * &lt;?xml version="1.0" encoding="iso-8859-1"?&gt;
115      * &lt;ejen&gt;
116      * &lt;name&gt;Dear Name1&lt;/name&gt;
117      * &lt;name&gt;Dear Name2&lt;/name&gt;
118      * &lt;name&gt;Dear Name3&lt;/name&gt;
119      * ...
120      * &lt;/ejen&gt;
121      * </code></pre></td></tr></table>
122      * You can use this filter stylesheet (with the foreach attribute set to "/ejen/name"):
123      * <table class="usage">
124      * <tr><td class="usage"><pre><code>
125      * &lt;?xml version="1.0" encoding="iso-8859-1"?&gt;
126      * &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
127      * version="1.0"&gt;
128      * &lt;xsl:output method="xml" encoding="iso-8859-1"/&gt;
129      * <b>&lt;xsl:template match="name"&gt;
130      * &lt;name&gt;Dear &lt;xsl:value-of select="."/&gt;&lt;/name&gt;
131      * &lt;/xsl:template&gt;</b>
132      * &lt;/xsl:stylesheet&gt;
133      * </code></pre></td></tr></table>
134      * <p>
135      * If this attribute is used, a parameter whose name is "root" and value is the root
136      * node of the current DOM tree is always and automaticaly passed to the filter
137      * stylesheet (you may have the line "<code>&lt;xsl:param name"root"/&gt;</code>" in
138      * the stylesheet).
139      * <p>
140      * @param foreach foreach String (default is null, meaning "apply this filter
141      * stylesheet to the entire current DOM tree").
142      */

143     public void setForeach(String JavaDoc foreach) {
144         _foreach = foreach;
145     }
146     
147     /**
148      * Executes this EjenFilterNode.
149      * @throws org.ejen.EjenException if something goes wrong...
150      */

151     public void process() {
152         super.process();
153         TransformerImpl ti = null;
154         DOMSource JavaDoc src = null;
155
156         try {
157             ti = (TransformerImpl) (getFromContext(CTX_TRANSFORMER_IMPL));
158             src = (DOMSource JavaDoc) (getFromGlobalContext(CTX_DOM_SOURCE));
159         } catch (Exception JavaDoc e) {
160             throw new EjenException(this, null, e);
161         }
162         if (ti == null) {
163             throw new EjenException(this,
164                     "no '" + CTX_TRANSFORMER_IMPL + "' in context");
165         }
166         if (src == null) {
167             throw new EjenException(this,
168                     "no '" + CTX_DOM_SOURCE + "' in global context");
169         }
170         if (_foreach != null) {
171             NodeList JavaDoc nl = null;
172
173             try {
174                 ti.setParameter("root", src.getNode());
175                 nl = XPathAPI.selectNodeList(src.getNode(),
176                         evaluateAVT(ti, _foreach));
177             } catch (Exception JavaDoc e) {
178                 throw new EjenException(this,
179                         "invalid 'foreach' attribute: " + _foreach, e);
180             }
181             try {
182                 Document JavaDoc doc = (Document JavaDoc) (src.getNode());
183
184                 for (int i = 0; i < nl.getLength(); i++) {
185                     Node JavaDoc parent = nl.item(i).getParentNode();
186
187                     if (parent == null) {
188                         throw new EjenException(this,
189                                 "Invalid 'foreach' attribute:" + _foreach
190                                 + " (node " + i + " has no parent)");
191                     }
192                     DOMResult JavaDoc res = new DOMResult JavaDoc();
193
194                     ti.transform(new DOMSource JavaDoc(nl.item(i)), res);
195                     Node JavaDoc root = doc.importNode(((Document JavaDoc) (res.getNode())).getDocumentElement(),
196                             true);
197
198                     parent.replaceChild(root, nl.item(i));
199                 }
200             } catch (EjenException e) {
201                 throw e;
202             } catch (Exception JavaDoc e) {
203                 throw new EjenException(this, null, e);
204             }
205         } else {
206             DOMResult JavaDoc res = new DOMResult JavaDoc();
207
208             try {
209                 ti.transform(src, res);
210                 putInGlobalContext(CTX_DOM_SOURCE, new DOMSource JavaDoc(res.getNode()));
211             } catch (Exception JavaDoc e) {
212                 throw new EjenException(this, null, e);
213             }
214         }
215     }
216 }
217
Popular Tags