Author: tcurdt
Date: Sun Feb 25 16:47:10 2007
New Revision: 511677

URL: http://svn.apache.org/viewvc?view=rev&rev=511677
Log:
a first sort of working version of compiling javascript to java through rhino 
via jci. still needs some work


Added:
    jakarta/commons/sandbox/jci/trunk/compilers/rhino/
    jakarta/commons/sandbox/jci/trunk/compilers/rhino/pom.xml   (with props)
    jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/
    jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/
    jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/
    jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/
    jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/
    
jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/
    
jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/
    
jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/
    
jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/RhinoCompilationProblem.java
   (with props)
    
jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/RhinoCompilingClassLoader.java
   (with props)
    
jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/RhinoJavaCompiler.java
   (with props)
    jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/test/
    jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/test/java/
    jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/test/java/org/
    jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/test/java/org/apache/
    
jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/test/java/org/apache/commons/
    
jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/test/java/org/apache/commons/jci/
    
jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/test/java/org/apache/commons/jci/compilers/
    
jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/test/java/org/apache/commons/jci/compilers/RhinoJavaCompilerTestCase.java
   (with props)

Added: jakarta/commons/sandbox/jci/trunk/compilers/rhino/pom.xml
URL: 
http://svn.apache.org/viewvc/jakarta/commons/sandbox/jci/trunk/compilers/rhino/pom.xml?view=auto&rev=511677
==============================================================================
--- jakarta/commons/sandbox/jci/trunk/compilers/rhino/pom.xml (added)
+++ jakarta/commons/sandbox/jci/trunk/compilers/rhino/pom.xml Sun Feb 25 
16:47:10 2007
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project
+  xmlns="http://maven.apache.org/POM/4.0.0";
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd";>
+
+  <modelVersion>4.0.0</modelVersion>
+  <parent>
+    <groupId>org.apache.commons</groupId>
+    <artifactId>commons-jci</artifactId>
+    <version>1.0-SNAPSHOT</version>
+  </parent>
+
+  <packaging>jar</packaging>
+  <artifactId>commons-jci-rhino</artifactId>
+  <version>1.0-SNAPSHOT</version>
+  <name>compiler-rhino</name>
+
+  <dependencies>
+
+    <dependency>
+      <groupId>org.apache.commons</groupId>
+      <artifactId>commons-jci-core</artifactId>
+      <version>1.0-SNAPSHOT</version>
+    </dependency>
+
+    <dependency>
+      <groupId>rhino</groupId>
+      <artifactId>js</artifactId>
+      <version>1.6R5</version>
+    </dependency>
+
+  </dependencies>
+  
+</project>

Propchange: jakarta/commons/sandbox/jci/trunk/compilers/rhino/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/commons/sandbox/jci/trunk/compilers/rhino/pom.xml
------------------------------------------------------------------------------
    svn:keywords = Date Revision Author HeadURL Id

Propchange: jakarta/commons/sandbox/jci/trunk/compilers/rhino/pom.xml
------------------------------------------------------------------------------
    svn:mime-type = text/xml

Added: 
jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/RhinoCompilationProblem.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/RhinoCompilationProblem.java?view=auto&rev=511677
==============================================================================
--- 
jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/RhinoCompilationProblem.java
 (added)
+++ 
jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/RhinoCompilationProblem.java
 Sun Feb 25 16:47:10 2007
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.jci.compilers;
+
+import org.apache.commons.jci.problems.CompilationProblem;
+
+public final class RhinoCompilationProblem implements CompilationProblem {
+
+       public int getEndColumn() {
+               // TODO Auto-generated method stub
+               return 0;
+       }
+
+       public int getEndLine() {
+               // TODO Auto-generated method stub
+               return 0;
+       }
+
+       public String getFileName() {
+               // TODO Auto-generated method stub
+               return null;
+       }
+
+       public String getMessage() {
+               // TODO Auto-generated method stub
+               return null;
+       }
+
+       public int getStartColumn() {
+               // TODO Auto-generated method stub
+               return 0;
+       }
+
+       public int getStartLine() {
+               // TODO Auto-generated method stub
+               return 0;
+       }
+
+       public boolean isError() {
+               // TODO Auto-generated method stub
+               return false;
+       }
+
+
+}

