You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by co...@apache.org on 2001/05/28 04:17:29 UTC

cvs commit: jakarta-tomcat-jasper/jasper34/liaison/org/apache/jasper34/cli CommandLineCompiler.java CommandLineContext.java JspC.java

costin      01/05/27 19:17:29

  Added:       jasper34/liaison/org/apache/jasper34/cli
                        CommandLineCompiler.java CommandLineContext.java
                        JspC.java
  Log:
  Added the "command line interface" to jasper ( JspC )
  
  Revision  Changes    Path
  1.1                  jakarta-tomcat-jasper/jasper34/liaison/org/apache/jasper34/cli/CommandLineCompiler.java
  
  Index: CommandLineCompiler.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, 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 acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", 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 apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * 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 (INCLUDING, 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.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  package org.apache.jasper34.cli;
  
  import java.io.File;
  import java.io.IOException;
  
  
  import org.apache.jasper34.core.*;
  import org.apache.jasper34.runtime.*;
  import org.apache.jasper34.generator.*;
  import org.apache.jasper34.generator.Compiler;
  
  /**
   * Overrides some methods so that we get the desired effects.
   *@author Danno Ferrin
   */
  public class CommandLineCompiler extends Compiler implements Mangler {
  
      String javaFileName;
      String classFileName;
      String pkgName;
      String className;
      File jsp;
      String outputDir;
  
      public CommandLineCompiler(CommandLineContext ctxt) {
          super(ctxt);
  
          jsp = new File(ctxt.getJspFile());
          outputDir =  ctxt.getOptions().getScratchDir().getAbsolutePath();
  
          setMangler(this);
  
          computePackageName();
          ctxt.setServletPackageName(pkgName);
          className = getBaseClassName();
          // yes this is kind of messed up ... but it works
          if (ctxt.isOutputInDirs()) {
              String pkgName = ctxt.getServletPackageName();
              if (pkgName == null) {
                  pkgName = "";
              }
              String tmpDir = outputDir
                     + File.separatorChar
                     + pkgName.replace('.', File.separatorChar);
              File f = new File(tmpDir);
              if (!f.exists()) {
                  if (f.mkdirs()) {
                      outputDir = tmpDir;
                  }
              } else {
                  outputDir = tmpDir;
              }
          }
          computeClassFileName();
          computeJavaFileName();
      }
  
  
      /**
       * Always outDated.  (Of course we are, this is an explicit invocation
       *@returns true
       */
      public boolean isOutDated() {
          return true;
      }
  
  
      public final void computeJavaFileName() {
  	javaFileName = ctxt.getServletClassName() + ".java";
  	if ("null.java".equals(javaFileName)) {
      	    javaFileName = getBaseClassName() + ".java";
      	};
  	if (outputDir != null && !outputDir.equals(""))
  	    javaFileName = outputDir + File.separatorChar + javaFileName;
      }
  
      void computeClassFileName() {
          String prefix = getPrefix(jsp.getPath());
          classFileName = prefix + getBaseClassName() + ".class";
  	if (outputDir != null && !outputDir.equals(""))
  	    classFileName = outputDir + File.separatorChar + classFileName;
      }
  
      public static String [] keywords = {
          "abstract", "boolean", "break", "byte",
          "case", "catch", "char", "class",
          "const", "continue", "default", "do",
          "double", "else", "extends", "final",
          "finally", "float", "for", "goto",
          "if", "implements", "import",
          "instanceof", "int", "interface",
          "long", "native", "new", "package",
          "private", "protected", "public",
          "return", "short", "static", "super",
          "switch", "synchronized", "this",
          "throw", "throws", "transient",
          "try", "void", "volatile", "while"
      };
  
      void computePackageName() {
  	String pathName = jsp.getPath();
  	StringBuffer modifiedpkgName = new StringBuffer ();
          int indexOfSepChar = pathName.lastIndexOf(File.separatorChar);
  
          if (("".equals(ctxt.getServletPackageName())) ||
  	    (indexOfSepChar == -1) || (indexOfSepChar == 0)) {
  	    pkgName = null;
  	} else {
  	    for (int i = 0; i < keywords.length; i++) {
  		char fs = File.separatorChar;
  		int index;
  		if (pathName.startsWith(keywords[i] + fs)) {
  		    index = 0;
  		} else {
  		    index = pathName.indexOf(fs + keywords[i] + fs);
  		}
  		while (index != -1) {
  		    String tmpathName = pathName.substring (0,index+1) + '%';
  		    pathName = tmpathName + pathName.substring (index+2);
  		    index = pathName.indexOf(fs + keywords[i] + fs);
  		}
  	    }
  	
  	    // XXX fix for paths containing '.'.
  	    // Need to be more elegant here.
              pathName = pathName.replace('.','_');
  	
  	    pkgName = pathName.substring(0, pathName.lastIndexOf(
  	    		File.separatorChar)).replace(File.separatorChar, '.');
  	    if (ctxt.getServletPackageName() != null) {
  	        pkgName = ctxt.getServletPackageName();
  	    }
  	    for (int i=0; i<pkgName.length(); i++)
  		if (Character.isLetter(pkgName.charAt(i)) == true ||
  		    pkgName.charAt(i) == '.') {
  		    modifiedpkgName.append(pkgName.substring(i,i+1));
  		}
  		else
  		    modifiedpkgName.append(mangleChar(pkgName.charAt(i)));
  
  	    if (modifiedpkgName.charAt(0) == '.') {
                  String modifiedpkgNameString = modifiedpkgName.toString();
                  pkgName = modifiedpkgNameString.substring(1,
                                                           modifiedpkgName.length ());
              }
  	    else
  	        pkgName = modifiedpkgName.toString();
  	}
  
      }
  
  					
      private final String getInitialClassName() {
          String prefix = getPrefix(jsp.getPath());
  
          return prefix + getBaseClassName() + Constants.JSP_TOKEN + "0";
      }
  
      private final String getBaseClassName() {
  	String className = ctxt.getServletClassName();
  
  	if (className == null) {
              if (jsp.getName().endsWith(".jsp"))
                  className = jsp.getName().substring(0, jsp.getName().length() - 4);
              else
                  className = jsp.getName();
          }
  	
  	// since we don't mangle extensions like the servlet does,
  	// we need to check for keywords as class names
  	for (int i = 0; i < keywords.length; i++) {
  	    if (className.equals(keywords[i])) {
  		className += "%";
  	    };
  	};
  	
  	// Fix for invalid characters. If you think of more add to the list.
  	StringBuffer modifiedClassName = new StringBuffer();
  	for (int i = 0; i < className.length(); i++) {
  	    if (Character.isLetterOrDigit(className.charAt(i)) == true)
  		modifiedClassName.append(className.substring(i,i+1));
  	    else
  		modifiedClassName.append(mangleChar(className.charAt(i)));
  	}
  	
  	return modifiedClassName.toString();
      }
  
      private final String getPrefix(String pathName) {
  	if (pathName != null) {
  	    StringBuffer modifiedName = new StringBuffer();
  	    for (int i = 0; i < pathName.length(); i++) {
  		if (Character.isLetter(pathName.charAt(i)) == true)
  		    modifiedName.append(pathName.substring(i,i+1));
  		else
  		    modifiedName.append(mangleChar(pathName.charAt(i)));
   	    }
  	    return modifiedName.toString();
  	}
  	else
              return "";
      }
  
      private static final String mangleChar(char ch) {
  	
          if(ch == File.separatorChar) {
  	    ch = '/';
  	}
  	String s = Integer.toHexString(ch);
  	int nzeros = 5 - s.length();
  	char[] result = new char[6];
  	result[0] = '_';
  	for (int i = 1; i <= nzeros; i++)
  	    result[i] = '0';
  	for (int i = nzeros+1, j = 0; i < 6; i++, j++)
  	    result[i] = s.charAt(j);
  	return new String(result);
      }
  
  
      public final String getPackageName() {
          return pkgName;
      }
  
      public final String getClassName() {
          return className;
      }
  
      public final String getJavaFileName() {
          return javaFileName;
      }
  
      public final String getClassFileName() {
          return classFileName;
      }
  
  
  }
  
  
  
  1.1                  jakarta-tomcat-jasper/jasper34/liaison/org/apache/jasper34/cli/CommandLineContext.java
  
  Index: CommandLineContext.java
  ===================================================================
  /*
   * ====================================================================
   * 
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights 
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, 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 acknowlegement:  
   *       "This product includes software developed by the 
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", 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 apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * 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 (INCLUDING, 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.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */ 
  
  package org.apache.jasper34.cli;
  
  import java.io.*;
  
  import org.apache.jasper34.generator.*;
  import org.apache.jasper34.generator.Compiler;
  import org.apache.jasper34.core.*;
  import org.apache.jasper34.runtime.*;
  
  //import org.apache.jasper.runtime.JspLoader;
  // Use the jasper loader - the only function used is to add a jar
  import org.apache.jasper34.servlet.JasperLoader;
  import org.apache.jasper34.runtime.JasperException;
  
  /**
   * Holds data used on a per-page compilation context that would otherwise spill
   * over to other pages being compiled.  Things like the taglib classloaders
   * and directives.
   *
   *@author Danno Ferrin
   */
  public class CommandLineContext implements JspCompilationContext {
  
      String classPath;
      JspReader reader;
      ServletWriter writer;
      JasperLoader loader;
      boolean errPage;
      String jspFile;
      String servletClassName;
      String servletPackageName;
      String servletJavaFileName;
      String contentType;
      Options options;
  
      String uriBase;
      File uriRoot;
  
      boolean packageNameLocked;
      boolean classNameLocked;
      boolean outputInDirs;
  
      public CommandLineContext(JasperLoader newLoader, String newClassPath,
                                String newJspFile, String newUriBase,
                                String newUriRoot, boolean newErrPage,
                                Options newOptions)
      throws JasperException
      {
          loader = newLoader;
          classPath = newClassPath;
          uriBase = newUriBase;
          String tUriRoot = newUriRoot;
          jspFile = newJspFile;
          errPage = newErrPage;
          options = newOptions;
  
          if (uriBase == null) {
              uriBase = "/";
          } else if (uriBase.charAt(0) != '/') {
              // strip the basde slash since it will be combined with the
              // uriBase to generate a file
              uriBase = "/" + uriBase;
          }
  
          if (uriBase.charAt(uriBase.length() - 1) != '/') {
              uriBase += '/';
          }
  
          if (tUriRoot == null) {
              uriRoot = new File("");
          } else {
              uriRoot = new File(tUriRoot);
              if (!uriRoot.exists() || !uriRoot.isDirectory()) {
                 throw new JasperException(
                          Constants.getString("jsp.error.jspc.uriroot_not_dir"));
              }
          }
      }
  
      /**
       * The classpath that is passed off to the Java compiler. 
       */
      public String getClassPath() {
          return classPath;
      }
      
      /**
       * Get the input reader for the JSP text. 
       */
      public JspReader getReader() {
          return reader;
      }
      
      /**
       * Where is the servlet being generated?
       */
      public ServletWriter getWriter() {
          return writer;
      }
      
      /**
       * What class loader to use for loading classes while compiling
       * this JSP? I don't think this is used right now -- akv. 
       */
      public ClassLoader getClassLoader() {
          return loader;
      }
  
      public void addJar( String jar ) throws IOException  {
  	loader.addJar( jar );
      }
      
      /**
       * Are we processing something that has been declared as an
       * errorpage? 
       */
      public boolean isErrorPage() {
          return errPage;
      }
      
      /**
       * What is the scratch directory we are generating code into?
       * FIXME: In some places this is called scratchDir and in some
       * other places it is called outputDir.
       */
      public String getOutputDir() {
          return options.getScratchDir().toString();
      }
      
      /**
       * Path of the JSP URI. Note that this is not a file name. This is
       * the context rooted URI of the JSP file. 
       */
      public String getJspFile() {
          return jspFile;
      }
      
      /**
       * Just the class name (does not include package name) of the
       * generated class. 
       */
      public String getServletClassName() {
          return servletClassName;
      }
      
      /**
       * The package name into which the servlet class is generated. 
       */
      public String getServletPackageName() {
          return servletPackageName;
      }
  
      /**
       * Utility method to get the full class name from the package and
       * class name. 
       */
      public String getFullClassName() {
          String pkg = getServletPackageName();
          if (pkg != null) {
              pkg += ".";
          } else {
              pkg = "";
          }
          return pkg + getServletClassName();
     }
  
      /**
       * Full path name of the Java file into which the servlet is being
       * generated. 
       */
      public String getServletJavaFileName() {
          if (outputInDirs) {
              return getServletPackageName().replace('.', File.separatorChar)
                     + servletJavaFileName;
          } else {
              return servletJavaFileName;
          }
      }
  
      /**
       * Are we keeping generated code around?
       */
      public boolean keepGenerated() {
          return options.getKeepGenerated();
      }
  
      /**
       * What's the content type of this JSP? Content type includes
       * content type and encoding. 
       */
      public String getContentType() {
          return contentType;
      }
  
      /**
       * Get hold of the Options object for this context. 
       */
      public Options getOptions() {
          return options;
      }
  
      public void setContentType(String contentType) {
          this.contentType = contentType;
      }
  
      public void setReader(JspReader reader) {
          this.reader = reader;
      }
      
      public void setWriter(ServletWriter writer) {
          this.writer = writer;
      }
      
      public void setServletClassName(String servletClassName) {
          if (classNameLocked) {
              //System.out.println("Did not change clazz to " + servletClassName);
          } else {
              this.servletClassName = servletClassName;
          }
      }
      
      public void setServletPackageName(String servletPackageName) {
          if (packageNameLocked) {
              //System.out.println("Did not change pkg to " + servletPackageName);
          } else {
              this.servletPackageName = servletPackageName;
          }
      }
      
      public void setServletJavaFileName(String servletJavaFileName) {
          this.servletJavaFileName = servletJavaFileName;
      }
      
      public void setErrorPage(boolean isErrPage) {
          errPage = isErrPage;
      }
  
      public void lockPackageName() {
          packageNameLocked = true;
      }
  
      public void lockClassName() {
          classNameLocked = true;
      }
  
      public void setOutputInDirs(boolean newValue) {
          outputInDirs = true;
      }
  
      public boolean isOutputInDirs() {
          return outputInDirs;
      }
  
      /**
       * Create a "Compiler" object based on some init param data. This
       * is not done yet. Right now we're just hardcoding the actual
       * compilers that are created. 
       */
      public Compiler createCompiler() throws JasperException {
          return new CommandLineCompiler(this);
      }
  
  
      /** 
       * Get the full value of a URI relative to this compilations context
       * uses current file as the base.
       */
      public String resolveRelativeUri(String uri) {
          if (uri.startsWith("/")) {
              return uri;
          } else {
              return uriBase + uri;
          }
      }
  
  
      /**
       * Gets a resource as a stream, relative to the meanings of this
       * context's implementation.
       *@returns a null if the resource cannot be found or represented 
       *         as an InputStream.
       */
      public java.io.InputStream getResourceAsStream(String res) {
          InputStream in;
          // fisrt try and get it from the URI
          try {
              in = new FileInputStream(getRealPath(res));
          } catch (IOException ioe) {
              in = null;
          }
          // next, try it as an absolute name
          if (in == null) try {
              in = new FileInputStream(res);
          } catch (IOException ioe) {
              in = null;
          }
          // that dind't work, last chance is to try the classloaders
          if (in == null) {
              in = loader.getResourceAsStream(res);
          }
          return in;
      }
  
  
      /** 
       * Gets the actual path of a URI relative to the context of
       * the compilation.
       */
      public String getRealPath(String path) {
          path = resolveRelativeUri(path);
          if (path.startsWith("/")) {
              path = path.substring(1);
          }
          File f = new File(uriRoot, path.replace('/', File.separatorChar));
          return f.getAbsolutePath();
      }
  
  }
  
  
  
  
  1.1                  jakarta-tomcat-jasper/jasper34/liaison/org/apache/jasper34/cli/JspC.java
  
  Index: JspC.java
  ===================================================================
  /*
   * ====================================================================
   * 
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 1999 The Apache Software Foundation.  All rights 
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, 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 acknowlegement:  
   *       "This product includes software developed by the 
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", 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 apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * 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 (INCLUDING, 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.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */ 
  
  package org.apache.jasper34.cli;
  
  import java.io.*;
  import java.util.*;
  
  import org.apache.jasper34.generator.*;
  import org.apache.jasper34.generator.Compiler;
  import org.apache.jasper34.runtime.*;
  import org.apache.jasper34.core.*;
  
  // XXX Will be replaced with URLClassLoader ( via JDKCompat )
  import org.apache.jasper34.servlet.JasperLoader;
  
  import org.apache.tomcat.util.log.*;
  
  /**
   * Shell for the jspc compiler.  Handles all options associated with the 
   * command line and creates compilation contexts which it then compiles
   * according to the specified options.
   * @author Danno Ferrin
   */
  public class JspC implements Options { //, JspCompilationContext {
  
      public static final String DEFAULT_IE_CLASS_ID = 
              "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93";
      
      public static final String SWITCH_VERBOSE = "-v";
      public static final String SWITCH_QUIET = "-q";
      public static final String SWITCH_OUTPUT_DIR = "-d";
      public static final String SWITCH_OUTPUT_SIMPLE_DIR = "-dd";
      public static final String SWITCH_IE_CLASS_ID = "-ieplugin";
      public static final String SWITCH_PACKAGE_NAME = "-p";
      public static final String SWITCH_CLASS_NAME = "-c";
      public static final String SWITCH_FULL_STOP = "--";
      public static final String SWITCH_URI_BASE = "-uribase";
      public static final String SWITCH_URI_ROOT = "-uriroot";
      public static final String SWITCH_FILE_WEBAPP = "-webapp";
      public static final String SWITCH_WEBAPP_INC = "-webinc";
      public static final String SWITCH_WEBAPP_XML = "-webxml";
      public static final String SWITCH_MAPPED = "-mapped";
      public static final String SWITCH_DIE = "-die";
  
      public static final int NO_WEBXML = 0;
      public static final int INC_WEBXML = 10;
      public static final int ALL_WEBXML = 20;
  
      public static final int DEFAULT_DIE_LEVEL = 1;
      public static final int NO_DIE_LEVEL = 0;
  
      // future direction
      //public static final String SWITCH_XML_OUTPUT = "-xml";
    
      
      boolean largeFile = false;
      boolean mappedFile = false;
  
      int jspVerbosityLevel = Log.INFORMATION;
  
      File scratchDir;
  
      String ieClassId = DEFAULT_IE_CLASS_ID;
  
      //String classPath;
      
      String targetPackage;
      
      String targetClassName;
  
      String uriBase;
  
      String uriRoot;
  
      String webxmlFile;
  
      int webxmlLevel;
  
      int dieLevel;
      boolean dieOnExit = false;
      static int die; // I realize it is duplication, but this is for
                      // the static main catch
  
      //JspLoader loader;
  
      boolean dirset;
  
      Vector extensions;
  
      public boolean getKeepGenerated() {
          // isn't this why we are running jspc?
          return true;
      }
      
      public boolean getLargeFile() {
          return largeFile;
      }
  
      /**
       * Are we supporting HTML mapped servlets?
       */
      public boolean getMappedFile() {
  		return mappedFile;
  	}
  
      // Off-line compiler, no need for security manager
      public Object getProtectionDomain() {
  	return null;
      }
      
      public boolean getSendErrorToClient() {
          // implied send to System.err
          return true;
      }
   
      public boolean getClassDebugInfo() {
          // compile with debug info
          return false;
      }
  
      public String getIeClassId() {
          return ieClassId;
      }
      
      public int getJspVerbosityLevel() {
          return jspVerbosityLevel;
      }
  
      public File getScratchDir() {
          return scratchDir;
      }
  
      //public String getClassPath() {
      //    return classpath;
      //}
  
      public Class getJspCompilerPlugin() {
         // we don't compile, so this is meanlingless
          return null;
      }
  
      public String getJspCompilerPath() {
         // we don't compile, so this is meanlingless
          return null;
      }
  
      public String getJavaEncoding() {
  	return "UTF-8";
      }
  
      public String getClassPath() {
          return System.getProperty("java.class.path");
      }
      
      int argPos;
      // value set by beutifully obsfucscated java
      boolean fullstop = false;
      String args[];
  
      private void pushBackArg() {
          if (!fullstop) {
              argPos--;
          }
      }
  
      private String nextArg() {
          if ((argPos >= args.length)
              || (fullstop = SWITCH_FULL_STOP.equals(args[argPos]))) {
              return null;
          } else {
              return args[argPos++];
          }
      }
          
      private String nextFile() {
          if (fullstop) argPos++;
          if (argPos >= args.length) {
              return null;
          } else {
              return args[argPos++];
          }
      }
  
      public JspC(String[] arg, PrintStream log) {
          args = arg;
          String tok;
  
          int verbosityLevel = Log.WARNING;
          dieLevel = NO_DIE_LEVEL;
          die = dieLevel;
  
  	// Hack for Runtime package
  	String rt=System.getProperty( "jsp.runtime.package" );
  	if( rt!=null ) {
  	    Constants.JSP_RUNTIME_PACKAGE=rt;
  	    Constants.JSP_SERVLET_BASE=rt+".HttpJspBase";
  	}
  	
          while ((tok = nextArg()) != null) {
              if (tok.equals(SWITCH_QUIET)) {
                  verbosityLevel = Log.WARNING;
              } else if (tok.equals(SWITCH_VERBOSE)) {
                  verbosityLevel = Log.INFORMATION;
              } else if (tok.startsWith(SWITCH_VERBOSE)) {
                  try {
                      verbosityLevel
                       = Integer.parseInt(tok.substring(SWITCH_VERBOSE.length()));
                  } catch (NumberFormatException nfe) {
                      log.println(
                          "Verbosity level " 
                          + tok.substring(SWITCH_VERBOSE.length()) 
                          + " is not valid.  Option ignored.");
                  }
              } else if (tok.equals(SWITCH_OUTPUT_DIR)) {
                  tok = nextArg();
                  if (tok != null) {
                      scratchDir = new File(new File(tok).getAbsolutePath());
                      dirset = true;
                  } else {
                      // either an in-java call with an explicit null
                      // or a "-d --" sequence should cause this,
                      // which would mean default handling
                      /* no-op */
                      scratchDir = null;
                  }
              } else if (tok.equals(SWITCH_OUTPUT_SIMPLE_DIR)) {
                  tok = nextArg();
                  if (tok != null) {
                      scratchDir = new File(new File(tok).getAbsolutePath());
                  } else {
                      // either an in-java call with an explicit null
                      // or a "-d --" sequence should cause this,
                      // which would mean default handling
                      /* no-op */
                      scratchDir = null;
                  }
              } else if (tok.equals(SWITCH_PACKAGE_NAME)) {
                  targetPackage = nextArg();
              } else if (tok.equals(SWITCH_CLASS_NAME)) {
                  targetClassName = nextArg();
              } else if (tok.equals(SWITCH_URI_BASE)) {
                  uriBase = nextArg();
              } else if (tok.equals(SWITCH_URI_ROOT)) {
                  uriRoot = nextArg();
              } else if (tok.equals(SWITCH_WEBAPP_INC)) {
                  webxmlFile = nextArg();
                  if (webxmlFile != null) {
                      webxmlLevel = INC_WEBXML;
                  }
              } else if (tok.equals(SWITCH_WEBAPP_XML)) {
                  webxmlFile = nextArg();
                  if (webxmlFile != null) {
                      webxmlLevel = ALL_WEBXML;
                  }
              } else if (tok.equals(SWITCH_MAPPED)) {
                  mappedFile = true;
              } else if (tok.startsWith(SWITCH_DIE)) {
                  try {
                      dieLevel = Integer.parseInt(
                          tok.substring(SWITCH_DIE.length()));
                  } catch (NumberFormatException nfe) {
                      dieLevel = DEFAULT_DIE_LEVEL;
                  }
                  die = dieLevel;
              } else {
                  pushBackArg();
                  // Not a recognized Option?  Start treting them as JSP Pages
                  break;
              }
          }
  
  // 	QueueLogger ql = new QueueLogger();
  // 	ql.setVerbosityLevel(verbosityLevel);
          Constants.jasperLog = Log.getLog("JASPER_LOG", this );
  //         Constants.jasperLog.setLogger( ql );
  
      }
      
      public boolean parseFile(PrintStream log, String file, Writer servletout, Writer mappingout)
      {
          try {
              JasperLoader loader =
                      new JasperLoader();
  	    loader.setParentClassLoader(getClass().getClassLoader());
  	    loader.setOptions( this);
              CommandLineContext clctxt = new CommandLineContext(
                      loader, getClassPath(), file, uriBase, uriRoot, false,
                      this);
              if ((targetClassName != null) && (targetClassName.length() > 0)) {
                  clctxt.setServletClassName(targetClassName);
                  clctxt.lockClassName();
              }
              if (targetPackage != null) {
                  clctxt.setServletPackageName(targetPackage);
                  clctxt.lockPackageName();
              }
              if (dirset) {
                  clctxt.setOutputInDirs(true);
              }
              File uriDir = new File(clctxt.getRealPath("/"));
              if (uriDir.exists()) {
                  if ((new File(uriDir, "WEB-INF/classes")).exists()) {
                      loader.addJar(clctxt.getRealPath("/WEB-INF/classes"));
                  }
                  File lib = new File(clctxt.getRealPath("WEB-INF/lib"));
                  if (lib.exists() && lib.isDirectory()) {
                      String[] libs = lib.list();
                      for (int i = 0; i < libs.length; i++) {
                          try {
                              loader.addJar(lib.getCanonicalPath()
                                      + File.separator
                                      + libs[i]);
                          } catch (IOException ioe) {
                              // failing a toCanonicalPath on a file that
                              // exists() should be a JVM regression test,
                              // therefore we have permission to freak out
                              throw new RuntimeException(ioe.toString());
                          }
                      }
                  }
              }
              CommandLineCompiler clc = new CommandLineCompiler(clctxt);
  
              clc.compile();
  
              targetClassName = null;
              String thisServletName;
              if  (clc.getPackageName() == null) {
                  thisServletName = clc.getClassName();
               } else {
                  thisServletName = clc.getPackageName()
                      + '.' + clc.getClassName();
              }
              if (servletout != null) {
                  servletout.write("\n\t<servlet>\n\t\t<servlet-name>");
                  servletout.write(thisServletName);
                  servletout.write("</servlet-name>\n\t\t<servlet-class>");
                  servletout.write(thisServletName);
                  servletout.write("</servlet-class>\n\t</servlet>\n");
              }
              if (mappingout != null) {
                  mappingout.write("\n\t<servlet-mapping>\n\t\t<url-pattern>");
                  mappingout.write(file);
                  mappingout.write("</url-pattern>\n\t\t<servlet-name>");
                  mappingout.write(thisServletName);
                  mappingout.write("</servlet-name>\n\t</servlet-mapping>\n");
              }
              return true;
          } catch (JasperException je) {
              //je.printStackTrace(log);
              Constants.message("jspc.error.jasperException", 
                      new Object[] {file, je}, Log.ERROR);
              if (dieLevel != NO_DIE_LEVEL) {
                  dieOnExit = true;
              }
          } catch (FileNotFoundException fne) {
                  Constants.message("jspc.error.fileDoesNotExist", 
                          new Object[] {fne.getMessage()}, Log.WARNING);
          } catch (Exception e) {
              Constants.message("jspc.error.generalException", 
                      new Object[] {file, e}, Log.ERROR);
              if (dieLevel != NO_DIE_LEVEL) {
                  dieOnExit = true;
              }
          }
          return false;
      }
  
  
      public void parseFiles(PrintStream log)  throws JasperException {
  
          boolean scratchDirSet = (scratchDir != null);
          boolean urirootSet = (uriRoot != null);
  
          // set up a scratch/output dir if none is provided
          if (scratchDir == null) {
              String temp = System.getProperty("java.io.tempdir");
              if (temp == null) {
                  temp = "";
              }
              scratchDir = new File(new File(temp).getAbsolutePath());
          }
  
   
          File f = new File(args[argPos]);
          while (!f.exists()) {
              boolean webApp = false;
              if (SWITCH_FILE_WEBAPP.equals(args[argPos])) {
                  webApp = true;
                  if (args.length > argPos + 1) {
                      f = new File(args[argPos + 1]);
                  } else {
                      // end of arguments, nothing left to parse
                      Constants.message("jspc.error.emptyWebApp", 
                              Log.ERROR);
                      return;
                  }
              }
              if (!f.exists()) {
                  Constants.message("jspc.error.fileDoesNotExist", 
                          new Object[] {f}, Log.WARNING);
                  argPos++;
                  if (webApp) {
                      argPos++;
                  }
                  if (argPos >= args.length) {
                      // end of arguments, nothing left to parse
                      return;
                  } else {
                      f = new File(args[argPos]);
                  }
              }
          }
          if (uriRoot == null) {
              if (SWITCH_FILE_WEBAPP.equals(args[argPos])) {
                  if (args.length > argPos + 1) {
                      f = new File(args[argPos + 1]);
                  } else {
                      // end of arguments, nothing left to parse
                      return;
                  }
              }
              // set up the uri root if none is explicitly set
              String tUriBase = uriBase;
              if (tUriBase == null) {
                  tUriBase = "/";
              }
              try {
                  if (f.exists()) {
                      f = new File(f.getCanonicalPath());
                      while (f != null) {
                          File g = new File(f, "WEB-INF");
                          if (g.exists() && g.isDirectory()) {
                              uriRoot = f.getCanonicalPath();
                              uriBase = tUriBase;
                              Constants.message("jspc.implicit.uriRoot",
                                                new Object[] { uriRoot },
                                                Log.INFORMATION);
                              break;
                          }
                          if (f.exists() && f.isDirectory()) {
                              tUriBase = "/" + f.getName() + "/" + tUriBase;
                          }
                          
                          String fParent = f.getParent();
                          if (fParent == null) {
                              f = new File(args[argPos]);
                              fParent = f.getParent();
                              if (fParent == null) {
                                  fParent = File.separator;
                              }
                              uriRoot = new File(fParent).getCanonicalPath();
                              uriBase = "/";
                              break;
                          } else {
                              f = new File(fParent);
                          }
  
                          // If there is no acceptible candidate, uriRoot will
                          // remain null to indicate to the CompilerContext to
                          // use the current working/user dir.
                      }
                  }
              } catch (IOException ioe) {
                  // since this is an optional default and a null value
                  // for uriRoot has a non-error meaning, we can just
                  // pass straight through
              }
          }
  
  
          String file = nextFile();
          File froot = new File(uriRoot);
          String ubase = null;
          try {
              ubase = froot.getCanonicalPath();
          } catch (IOException ioe) {
              // if we cannot get the base, leave it null
          }
  
          while (file != null) {
              if (SWITCH_FILE_WEBAPP.equals(file)) {
                  String base = nextFile();
                  if (base == null) {
                      Constants.message("jspc.error.emptyWebApp", 
                              Log.ERROR);
                      return;
                  }// else if (".".equals(base)) {
                  //    base = "";
                  //}
                  String oldRoot = uriRoot;
                  if (!urirootSet) {
                      uriRoot = base;
                  }
                  Vector pages = new Vector();
  
                  Stack dirs = new Stack();
                  dirs.push(base);
                  if (extensions == null) {
                      extensions = new Vector();
                      extensions.addElement("jsp");
                  }
                  while (!dirs.isEmpty()) {
                      String s = dirs.pop().toString();
                      //System.out.println("--" + s);
                      f = new File(s);
                      if (f.exists() && f.isDirectory()) {
                          String[] files = f.list();
                          String ext;
                          for (int i = 0; i < files.length; i++) {
                              File f2 = new File(s, files[i]);
                              //System.out.println(":" + f2.getPath());
                              if (f2.isDirectory()) {
                                  dirs.push(f2.getPath());
                                  //System.out.println("++" + f2.getPath());
                              } else {
                                  ext = files[i].substring(
                                          files[i].lastIndexOf('.') + 1);
                                  if (extensions.contains(ext)) {
                                      //System.out.println(s + "?" + files[i]);
                                      pages.addElement(
                                          s + File.separatorChar + files[i]);
                                  } else {
                                      //System.out.println("not done:" + ext);
                                  }
                              }
                          }
                      }
                  }
  
                  String ubaseOld = ubase;
                  File frootOld = froot;
                  froot = new File(uriRoot);
  
                  try {
                      ubase = froot.getCanonicalPath();
                  } catch (IOException ioe) {
                      // if we cannot get the base, leave it null
                  }
  
                  //System.out.println("==" + ubase);
  
  
                  Writer mapout;
                  CharArrayWriter servletout, mappingout;
                  try {
                      if (webxmlLevel >= INC_WEBXML) {
                          File fmapings = new File(webxmlFile);
                          mapout = new FileWriter(fmapings);
                          servletout = new CharArrayWriter();
                          mappingout = new CharArrayWriter();
                      } else {
                          mapout = null;
                          servletout = null;
                          mappingout = null;
                      }
                      if (webxmlLevel >= ALL_WEBXML) {
                          mapout.write(Constants.getString("jspc.webxml.header"));
                      } else if (webxmlLevel>= INC_WEBXML) {
                          mapout.write(Constants.getString("jspc.webinc.header"));
                      }
                  } catch (IOException ioe) {
                      mapout = null;
                      servletout = null;
                      mappingout = null;
                  }
  
                  Enumeration e = pages.elements();
                  while (e.hasMoreElements())
                  {
                      String nextjsp = e.nextElement().toString();
                      try {
                          if (ubase != null) {
                              File fjsp = new File(nextjsp);
                              String s = fjsp.getCanonicalPath();
                              //System.out.println("**" + s);
                              if (s.startsWith(ubase)) {
                                  nextjsp = s.substring(ubase.length());
                              }
                          }
                      } catch (IOException ioe) {
                          // if we got problems dont change the file name
                      }
  
                      if (nextjsp.startsWith("." + File.separatorChar)) {
                          nextjsp = nextjsp.substring(2);
                      }
  
                      parseFile(log, nextjsp, servletout, mappingout);
                  }
                  uriRoot = oldRoot;
                  ubase = ubaseOld;
                  froot = frootOld;
  
                  if (mapout != null) {
                      try {
                          servletout.writeTo(mapout);
                          mappingout.writeTo(mapout);
                          if (webxmlLevel >= ALL_WEBXML) {
                              mapout.write(Constants.getString("jspc.webxml.footer"));
                          } else if (webxmlLevel >= INC_WEBXML) {
                              mapout.write(Constants.getString("jspc.webinc.footer"));
                          }
                          mapout.close();
                      } catch (IOException ioe) {
                          // noting to do if it fails since we are done with it
                      }
                  }
              } else {
                  try {
                      if (ubase != null) {
                          File fjsp = new File(file);
                          String s = fjsp.getCanonicalPath();
                          if (s.startsWith(ubase)) {
                              file = s.substring(ubase.length());
                          }
                      }
                  } catch (IOException ioe) {
                       // if we got problems dont change the file name
                  }
  
                  parseFile(log, file, null, null);
              }
              file = nextFile();
          }
          if (dieOnExit) {
              System.exit(die);
          }
      }
  
      public static void main(String arg[]) {
          if (arg.length == 0) {
             System.out.println(Constants.getString("jspc.usage"));
          } else {
              try {
                  JspC jspc = new JspC(arg, System.out);
                  jspc.parseFiles(System.out);
              } catch (JasperException je) {
                  System.err.print("error:");
                  System.err.println(je.getMessage());
                  if (die != NO_DIE_LEVEL) {
                      System.exit(die);
                  }
              }
          }
      }
  
  }