KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejen > EjenSourceNode


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 org.ejen.ext.Version;
25 import org.ejen.util.DOMUtil;
26 import javax.xml.transform.dom.DOMSource JavaDoc;
27 import org.w3c.dom.Document JavaDoc;
28
29 /**
30  * Source node class (child of an EjenRootNode).
31  * <p>
32  * A Source node always replace an existing in memory DOM tree, even
33  * if it is the first sub-node of an "ejen" node (see
34  * {@link org.ejen.EjenRootNode#beforeProcess()}).
35  * <p>
36  * <table class="usage">
37  * <tr><th class="usage">Usage (ant build file)</th></tr>
38  * <tr><td class="usage"><pre><code>
39  * &lt;?xml version="1.0" encoding="UTF-8"?&gt;
40  *
41  * &lt;project name="generate" default="build"&gt;
42  *
43  * &lt;taskdef name="ejen" classname="org.ejen.EjenTask"/&gt;
44  *
45  * &lt;target name="build"&gt;
46  * &lt;{@link org.ejen.EjenTask ejen} ...&gt;
47  * ...
48  * <b>&lt;source [{@link #setFile(String) file}="source.xml"]/&gt;</b>
49  * ...
50  * &lt;/ejen&gt;
51  * &lt;/target&gt;
52  *
53  * &lt;/project&gt;
54  * </code></pre></td></tr></table>
55  * <p>
56  * <b>Parent nodes</b>:
57  * <ul>
58  * <li>{@link org.ejen.EjenTask ejen}
59  * </ul>
60  * @author F. Wolff
61  * @version 1.0
62  */

63 public class EjenSourceNode extends EjenChildNode {
64     protected String JavaDoc _file = null;
65
66     /**
67      * Returns the name of this EjenSourceNode (always "source").
68      * @return the name of this EjenSourceNode.
69      */

70     public String JavaDoc nodeName() {
71         return "source";
72     }
73
74     /**
75      * Returns all non null attributes of this EjenSourceNode.
76      * @return non null attributes of this EjenSourceNode.
77      */

78     public Properties JavaDoc getAttributes() {
79         Properties JavaDoc attrs = super.getAttributes();
80
81         if (_file != null) {
82             attrs.setProperty("file", _file);
83         }
84         return attrs;
85     }
86
87     /**
88      * <b>[optional/AVT]</b> - sets the file attribute.
89      * If this attribute is missing, the current DOM tree will be reset to
90      * default ({@link org.ejen.EjenConstants#DEFAULT_XML_DATA}).
91      * @param file the XML file name to load.
92      */

93     public void setFile(String JavaDoc file) {
94         _file = file;
95         putInGlobalContext("DOM_SOURCE_FILE", file);
96     }
97
98     /**
99      * Checks this EjenSourceNode for mandatory attributes.
100      */

101     public void check() {
102         super.check();
103     }
104     
105     /**
106      * Executes this EjenSourceNode. Even if a file is loaded, an "ejen-version"
107      * attribute, with the current Ejen version, is always added to the root
108      * node of the new DOM tree.
109      * @throws org.ejen.EjenException if something goes wrong...
110      */

111     public void process() {
112         super.process();
113         try {
114             Document JavaDoc doc = null;
115
116             if (_file != null) {
117                 doc = DOMUtil.parseXMLFile(evaluateAVT(_file));
118             } else {
119                 doc = DOMUtil.parseXMLString(null);
120             }
121             doc.getDocumentElement().setAttribute("ejen-version",
122                     Version.toString(null));
123             putInGlobalContext(CTX_DOM_SOURCE, new DOMSource JavaDoc(doc));
124         } catch (EjenException e) {
125             throw e;
126         } catch (Exception JavaDoc e) {
127             throw new EjenException(this, "bad source file: " + _file, e);
128         }
129     }
130 }
131
Popular Tags