001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: * Paul Mahar
021: *
022: */
023: package org.enhydra.kelp.common.xmlc;
024:
025: // Enhydra XMLC imports
026: import org.enhydra.xml.xmlc.XMLCException;
027:
028: // ToolBox imports
029: import org.enhydra.tool.common.FileUtil;
030: import org.enhydra.tool.common.PathHandle;
031:
032: // Kelp imports
033: import org.enhydra.kelp.common.AbstractNodeBuilder;
034: import org.enhydra.kelp.common.Constants;
035: import org.enhydra.kelp.common.Writer;
036: import org.enhydra.kelp.common.event.WriteListener;
037: import org.enhydra.kelp.common.map.Mapper;
038: import org.enhydra.kelp.common.node.OtterTextFileNode;
039: import org.enhydra.kelp.common.node.OtterImageFileNode;
040: import org.enhydra.kelp.common.node.OtterProject;
041: import org.enhydra.kelp.common.node.OtterNode;
042: import org.enhydra.kelp.common.node.OtterXMLCNode;
043:
044: // Standard imports
045: import java.io.IOException;
046: import java.io.PrintWriter;
047: import java.io.File;
048: import java.util.ResourceBundle;
049: import java.text.MessageFormat;
050:
051: public class XMLCNodeBuilder extends AbstractNodeBuilder {
052: static ResourceBundle res = ResourceBundle
053: .getBundle("org.enhydra.kelp.common.Res"); // nores
054:
055: /**
056: * Create an instance with listeners ready to
057: * recieve output from XMLC.
058: *
059: *
060: * @param listeners
061: * A listener implements an onWrite method that is called
062: * whenever XMLC writes output. The writes are captured
063: * for display in a wizard.
064: */
065: public XMLCNodeBuilder(WriteListener[] listeners) {
066: super (listeners);
067: }
068:
069: public XMLCNodeBuilder(WriteListener listener, boolean buffer) {
070: super (listener, buffer);
071: }
072:
073: /**
074: * Loop through all selected HTML files and call XMLC for each
075: * and write out exceptions to any registered listeners.
076: */
077: public void buildImpl() {
078: if (getNodes().length >= 1) {
079: makeSelected();
080: }
081: }
082:
083: //
084: private void makeSelected() {
085: int allCount = getNodes().length;
086: int selectCount = 0;
087: double selectIndex = 0.0;
088: XMLCRunner runner = new XMLCRunner();
089:
090: refreshProgress(5, res.getString("XMLC_starting_"));
091: runner.setTraceWriter(getEchoWriter());
092: for (int i = 0; i < allCount; i++) {
093: OtterXMLCNode xmlcNode = null;
094:
095: xmlcNode = (OtterXMLCNode) getNodes()[i];
096: if (xmlcNode.isSelected()) {
097: selectCount++;
098: }
099: }
100: for (int i = 0; i < allCount; i++) {
101: OtterXMLCNode xmlcNode = null;
102:
103: xmlcNode = (OtterXMLCNode) getNodes()[i];
104: xmlcNode.setException(null);
105: if (!isFresh()) {
106: break;
107: }
108: try {
109: if (xmlcNode.isSelected() && isFresh()) {
110: File sourceDoc = null;
111: PathHandle sourcePath = null;
112: String message = new String();
113: int progValue = 0;
114: Integer[] counts = new Integer[0];
115:
116: selectIndex++;
117: progValue = (int) (5.0 + (80.0 * (selectIndex / selectCount)));
118: counts = new Integer[2];
119: counts[0] = new Integer((int) selectIndex);
120: counts[1] = new Integer(selectCount);
121: message = MessageFormat.format(res
122: .getString("XMLC_processing"), counts);
123: refreshProgress(progValue, message);
124: sourcePath = PathHandle.createPathHandle(xmlcNode
125: .getMetaDataHandler().getInputDocument());
126: if (sourcePath.getFile().exists()) {
127: if (isEcho()) {
128: getEchoWriter().println(
129: res.getString("Compiling_"));
130: getEchoWriter().println(
131: Constants.TAB4
132: + sourcePath.getPath());
133: }
134: xmlcNode.preCompile();
135: runner.setNode(xmlcNode);
136: runner.compile();
137: } else {
138: if (isEcho()) {
139: getEchoWriter()
140: .println(
141: res
142: .getString("Source_document_not")
143: + sourcePath
144: .getPath());
145: }
146: }
147: }
148: } catch (XMLCException e) {
149: e.printStackTrace();
150: xmlcNode.setException(e);
151: if (isEcho()) {
152: getEchoWriter().println(new String());
153: }
154: } catch (Throwable e) {
155: xmlcNode.setException(e);
156: if (isEcho()) {
157: getEchoWriter().println(new String());
158: }
159: }
160: }
161: if (isFresh()) {
162: refreshProgress(90, res.getString("XMLC_gathering"));
163: if (isEcho()) {
164: printResults();
165: }
166: printExceptions();
167: refreshProgress(95, res.getString("XMLC_complete"));
168: if (isEcho()) {
169: getEchoWriter().println(new String());
170: getEchoWriter().println(
171: res.getString("_XMLC_complete_"));
172: getEchoWriter().println(new String());
173: }
174: } else {
175: refreshProgress(90, res.getString("XMLC_interrupted"));
176: printExceptions();
177: getEchoWriter().println(new String());
178: getEchoWriter()
179: .println(res.getString("_XMLC_interrupted_"));
180: getEchoWriter().println(new String());
181: }
182: refreshProgress(100, res.getString("XMLC_complete"));
183: }
184:
185: /**
186: * Determine what files were created and write out the
187: * compilation results to any registered listeners.
188: */
189: public void printResults() {
190: int eCount = 0;
191: int totalCount = getNodes().length;
192: int selectCount = 0;
193: PathHandle path = null;
194:
195: getEchoWriter().println(new String());
196: getEchoWriter().println(res.getString("Gathering_results_"));
197: for (int i = 0; i < totalCount; i++) {
198: if (getNodes()[i].isSelected()
199: && getNodes()[i].getException() != null) {
200: eCount++;
201: }
202: }
203: sleepInSeconds(0.5);
204: for (int i = 0; i < totalCount; i++) {
205: if (getNodes()[i].isSelected()) {
206: selectCount++;
207: }
208: }
209: if (eCount < selectCount) {
210: getEchoWriter().println(new String());
211: getEchoWriter().println(res.getString("New_java_files_"));
212: for (int i = 0; i < totalCount; i++) {
213: OtterXMLCNode xmlcNode = (OtterXMLCNode) getNodes()[i];
214:
215: sleepInSeconds(0.1);
216: if (!isFresh()) {
217: break;
218: }
219: if (xmlcNode.isSelected() && xmlcNode.isNewJavaFound()) {
220: xmlcNode.replaceGeneratedSource(xmlcNode
221: .getMetaDataHandler().getRecompilation());
222: path = PathHandle.createPathHandle(xmlcNode
223: .getMetaDataHandler().getJavaClassSource());
224: getEchoWriter().println(
225: Constants.TAB2 + path.getPath());
226: }
227: }
228: }
229: sleepInSeconds(0.1);
230: }
231:
232: }
|