Propchange: 
jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/RhinoCompilationProblem.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/RhinoCompilationProblem.java
------------------------------------------------------------------------------
    svn:executable = *

Propchange: 
jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/RhinoCompilationProblem.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision Author HeadURL Id

Propchange: 
jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/RhinoCompilationProblem.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/RhinoCompilingClassLoader.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/RhinoCompilingClassLoader.java?view=auto&rev=511677
==============================================================================
--- 
jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/RhinoCompilingClassLoader.java
 (added)
+++ 
jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/RhinoCompilingClassLoader.java
 Sun Feb 25 16:47:10 2007
@@ -0,0 +1,235 @@
+package org.apache.commons.jci.compilers;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.util.ArrayList;
+
+import org.apache.commons.jci.readers.ResourceReader;
+import org.apache.commons.jci.stores.ResourceStore;
+import org.mozilla.javascript.CompilerEnvirons;
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.GeneratedClassLoader;
+import org.mozilla.javascript.ImporterTopLevel;
+import org.mozilla.javascript.JavaScriptException;
+import org.mozilla.javascript.NativeArray;
+import org.mozilla.javascript.Scriptable;
+import org.mozilla.javascript.ScriptableObject;
+import org.mozilla.javascript.optimizer.ClassCompiler;
+
+/**
+ * @author tcurdt
+ * based on code from dev.helma.org
+ * 
http://dev.helma.org/source/file/helma/branches/rhinoloader/src/org/helma/javascript/RhinoLoader.java/?revision=95
+ */
+
+public final class RhinoCompilingClassLoader extends ClassLoader {
+
+
+       File scriptDir;
+       private final ScriptableObject scope;
+       private final ResourceReader reader;
+       private final ResourceStore store;
+
+       public RhinoCompilingClassLoader( final ResourceReader pReader, final 
ResourceStore pStore, final ClassLoader pClassLoader) {
+               super(pClassLoader);
+               
+               reader = pReader;
+               store = pStore;
+
+               final Context cx = Context.enter();
+               scope = new ImporterTopLevel(cx);
+               Context.exit();
+       }
+
+
+
+       /**
+        * Find and load a java class implemented in JavaScript.
+        * @param name the class name
+        * @return the class
+        * @throws ClassNotFoundException
+        * @throws JavaScriptException
+        */
+
+       protected Class findClass(String name) throws ClassNotFoundException {
+
+               final Context cx = Context.enter();
+
+               try {
+                       return compileClass(cx, name);
+               } catch (IOException iox) {
+                       throw new ClassNotFoundException(iox.getMessage(), iox);
+               } finally {
+                       Context.exit();
+               }
+       }
+
+
+
+       /**
+        *
+        * @param cx the rhino context
+        * @param className the class name
+        * @return the compiled class
+        * @throws IOException an i/o related error occurred
+        * @throws ClassNotFoundException class couldn't be found
+        */
+
+       private Class compileClass(Context cx, String className) throws 
IOException, ClassNotFoundException {
+
+               Class superclass = null;
+               Class[] interfaces = null;
+
+               String file = className.replace('.', '/') + ".js";
+               
+               Scriptable target = evaluate(cx, file);
+
+               // first get the base class
+
+               Object ext = ScriptableObject.getProperty(target, 
"__extends__");
+
+               if (ext instanceof String) {
+                       superclass = Class.forName((String) ext);
+               }
+
+               // then add the implemented interfaces
+
+               ArrayList list = new ArrayList();
+
+               Object impl = ScriptableObject.getProperty(target, 
"__implements__");
+
+               if (impl instanceof NativeArray) {
+
+                       NativeArray array = (NativeArray) impl;
+
+                       for (int i=0; i<array.getLength(); i++) {
+
+                               Object obj = array.get(i, array);
+
+                               if (obj instanceof String) {
+
+                                       list.add(Class.forName((String) obj));
+
+                               }
+
+                       }
+
+               } else if (impl instanceof String) {
+
+                       list.add(Class.forName((String) impl));
+
+               }
+
+
+               if (!list.isEmpty()) {
+
+                       interfaces = new Class[list.size()];
+
+                       interfaces = (Class[]) list.toArray(interfaces);
+
+               }
+
+               return compileClass(cx, file, className, superclass, 
interfaces);
+
+       }
+
+
+
+       /**
+        *
+        * @param cx the rhino context
+        * @param file the file to read fom
+        * @param className the class name
+        * @param superclass the super class
+        * @param interfaces the implemented interfaces
+        * @return the compiled class
+        * @throws IOException an i/o related error occured
+        */
+
+       private Class compileClass(Context cx, String file, String className, 
Class superclass, Class[] interfaces) throws IOException {
+
+               CompilerEnvirons compEnv = new CompilerEnvirons();
+               compEnv.initFromContext(cx);
+               ClassCompiler compiler = new ClassCompiler(compEnv);
+
+               if (superclass != null) {
+                       compiler.setTargetExtends(superclass);
+               }
+
+               if (interfaces != null) {
+                       compiler.setTargetImplements(interfaces);
+               }
+
+
+               byte[] buf = reader.getBytes(file);
+
+               Object[] classes = compiler.compileToClassFiles(new 
String(buf), getName(file), 1, className);
+
+               GeneratedClassLoader loader = 
cx.createClassLoader(cx.getApplicationClassLoader());
+
+               Class clazz = null;
+
+               for (int i = 0; i < classes.length; i += 2) {
+
+                       final String clazzName = (String) classes[i];
+                       final byte[] clazzBytes = (byte[]) classes[i+1];
+                       
+                       store.write(clazzName.replace('.', '/') + ".class", 
clazzBytes);
+                       
+                       Class c = loader.defineClass(clazzName, clazzBytes);
+                       loader.linkClass(c);
+
+                       if (i == 0) {
+                               clazz = c;
+                       }
+
+               }
+
+               return clazz;
+       }
+
+       private String getName(String s) {
+               final int i = s.lastIndexOf('/');
+               if (i < 0) {
+                       return s;
+               }
+               
+               return s.substring(i + 1);
+       }
+       
+       /**
+        * Evaluate the script on a new Scriptable object.
+        *
+        * @param cx the current Context
+        * @param file the file to evaluate
+        * @return the scriptable object on which the script was evaluated
+        * @throws JavaScriptException if an error occurred evaluating the
+        *                             prototype script code
+        * @throws IOException if an error occurred reading the script file
+        */
+
+       private Scriptable evaluate(Context cx, String sourcePath) throws 
JavaScriptException, IOException {
+
+               if (!reader.isAvailable(sourcePath)) {
+                       throw new FileNotFoundException("File " + sourcePath + 
" not found or not readable");
+               }
+
+               final Scriptable target = cx.newObject(scope);
+
+               final byte[] script = reader.getBytes(sourcePath);
+               
+               Reader reader = new InputStreamReader(new 
ByteArrayInputStream(script));
+
+               System.out.println(new String(script));
+               
+               cx.evaluateReader(target, reader, getName(sourcePath), 1, null);
+
+               return target;
+       }
+
+}
\ No newline at end of file

