Java Code Examples for org.mozilla.javascript.ScriptableObject#hasProperty()

The following examples show how to use org.mozilla.javascript.ScriptableObject#hasProperty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EcmaScriptDataModel.java    From JVoiceXML with GNU Lesser General Public License v2.1 6 votes vote down vote up
private int createArray(final String arrayName, final int dimension,
        final Scope scope, final Scriptable start) {
    if (start == null) {
        return ERROR_SCOPE_NOT_FOUND;
    }
    final Scriptable targetScope = getAndCreateScope(start, arrayName);
    final String name;
    int dotPos = arrayName.lastIndexOf('.');
    if (dotPos >= 0) {
        name = arrayName.substring(dotPos + 1);
    } else {
        name = arrayName;
    }
    if (ScriptableObject.hasProperty(targetScope, name)) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("'" + arrayName + "' already defined");
        }
        return ERROR_VARIABLE_ALREADY_DEFINED;
    }
    final NativeArray array = new NativeArray(dimension);
    ScriptableObject.putProperty(targetScope, name, array);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("created '" + arrayName  + "' in scope '"
                + getScope(targetScope) + "' as an array of "
                + dimension);
    }

    // Fill the array
    for (int i = 0; i < dimension; i++) {
        final Context context = getContext();
        final Scriptable scriptable = context.newObject(topmostScope);
        ScriptableObject.putProperty(array, i, scriptable);
    }
    return NO_ERROR;
}
 
Example 2
Source File: EcmaScriptDataModel.java    From JVoiceXML with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Retrieves the scope identified by the variable's full name.
 * @param scope the scope to start searching
 * @param name the fully qualified name of a variable
 * @return found scope, {@code null} if no such scope exists
 * @since 0.7.7
 */
private Scriptable getScope(final Scriptable scope, final String name) {
    int dotPos = name.indexOf('.');
    if (dotPos >= 0) {
        final String prefix = name.substring(0, dotPos);
        final String suffix = name.substring(dotPos + 1);
        final Object value;
        if (ScriptableObject.hasProperty(scope, prefix)) {
            value = ScriptableObject.getProperty(scope, prefix);
        } else {
            return null;
        }
        if (value instanceof Scriptable) {
            final Scriptable subscope = (Scriptable) value;
            return getScope(subscope, suffix);
        } else {
            return null;
        }
    } else {
        return scope;
    }
}
 
Example 3
Source File: EcmaScriptDataModel.java    From JVoiceXML with GNU Lesser General Public License v2.1 6 votes vote down vote up
public boolean existsVariable(final String variableName, final Scope scope,
        final Scriptable start) {
    if (start == null) {
        return false;
    }
    final Scriptable targetScope = getScope(start, variableName);
    if (targetScope == null) {
        return false;
    }
    final String property;
    int dotPos = variableName.lastIndexOf('.');
    if (dotPos >= 0) {
        property = variableName.substring(dotPos + 1);
    } else {
        property = variableName;
    }
    return ScriptableObject.hasProperty(targetScope, property);
}
 
Example 4
Source File: EcmaScriptDataModel.java    From JVoiceXML with GNU Lesser General Public License v2.1 6 votes vote down vote up
private <T> T readArray(final String arrayName, final int position,
        final Scope scope, final Scriptable start, final Class<T> type)
        throws SemanticError {
    if (start == null) {
        throw new SemanticError("no scope '" + scope
                + "' present to read '" + arrayName + "'");
    }
    final Scriptable targetScope = getFullScope(topmostScope, arrayName);
    if (targetScope == null) {
        throw new SemanticError("'" + arrayName + "' not found");
    }
    if (!ScriptableObject.hasProperty(targetScope, position)) {
        throw new SemanticError("'" + arrayName + "' has no position "
                + position);
    }
    final Object value = ScriptableObject
            .getProperty(targetScope, position);
    if (value == getUndefinedValue()) {
        return null;
    }
    @SuppressWarnings("unchecked")
    final T t = (T) Context.jsToJava(value, type);
    return t;
}
 
