KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > ant > xmlc > AntXMLCBuilder


1 package org.enhydra.kelp.ant.xmlc;
2 //Kelp
3
import org.enhydra.kelp.common.*;
4 import org.enhydra.kelp.common.event.WriteListener;
5 import org.enhydra.kelp.ant.AntFacade;
6
7 //Toolbox
8
import org.enhydra.tool.common.ToolException;
9
10 //java
11
import java.io.File JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.io.PrintWriter JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.io.BufferedReader JavaDoc;
16 import java.io.InputStreamReader JavaDoc;
17 import org.enhydra.kelp.common.event.WriteListener;
18 import org.enhydra.kelp.ant.node.AntProject;
19 import java.io.FileWriter JavaDoc;
20 import java.io.BufferedWriter JavaDoc;
21
22 import org.apache.tools.ant.BuildException;
23
24 /**
25  * <p>Title: </p>
26  * <p>Description: This class execute XML compiler using <code>Ant tool</code>
27  * and target <code>xmlc</code> defined in file <code>build.xml</code> </p>
28  * <p>Copyright: Copyright (c) 2003</p>
29  * <p>Company: </p>
30  * @author Damir Milovic
31  * @version 1.0
32  */

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   //Implementation
56
protected void buildImpl() throws org.enhydra.tool.common.ToolException {
57     File JavaDoc root = new File JavaDoc(getProject().getWorkingPath());
58     try{
59       String JavaDoc enhydraDir = getProject().getProperty(AntProject.ENHYDRA_DIR);
60       String JavaDoc execLine = enhydraDir + "/bin/ant";
61       if (System.getProperty("os.name").toLowerCase().startsWith("windows")) {
62           execLine += ".bat";
63       }
64
65       Process JavaDoc process = Runtime.getRuntime().exec(execLine + " xmlc", null, root);
66       InputStream JavaDoc inputStream = process.getInputStream();
67       BufferedReader JavaDoc bufferedReader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(inputStream));
68       InputStream JavaDoc errorStream = process.getErrorStream();
69       BufferedReader JavaDoc errorBufferedReader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(errorStream));
70
71       // init log (ouput) file:
72
File JavaDoc logFile = null;
73       FileWriter JavaDoc logFileWriter = null;
74       BufferedWriter JavaDoc logBuffer = null;
75       PrintWriter JavaDoc logPrint = null;
76       boolean logFileOK = false;
77       String JavaDoc logFileName = ((AntProject)getProject()).getOutputFilename();
78       if (logFileName != null) {
79         logFile = new File JavaDoc(logFileName);
80       }
81       if ((logFile != null) && (!logFile.isDirectory())
82             && ((AntProject)getProject()).isOutputFileEnabled()) {
83           if (logFile.length() > 1e6) {
84               logFileWriter = new FileWriter JavaDoc(logFile, false /* overwrite */ );
85           } else {
86               logFileWriter = new FileWriter JavaDoc(logFile, true /* append */ );
87           }
88           logBuffer = new BufferedWriter JavaDoc(logFileWriter, 4096);
89           logPrint = new PrintWriter JavaDoc(logBuffer, false /* auto flush */);
90           logFileOK = true;
91       }
92
93       String JavaDoc s = null;
94       String JavaDoc er = null;
95 /* Eclipse
96       while(((s = bufferedReader.readLine()) != null) || ((er = errorBufferedReader.readLine()) != null)){
97 // while(true){
98         s = bufferedReader.readLine();
99         er = errorBufferedReader.readLine();
100         if ((s==null) && (er==null))
101           break;
102         if(s != null){
103           getEchoWriter().println(s);
104           if (logFileOK) {
105               logPrint.println(s);
106           }
107           s = null;
108         }
109         if(er != null){
110           getEchoWriter().println(er);
111           if (logFileOK) {
112               logPrint.println(er);
113           }
114           er = null; //must reset becouse second condition is not executed if firs condition is true
115         }
116       }
117 */

118 //------------------------------------------
119
/* JBuilder 8 */
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 //-----------------------------------------
135
if (logFileOK) {
136           logPrint.flush();
137           logPrint.close();
138       }
139       process.destroy();
140     }catch(IOException JavaDoc e){
141       throw new ToolException(e,"XML Compile error: "+e.toString());
142     }catch (Exception JavaDoc le) {
143       System.out.println(le);
144     }
145   }
146   class ErrorReader extends Thread JavaDoc
147   {
148
149       BufferedReader JavaDoc er;
150       boolean logFileOK2;
151       PrintWriter JavaDoc logPrint2;
152       public ErrorReader(BufferedReader JavaDoc bufferedreader,boolean logFileOK1,PrintWriter JavaDoc logPrint1)
153       {
154           er = bufferedreader;
155           //System.out.println("Making ER");
156
logFileOK2 = logFileOK1;
157       }
158
159       public void run()
160       {
161           try
162           {
163               for(; !er.ready(); Thread.sleep(100L)) { }
164               String JavaDoc 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 JavaDoc exception) {
174
175                   }
176       }
177 }
178 }
Popular Tags