KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > common > xmlc > XMLCNodeBuilder


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  * Paul Mahar
21  *
22  */

23 package org.enhydra.kelp.common.xmlc;
24
25 // Enhydra XMLC imports
26
import org.enhydra.xml.xmlc.XMLCException;
27
28 // ToolBox imports
29
import org.enhydra.tool.common.FileUtil;
30 import org.enhydra.tool.common.PathHandle;
31
32 // Kelp imports
33
import org.enhydra.kelp.common.AbstractNodeBuilder;
34 import org.enhydra.kelp.common.Constants;
35 import org.enhydra.kelp.common.Writer;
36 import org.enhydra.kelp.common.event.WriteListener;
37 import org.enhydra.kelp.common.map.Mapper;
38 import org.enhydra.kelp.common.node.OtterTextFileNode;
39 import org.enhydra.kelp.common.node.OtterImageFileNode;
40 import org.enhydra.kelp.common.node.OtterProject;
41 import org.enhydra.kelp.common.node.OtterNode;
42 import org.enhydra.kelp.common.node.OtterXMLCNode;
43
44
45 // Standard imports
46
import java.io.IOException JavaDoc;
47 import java.io.PrintWriter JavaDoc;
48 import java.io.File JavaDoc;
49 import java.util.ResourceBundle JavaDoc;
50 import java.text.MessageFormat JavaDoc;
51
52 public class XMLCNodeBuilder extends AbstractNodeBuilder {
53     static ResourceBundle JavaDoc res =
54         ResourceBundle.getBundle("org.enhydra.kelp.common.Res"); // nores
55

56     /**
57      * Create an instance with listeners ready to
58      * recieve output from XMLC.
59      *
60      *
61      * @param listeners
62      * A listener implements an onWrite method that is called
63      * whenever XMLC writes output. The writes are captured
64      * for display in a wizard.
65      */

66     public XMLCNodeBuilder(WriteListener[] listeners) {
67         super(listeners);
68     }
69
70     public XMLCNodeBuilder(WriteListener listener, boolean buffer) {
71         super(listener, buffer);
72     }
73
74
75     /**
76      * Loop through all selected HTML files and call XMLC for each
77      * and write out exceptions to any registered listeners.
78      */

79     public void buildImpl() {
80         if (getNodes().length >= 1) {
81             makeSelected();
82         }
83     }
84
85     //
86
private void makeSelected() {
87         int allCount = getNodes().length;
88         int selectCount = 0;
89         double selectIndex = 0.0;
90         XMLCRunner runner = new XMLCRunner();
91
92         refreshProgress(5, res.getString("XMLC_starting_"));
93         runner.setTraceWriter(getEchoWriter());
94         for (int i = 0; i < allCount; i++) {
95             OtterXMLCNode xmlcNode = null;
96
97             xmlcNode = (OtterXMLCNode) getNodes()[i];
98             if (xmlcNode.isSelected()) {
99                 selectCount++;
100             }
101         }
102         for (int i = 0; i < allCount; i++) {
103             OtterXMLCNode xmlcNode = null;
104
105             xmlcNode = (OtterXMLCNode) getNodes()[i];
106             xmlcNode.setException(null);
107             if (!isFresh()) {
108                 break;
109             }
110             try {
111                 if (xmlcNode.isSelected() && isFresh()) {
112                     File JavaDoc sourceDoc = null;
113                     PathHandle sourcePath = null;
114                     String JavaDoc message = new String JavaDoc();
115                     int progValue = 0;
116                     Integer JavaDoc[] counts = new Integer JavaDoc[0];
117
118                     selectIndex++;
119                     progValue = (int) (5.0
120                                        + (80.0
121                                           * (selectIndex / selectCount)));
122                     counts = new Integer JavaDoc[2];
123                     counts[0] = new Integer JavaDoc((int) selectIndex);
124                     counts[1] = new Integer JavaDoc(selectCount);
125                     message =
126                         MessageFormat.format(res.getString("XMLC_processing"),
127                                              counts);
128                     refreshProgress(progValue, message);
129                     sourcePath =
130                         PathHandle.createPathHandle(xmlcNode.getMetaDataHandler().getInputDocument());
131                     if (sourcePath.getFile().exists()) {
132                         if (isEcho()) {
133                             getEchoWriter().println(res.getString("Compiling_"));
134                             getEchoWriter().println(Constants.TAB4
135                                                     + sourcePath.getPath());
136                         }
137                         xmlcNode.preCompile();
138                         runner.setNode(xmlcNode);
139                         runner.compile();
140                     } else {
141                         if (isEcho()) {
142                             getEchoWriter().println(res.getString("Source_document_not")
143                                                     + sourcePath.getPath());
144                         }
145                     }
146                 }
147             } catch (XMLCException e) {
148                 e.printStackTrace();
149                 xmlcNode.setException(e);
150                 if (isEcho()) {
151                     getEchoWriter().println(new String JavaDoc());
152                 }
153             } catch (Throwable JavaDoc e) {
154                 xmlcNode.setException(e);
155                 if (isEcho()) {
156                     getEchoWriter().println(new String JavaDoc());
157                 }
158             }
159         }
160         if (isFresh()) {
161             refreshProgress(90, res.getString("XMLC_gathering"));
162             if (isEcho()) {
163                 printResults();
164             }
165             printExceptions();
166             refreshProgress(95, res.getString("XMLC_complete"));
167             if (isEcho()) {
168                 getEchoWriter().println(new String JavaDoc());
169                 getEchoWriter().println(res.getString("_XMLC_complete_"));
170                 getEchoWriter().println(new String JavaDoc());
171             }
172         } else {
173             refreshProgress(90, res.getString("XMLC_interrupted"));
174             printExceptions();
175             getEchoWriter().println(new String JavaDoc());
176             getEchoWriter().println(res.getString("_XMLC_interrupted_"));
177             getEchoWriter().println(new String JavaDoc());
178         }
179         refreshProgress(100, res.getString("XMLC_complete"));
180     }
181
182     /**
183      * Determine what files were created and write out the
184      * compilation results to any registered listeners.
185      */

186     public void printResults() {
187         int eCount = 0;
188         int totalCount = getNodes().length;
189         int selectCount = 0;
190         PathHandle path = null;
191
192         getEchoWriter().println(new String JavaDoc());
193         getEchoWriter().println(res.getString("Gathering_results_"));
194         for (int i = 0; i < totalCount; i++) {
195             if (getNodes()[i].isSelected()
196                     && getNodes()[i].getException() != null) {
197                 eCount++;
198             }
199         }
200         sleepInSeconds(0.5);
201         for (int i = 0; i < totalCount; i++) {
202             if (getNodes()[i].isSelected()) {
203                 selectCount++;
204             }
205         }
206         if (eCount < selectCount) {
207             getEchoWriter().println(new String JavaDoc());
208             getEchoWriter().println(res.getString("New_java_files_"));
209             for (int i = 0; i < totalCount; i++) {
210                 OtterXMLCNode xmlcNode = (OtterXMLCNode) getNodes()[i];
211
212                 sleepInSeconds(0.1);
213                 if (!isFresh()) {
214                     break;
215                 }
216                 if (xmlcNode.isSelected() && xmlcNode.isNewJavaFound()) {
217                     xmlcNode.replaceGeneratedSource(xmlcNode.getMetaDataHandler().getRecompilation());
218                     path =
219                         PathHandle.createPathHandle(xmlcNode.getMetaDataHandler().getJavaClassSource());
220                     getEchoWriter().println(Constants.TAB2 + path.getPath());
221                 }
222             }
223         }
224         sleepInSeconds(0.1);
225     }
226
227 }
228
Popular Tags