stefano 00/08/21 10:41:48
Modified: src/org/apache/cocoon/components/language/programming Tag:
xml-cocoon2 CompilerError.java
LanguageCompiler.java
src/org/apache/cocoon/components/language/programming/java
Tag: xml-cocoon2 AbstractJavaCompiler.java
JavaLanguage.java Javac.java Jikes.java
Log:
added better support for Modern JavaC (found in jdk 1.3)
Revision Changes Path
No revision
No revision
1.1.2.4 +10 -1
xml-cocoon/src/org/apache/cocoon/components/language/programming/Attic/CompilerError.java
Index: CompilerError.java
===================================================================
RCS file:
/home/cvs/xml-cocoon/src/org/apache/cocoon/components/language/programming/Attic/CompilerError.java,v
retrieving revision 1.1.2.3
retrieving revision 1.1.2.4
diff -u -r1.1.2.3 -r1.1.2.4
--- CompilerError.java 2000/07/29 18:30:30 1.1.2.3
+++ CompilerError.java 2000/08/21 17:41:45 1.1.2.4
@@ -12,7 +12,7 @@
* This class encapsulates an error message produced by a programming
language
* processor (whether interpreted or compiled)
* @author <a href="mailto:[EMAIL PROTECTED]">Stefano Mazzocchi</a>
- * @version $Revision: 1.1.2.3 $ $Date: 2000/07/29 18:30:30 $
+ * @version $Revision: 1.1.2.4 $ $Date: 2000/08/21 17:41:45 $
* @since 2.0
*/
@@ -76,6 +76,15 @@
this.message = message;
}
+ /**
+ * The error message constructor.
+ *
+ * @param message The actual error text produced by the language processor
+ */
+ public CompilerError(String message) {
+ this.message = message;
+ }
+
/**
* Return the filename associated with this compiler error.
*
1.1.2.5 +3 -3
xml-cocoon/src/org/apache/cocoon/components/language/programming/Attic/LanguageCompiler.java
Index: LanguageCompiler.java
===================================================================
RCS file:
/home/cvs/xml-cocoon/src/org/apache/cocoon/components/language/programming/Attic/LanguageCompiler.java,v
retrieving revision 1.1.2.4
retrieving revision 1.1.2.5
diff -u -r1.1.2.4 -r1.1.2.5
--- LanguageCompiler.java 2000/07/29 18:30:30 1.1.2.4
+++ LanguageCompiler.java 2000/08/21 17:41:45 1.1.2.5
@@ -8,7 +8,7 @@
package org.apache.cocoon.components.language.programming;
-import java.util.Vector;
+import java.util.List;
import org.apache.avalon.Component;
import java.io.IOException;
@@ -17,7 +17,7 @@
* This interface defines a compiler's functionality for all
* (Java-based) compiled languages
* @author <a href="mailto:[EMAIL PROTECTED]">Stefano Mazzocchi</a>
- * @version $Revision: 1.1.2.4 $ $Date: 2000/07/29 18:30:30 $
+ * @version $Revision: 1.1.2.5 $ $Date: 2000/08/21 17:41:45 $
* @since 2.0
*/
public interface LanguageCompiler extends Component {
@@ -76,5 +76,5 @@
* @return The list of errors generated by this compilation
* @exception IOException If an error occurs during message collection
*/
- Vector getErrors() throws IOException;
+ List getErrors() throws IOException;
}
No revision
No revision
1.1.2.5 +22 -26
xml-cocoon/src/org/apache/cocoon/components/language/programming/java/Attic/AbstractJavaCompiler.java
Index: AbstractJavaCompiler.java
===================================================================
RCS file:
/home/cvs/xml-cocoon/src/org/apache/cocoon/components/language/programming/java/Attic/AbstractJavaCompiler.java,v
retrieving revision 1.1.2.4
retrieving revision 1.1.2.5
diff -u -r1.1.2.4 -r1.1.2.5
--- AbstractJavaCompiler.java 2000/07/29 18:30:31 1.1.2.4
+++ AbstractJavaCompiler.java 2000/08/21 17:41:46 1.1.2.5
@@ -1,12 +1,11 @@
-/**
-* ***************************************************************************
- * Copyright (C) The Apache Software Foundation. All rights reserved. *
+/*****************************************************************************
+ * Copyright (C) The Apache Software Foundation. All rights reserved.
*
* -------------------------------------------------------------------------
*
* This software is published under the terms of the Apache Software License
*
* version 1.1, a copy of which has been included with this distribution in
*
- * the LICENSE file. *
- * **************************************************************************
- */
+ * the LICENSE file.
*
+
*****************************************************************************/
+
package org.apache.cocoon.components.language.programming.java;
import java.io.*;
@@ -18,7 +17,7 @@
/**
* This class implements the functionality common to all Java compilers.
* @author <a href="mailto:[EMAIL PROTECTED]">Stefano Mazzocchi</a>
- * @version $Revision: 1.1.2.4 $ $Date: 2000/07/29 18:30:31 $
+ * @version $Revision: 1.1.2.5 $ $Date: 2000/08/21 17:41:46 $
* @since 2.0
*/
public abstract class AbstractJavaCompiler implements LanguageCompiler {
@@ -102,7 +101,7 @@
* @return The list of errors generated by this compilation
* @exception IOException If an error occurs during message collection
*/
- public Vector getErrors() throws IOException {
+ public List getErrors() throws IOException {
return parseStream(new BufferedReader(new InputStreamReader(errors)));
}
@@ -114,7 +113,7 @@
* @return The list of compiler error messages
* @exception IOException If an error occurs during message collection
*/
- protected abstract Vector parseStream(BufferedReader errors)
+ protected abstract List parseStream(BufferedReader errors)
throws IOException;
/**
@@ -123,29 +122,26 @@
* @param argument The list of compilation arguments
* @return The prepared list of compilation arguments
*/
- protected Vector fillArguments(Vector arguments) {
+ protected List fillArguments(List arguments) {
// destination directory
- arguments.addElement("-d");
- arguments.addElement(destDir);
+ arguments.add("-d");
+ arguments.add(destDir);
// classpath
- arguments.addElement("-classpath");
-
- if (System.getProperty("java.version").startsWith("1.1")) {
- arguments.addElement(classpath + File.pathSeparator + srcDir);
- } else {
- arguments.addElement(classpath);
- arguments.addElement("-sourcepath");
- arguments.addElement(srcDir);
- }
+ arguments.add("-classpath");
+ arguments.add(classpath);
+
+ // sourcepath
+ arguments.add("-sourcepath");
+ arguments.add(srcDir);
// add optimization (for what is worth)
- arguments.addElement("-O");
+ arguments.add("-O");
// add encoding if set
if (encoding != null) {
- arguments.addElement("-encoding");
- arguments.addElement(encoding);
+ arguments.add("-encoding");
+ arguments.add(encoding);
}
return arguments;
@@ -157,12 +153,12 @@
* @param arguments The compiler arguments
* @return A string array containing compilation arguments
*/
- protected String[] toStringArray(Vector arguments) {
+ protected String[] toStringArray(List arguments) {
int i;
String[] args = new String[arguments.size() + 1];
for (i = 0; i < arguments.size(); i++) {
- args[i] = (String) arguments.elementAt(i);
+ args[i] = (String) arguments.get(i);
}
args[i] = file;
1.1.2.5 +30 -26
xml-cocoon/src/org/apache/cocoon/components/language/programming/java/Attic/JavaLanguage.java
Index: JavaLanguage.java
===================================================================
RCS file:
/home/cvs/xml-cocoon/src/org/apache/cocoon/components/language/programming/java/Attic/JavaLanguage.java,v
retrieving revision 1.1.2.4
retrieving revision 1.1.2.5
diff -u -r1.1.2.4 -r1.1.2.5
--- JavaLanguage.java 2000/07/29 18:30:31 1.1.2.4
+++ JavaLanguage.java 2000/08/21 17:41:46 1.1.2.5
@@ -5,10 +5,12 @@
* version 1.1, a copy of which has been included with this distribution in
*
* the LICENSE file.
*
*****************************************************************************/
+
package org.apache.cocoon.components.language.programming.java;
import java.io.File;
-import java.util.Vector;
+import java.io.IOException;
+import java.util.List;
import org.apache.avalon.utils.Parameters;
@@ -26,9 +28,10 @@
* The Java programming language processor
*
* @author <a href="mailto:[EMAIL PROTECTED]">Ricardo Rocha</a>
- * @version CVS $Revision: 1.1.2.4 $ $Date: 2000/07/29 18:30:31 $
+ * @version CVS $Revision: 1.1.2.5 $ $Date: 2000/08/21 17:41:46 $
*/
public class JavaLanguage extends CompiledProgrammingLanguage {
+
/** The class loader */
ClassLoaderManager classLoaderManager;
@@ -126,53 +129,54 @@
) throws LanguageException {
try {
- AbstractJavaCompiler compiler =
- (AbstractJavaCompiler) this.compilerClass.newInstance();
-
+
+ AbstractJavaCompiler compiler = (AbstractJavaCompiler)
this.compilerClass.newInstance();
+
int pos = name.lastIndexOf(File.separatorChar);
String filename = name.substring(pos + 1);
String pathname =
baseDirectory + File.separator +
name.substring(0, pos).replace(File.separatorChar, '/');
-
+
compiler.setFile(
pathname + File.separator +
filename + "." + this.getSourceExtension()
);
-
+
compiler.setSource(pathname);
-
+
compiler.setDestination(baseDirectory);
-
+
compiler.setClasspath(
System.getProperty("java.class.path") + File.pathSeparator +
baseDirectory
);
-
+
if (encoding != null) {
compiler.setEncoding(encoding);
}
-
+
if (!compiler.compile()) {
- StringBuffer message =
- new StringBuffer("Error compiling " + filename + ":\n");
-
- Vector errors = compiler.getErrors();
+ StringBuffer message = new StringBuffer("Error compiling " +
filename + ":\n");
+
+ List errors = compiler.getErrors();
int count = errors.size();
for (int i = 0; i < count; i++) {
- CompilerError error = (CompilerError) errors.elementAt(i);
- message.append(
- "Line " + error.getStartLine() +
- ", column " + error.getStartColumn() +
- ": " + error.getMessage()
- );
+ CompilerError error = (CompilerError) errors.get(i);
+ message.append("Line " + error.getStartLine()
+ + ", column " + error.getStartColumn()
+ + ": " + error.getMessage());
}
-
+
throw new LanguageException(message.toString());
}
- } catch (Exception e) {
-e.printStackTrace();
- throw new LanguageException(e.getMessage());
+
+ } catch (InstantiationException e) {
+ throw new LanguageException("Could not instantiate the compiler: " +
e.getMessage());
+ } catch (IllegalAccessException e) {
+ throw new LanguageException("Could not access the compiler class: " +
e.getMessage());
+ } catch (IOException e) {
+ throw new LanguageException("Error during compilation: " +
e.getMessage());
}
}
@@ -226,7 +230,7 @@
buffer.append("\\n");
break;
case '"':
- case '\\':
+ case '\\':
buffer.append('\\');
// Fall through
default:
1.1.2.4 +117 -24
xml-cocoon/src/org/apache/cocoon/components/language/programming/java/Attic/Javac.java
Index: Javac.java
===================================================================
RCS file:
/home/cvs/xml-cocoon/src/org/apache/cocoon/components/language/programming/java/Attic/Javac.java,v
retrieving revision 1.1.2.3
retrieving revision 1.1.2.4
diff -u -r1.1.2.3 -r1.1.2.4
--- Javac.java 2000/07/29 18:30:31 1.1.2.3
+++ Javac.java 2000/08/21 17:41:46 1.1.2.4
@@ -1,9 +1,9 @@
/*****************************************************************************
- * Copyright (C) The Apache Software Foundation. All rights reserved. *
+ * Copyright (C) The Apache Software Foundation. All rights reserved.
*
* -------------------------------------------------------------------------
*
* This software is published under the terms of the Apache Software License
*
* version 1.1, a copy of which has been included with this distribution in
*
- * the LICENSE file. *
+ * the LICENSE file.
*
*****************************************************************************/
package org.apache.cocoon.components.language.programming.java;
@@ -13,13 +13,36 @@
import org.apache.cocoon.components.language.programming.*;
/**
- * This class wraps the Sun's built-in Java compiler.
+ * This class wraps the Sun's Javac Compiler.
+ *
* @author <a href="mailto:[EMAIL PROTECTED]">Stefano Mazzocchi</a>
- * @version $Revision: 1.1.2.3 $ $Date: 2000/07/29 18:30:31 $
+ * @version $Revision: 1.1.2.4 $ $Date: 2000/08/21 17:41:46 $
* @since 2.0
*/
public class Javac extends AbstractJavaCompiler {
+
+ public final static String CLASSIC_CLASS = "sun.tools.javac.Main";
+ public final static String MODERN_CLASS = "com.sun.tools.javac.Main";
+
+ private boolean modern = false;
+
+ public Javac() {
+
+ // Use reflection to be able to build on all JDKs
+ try {
+ Class.forName(MODERN_CLASS);
+ modern = true;
+ } catch (ClassNotFoundException e) {
+ try {
+ Class.forName(CLASSIC_CLASS);
+ modern = false;
+ } catch (ClassNotFoundException ex) {
+ throw new RuntimeException("No compiler found in your classpath.
Make sure you added 'tools.jar'");
+ }
+ }
+ }
+
/**
* Compile a source file yielding a loadable class file.
*
@@ -32,12 +55,11 @@
public boolean compile() throws IOException {
ByteArrayOutputStream err = new ByteArrayOutputStream();
sun.tools.javac.Main compiler = new sun.tools.javac.Main(err, "javac");
- boolean result =
- compiler.compile(toStringArray(fillArguments(new Vector(10))));
+ boolean result = compiler.compile(toStringArray(fillArguments(new
ArrayList())));
this.errors = new ByteArrayInputStream(err.toByteArray());
return result;
}
-
+
/**
* Parse the compiler error stream to produce a list of
* <code>CompilerError</code>s
@@ -46,8 +68,24 @@
* @return The list of compiler error messages
* @exception IOException If an error occurs during message collection
*/
- protected Vector parseStream(BufferedReader input) throws IOException {
- Vector errors = null;
+ protected List parseStream(BufferedReader input) throws IOException {
+ if (modern) {
+ return parseModernStream(input);
+ } else {
+ return parseClassicStream(input);
+ }
+ }
+
+ /**
+ * Parse the compiler error stream to produce a list of
+ * <code>CompilerError</code>s
+ *
+ * @param errors The error stream
+ * @return The list of compiler error messages
+ * @exception IOException If an error occurs during message collection
+ */
+ protected List parseModernStream(BufferedReader input) throws IOException {
+ List errors = null;
String line = null;
StringBuffer buffer = new StringBuffer();
@@ -55,6 +93,62 @@
// cleanup the buffer
buffer.delete(0, buffer.length());
+ // each error terminates with the '^' char
+ do {
+ if ((line = input.readLine()) == null) return errors;
+ buffer.append(line);
+ buffer.append('\n');
+ } while (!line.endsWith("^"));
+
+ // if error is found create the vector
+ if (errors == null) errors = new ArrayList();
+
+ // add the error bean
+ errors.add(parseModernError(buffer.toString()));
+ }
+ }
+
+ /**
+ * Parse an individual compiler error message with modern style.
+ *
+ * @param error The error text
+ * @return A messaged <code>CompilerError</code>
+ */
+ private CompilerError parseModernError(String error) {
+ StringTokenizer tokens = new StringTokenizer(error, ":");
+ String file = tokens.nextToken();
+ if (file.length() == 1) file += ":" + tokens.nextToken();
+ int line = Integer.parseInt(tokens.nextToken());
+
+ // FIXME (SM) finish writing a decent parser for modern errors
+ /*String message = tokens.nextToken();
+ String context = tokens.nextToken();
+ String pointer = tokens.nextToken();
+ int startcolumn = pointer.indexOf("^");
+ int endcolumn = context.indexOf(" ", startcolumn);
+ if (endcolumn == -1) endcolumn = context.length();*/
+
+ return new CompilerError(error);
+ }
+
+ /**
+ * Parse the compiler error stream to produce a list of
+ * <code>CompilerError</code>s
+ *
+ * @param errors The error stream
+ * @return The list of compiler error messages
+ * @exception IOException If an error occurs during message collection
+ */
+ protected List parseClassicStream(BufferedReader input) throws IOException
{
+
+ List errors = null;
+ String line = null;
+ StringBuffer buffer = new StringBuffer();
+
+ while (true) {
+ // cleanup the buffer
+ buffer.delete(0, buffer.length());
+
// each error has 3 lines
for (int i = 0; i < 3 ; i++) {
if ((line = input.readLine()) == null) return errors;
@@ -63,24 +157,25 @@
}
// if error is found create the vector
- if (errors == null) errors = new Vector(10);
-
+ if (errors == null) errors = new ArrayList();
+
// add the error bean
- errors.addElement(parseError(buffer.toString()));
+ errors.add(parseClassicError(buffer.toString()));
}
}
-
+
/**
- * Parse an individual compiler error message
+ * Parse an individual compiler error message with classic style.
*
* @param error The error text
- * @return A mssaged <code>CompilerError</code>
+ * @return A messaged <code>CompilerError</code>
*/
- private CompilerError parseError(String error) {
+ private CompilerError parseClassicError(String error) {
+
StringTokenizer tokens = new StringTokenizer(error, ":");
String file = tokens.nextToken();
int line = Integer.parseInt(tokens.nextToken());
-
+
tokens = new StringTokenizer(tokens.nextToken().trim(), "\n");
String message = tokens.nextToken();
String context = tokens.nextToken();
@@ -88,13 +183,11 @@
int startcolumn = pointer.indexOf("^");
int endcolumn = context.indexOf(" ", startcolumn);
if (endcolumn == -1) endcolumn = context.length();
-
- String type = "error";
-
- return new CompilerError(srcDir + File.separator + file,
type.equals("error"), line, startcolumn, line, endcolumn, message);
+
+ return new CompilerError(srcDir + File.separator + file, true, line,
startcolumn, line, endcolumn, message);
}
-
- public String getStatus() {
- return "Sun Classic JavaC";
+
+ public String toString() {
+ return "Sun Javac Compiler";
}
}
1.1.2.4 +8 -8
xml-cocoon/src/org/apache/cocoon/components/language/programming/java/Attic/Jikes.java
Index: Jikes.java
===================================================================
RCS file:
/home/cvs/xml-cocoon/src/org/apache/cocoon/components/language/programming/java/Attic/Jikes.java,v
retrieving revision 1.1.2.3
retrieving revision 1.1.2.4
diff -u -r1.1.2.3 -r1.1.2.4
--- Jikes.java 2000/07/29 18:30:31 1.1.2.3
+++ Jikes.java 2000/08/21 17:41:46 1.1.2.4
@@ -15,7 +15,7 @@
/**
* This class wraps IBM's <i>Jikes</i> Java compiler
* @author <a href="mailto:[EMAIL PROTECTED]">Stefano Mazzocchi</a>
- * @version $Revision: 1.1.2.3 $ $Date: 2000/07/29 18:30:31 $
+ * @version $Revision: 1.1.2.4 $ $Date: 2000/08/21 17:41:46 $
* @since 2.0
*/
@@ -30,7 +30,7 @@
* @exception LanguageException If an error occurs during compilation
*/
public boolean compile() throws IOException {
- Vector args = new Vector(12);
+ List args = new ArrayList();
// command line name
args.add("jikes");
// indicate Emacs output mode must be used
@@ -56,8 +56,8 @@
* @return The list of compiler error messages
* @exception IOException If an error occurs during message collection
*/
- protected Vector parseStream(BufferedReader input) throws IOException {
- Vector errors = null;
+ protected List parseStream(BufferedReader input) throws IOException {
+ List errors = null;
String line = null;
StringBuffer buffer = new StringBuffer();
@@ -79,10 +79,10 @@
}
// if error is found create the vector
- if (errors == null) errors = new Vector(10);
+ if (errors == null) errors = new ArrayList();
// add the error bean
- errors.addElement(parseError(buffer.toString()));
+ errors.add(parseError(buffer.toString()));
}
}
@@ -105,7 +105,7 @@
return new CompilerError(file, type.equals("error"), startline,
startcolumn, endline, endcolumn, message);
}
- public String getStatus() {
- return "IBM Jikes";
+ public String toString() {
+ return "IBM Jikes Compiler";
}
}