Subversion Repositories pub

Compare Revisions

Ignore whitespace Rev 184 → Rev 65

/pluggablejs/trunk/src/net/outlyer/plugins/utils/RedundantLanguageExtensions.java
File deleted
Property changes:
Deleted: svn:keyword
-Rev Id Date
\ No newline at end of property
/pluggablejs/trunk/src/net/outlyer/plugins/utils/LanguageExtensions.java
28,6 → 28,7
 
import java.lang.reflect.Array;
import java.util.Arrays;
import net.outlyer.plugins.Functor;
import net.outlyer.plugins.SandboxAccessor;
 
/**
38,16 → 39,8
public class LanguageExtensions extends LanguageExtensions_V1
implements SandboxAccessor {
 
public final int interfaceRevision;
public static final int revision = 2;
 
public LanguageExtensions() {
this(2);
}
protected LanguageExtensions(int revision) {
interfaceRevision = revision;
}
 
// The array() family is mostly redundant since rhino translates
// automatically as required from JavaScript arrays to Java arrays
// but there are some edge-cases in which they might be desirable
56,7 → 49,7
* Create an array of the same class as the provided object.
* @param tplt Template for the array, must be non-null and non-Void
* @param size Size of the array to create (can be 0)
* @since {@link #interfaceRevision} 2
* @since {@link #revision} 2
*/
public <T> T[] array(final T tplt, int size) {
if (null == tplt || Void.class == tplt.getClass()) {
69,7 → 62,7
* Create an array of the provided class.
* @param c Class of the array elements, must be non-null and different from Void.class
* @param size Size of the array (can be 0)
* @since {@link #interfaceRevision} 2
* @since {@link #revision} 2
*/
public <T> T[] array(final Class<T> c, int size) {
return (T[]) Array.newInstance(c, size);
83,7 → 76,7
* <code>initValues.length</code> instead) (i.e., use 0 to
* get the same size as initValues)
* @param initValues Initial values to put in the array
* @since {@link #interfaceRevision} 2
* @since {@link #revision} 2
*/
public <T> T[] array(final T[] initValues, int size) {
if (null == initValues) {
102,7 → 95,7
/**
* Shorthand form, allows getting a Java array from a JavaScript array.
* @param initValues
* @since {@link #interfaceRevision} 2
* @since {@link #revision} 2
*/
public <T> T[] array(final T[] initValues) {
return array(initValues, initValues.length);
169,4 → 162,36
}
return sb.toString();
}
 
/**
* Apply a function to each element of an iterable collection.
* @see #for_each(Object[], Functor)
* @param c Collection to which to apply
* @param f Function to apply
* @since {@link #revision} 2
*/
public <T> void for_each(final Iterable<T> c, final Functor<Object, T> f) {
for (final T elem : c) {
f.apply(elem);
}
}
/**
* Apply a function to each element of an array
* A JavaScript function can be used transparently as a functor, e.g.:
* <br />
* <code>var arr = lang.array(new Array(1, 2, 3));<br />
* lang.for_each(arr, function(x) { err.println("Element: "+x); });</code>>
* Will produce:
* <pre>Element: 1<br />Element: 2<br />Element: 3</pre>
* @param a Array to which to apply
* @param f Function to apply
* @since {@link #revision} 2
*/
public <T> void for_each(final T[] a, final Functor<Object, T> f) {
if (null == a || null == f) {
return; // FIXME: Error handling
}
for_each(Arrays.asList(a), f);
}
}
/pluggablejs/trunk/src/net/outlyer/plugins/utils/LanguageExtensions_V1.java
1,5 → 1,11
package net.outlyer.plugins.utils;
 
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import javax.script.ScriptException;
import net.outlyer.plugins.SandboxAccessorImpl;
 
/*
* Copyright (c) 2008, Toni Corvera. All rights reserved.
*
26,16 → 32,6
 
// $Id$
 
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import javax.script.ScriptException;
import net.outlyer.plugins.PluginException;
import net.outlyer.plugins.PluginReader;
import net.outlyer.plugins.Sandbox;
import net.outlyer.plugins.SandboxAccessorImpl;
 
/**
* Initial version of LanguageExtensions.
* @see LanguageExtensions
65,53 → 61,19
* Load an external file
* @deprecated Not yet stabilised
*/
public Object include(final String path) { // TODO: Support jar URIs
public Object include(final String fileName) { // TODO: Support jar URIs
try {
if (path.startsWith("file:/") || path.startsWith("jar:file:/")) {
return include(new URI(path));
}
final File f = new File(path);
if (f.isAbsolute()) {
return include(f.toURI());
}
// Get a reasonable path
final Sandbox s = getSandbox();
final URI callerUri = s.loadedFrom();
// This isn't very elegant but works (in most cases?)...
final String c = callerUri.toString();
final int idx = callerUri.toString().lastIndexOf('/');
final URI reqUri = new URI(c.substring(0, idx)+"/"+path).normalize();
return include(reqUri);
return getSandbox().getCurrentEngine().eval(new FileReader(new File(fileName)));
}
catch (final URISyntaxException e) {
e.printStackTrace();
}
return null;
}
 
public Object include(final URI uri) {
if (null == uri) {
// TODO: Handle better
return null;
}
if (null == uri.getScheme() ||
(!"file".equals(uri.getScheme()) && !"jar".equals(uri.getScheme()))) {
throw new IllegalArgumentException("Only file and jar-contained files can be included");
}
 
try {
return getSandbox().inject(new PluginReader(uri));
}
catch (final IOException e) {
catch (final FileNotFoundException e) {
// FIXME: Handle better
System.err.println("Failed to read " + uri);
System.err.println("Failed to read " + fileName);
}
catch (final PluginException e) {
catch (final ScriptException e) {
// FIXME: Handle better
System.err.println("Exception: " + e.getMessage());
}
return null;
}
 
}