vgritsenko 02/02/08 19:32:05 Modified: src/java/org/apache/cocoon/components/language/programming/javascript JavascriptLanguage.java Added: lib/optional rhino-1.5r3.jar src/java/org/apache/cocoon/components/language/programming/javascript CompiledJavascriptLanguage.java JavascriptProgram.java Log: Javascript changes: - Upgrade rhino - Move compiled javascript to CompiledJavascriptLanguage - Add JavascriptProgram representing interpreted Javascripts Cocoon Programs Revision Changes Path 1.1 xml-cocoon2/lib/optional/rhino-1.5r3.jar <<Binary file>> 1.5 +94 -63 xml-cocoon2/src/java/org/apache/cocoon/components/language/programming/javascript/JavascriptLanguage.java Index: JavascriptLanguage.java =================================================================== RCS file: /home/cvs/xml-cocoon2/src/java/org/apache/cocoon/components/language/programming/javascript/JavascriptLanguage.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- JavascriptLanguage.java 4 Feb 2002 12:22:24 -0000 1.4 +++ JavascriptLanguage.java 9 Feb 2002 03:32:05 -0000 1.5 @@ -56,74 +56,105 @@ package org.apache.cocoon.components.language.programming.javascript; import org.apache.cocoon.components.language.LanguageException; -import org.apache.cocoon.components.language.programming.java.JavaLanguage; -import org.mozilla.javascript.tools.jsc.Main; +import org.apache.cocoon.components.language.markup.xsp.java.XSLTExtension; +import org.apache.cocoon.components.language.programming.AbstractProgrammingLanguage; +import org.apache.cocoon.components.language.programming.Program; +import org.apache.cocoon.components.language.programming.ProgrammingLanguage; +import org.apache.cocoon.util.ClassUtils; +import org.apache.cocoon.util.IOUtils; +import java.io.BufferedReader; import java.io.File; +import java.io.FileReader; +import java.io.IOException; /** - * The compiled Javascript (Rhino) programming language processor + * The interpreted Javascript programming language. + * Program in Javascript must have comment line as first line of file: + * <pre> + * // $Cocoon extends: org.apache.cocoon.components.language.xsp.JSGenerator$ + * </pre> + * The class specified will be used as a Java wrapper interpreting javascript program. * - * @author <a href="mailto:[EMAIL PROTECTED]">Ricardo Rocha</a> - * @version CVS $Id: JavascriptLanguage.java,v 1.4 2002/02/04 12:22:24 cziegeler Exp $ + * @author <a href="mailto:[EMAIL PROTECTED]">Vadim Gritsenko</a> + * @version CVS $Id: JavascriptLanguage.java,v 1.5 2002/02/09 03:32:05 vgritsenko Exp $ */ -public class JavascriptLanguage extends JavaLanguage -{ - /** - * Return the language name - * - * @return The language name - */ - public String getName() { - return "javascript"; - } - - /** - * Return the language's canonical source file extension. - * - * @return The source file extension - */ - public String getSourceExtension() { - return "js"; - } - - /** - * Compile a source file yielding a loadable class file. - * - * @param filename The object program base file name - * @param baseDirectory The directory containing the object program file - * @param encoding The encoding expected in the source file or - * <code>null</code> if it is the platform's default encoding - * @exception LanguageException If an error occurs during compilation - */ - protected void compile( - String name, File baseDirectory, String encoding - ) throws LanguageException { - try { - Main compiler = (Main) this.compilerClass.newInstance(); - - int pos = name.lastIndexOf(File.separatorChar); - String filename = name.substring(pos + 1); - String pathname = - baseDirectory.getCanonicalPath() + File.separator + - name.substring(0, pos).replace(File.separatorChar, '/'); - String packageName = - name.substring(0, pos).replace(File.separatorChar, '.'); - - String[] args = { - "-extends", - "org.apache.cocoon.components.language.markup.xsp.javascript.JSGenerator", - "-nosource", - "-O", "9", - "-package", packageName, - new StringBuffer(pathname).append(File.separator) - .append(filename).append(".").append(this.getSourceExtension()).toString() - }; - - compiler.main(args); - } catch (Exception e) { - getLogger().warn("JavascriptLanguage.compile", e); - throw new LanguageException(e.getMessage()); +public class JavascriptLanguage extends AbstractProgrammingLanguage implements ProgrammingLanguage { + + public Program load(String filename, File baseDirectory, String encoding) throws LanguageException { + // Does source file exist? + File sourceFile = new File(baseDirectory, + filename + "." + this.getSourceExtension()); + if (!sourceFile.exists()) { + throw new LanguageException("Can't load program - File doesn't exist: " + + IOUtils.getFullFilename(sourceFile)); + } + if (!sourceFile.isFile()) { + throw new LanguageException("Can't load program - File is not a normal file: " + + IOUtils.getFullFilename(sourceFile)); + } + if (!sourceFile.canRead()) { + throw new LanguageException("Can't load program - File cannot be read: " + + IOUtils.getFullFilename(sourceFile)); + } + + Class clazz = null; + String className = null; + BufferedReader r = null; + try { + r = new BufferedReader(new FileReader(sourceFile)); + className = r.readLine(); + if (className != null) { + int i = className.indexOf("$Cocoon extends: "); + if (i != -1) { + int j = className.indexOf("$", i + 1); + if (j != -1) { + className = className.substring(i + "$Cocoon extends: ".length(), j); + } else { + className = null; + } + } else { + className = null; + } + } + if (className == null) { + throw new LanguageException("Can't load program - Signature is not found: " + + IOUtils.getFullFilename(sourceFile)); + } + + clazz = ClassUtils.loadClass(className); + } catch (IOException e) { + throw new LanguageException("Can't load program - Signature is not found: " + + IOUtils.getFullFilename(sourceFile)); + } catch (ClassNotFoundException e) { + throw new LanguageException("Can't load program - Base class " + className + " is not found: " + + IOUtils.getFullFilename(sourceFile)); + } finally { + if (r != null) try { + r.close(); + } catch (IOException ignored) { + } + } + + return new JavascriptProgram(sourceFile, clazz); + } + + protected void doUnload(Object program, String filename, File baseDir) + throws LanguageException { + System.out.println("IJavascriptLanguage: Unloading " + filename); + // Do nothing. Source is already deleted by the AbstractProgrammingLanguage. + } + + public String quoteString(String constant) { + return XSLTExtension.escapeString(constant); + } + + /** + * Return the language's canonical source file extension. + * + * @return The source file extension + */ + public String getSourceExtension() { + return "js"; } - } } 1.1 xml-cocoon2/src/java/org/apache/cocoon/components/language/programming/javascript/CompiledJavascriptLanguage.java Index: CompiledJavascriptLanguage.java =================================================================== /* * The Apache Software License, Version 1.1 * * * Copyright (c) 2001 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 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 "Apache 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 (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.cocoon.components.language.programming.javascript; import org.apache.cocoon.components.language.LanguageException; import org.apache.cocoon.components.language.programming.java.JavaLanguage; import org.mozilla.javascript.tools.jsc.Main; import java.io.File; /** * The compiled Javascript (Rhino) programming language processor * * @author <a href="mailto:[EMAIL PROTECTED]">Ricardo Rocha</a> * @version CVS $Id: CompiledJavascriptLanguage.java,v 1.1 2002/02/09 03:32:05 vgritsenko Exp $ */ public class CompiledJavascriptLanguage extends JavaLanguage { /** * Return the language's canonical source file extension. * * @return The source file extension */ public String getSourceExtension() { return "js"; } /** * Compile a source file yielding a loadable class file. * * @param filename The object program base file name * @param baseDirectory The directory containing the object program file * @param encoding The encoding expected in the source file or * <code>null</code> if it is the platform's default encoding * @exception LanguageException If an error occurs during compilation */ protected void compile( String name, File baseDirectory, String encoding ) throws LanguageException { try { Main compiler = (Main) this.compilerClass.newInstance(); int pos = name.lastIndexOf(File.separatorChar); String filename = name.substring(pos + 1); String pathname = baseDirectory.getCanonicalPath() + File.separator + name.substring(0, pos).replace(File.separatorChar, '/'); String packageName = name.substring(0, pos).replace(File.separatorChar, '.'); String[] args = { "-extends", "org.apache.cocoon.components.language.markup.xsp.JSGenerator", "-nosource", "-O", "9", "-package", packageName, "-o", filename + ".class", pathname + File.separator + filename + "." + this.getSourceExtension() }; compiler.main(args); } catch (Exception e) { getLogger().warn("JavascriptLanguage.compile", e); throw new LanguageException(e.getMessage()); } } } 1.1 xml-cocoon2/src/java/org/apache/cocoon/components/language/programming/javascript/JavascriptProgram.java Index: JavascriptProgram.java =================================================================== /* * The Apache Software License, Version 1.1 * * * Copyright (c) 2001 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 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 "Apache 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 (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.cocoon.components.language.programming.javascript; import org.apache.avalon.framework.component.ComponentManager; import org.apache.avalon.framework.configuration.Configurable; import org.apache.avalon.framework.configuration.DefaultConfiguration; import org.apache.avalon.framework.context.Context; import org.apache.avalon.excalibur.component.ComponentHandler; import org.apache.avalon.excalibur.component.RoleManager; import org.apache.avalon.excalibur.logger.LogKitManager; import org.apache.cocoon.components.language.generator.CompiledComponent; import org.apache.cocoon.components.language.programming.Program; import java.io.File; public class JavascriptProgram implements Program { protected File file; protected Class clazz; protected DefaultConfiguration config; public JavascriptProgram(File file, Class clazz) { this.file = file; this.clazz = clazz; config = new DefaultConfiguration("", "GeneratorSelector"); DefaultConfiguration child = new DefaultConfiguration("file", ""); child.setValue(file.toString()); config.addChild(child); } public String getName() { return file.toString(); } public ComponentHandler getHandler(ComponentManager manager, Context context, RoleManager roles, LogKitManager logKitManager) throws Exception { return ComponentHandler.getComponentHandler( clazz, config, manager, context, roles, logKitManager); } public CompiledComponent newInstance() throws Exception { CompiledComponent instance = (CompiledComponent) clazz.newInstance(); if (instance instanceof Configurable) ((Configurable) instance).configure(config); return instance; } }
---------------------------------------------------------------------- In case of troubles, e-mail: [EMAIL PROTECTED] To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]