Java Utililty Methods Javascript Mozilla Library

List of utility methods to do Javascript Mozilla Library

Description

The list of methods to do Javascript Mozilla Library are organized into topic(s).

Method

TprototypeCast(Scriptable s, Class type)
prototype Cast
Scriptable obj = s;
while (obj != null && !type.isInstance(obj)) {
    obj = obj.getPrototype();
return type.cast(obj);
voidremoveLinkFromXhtml(Object xhtml, String link)
remove Link From Xhtml
if (xhtml instanceof Scriptable) {
    Scriptable sxhtml = (Scriptable) xhtml;
    String classname = sxhtml.getClassName().toLowerCase();
    if ("xml".equals(classname) || "xmllist".equals(classname)) {
        final String XHTML_DEL_LINK = "var link_re = new RegExp('" + link + "/?');"
                + "for each (var x in xhtml..*.(link_re.test(@href.toString() ||  @src.toString()))) { "
                + "x.parent().replace(x.childIndex(), x.*); " + "}";
        Context cx = Context.getCurrentContext();
...
voidrunWithAllOptimizationLevels(final ContextAction action)
Runs the action successively with all available optimization levels
runWithOptimizationLevel(action, -1);
runWithOptimizationLevel(action, 0);
runWithOptimizationLevel(action, 1);
voidrunWithOptimizationLevel(final ContextAction action, final int optimizationLevel)
Runs the provided action at the given optimization level
runWithOptimizationLevel(new ContextFactory(), action, optimizationLevel);
StringscriptableObjectToString(Object scriptObject)
scriptable Object To String
String objectName = scriptObject.getClass().getSimpleName();
if (objectName.equals("Double") || scriptObject.getClass().getSimpleName().equals("Integer")) {
    return scriptObject.toString();
if (objectName.equals("NativeArray")) {
    return nativeArrayToString(scriptObject);
if (objectName.equals("NativeObject")) {
...
doublestringToNumber(CharSequence string)
Decodes a JSON-compatible number from string.
int len = string.length();
int start = 0;
char startChar;
for (;;) {
    if (start == len) {
        return +0.0;
    startChar = string.charAt(start);
...
StringstringValue(String name, Scriptable scope)
Returns the value of a JavaScript variable, or null.
if (scope == null) {
    return null;
Object o = scope.get(name, scope);
return o instanceof String ? (String) o : null;
inttoInt(Object obj, int defaultValue)
Try to convert an object to an int value, returning the default value if conversion fails.
double d = ScriptRuntime.toNumber(obj);
if (d == ScriptRuntime.NaN || (int) d != d) {
    return defaultValue;
return (int) d;
booleantoJavaBoolean(final Scriptable options, final String key)
to Java Boolean
try {
    return (boolean) Context.jsToJava(ScriptableObject.getProperty(options, key), Boolean.TYPE);
} catch (Exception e) {
    return false;
IntegertoJavaInt(final Object fromScript)
to Java Int
try {
    return (Integer) Context.jsToJava(fromScript, Integer.class);
} catch (Exception e) {
    return null;