KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejen > EjenMergeNode


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.ejen.util.DOMUtil;
24 import java.util.Properties JavaDoc;
25 import javax.xml.transform.dom.DOMSource JavaDoc;
26 import org.w3c.dom.Node JavaDoc;
27 import org.w3c.dom.NodeList JavaDoc;
28 import org.apache.xpath.XPathAPI;
29 import org.w3c.dom.Document JavaDoc;
30
31 /**
32  * Merge node class.
33  * <p>
34  * A merge node merges an XML file into the current in memory DOM tree.
35  * Attributes give a basic way to control the merge origin and destination.
36  * If this is not sufficient, it is still possible to use an
37  * {@link org.ejen.ext.XMLInclude} extension.
38  * <p>
39  * <table class="usage">
40  * <tr><th class="usage">Usage (ant build file)</th></tr>
41  * <tr><td class="usage"><pre><code>
42  * &lt;?xml version="1.0" encoding="UTF-8"?&gt;
43  *
44  * &lt;project name="generate" default="build"&gt;
45  *
46  * &lt;taskdef name="ejen" classname="org.ejen.EjenTask"/&gt;
47  *
48  * &lt;target name="build"&gt;
49  * &lt;{@link org.ejen.EjenTask ejen} ...&gt;
50  * ...
51  * <b>&lt;merge {@link #setFile(String) file}="merged.xml"
52  * [{@link #setSelect(String) select}="/ejen/entity-bean"]
53  * [{@link #setTo(String) to}="/ejen/others"]
54  * /&gt;</b>
55  * ...
56  * &lt;/ejen&gt;
57  * &lt;/target&gt;
58  *
59  * &lt;/project&gt;
60  * </code></pre></td></tr></table>
61  * <p>
62  * <b>Parent nodes</b>:
63  * <ul>
64  * <li>{@link org.ejen.EjenTask ejen}
65  * </ul>
66  * @author F. Wolff
67  * @version 1.0
68  * @see org.ejen.EjenSourceNode
69  * @see org.ejen.EjenSaveNode
70  * @see org.ejen.ext.XMLInclude
71  */

72 public class EjenMergeNode extends EjenChildNode {
73     protected String JavaDoc _file = null;
74     protected String JavaDoc _select = null;
75     protected String JavaDoc _to = null;
76
77     /**
78      * Returns the name of this EjenMergeNode (always "merge").
79      * @return the name of this EjenMergeNode.
80      */

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

89     public Properties JavaDoc getAttributes() {
90         Properties JavaDoc attrs = super.getAttributes();
91
92         if (_file != null) {
93             attrs.setProperty("file", _file);
94         }
95         if (_select != null) {
96             attrs.setProperty("select", _select);
97         }
98         if (_to != null) {
99             attrs.setProperty("to", _to);
100         }
101         return attrs;
102     }
103         
104     /**
105      * <b>[mandatory/AVT]</b> - sets the file attribute.
106      * @param file name of the XML file to be merged into the current in
107      * memory DOM tree.
108      */

109     public void setFile(String JavaDoc file) {
110         _file = file;
111     }
112
113     /**
114      * <b>[optional/AVT]</b> - sets the select attribute.
115      * @param select may be used to select only a sub-nodes set in the
116      * XML file to be merged. Default is root node.
117      */

118     public void setSelect(String JavaDoc select) {
119         _select = select;
120     }
121
122     /**
123      * <b>[optional/AVT]</b> - sets the to attribute.
124      * @param to may be used to change the destination node (parent
125      * node of the nodes to be merged). Default is root node.
126      */

127     public void setTo(String JavaDoc to) {
128         _to = to;
129     }
130
131     /**
132      * Checks this EjenMergeNode for mandatory attributes.
133      * @throws org.ejen.EjenException if file attribute is not set.
134      */

135     public void check() {
136         super.check();
137         if (_file == null) {
138             throw new EjenException(this, "No 'file' attribute");
139         }
140     }
141     
142     /**
143      * Executes this EjenMergeNode.
144      * @throws org.ejen.EjenException if something goes wrong...
145      */

146     public void process() {
147         super.process();
148         DOMSource JavaDoc src = null;
149
150         try {
151             src = (DOMSource JavaDoc) (getFromGlobalContext(CTX_DOM_SOURCE));
152         } catch (Exception JavaDoc e) {
153             throw new EjenException(this, null, e);
154         }
155         if (src == null) {
156             throw new EjenException(this,
157                     "no '" + CTX_DOM_SOURCE + "' in global context");
158         }
159         try {
160             Document JavaDoc srcDoc = (Document JavaDoc) (src.getNode());
161             Node JavaDoc to = srcDoc.getDocumentElement();
162
163             if (_to != null) {
164                 to = XPathAPI.selectSingleNode(srcDoc, evaluateAVT(_to));
165             }
166             Document JavaDoc mergeDoc = DOMUtil.parseXMLFile(evaluateAVT(_file));
167
168             if (_select == null) {
169                 to.appendChild(srcDoc.importNode(mergeDoc.getDocumentElement().cloneNode(true),
170                         true));
171             } else {
172                 NodeList JavaDoc nl = XPathAPI.selectNodeList(mergeDoc,
173                         evaluateAVT(_select));
174
175                 for (int i = 0; i < nl.getLength(); i++) {
176                     to.appendChild(srcDoc.importNode(nl.item(i), true));
177                 }
178             }
179         } catch (EjenException e) {
180             throw e;
181         } catch (Exception JavaDoc e) {
182             throw new EjenException(this, "merge error", e);
183         }
184     }
185 }
186
Popular Tags