import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.PrintStream;
import java.util.Vector;
import javax.swing.JMenu;
import javax.swing.JTextArea;
import org.jdom.JDOMException;
import org.w3c.dom.Document;
import fbench.FBench;
import fbench.Library;
import fbench.dom.DOMTranslationModel;
public class ProjectBuilder extends javax.swing.JFrame implements Runnable {
private static final long serialVersionUID = 3904737411250462335L;
private javax.swing.JButton exitButton;
private javax.swing.JLabel currentFileLabel;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JLabel operationLabel;
private javax.swing.JTextArea output;
private javax.swing.JProgressBar progressBar;
private boolean exitFlag = true;
private Vector<String> toBuild = new Vector<String>();
private Vector<String> toCompile = new Vector<String>();
private InputStream errStream = null;
private Process compileProc = null;
private static String errorBuffer = null;
public ProjectBuilder() {
this.initComponents();
}
public void build(String target, boolean isFile) {
exitFlag = false;
output.setText("");
progressBar.setMaximum(1);
progressBar.setMinimum(0);
progressBar.setValue(0);
exitButton.setText("Cancel");
System.out.println("Project build started.");
this.operationLabel.setText("Scanning: ");
if (isFile) {
currentFileLabel.setText(target);
String fbName = target.substring(target
.lastIndexOf(File.separatorChar));
fbName = fbName.substring(0, fbName.length() - 4);
if (target.endsWith(".fbt")) {
FBench.theBench.fileSaveAsJava(new Vector());
if (exitFlag) {
close();
return;
}
}
} else {
currentFileLabel.setText("Source code");
}
setVisible(true);
if ((isFile && target.endsWith(".fbt"))) {
output.append("Generating source code.\n");
FBench.theBench.fileSaveAsJava(new Vector());
if (exitFlag) {
setVisible(false);
return;
}
}
output.append("Locating dependencies... ");
try {
new DependencyBuilder(target, isFile, null).getDependencyTreeXML();
} catch (IOException e) {
e.printStackTrace();
} catch (JDOMException e) {
e.printStackTrace();
}
if (exitFlag) {
setVisible(false);
return;
}
// Quick and dirty parse to get FBTypes
File docFile = new File(System.getProperty("user.dir")
+ "\\DependencyTree.xml");
Vector<String> lines = new Vector<String>();
if (docFile.exists()) {
toBuild.clear();
toCompile.clear();
try {
FileInputStream fi = new FileInputStream(docFile);
BufferedReader br = new BufferedReader(
new InputStreamReader(fi));
String line = "";
while ((line = br.readLine()) != null) {
lines.add(line.trim());
}
br.close();
fi.close();
} catch (IOException e) {
// System.out.println("Unable to parse " + systemFile + ": " +
// e.getMessage());
e.printStackTrace();
}
}
for (String line : lines) {
if (exitFlag) {
setVisible(false);
return;
}
if (line.contains("<FBType Name=")) {
int start = line.indexOf("FileLocation=\"")
+ "FileLocation=\"".length();
int end = line.lastIndexOf("\"");
String loc = line.substring(start, end);
if (loc.equals("File Not Found")) {
start = line.indexOf("Type=\"") + "Type=\"".length();
end = line.indexOf("\" FileLocation");
String type = line.substring(start, end);
// publish and subscribe are special cases
if(!type.contains("SUBL") && !type.contains("PUBL") &&
!type.contains("SUBSCRIBE") && !type.contains("PUBLISH")){
System.err.println("Unable to find: " + type);
System.out.println("Build halted due to errors.");
return;
}
}
if (checkClass(loc))
continue;
String javaSourcePath = adjustPath(loc, ".java");
String javaClassPath = adjustPath(loc, ".class");
File fbFile = new File(loc);
File javaSourceFile = new File(javaSourcePath);
File classFile = new File(javaClassPath);
if (toCompile.contains(javaSourcePath))
continue;
if ((!javaSourceFile.exists()
|| (javaSourceFile.exists() && javaSourceFile
.lastModified() < fbFile.lastModified()) || (classFile
.exists() && classFile.lastModified() < fbFile
.lastModified()))
&& !toBuild.contains(loc))
addBuild(loc);
addCompile(javaSourcePath);
}
}
int totalOps = toBuild.size() + toCompile.size();
output.append("Found " + toCompile.size() + ".\n");
progressBar.setMinimum(0);
progressBar.setMaximum(totalOps);
progressBar.setValue(0);
if (!buildSource()) {
if (!compile()) {
System.out.println("Build complete.");
exitFlag = true;
operationLabel.setText("Finished");
currentFileLabel.setText("");
progressBar.setMaximum(1);
progressBar.setValue(1);
exitButton.setText("OK");
return;
}
}
System.out.println("Build ended before completion.");
}
private void close() {
setVisible(false);
dispose();
}
private void addBuild(String path) {
toBuild.add(path);
}
private void addCompile(String path) {
toCompile.add(path);
}
/**
* Checks that the given fbt doesn't already exist in fbrt.jar.
*
* @param loc
* Path to fbt file
* @return True = present in fbrt.jar
*/
private boolean checkClass(String loc) {
String srcPath = System.getProperty("user.dir") + File.separatorChar
+ "src";
String className = new String(loc);
// Handle special cases
if(className.contains("SUBL") || className.contains("PUBL") ||
className.contains("SUBSCRIBE") || className.contains("PUBLISH"))
return true;
if (className.contains(srcPath))
className = className.substring(srcPath.length() + 1);
className = className.substring(0, className.length() - 4);
className = "fb.rt." + className.replace(File.separatorChar, '.');
try {
Class theClass = Class.forName(className);
String url = theClass.getProtectionDomain().getCodeSource()
.getLocation().toExternalForm();
if (url.endsWith("fbrt.jar"))
return true;
} catch (Throwable e) {
}
return false;
}
private String adjustPath(String path, String ex) {
String result = path.replace("src", "rt\\fb\\rt".replace('\\',
File.separatorChar));
result = result.substring(0, result.length() - 4) + ex;
return result;
}
private boolean buildSource() {
boolean errorOccured = false;
String error = null;
operationLabel.setText("Building: ");
for (String fbFile : toBuild) {
if (exitFlag) {
setVisible(false);
return false;
}
currentFileLabel.setText(fbFile);
System.out.println("Building java source code: " + fbFile);
output.append("Building java source code: " + fbFile + "\n");
Document doc = Library.load(new File(fbFile));
if (doc != null) {
DOMTranslationModel translator = new DOMTranslationModel(doc,
null, "DOMtoJava.xsl", "Java");
String source = translator.performTranslation();
if (source != null) {
source = FBench.removeHTML(source);
String javaSourcePath = adjustPath(fbFile, ".java");
File javaFile = new File(javaSourcePath);
if (javaFile.exists())
javaFile.delete();
FileOutputStream fo = null;
try {
javaFile.getParentFile().mkdirs();
javaFile.createNewFile();
fo = new FileOutputStream(javaFile);
PrintStream ps = new PrintStream(fo);
ps.println(source);
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
error = "File error: " + javaSourcePath;
} catch (IOException e) {
e.printStackTrace();
error = "File error: " + javaSourcePath;
}
progressBar.setValue(progressBar.getValue() + 1);
continue;
} else
error = "Translation to java failed.";
errorOccured = true;
System.err.println(error);
output.append(error + "\n");
break;
} else
error = "File error: " + fbFile;
System.err.println(error);
errorOccured = true;
break;
}
return errorOccured;
}
private boolean compile() {
boolean errorsOccured = false;
operationLabel.setText("Compiling: ");
StringBuilder sb = new StringBuilder();
for (String sourceFile : toCompile) {
sb.append("\""+sourceFile+"\" ");
}
// if (exitFlag) {
// setVisible(false);
// return false;
// }
// currentFileLabel.setText(sourceFile);
// System.out.println("Compiling: " + sourceFile);
// output.append("Compiling: " + sourceFile + "\n");
// File file = new File(sourceFile);
// if (file.exists()) {
String sourceFiles = sb.toString();
try {
errorBuffer = null;
String compileCmd = FBench.adjustCommand(FBench.theBench
.getProps().getProperty("javac")
+ " -classpath "+"\""+System.getProperty("java.class.path")+"\""+" ");
compileProc = Runtime.getRuntime()
.exec(compileCmd + sourceFiles);
errStream = compileProc.getErrorStream();
(new Thread(this)).start();
compileProc.waitFor();
} catch (IOException e) {
e.printStackTrace();
//System.err.println("File not found: " + sourceFiles);
//output.append("File not found: " + sourceFiles + "\n");
errorsOccured = true;
// break;
} catch (InterruptedException e) {
System.err.println("Compile interrupted.");
output.append("Compile interrupted.\n");
errorsOccured = true;
// break;
}
if (errorBuffer != null && errorBuffer.length() > 0) {
output.append(errorBuffer + "\n");
System.err.println(output.getText());
errorsOccured = true;
// break;
}
progressBar.setValue(progressBar.getValue() + 1);
// } else {
// System.err.println("File not found: " + sourceFile);
// errorsOccured = true;
// break;
// }
// }
if (!errorsOccured) {
this.operationLabel.setText("Finished");
this.currentFileLabel.setText("");
output.append("Build completed successfully.");
}
return errorsOccured;
}
public void showDialog(Vector args) {
FBench fBench = ((FBench) args.get(1));
String doc = fBench.getTextArea().getText();
build(doc, false);
}
public void refreshMenu(Vector args) {
JMenu menu = (JMenu) args.get(0);
FBench fBench = (FBench) args.get(1);
menu.getMenuComponent(0).setEnabled(fBench.getTextArea() != null);
}
private void initComponents() {
setSize(800, 500);
setTitle("Build");
jScrollPane2 = new javax.swing.JScrollPane();
output = new javax.swing.JTextArea();
operationLabel = new javax.swing.JLabel();
progressBar = new javax.swing.JProgressBar();
currentFileLabel = new javax.swing.JLabel();
exitButton = new javax.swing.JButton();
output.setColumns(20);
output.setRows(5);
output.setEditable(false);
jScrollPane2.setViewportView(output);
operationLabel.setText("Building:");
currentFileLabel.setText("currentFile");
exitButton.setText("Cancel");
exitButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitButtonActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(progressBar, javax.swing.GroupLayout.DEFAULT_SIZE, 550, Short.MAX_VALUE)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 550, Short.MAX_VALUE)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addComponent(operationLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(currentFileLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 473, Short.MAX_VALUE)
.addContainerGap())
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(exitButton)
.addContainerGap())))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(operationLabel)
.addComponent(currentFileLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(progressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(exitButton)
.addContainerGap())
);
}
private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {
exitFlag = true;
close();
}
public void run() {
log(errStream, output, false);
}
public static void log(InputStream strm, JTextArea pane, boolean lnos) {
try {
LineNumberReader lrdr = new LineNumberReader(new InputStreamReader(
strm));
for (String line = lrdr.readLine(); line != null; line = lrdr
.readLine()) {
if (lnos)
errorBuffer += lrdr.getLineNumber() + " ";
errorBuffer += line + '\n';
}
lrdr.close();
} catch (IOException ioexception) {
}
}
public static void main(String[] args) {
new ProjectBuilder().setVisible(true);
}
}