JavaScript functions in Java. Internally calls Rhino's implementation of the function calls. : Script Engines « JDK 6 « Java






JavaScript functions in Java. Internally calls Rhino's implementation of the function calls.

     
/*
 * Copyright WizTools.org
 * Licensed under the Apache License, Version 2.0:
 * http://www.apache.org/licenses/LICENSE-2.0
 */
//package org.wiztools.commons;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

/**
 * JavaScript functions in Java. Internally calls Rhino's implementation of the
 * function calls.
 * @author subhash
 */
public class JavaScript {
    private JavaScript() {}

    private static final ScriptEngine jsEngine;
    static {
        jsEngine = new ScriptEngineManager().getEngineByName("JavaScript");
    }

    private static String toUpperCaseEncodedChars(final String inStr) {
        int lastPercentPosition = -3; // 0- (-3 ) > 2: ensures the first character is not uppercased accidently
        final char[] c = inStr.toCharArray();
        char[] cOut = new char[c.length];
        for(int i=0; i< c.length; i++) {
            if(c[i] == '%') {
                lastPercentPosition = i;
            }
            final int diff = i - lastPercentPosition;
            if(diff == 1 || diff == 2) {
                cOut[i] = Character.toUpperCase(c[i]);
            }
            else {
                cOut[i] = c[i];
            }
        }
        return new String(cOut);
    }

    public static String escape(final String str) {
        try{
            return (String) ((Invocable) jsEngine).invokeFunction("escape", str);
        }
        catch(ScriptException ex) {
            throw new RuntimeException(ex);
        }
        catch(NoSuchMethodException ex) {
            throw new RuntimeException(ex);
        }
    }

    public static String unescape(final String str) {
        try{
            return (String) ((Invocable) jsEngine).invokeFunction("unescape", str);
        }
        catch(ScriptException ex) {
            throw new RuntimeException(ex);
        }
        catch(NoSuchMethodException ex) {
            throw new RuntimeException(ex);
        }
    }

    public static String encodeURI(final String str) {
        try{
            final String out = (String) ((Invocable) jsEngine).invokeFunction("encodeURI", str);
            return toUpperCaseEncodedChars(out);
        }
        catch(ScriptException ex) {
            throw new RuntimeException(ex);
        }
        catch(NoSuchMethodException ex) {
            throw new RuntimeException(ex);
        }
    }

    public static String decodeURI(final String str) {
        try{
            return (String) ((Invocable) jsEngine).invokeFunction("decodeURI", str);
        }
        catch(ScriptException ex) {
            throw new RuntimeException(ex);
        }
        catch(NoSuchMethodException ex) {
            throw new RuntimeException(ex);
        }
    }

    public static String encodeURIComponent(final String str) {
        try{
            final String out = (String) ((Invocable) jsEngine).invokeFunction("encodeURIComponent", str);
            return toUpperCaseEncodedChars(out);
        }
        catch(ScriptException ex) {
            throw new RuntimeException(ex);
        }
        catch(NoSuchMethodException ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * 
     * @param str
     * @return
     */
    public static String decodeURIComponent(final String str) {
        try{
            return (String) ((Invocable) jsEngine).invokeFunction("decodeURIComponent", str);
        }
        catch(ScriptException ex) {
            throw new RuntimeException(ex);
        }
        catch(NoSuchMethodException ex) {
            throw new RuntimeException(ex);
        }
    }
}

   
    
    
    
    
  








Related examples in the same category

1.Listing All Script Engines
2.Running Scripts with Java Script Engine
3.Execute Javascript script in a file
4.Variables bound through ScriptEngine
5.Catch ScriptException
6.Any script have to be compiled into intermediate code.
7.With Compilable interface you store the intermediate code of an entire script
8.List the script engines
9.Run JavaScript and get the result by using Java
10.Pass parameter to JavaScript through Java code
11.Get the value in JavaScript from Java Code by reference the variable name
12.Working with Compilable Scripts
13.Call a JavaScript function three times
14.Save the compiled JavaScript to CompiledScript objectSave the compiled JavaScript to CompiledScript object
15.Invoke an function
16.Using thread to run JavaScript by Java
17.Get Script engine by extension name
18.Get a ScriptEngine by MIME type
19.Retrieving Script Engines by Name
20.Retrieving the Metadata of Script Engines
21.Retrieving the Supported File Extensions of a Scripting Engine
22.Retrieving the Supported Mime Types of a Scripting Engine
23.Retrieving the Registered Name of a Scripting Engine
24.Executing Scripts from Java Programs
25.Read and execute a script source file
26.Using Java Objects in JavaScript
27.Pass or retrieve values from a scripting engine (jdk1.6)
28.Use an external file containing the javascript code. The .JS file is loaded from the classpath.
29.Use Java scripting engine
30.Run Javascript function with ScriptEngine
31.Use Java scripting engine (JDK 1.6)
32.Use an external file containing the javascript code
33.Java Language Binding with JavaScript
34.Compresses a String containing JavaScript by removing comments and whitespace.
35.Escape and unescape javascript code
36.Utility to take xml string and convert it to a javascript based tree within html.
37.JavaScript Utils
38.Various Javascript code utilities.