001: package org.enhydra.kelp.ant.xmlc;
002:
003: //Kelp
004: import org.enhydra.kelp.common.*;
005: import org.enhydra.kelp.common.event.WriteListener;
006:
007: //Toolbox
008: import org.enhydra.tool.common.ToolException;
009:
010: //java
011: import java.io.File;
012: import java.io.IOException;
013: import java.io.PrintWriter;
014: import java.io.InputStream;
015: import java.io.BufferedReader;
016: import java.io.InputStreamReader;
017: import org.enhydra.kelp.ant.node.AntProject;
018: import java.io.FileWriter;
019: import java.io.BufferedWriter;
020:
021: import org.apache.tools.ant.BuildException;
022:
023: /**
024: * <p>Title: </p>
025: * <p>Description: This class execute XML compiler using <code>Ant tool</code>
026: * and target <code>xmlc</code> defined in file <code>build.xml</code> </p>
027: * <p>Copyright: Copyright (c) 2003</p>
028: * <p>Company: </p>
029: * @author Damir Milovic
030: * @version 1.0
031: */
032:
033: public class AntXMLCBuilder extends AbstractEchoBuilder {
034:
035: public AntXMLCBuilder() {
036: super ();
037: }
038:
039: public AntXMLCBuilder(WriteListener listener) {
040: super (listener);
041: }
042:
043: public AntXMLCBuilder(WriteListener[] listeners) {
044: super ();
045: for (int i = 0; i < listeners.length; i++) {
046: addWriteListener(listeners[i]);
047: }
048: }
049:
050: public AntXMLCBuilder(WriteListener listener, boolean buffer) {
051: super (listener, buffer);
052: }
053:
054: //Implementation
055: protected void buildImpl()
056: throws org.enhydra.tool.common.ToolException {
057: File root = new File(getProject().getWorkingPath());
058: try {
059: String enhydraDir = getProject().getProperty(
060: AntProject.ENHYDRA_DIR);
061: String execLine = enhydraDir + "/bin/enhydra-ant";
062: if (System.getProperty("os.name").toLowerCase().startsWith(
063: "windows")) {
064: execLine += ".bat";
065: }
066:
067: Process process = Runtime.getRuntime().exec(
068: execLine + " xmlc", null, root);
069: InputStream inputStream = process.getInputStream();
070: BufferedReader bufferedReader = new BufferedReader(
071: new InputStreamReader(inputStream));
072: InputStream errorStream = process.getErrorStream();
073: BufferedReader errorBufferedReader = new BufferedReader(
074: new InputStreamReader(errorStream));
075:
076: // init log (ouput) file:
077: File logFile = null;
078: FileWriter logFileWriter = null;
079: BufferedWriter logBuffer = null;
080: PrintWriter logPrint = null;
081: boolean logFileOK = false;
082: String logFileName = ((AntProject) getProject())
083: .getOutputFilename();
084: if (logFileName != null) {
085: logFile = new File(logFileName);
086: }
087: if ((logFile != null)
088: && (!logFile.isDirectory())
089: && ((AntProject) getProject())
090: .isOutputFileEnabled()) {
091: if (logFile.length() > 1e6) {
092: logFileWriter = new FileWriter(logFile, false /* overwrite */);
093: } else {
094: logFileWriter = new FileWriter(logFile, true /* append */);
095: }
096: logBuffer = new BufferedWriter(logFileWriter, 4096);
097: logPrint = new PrintWriter(logBuffer, false /* auto flush */);
098: logFileOK = true;
099: }
100:
101: String s = null;
102:
103: (new ErrorReader(errorBufferedReader, logFileOK, logPrint))
104: .start();
105: while ((s = bufferedReader.readLine()) != null) {
106: getEchoWriter().println(s);
107: if (logFileOK) {
108: logPrint.println(s);
109: }
110:
111: }
112: int k = process.waitFor();
113: if (k != 0)
114: throw (new BuildException("Loader: Error occured!"));
115:
116: /* */
117: //-----------------------------------------
118: if (logFileOK) {
119: logPrint.flush();
120: logPrint.close();
121: }
122: process.destroy();
123: } catch (IOException e) {
124: throw new ToolException(e, "XML Compile error: "
125: + e.toString());
126: } catch (Exception le) {
127: le.printStackTrace();
128: }
129: }
130:
131: class ErrorReader extends Thread {
132:
133: BufferedReader er;
134: boolean logFileOK2;
135: PrintWriter logPrint2;
136:
137: public ErrorReader(BufferedReader bufferedreader,
138: boolean logFileOK1, PrintWriter logPrint1) {
139: er = bufferedreader;
140: //System.out.println("Making ER");
141: logFileOK2 = logFileOK1;
142: }
143:
144: public void run() {
145: try {
146: for (; !er.ready(); Thread.sleep(100L)) {
147: }
148: String s;
149: while ((s = er.readLine()) != null) {
150: getEchoWriter().println(s);
151: if (logFileOK2) {
152: logPrint2.println(s);
153: }
154: }
155: } catch (Exception exception) {
156:
157: }
158: }
159: }
160: }
|