Propchange: 
jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/RhinoCompilingClassLoader.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/RhinoCompilingClassLoader.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision Author HeadURL Id

Propchange: 
jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/RhinoCompilingClassLoader.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/RhinoJavaCompiler.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/RhinoJavaCompiler.java?view=auto&rev=511677
==============================================================================
--- 
jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/RhinoJavaCompiler.java
 (added)
+++ 
jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/RhinoJavaCompiler.java
 Sun Feb 25 16:47:10 2007
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.jci.compilers;
+
+import org.apache.commons.jci.problems.CompilationProblem;
+import org.apache.commons.jci.readers.ResourceReader;
+import org.apache.commons.jci.stores.ResourceStore;
+import org.apache.commons.jci.utils.ClassUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * 
+ * @author tcurdt
+ * based on code from dev.helma.org
+ */
+public final class RhinoJavaCompiler extends AbstractJavaCompiler {
+
+    private final Log log = LogFactory.getLog(RhinoJavaCompiler.class);
+
+       public CompilationResult compile( final String[] pResourcePaths, final 
ResourceReader pReader, final ResourceStore pStore, final ClassLoader 
classLoader) {
+
+               final RhinoCompilingClassLoader cl = new 
RhinoCompilingClassLoader(pReader, pStore, classLoader);
+               
+               
+               for (int i = 0; i < pResourcePaths.length; i++) {
+            log.debug("compiling " + pResourcePaths[i]);
+            
+            final String clazzName = 
ClassUtils.convertResourceToClassName(pResourcePaths[i]);
+            try {
+                               cl.loadClass(clazzName);
+                       } catch (ClassNotFoundException e) {
+                               e.printStackTrace();
+                       }
+               }
+               return new CompilationResult(new CompilationProblem[0]);
+       }
+       
+}

