/pluggablejs/trunk/src/net/outlyer/plugins/utils/LanguageExtensions.java |
---|
28,7 → 28,6 |
import java.lang.reflect.Array; |
import java.util.Arrays; |
import net.outlyer.plugins.Functor; |
import net.outlyer.plugins.SandboxAccessor; |
/** |
39,8 → 38,16 |
public class LanguageExtensions extends LanguageExtensions_V1 |
implements SandboxAccessor { |
public static final int revision = 2; |
public final int interfaceRevision; |
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 |
49,7 → 56,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 #revision} 2 |
* @since {@link #interfaceRevision} 2 |
*/ |
public <T> T[] array(final T tplt, int size) { |
if (null == tplt || Void.class == tplt.getClass()) { |
62,7 → 69,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 #revision} 2 |
* @since {@link #interfaceRevision} 2 |
*/ |
public <T> T[] array(final Class<T> c, int size) { |
return (T[]) Array.newInstance(c, size); |
76,7 → 83,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 #revision} 2 |
* @since {@link #interfaceRevision} 2 |
*/ |
public <T> T[] array(final T[] initValues, int size) { |
if (null == initValues) { |
95,7 → 102,7 |
/** |
* Shorthand form, allows getting a Java array from a JavaScript array. |
* @param initValues |
* @since {@link #revision} 2 |
* @since {@link #interfaceRevision} 2 |
*/ |
public <T> T[] array(final T[] initValues) { |
return array(initValues, initValues.length); |
162,36 → 169,4 |
} |
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/RedundantLanguageExtensions.java |
---|
0,0 → 1,80 |
package net.outlyer.plugins.utils; |
/* |
* Copyright (c) 2008, Toni Corvera. All rights reserved. |
* |
* Redistribution and use in source and binary forms, with or without |
* modification, are permitted provided that the following conditions are met: |
* |
* * Redistributions of source code must retain the above copyright |
* notice, this list of conditions and the following disclaimer. |
* * 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. |
* |
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS 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 |
* COPYRIGHT OWNER OR 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. |
*/ |
// $Id$ |
import java.util.Arrays; |
import net.outlyer.plugins.Functor; |
/** |
* Language extensions provided already by JavaScript. |
* Versions here provide alternative syntax or semantics to the builtin |
* versions, based on other languages. |
* <br /> |
* In general {@link LanguageExtensions} will be preferable over this class. |
* @since {@link net.outlyer.plugins.API#REVISION} 1 |
* @since {@link #interfaceRevision} 2 |
*/ |
public class RedundantLanguageExtensions extends LanguageExtensions { |
public RedundantLanguageExtensions() { |
super(2); |
} |
/** |
* Apply a function to each element of an iterable collection. |
* Note JavaScript 1.6 DOES have a "for each" loop |
* ({@link http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Statements:for_each...in}) |
* @see #for_each(Object[], Functor) |
* @param c Collection to which to apply |
* @param f Function to apply |
* @since {@link #interfaceRevision} 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 #interfaceRevision} 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); |
} |
} |
Property changes: |
Added: svn:keyword |
+Rev Id Date |
\ No newline at end of property |
/pluggablejs/trunk/src/net/outlyer/plugins/utils/LanguageExtensions_V1.java |
---|
1,11 → 1,5 |
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. |
* |
32,6 → 26,16 |
// $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 |
61,19 → 65,53 |
* Load an external file |
* @deprecated Not yet stabilised |
*/ |
public Object include(final String fileName) { // TODO: Support jar URIs |
public Object include(final String path) { // TODO: Support jar URIs |
try { |
return getSandbox().getCurrentEngine().eval(new FileReader(new File(fileName))); |
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); |
} |
catch (final FileNotFoundException e) { |
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) { |
// FIXME: Handle better |
System.err.println("Failed to read " + fileName); |
System.err.println("Failed to read " + uri); |
} |
catch (final ScriptException e) { |
catch (final PluginException e) { |
// FIXME: Handle better |
System.err.println("Exception: " + e.getMessage()); |
} |
return null; |
} |
} |