Example 5
Source File: EcmaScriptDataModel.java    From JVoiceXML with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Actually create the variable in the specified scope.
 * 
 * @param variableName
 *            the name of the variable
 * @param value
 *            the initial value of the variable
 * @param scope
 *            logical scope where to create the variable
 * @param start
 *            scriptable scope where to create the variable
 * @return {@code 0} if successful
 */
private int createVariable(final String variableName, final Object value,
        final Scope scope, final Scriptable start) {
    if (start == null) {
        return ERROR_SCOPE_NOT_FOUND;
    }
    final Scriptable subscope = getAndCreateScope(start, variableName);
    if (subscope == null) {
        LOGGER.warn("unable to create scope for '" + variableName + "'."
                + " Please check object creation in var tag or in your"
                + " grammar file.");
        return ERROR_SCOPE_NOT_FOUND;
    }
    final String name;
    int dotPos = variableName.lastIndexOf('.');
    if (dotPos >= 0) {
        name = variableName.substring(dotPos + 1);
    } else {
        name = variableName;
    }
    if (ScriptableObject.hasProperty(subscope, name)) {
        LOGGER.warn("'" + variableName + "' already defined");
        return ERROR_VARIABLE_ALREADY_DEFINED;
    }
    final Object wrappedValue = Context.javaToJS(value, start);
    ScriptableObject.putProperty(subscope, name, wrappedValue);
    if (LOGGER.isDebugEnabled()) {
        final String json = toString(wrappedValue);
        if (scope == null) {
            LOGGER.debug("created '" + variableName + "' in scope '"
                    + getScope(subscope) + "' with '" + json + "'");
        } else {
            LOGGER.debug("created '" + variableName + "' in scope '"
                    + getScope(subscope) + "' with '" + json + "'");
        }
    }
    return NO_ERROR;
}
 
Example 6
Source File: EcmaScriptDataModel.java    From JVoiceXML with GNU Lesser General Public License v2.1 5 votes vote down vote up
private int deleteVariable(final String variableName, final Scope scope,
        final Scriptable start) {
    if (start == null) {
        return ERROR_SCOPE_NOT_FOUND;
    }
    final Scriptable targetScope = getScope(start, variableName);
    if (targetScope == null) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("socpe for '" + variableName + "' not found");
        }
        return ERROR_VARIABLE_NOT_FOUND;
    }
    final String property;
    int dotPos = variableName.lastIndexOf('.');
    if (dotPos >= 0) {
        property = variableName.substring(dotPos + 1);
    } else {
        property = variableName;
    }
    if (!ScriptableObject.hasProperty(targetScope, property)) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("'" + variableName + "' not found");
        }
        return ERROR_VARIABLE_NOT_FOUND;
    }
    ScriptableObject.deleteProperty(targetScope, property);
    if (LOGGER.isDebugEnabled()) {
        if (scope == null) {
            LOGGER.debug("deleted '" + variableName + "'");
        } else {
            LOGGER.debug("deleted '" + variableName + "' in scope '"
                    + scope + "'");
        }
    }
    return 0;
}
 
Example 7
Source File: EcmaScriptDataModel.java    From JVoiceXML with GNU Lesser General Public License v2.1 5 votes vote down vote up
private int updateVariable(final String variableName,
        final Object newValue, final Scope scope, final Scriptable start) {
    if (start == null) {
        return ERROR_SCOPE_NOT_FOUND;
    }
    final Scriptable subscope = getAndCreateScope(start, variableName);
    final String property;
    int dotPos = variableName.lastIndexOf('.');
    if (dotPos >= 0) {
        property = variableName.substring(dotPos + 1);
    } else {
        property = variableName;
    }
    if (!ScriptableObject.hasProperty(subscope, property)) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("'" + variableName + "' not found");
        }
        return ERROR_VARIABLE_NOT_FOUND;
    }
    final Object jsValue = Context.javaToJS(newValue, subscope);
    ScriptableObject.putProperty(subscope, property, jsValue);
    if (LOGGER.isDebugEnabled()) {
        final String json = toString(jsValue);
        if (scope == null) {
            LOGGER.debug("set '" + variableName + "' in scope '"
                    + getScope(subscope) + "' to '" + json + "'");
        } else {
            LOGGER.debug("set '" + variableName + "' in scope '"
                    + getScope(subscope) + "' to '" + json + "'");
        }
    }
    return 0;
}
 
