KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejen > EjenRootNode


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.ext.Version;
24 import org.ejen.util.DOMUtil;
25 import org.ejen.util.XSLUtil;
26 import java.util.Vector JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import javax.xml.transform.dom.DOMSource JavaDoc;
29 import javax.xml.transform.TransformerFactory JavaDoc;
30 import org.w3c.dom.Document JavaDoc;
31 import org.apache.xalan.processor.TransformerFactoryImpl;
32
33 /**
34  * Ejen root node class (see {@link org.ejen.EjenTask EjenTask}).
35  * <p>
36  * @author F. Wolff
37  * @version 1.0
38  * @see org.ejen.EjenTask
39  * @see org.ejen.EjenSourceNode
40  * @see org.ejen.EjenMergeNode
41  * @see org.ejen.EjenSaveNode
42  * @see org.ejen.EjenFilterNode
43  * @see org.ejen.EjenTemplateNode
44  */

45 public class EjenRootNode extends EjenChildNode {
46     private Vector JavaDoc _childNodes = new Vector JavaDoc();
47
48     /**
49      * Returns the name of this node ("ejen").
50      * @return the name of this node.
51      */

52     public String JavaDoc nodeName() {
53         return "ejen";
54     }
55
56     /**
57      * Returns child nodes of this EjenRootNode.
58      * @return child nodes of this EjenRootNode.
59      */

60     public Vector JavaDoc getChildren() {
61         Vector JavaDoc children = super.getChildren();
62
63         children.addAll(_childNodes);
64         return children;
65     }
66
67     /**
68      * Appends a child node to the current list of child nodes.
69      * @param the child node to append.
70      * @throws org.ejen.EjenException if the child node is an instance
71      * of EjenParamNode, EjenIncludeNode or EjenImportNode.
72      */

73     public void appendChildNode(EjenChildNode ecn) {
74         if (ecn instanceof EjenParamNode || ecn instanceof EjenIncludeNode
75                 || ecn instanceof EjenImportNode) {
76             throw new EjenException(this, "initialisation error");
77         }
78         _childNodes.addElement(ecn);
79     }
80
81     /**
82      * Calls the check method for each child node.
83      * @throws org.ejen.EjenException if check failed.
84      */

85     public void check() {
86         super.check();
87         for (Enumeration JavaDoc e = _childNodes.elements(); e.hasMoreElements();) {
88             ((EjenChildNode) e.nextElement()).check();
89         }
90     }
91
92     /**
93      * Prepares the generation process.
94      * A default DOM tree is created by this method (see
95      * {@link org.ejen.EjenConstants#DEFAULT_XML_DATA}).
96      * @throws org.ejen.EjenException if something goes wrong...
97      */

98     public void beforeProcess() {
99         super.beforeProcess();
100         
101         try {
102             Document JavaDoc doc = DOMUtil.parseXMLString(null);
103
104             doc.getDocumentElement().setAttribute("ejen-version",
105                     Version.toString(null));
106             putInGlobalContext(CTX_DOM_SOURCE, new DOMSource JavaDoc(doc));
107             TransformerFactoryImpl tfi = (TransformerFactoryImpl) TransformerFactory.newInstance();
108
109             putInGlobalContext(CTX_TRANSFORMER_FACTORY_IMPL, tfi);
110             putInGlobalContext(CTX_TRANSFORMER_IMPL,
111                     XSLUtil.getDefaultTransformer(tfi));
112             putInGlobalContext(CTX_STYLESHEET_HANDLER, tfi.newTemplatesHandler());
113         } catch (EjenException e) {
114             throw e;
115         } catch (Exception JavaDoc e) {
116             throw new EjenException(this, "initialisation error", e);
117         }
118         
119     }
120
121     /**
122      * Starts the generation process (child nodes execution loop).
123      * @throws org.ejen.EjenException if something goes wrong.
124      */

125     public void process() {
126         super.process();
127         try {
128             for (Enumeration JavaDoc e = _childNodes.elements(); e.hasMoreElements();) {
129                 EjenChildNode ecn = (EjenChildNode) (e.nextElement());
130
131                 // sendMessageEvent("Processing " + ecn.toString());
132
_messageIndent = LOG_INDENT_STR2;
133                 ecn.beforeProcess();
134                 ecn.process();
135                 ecn.afterProcess();
136                 ecn.idle();
137                 _messageIndent = "";
138             }
139         } catch (EjenException e) {
140             throw e;
141         } catch (Exception JavaDoc e) {
142             throw new EjenException(this, null, e);
143         }
144     }
145 }
146
Popular Tags