stefano     00/08/18 15:44:00

  Modified:    src/org/apache/cocoon/processor/xsp/language/java
                        XSPJavaProcessor.java
  Added:       src/org/apache/cocoon/processor/xsp/language/java
                        JavaCompiler.java JikesJavaCompiler.java
                        SunJavaCompiler.java
  Log:
  added multiple compilers for XSP
  
  Revision  Changes    Path
  1.11      +19 -37    
xml-cocoon/src/org/apache/cocoon/processor/xsp/language/java/XSPJavaProcessor.java
  
  Index: XSPJavaProcessor.java
  ===================================================================
  RCS file: 
/home/cvs/xml-cocoon/src/org/apache/cocoon/processor/xsp/language/java/XSPJavaProcessor.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- XSPJavaProcessor.java     2000/05/11 12:00:20     1.10
  +++ XSPJavaProcessor.java     2000/08/18 22:43:59     1.11
  @@ -1,4 +1,4 @@
  -/*-- $Id: XSPJavaProcessor.java,v 1.10 2000/05/11 12:00:20 ricardo Exp $ --
  +/*-- $Id: XSPJavaProcessor.java,v 1.11 2000/08/18 22:43:59 stefano Exp $ --
   
    ============================================================================
                      The Apache Software License, Version 1.1
  @@ -55,20 +55,20 @@
   import org.w3c.dom.*;
   import javax.servlet.http.*;
   
  -import sun.tools.javac.Main;
  -
  +import org.apache.cocoon.framework.*;
   import org.apache.cocoon.processor.xsp.*;
   import org.apache.cocoon.processor.xsp.language.*;
   
   /**
    * @author <a href="mailto:[EMAIL PROTECTED]">Ricardo Rocha</a>
  - * @version $Revision: 1.10 $ $Date: 2000/05/11 12:00:20 $
  + * @version $Revision: 1.11 $ $Date: 2000/08/18 22:43:59 $
    */
  -public class XSPJavaProcessor implements XSPLanguageProcessor {
  +public class XSPJavaProcessor implements XSPLanguageProcessor, Configurable {
     // Create class loader
     protected File repository;
     protected String encoding;
     protected XSPClassLoader classLoader;
  +  protected JavaCompiler javac;
   
     protected boolean format;
   
  @@ -76,6 +76,14 @@
       this.format = false;
     }
   
  +  public void init(Configurations conf) {
  +    String javacClassName = (String) conf.get("compiler", 
"org.apache.cocoon.processor.xsp.language.java.SunJavaCompiler");
  +    try {
  +        this.javac = (JavaCompiler) 
Class.forName(javacClassName).newInstance();
  +    } catch (Exception e) {
  +    }
  +  }
  +
     public String getSourceExtension() {
       return "java";
     }
  @@ -109,40 +117,14 @@
       String repositoryName = this.repository.getCanonicalPath();
       String fullFilename = repositoryName + File.separator + filename;
   
  -    String[] compilerArgs = null;
  -
  -    if (this.encoding == null) {
  -      compilerArgs = new String[] {
  -        "-classpath",
  -          repositoryName +
  -          File.pathSeparator +
  -          System.getProperty("java.class.path"),
  -        "-O",
  -        // "-deprecation",
  -        // "-verbose",
  -        fullFilename
  -      };
  -    } else {
  -      compilerArgs = new String[] {
  -        "-classpath",
  -          repositoryName +
  -          File.pathSeparator +
  -          System.getProperty("java.class.path"),
  -        "-O",
  -     "-encoding", this.encoding,
  -        fullFilename
  -      };
  -    }
  -
  -    ByteArrayOutputStream err = new ByteArrayOutputStream();
  -    
  -    // FIXME: we should make this reflection based and also allow other
  -    // compilers to be plugged in. Maybe we can steal... ehmmm, borrow.. some
  -    // Tomcat code for this :) (SM)
  +    ByteArrayOutputStream err = new ByteArrayOutputStream(256);
       
  -    Main compiler = new Main(err, "javac");
  +    javac.setEncoding(this.encoding);
  +    javac.setClasspath(repositoryName + File.pathSeparator + 
System.getProperty("java.class.path"));
  +    javac.setOutputDir(repositoryName);
  +    javac.setMsgOutput(err);
   
  -    boolean compilationResult = compiler.compile(compilerArgs);
  +    boolean compilationResult = javac.compile(fullFilename);
   
       if (!compilationResult) {
         // Massage message
  
  
  
  1.1                  
xml-cocoon/src/org/apache/cocoon/processor/xsp/language/java/JavaCompiler.java
  
  Index: JavaCompiler.java
  ===================================================================
  /*-- $Id: JavaCompiler.java,v 1.1 2000/08/18 22:43:58 stefano Exp $ --
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Cocoon" and  "Apache Software Foundation"  must not be used to
      endorse  or promote  products derived  from this  software without  prior
      written permission. For written permission, please contact
      [EMAIL PROTECTED]
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
   This software  consists of voluntary contributions made  by many individuals
   on  behalf of the Apache Software  Foundation and was  originally created by
   Stefano Mazzocchi  <[EMAIL PROTECTED]>. For more  information on the Apache
   Software Foundation, please see <http://www.apache.org/>.
  
   Code borrowed from the Jakarta Tomcat Project
  
   */
  
  package org.apache.cocoon.processor.xsp.language.java;
  
  import java.io.OutputStream;
  
  public interface JavaCompiler
  {
      public abstract boolean compile(String s);
  
      public abstract void setClasspath(String s);
  
      public abstract void setCompilerPath(String s);
  
      public abstract void setEncoding(String s);
  
      public abstract void setMsgOutput(OutputStream outputstream);
  
      public abstract void setOutputDir(String s);
  
  }
  
  
  
  1.1                  
xml-cocoon/src/org/apache/cocoon/processor/xsp/language/java/JikesJavaCompiler.java
  
  Index: JikesJavaCompiler.java
  ===================================================================
  /*-- $Id: JikesJavaCompiler.java,v 1.1 2000/08/18 22:43:59 stefano Exp $ --
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Cocoon" and  "Apache Software Foundation"  must not be used to
      endorse  or promote  products derived  from this  software without  prior
      written permission. For written permission, please contact
      [EMAIL PROTECTED]
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
   This software  consists of voluntary contributions made  by many individuals
   on  behalf of the Apache Software  Foundation and was  originally created by
   Stefano Mazzocchi  <[EMAIL PROTECTED]>. For more  information on the Apache
   Software Foundation, please see <http://www.apache.org/>.
  
   Code borrowed from the Jakarta Tomcat Project
  
   */
  
  package org.apache.cocoon.processor.xsp.language.java;
  
  import java.io.*;
  
  public class JikesJavaCompiler
      implements JavaCompiler
  {
      static final int OUTPUT_BUFFER_SIZE = 1024;
      static final int BUFFER_SIZE = 512;
      String encoding;
      String classpath;
      String compilerPath;
      String outdir;
      OutputStream out;
  
      class StreamPumper extends Thread
      {
  
          public void pumpStream()
              throws IOException
          {
              byte buf[] = new byte[BUFFER_SIZE];
              if(!endOfStream)
              {
                  int bytesRead = stream.read(buf, 0, BUFFER_SIZE);
                  if(bytesRead > 0)
                      out.write(buf, 0, bytesRead);
                  else
                  if(bytesRead == -1)
                      endOfStream = true;
              }
          }
  
          public void run()
          {
              try
              {
                  while(!endOfStream) 
                  {
                      pumpStream();
                      Thread.sleep(SLEEP_TIME);
                  }
  
              }
              catch(InterruptedException ex) {}
              catch(IOException ex) {}
          }
  
          private BufferedInputStream stream;
          private boolean endOfStream;
          private boolean stopSignal;
          private int SLEEP_TIME;
          private OutputStream out;
  
          public StreamPumper(BufferedInputStream is, OutputStream out)
          {
              endOfStream = false;
              stopSignal = false;
              SLEEP_TIME = 5;
              stream = is;
              this.out = out;
          }
      }
  
  
      public JikesJavaCompiler()
      {
          compilerPath = "jikes";
      }
  
      public boolean compile(String source)
      {
          int exitValue = -1;
          String compilerCmd[] = {
              compilerPath, "-classpath", classpath, "-d", outdir, "-nowarn", 
source
          };
          ByteArrayOutputStream tmpErr = new 
ByteArrayOutputStream(OUTPUT_BUFFER_SIZE);
          try
          {
              Process p = Runtime.getRuntime().exec(compilerCmd);
              BufferedInputStream compilerErr = new 
BufferedInputStream(p.getErrorStream());
              StreamPumper errPumper = new StreamPumper(compilerErr, tmpErr);
              errPumper.start();
              p.waitFor();
              exitValue = p.exitValue();
              errPumper.join();
              compilerErr.close();
              p.destroy();
              tmpErr.close();
              tmpErr.writeTo(out);
          }
          catch(IOException ex)
          {
              return false;
          }
          catch(InterruptedException ex)
          {
              return false;
          }
          boolean isOkay = exitValue == 0;
          if(tmpErr.size() > 0)
              isOkay = false;
          return isOkay;
      }
  
      public void setClasspath(String classpath)
      {
          this.classpath = classpath;
      }
  
      public void setCompilerPath(String compilerPath)
      {
          this.compilerPath = compilerPath;
      }
  
      public void setEncoding(String encoding)
      {
          this.encoding = encoding;
      }
  
      public void setMsgOutput(OutputStream out)
      {
          this.out = out;
      }
  
      public void setOutputDir(String outdir)
      {
          this.outdir = outdir;
      }
  }
  
  
  
  1.1                  
xml-cocoon/src/org/apache/cocoon/processor/xsp/language/java/SunJavaCompiler.java
  
  Index: SunJavaCompiler.java
  ===================================================================
  /*-- $Id: SunJavaCompiler.java,v 1.1 2000/08/18 22:43:59 stefano Exp $ --
  
   ============================================================================
                     The Apache Software License, Version 1.1
   ============================================================================
  
   Copyright (C) @year@ The Apache Software Foundation. All rights reserved.
  
   Redistribution and use in source and binary forms, with or without modifica-
   tion, are permitted provided that the following conditions are met:
  
   1. Redistributions of  source code must  retain the above copyright  notice,
      this list of conditions and the following disclaimer.
  
   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
  
   3. The end-user documentation included with the redistribution, if any, must
      include  the following  acknowledgment:  "This product includes  software
      developed  by the  Apache Software Foundation  (http://www.apache.org/)."
      Alternately, this  acknowledgment may  appear in the software itself,  if
      and wherever such third-party acknowledgments normally appear.
  
   4. The names "Cocoon" and  "Apache Software Foundation"  must not be used to
      endorse  or promote  products derived  from this  software without  prior
      written permission. For written permission, please contact
      [EMAIL PROTECTED]
  
   5. Products  derived from this software may not  be called "Apache", nor may
      "Apache" appear  in their name,  without prior written permission  of the
      Apache Software Foundation.
  
   THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  
   This software  consists of voluntary contributions made  by many individuals
   on  behalf of the Apache Software  Foundation and was  originally created by
   Stefano Mazzocchi  <[EMAIL PROTECTED]>. For more  information on the Apache
   Software Foundation, please see <http://www.apache.org/>.
  
   Code borrowed from the Jakarta Tomcat Project
  
   */
  
  package org.apache.cocoon.processor.xsp.language.java;
  
  import java.io.OutputStream;
  import sun.tools.javac.Main;
  
  public class SunJavaCompiler implements JavaCompiler {
      
      String encoding;
      String classpath;
      String compilerPath;
      String outdir;
      OutputStream out;
  
      public boolean compile(String source) {
          Main compiler = new Main(out, "javac");
          String args[];
          if (this.encoding == null) {
              args = new String[] {
                  "-classpath", classpath, 
                  "-d", outdir,
                  "-O",
                  source
              };
          } else {
              args = new String[] {
                  "-encoding", encoding, 
                  "-classpath", classpath, 
                  "-d", outdir,
                  "-O",
                  source
              };
          }
          return compiler.compile(args);
      }
  
      public void setClasspath(String classpath) {
          this.classpath = classpath;
      }
  
      public void setCompilerPath(String compilerPath) {
          this.compilerPath = compilerPath;
      }
  
      public void setEncoding(String encoding) {
          this.encoding = encoding;
      }
  
      public void setMsgOutput(OutputStream out) {
          this.out = out;
      }
  
      public void setOut(OutputStream out) {
          this.out = out;
      }
  
      public void setOutputDir(String outdir) {
          this.outdir = outdir;
      }
  }
  
  
  

Reply via email to