1 package org.enhydra.kelp.ant.xmlc; 2 import org.enhydra.kelp.common.*; 4 import org.enhydra.kelp.common.event.WriteListener; 5 import org.enhydra.kelp.ant.AntFacade; 6 7 import org.enhydra.tool.common.ToolException; 9 10 import java.io.File ; 12 import java.io.IOException ; 13 import java.io.PrintWriter ; 14 import java.io.InputStream ; 15 import java.io.BufferedReader ; 16 import java.io.InputStreamReader ; 17 import org.enhydra.kelp.common.event.WriteListener; 18 import org.enhydra.kelp.ant.node.AntProject; 19 import java.io.FileWriter ; 20 import java.io.BufferedWriter ; 21 22 import org.apache.tools.ant.BuildException; 23 24 33 34 public class AntXMLCBuilder extends AbstractEchoBuilder { 35 36 public AntXMLCBuilder() { 37 super(); 38 } 39 40 public AntXMLCBuilder(WriteListener listener) { 41 super(listener); 42 } 43 44 public AntXMLCBuilder(WriteListener[] listeners) { 45 super(); 46 for (int i = 0; i < listeners.length; i++) { 47 addWriteListener(listeners[i]); 48 } 49 } 50 51 public AntXMLCBuilder(WriteListener listener, boolean buffer) { 52 super(listener, buffer); 53 } 54 55 protected void buildImpl() throws org.enhydra.tool.common.ToolException { 57 File root = new File (getProject().getWorkingPath()); 58 try{ 59 String enhydraDir = getProject().getProperty(AntProject.ENHYDRA_DIR); 60 String execLine = enhydraDir + "/bin/ant"; 61 if (System.getProperty("os.name").toLowerCase().startsWith("windows")) { 62 execLine += ".bat"; 63 } 64 65 Process process = Runtime.getRuntime().exec(execLine + " xmlc", null, root); 66 InputStream inputStream = process.getInputStream(); 67 BufferedReader bufferedReader = new BufferedReader (new InputStreamReader (inputStream)); 68 InputStream errorStream = process.getErrorStream(); 69 BufferedReader errorBufferedReader = new BufferedReader (new InputStreamReader (errorStream)); 70 71 File logFile = null; 73 FileWriter logFileWriter = null; 74 BufferedWriter logBuffer = null; 75 PrintWriter logPrint = null; 76 boolean logFileOK = false; 77 String logFileName = ((AntProject)getProject()).getOutputFilename(); 78 if (logFileName != null) { 79 logFile = new File (logFileName); 80 } 81 if ((logFile != null) && (!logFile.isDirectory()) 82 && ((AntProject)getProject()).isOutputFileEnabled()) { 83 if (logFile.length() > 1e6) { 84 logFileWriter = new FileWriter (logFile, false ); 85 } else { 86 logFileWriter = new FileWriter (logFile, true ); 87 } 88 logBuffer = new BufferedWriter (logFileWriter, 4096); 89 logPrint = new PrintWriter (logBuffer, false ); 90 logFileOK = true; 91 } 92 93 String s = null; 94 String er = null; 95 118 120 (new ErrorReader(errorBufferedReader,logFileOK,logPrint)).start(); 121 while((s = bufferedReader.readLine()) != null) 122 { 123 getEchoWriter().println(s); 124 if (logFileOK) { 125 logPrint.println(s); 126 } 127 128 } 129 int k = process.waitFor(); 130 if (k!=0) 131 throw (new BuildException("Loader: Error occured!")); 132 133 134 if (logFileOK) { 136 logPrint.flush(); 137 logPrint.close(); 138 } 139 process.destroy(); 140 }catch(IOException e){ 141 throw new ToolException(e,"XML Compile error: "+e.toString()); 142 }catch (Exception le) { 143 System.out.println(le); 144 } 145 } 146 class ErrorReader extends Thread 147 { 148 149 BufferedReader er; 150 boolean logFileOK2; 151 PrintWriter logPrint2; 152 public ErrorReader(BufferedReader bufferedreader,boolean logFileOK1,PrintWriter logPrint1) 153 { 154 er = bufferedreader; 155 logFileOK2 = logFileOK1; 157 } 158 159 public void run() 160 { 161 try 162 { 163 for(; !er.ready(); Thread.sleep(100L)) { } 164 String s; 165 while((s = er.readLine()) != null) 166 { 167 getEchoWriter().println(s); 168 if (logFileOK2) { 169 logPrint2.println(s); 170 } 171 } 172 } 173 catch(Exception exception) { 174 175 } 176 } 177 } 178 } | Popular Tags |