Example 8
Source File: EcmaScriptDataModel.java    From JVoiceXML with GNU Lesser General Public License v2.1 5 votes vote down vote up
private <T extends Object> T readVariable(String variableName, Scope scope,
        final Scriptable start, final Class<T> type) throws SemanticError {
    if (start == null) {
        throw new SemanticError("no scope '" + scope
                + "' present to read '" + variableName + "'");
    }
    final Scriptable subcope = getScope(topmostScope, variableName);
    if (subcope == null) {
        throw new SemanticError("'" + variableName + "' not found");
    }
    final String property;
    int dotPos = variableName.lastIndexOf('.');
    if (dotPos >= 0) {
        property = variableName.substring(dotPos + 1);
    } else {
        property = variableName;
    }
    if (!ScriptableObject.hasProperty(subcope, property)) {
        throw new SemanticError("'" + variableName + "' not found");
    }
    final Object value = ScriptableObject.getProperty(subcope, property);
    if (value == getUndefinedValue()) {
        return null;
    }
    @SuppressWarnings("unchecked")
    final T t = (T) Context.jsToJava(value, type);
    return t;
}
 
Example 9
Source File: LessCompiler.java    From projectforge-webapp with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Compiles the LESS input <code>String</code> to CSS.
 * 
 * @param input The LESS input <code>String</code> to compile.
 * @return The CSS.
 */
public String compile(final String input) throws LessException {
  synchronized(this){
    if (scope == null) {
      init();
    }
  }

  final long start = System.currentTimeMillis();

  try {
    final Context cx = Context.enter();
    final Object result = doIt.call(cx, scope, null, new Object[]{input, compress});

    if (log.isDebugEnabled()) {
      log.debug("Finished compilation of LESS source in " + (System.currentTimeMillis() - start) + " ms.");
    }

    return result.toString();
  }
  catch (final Exception e) {
    if (e instanceof JavaScriptException) {
      final Scriptable value = (Scriptable)((JavaScriptException)e).getValue();
      if (value != null && ScriptableObject.hasProperty(value, "message")) {
        final String message = ScriptableObject.getProperty(value, "message").toString();
        throw new LessException(message, e);
      }
    }
    throw new LessException(e);
  }finally{
    Context.exit();
  }
}
 
Example 10
Source File: EcmaScriptDataModel.java    From JVoiceXML with GNU Lesser General Public License v2.1 4 votes vote down vote up
private int updateArray(final String variableName, final int position,
        final Object newValue, final Scope scope, final Scriptable start) {
    if (start == null) {
        return ERROR_SCOPE_NOT_FOUND;
    }
    final Scriptable subscope = getAndCreateScope(start, variableName);
    final String property;
    int dotPos = variableName.lastIndexOf('.');
    if (dotPos >= 0) {
        property = variableName.substring(dotPos + 1);
    } else {
        property = variableName;
    }
    if (!ScriptableObject.hasProperty(subscope, property)) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("'" + variableName + "' not found");
        }
        return ERROR_VARIABLE_NOT_FOUND;
    }
    final Object jsValue = Context.javaToJS(newValue, subscope);
    final NativeArray array = (NativeArray) ScriptableObject.getProperty(
            subscope, property);
    if (!ScriptableObject.hasProperty(array, position)) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("'" + variableName + "' has no position "
                    + position);
        }
        return ERROR_VARIABLE_NOT_FOUND;
    }
    ScriptableObject.putProperty(array, position, jsValue);
    if (LOGGER.isDebugEnabled()) {
        final String json = toString(jsValue);
        if (scope == null) {
            LOGGER.debug("set '" + variableName + "[" + position
                    + "]' to '" + json + "'");
        } else {
            LOGGER.debug("set '" + variableName + "[" + position
                    + "]' to '" + json + "' in scope '" + scope + "'");
        }
    }
    return 0;
}