Propchange: 
jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/RhinoJavaCompiler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/RhinoJavaCompiler.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision Author HeadURL Id

Propchange: 
jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/main/java/org/apache/commons/jci/compilers/RhinoJavaCompiler.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/test/java/org/apache/commons/jci/compilers/RhinoJavaCompilerTestCase.java
URL: 
http://svn.apache.org/viewvc/jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/test/java/org/apache/commons/jci/compilers/RhinoJavaCompilerTestCase.java?view=auto&rev=511677
==============================================================================
--- 
jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/test/java/org/apache/commons/jci/compilers/RhinoJavaCompilerTestCase.java
 (added)
+++ 
jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/test/java/org/apache/commons/jci/compilers/RhinoJavaCompilerTestCase.java
 Sun Feb 25 16:47:10 2007
@@ -0,0 +1,65 @@
+package org.apache.commons.jci.compilers;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.commons.jci.readers.ResourceReader;
+import org.apache.commons.jci.stores.MemoryResourceStore;
+
+public final class RhinoJavaCompilerTestCase extends AbstractCompilerTestCase {
+
+       public JavaCompiler createJavaCompiler() {
+               return new RhinoJavaCompiler();
+       }
+       
+       public String getCompilerName() {
+               return "rhino";
+       }
+       
+       public void testSimpleCompile() throws Exception {
+               final JavaCompiler compiler = createJavaCompiler(); 
+               
+               final ResourceReader reader = new ResourceReader() {
+                       final private Map sources = new HashMap() {{
+                               put("jci/Simple.js", (
+                                       " var i = 0;\n" +
+                                       "\n"
+                                       ).getBytes());
+                       }};
+                       
+                       public byte[] getBytes( final String pResourceName ) {
+                               return (byte[]) sources.get(pResourceName);
+                       }
+
+                       public boolean isAvailable( final String pResourceName 
) {
+                               return sources.containsKey(pResourceName);
+                       }
+                       
+               };
+               
+               final MemoryResourceStore store = new MemoryResourceStore();
+               final CompilationResult result = compiler.compile(
+                               new String[] {
+                                               "jci/Simple.js"
+                               }, reader, store);
+               
+               assertEquals(toString(result.getErrors()), 0, 
result.getErrors().length);               
+               assertEquals(toString(result.getWarnings()), 0, 
result.getWarnings().length);
+               
+               final byte[] clazzBytes = store.read("jci/Simple.class");       
        
+               assertNotNull(clazzBytes);
+               assertTrue(clazzBytes.length > 0);
+       }
+
+       public void testExtendedCompile() throws Exception {
+       }
+
+       public void testInternalClassCompile() throws Exception {
+       }
+
+       public void testUppercasePackageNameCompile() throws Exception {
+       }
+       
+       
+
+}

Propchange: 
jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/test/java/org/apache/commons/jci/compilers/RhinoJavaCompilerTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/test/java/org/apache/commons/jci/compilers/RhinoJavaCompilerTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision Author HeadURL Id

Propchange: 
jakarta/commons/sandbox/jci/trunk/compilers/rhino/src/test/java/org/apache/commons/jci/compilers/RhinoJavaCompilerTestCase.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to