public JavascriptArrayFileNameProvider(
      FileProvider fileProvider, String sourceFile, String variableName) throws IOException {

    BufferedReader source = fileProvider.getResource(sourceFile);
    try {
      Context cx = Context.enter();
      Scriptable scope = cx.initStandardObjects();
      cx.evaluateReader(scope, source, sourceFile, 1, null);
      Object x = scope.get(variableName, scope);

      if (!(x instanceof NativeArray)) {
        throw new RuntimeException(
            "Could not find array called " + variableName + " in source file " + sourceFile);
      }
      NativeArray array = (NativeArray) x;
      for (int i = 0; i < array.getLength(); i++) {
        Object o = array.get(i, null);
        if (!(o instanceof String)) {
          throw new RuntimeException(
              "Array contained objects which are not Strings, cannot continue");
        }
        fileNames.add("/" + o);
      }
    } finally {
      Context.exit();
    }
  }
 @Override
 public boolean has(String name, Scriptable start) {
   if (wrapped == null) {
     return super.has(name, this);
   }
   return wrapped.has(name, wrapped) || properties.has(name, properties);
 }
Example #3
0
  private Ref xmlPrimaryReference(Context cx, XMLName xmlName, Scriptable scope) {
    XMLObjectImpl xmlObj;
    XMLObjectImpl firstXml = null;
    for (; ; ) {
      // XML object can only present on scope chain as a wrapper
      // of XMLWithScope
      if (scope instanceof XMLWithScope) {
        xmlObj = (XMLObjectImpl) scope.getPrototype();
        if (xmlObj.hasXMLProperty(xmlName)) {
          break;
        }
        if (firstXml == null) {
          firstXml = xmlObj;
        }
      }
      scope = scope.getParentScope();
      if (scope == null) {
        xmlObj = firstXml;
        break;
      }
    }

    // xmlObj == null corresponds to undefined as the target of
    // the reference
    if (xmlObj != null) {
      xmlName.initXMLObject(xmlObj);
    }
    return xmlName;
  }
Example #4
0
  /**
   * Sets a property based on the index. According to C.6.13.1 if the index is greater than the
   * current number of nodes, expand the size by one and add the new value to the end.
   *
   * @param index The index of the property to set
   * @param start The object who's property is being set
   * @param value The value being requested
   */
  public void put(int index, Scriptable start, Object value) {

    if (readOnly && !scriptField) {
      Context.reportError(READONLY_MSG);
      return;
    }

    if (!(value instanceof SFVec2d)) {
      Context.reportError(OBJECT_NOT_VEC_MSG);
      return;
    }

    Scriptable node = (SFVec2d) value;
    if (node.getParentScope() == null) node.setParentScope(this);

    if (index >= valueList.size()) {
      int toAdd = index - valueList.size();

      valueList.ensureCapacity(index + 1);

      // Add default values
      for (int i = 0; i < toAdd; i++) {
        valueList.add(new SFVec2d());
      }

      valueList.add(value);
      sizeInt.setValue(valueList.size());
    } else if (index >= 0) {
      valueList.set(index, value);
    }

    dataChanged = true;
  }
  public static String encodeFormVariables(String cs, Scriptable values)
      throws java.io.UnsupportedEncodingException {
    StringBuilder sb = new StringBuilder();

    for (Object o : values.getIds()) {
      if (sb.length() != 0) {
        sb.append("&");
      }

      if (o instanceof String) {
        String key = (String) o;
        String value = Context.toString(values.get(key, values));

        sb.append(URLEncoder.encode(key, cs));
        sb.append("=");
        sb.append(URLEncoder.encode(value, cs));
      } else {
        int key = (Integer) o;
        String value = Context.toString(values.get(key, values));

        sb.append(key).append("=");
        sb.append(URLEncoder.encode(value, cs));
      }
    }

    return sb.toString();
  }
Example #6
0
  /**
   * Initialize the JavaScript context using given parent scope.
   *
   * @param scPrototype Parent scope object. If it's null, use default scope.
   */
  public final void init(Scriptable scPrototype) throws ChartException {
    final Context cx = Context.enter();
    try {
      if (scPrototype == null) // NO PROTOTYPE
      {
        // scope = cx.initStandardObjects();
        scope = new ImporterTopLevel(cx);
      } else {
        scope = cx.newObject(scPrototype);
        scope.setPrototype(scPrototype);
        // !don't reset the parent scope here.
        // scope.setParentScope( null );
      }

      // final Scriptable scopePrevious = scope;
      // !deprecated, remove this later. use script context instead.
      // registerExistingScriptableObject( this, "chart" ); //$NON-NLS-1$
      // scope = scopePrevious; // RESTORE

      // !deprecated, remove this later, use logger from script context
      // instead.
      // ADD LOGGING CAPABILITIES TO JAVASCRIPT ACCESS
      final Object oConsole = Context.javaToJS(getLogger(), scope);
      scope.put("logger", scope, oConsole); // $NON-NLS-1$
    } catch (RhinoException jsx) {
      throw convertException(jsx);
    } finally {
      Context.exit();
    }
  }
 static {
   ctx = Context.enter();
   sharedScope = ctx.initStandardObjects();
   ctx.evaluateString(sharedScope, jsAsString, "re", 1, null);
   regexIsValid = (Function) sharedScope.get("regexIsValid", sharedScope);
   regMatch = (Function) sharedScope.get("regMatch", sharedScope);
   ctx.seal(null);
 }
Example #8
0
 @Override
 public boolean hasInstance(Scriptable instance) {
   Scriptable proto = instance.getPrototype();
   while (proto != null) {
     if (proto.equals(this)) return true;
     proto = proto.getPrototype();
   }
   return false;
 }
 /**
  * Implements the instanceof operator.
  *
  * @param instance The value that appeared on the LHS of the instanceof operator
  * @return true if "this" appears in value's prototype chain
  */
 public boolean hasInstance(Scriptable instance) {
   // Default for JS objects (other than Function) is to do prototype
   // chasing.
   Scriptable proto = instance.getPrototype();
   while (proto != null) {
     if (proto.equals(this)) return true;
     proto = proto.getPrototype();
   }
   return false;
 }
 /**
  * Validate that a regex is correct
  *
  * @param regex the regex to validate
  * @return true if the regex is valid
  */
 public static boolean regexIsValid(final String regex) {
   final Context context = Context.enter();
   try {
     final Scriptable scope = context.newObject(sharedScope);
     scope.setPrototype(sharedScope);
     scope.setParentScope(null);
     return (Boolean) regexIsValid.call(context, scope, scope, new Object[] {regex});
   } finally {
     Context.exit();
   }
 }
Example #11
0
 public Object evalFunction(String functionStr, Object... args) {
   Context ctx = Context.enter();
   try {
     Scriptable scope = new NativeObject();
     scope.setParentScope(global);
     Function func = compileIfNotCompiled(ctx, functionStr);
     return func.call(ctx, scope, null, args);
   } finally {
     Context.exit();
   }
 }
Example #12
0
 private Object[] toArray(Scriptable svalue) {
   int len = (Integer) Context.jsToJava(svalue.get("length", this), Integer.class);
   Object[] a = new Object[len];
   for (int i = 0; i < len; i++) {
     Object v = svalue.get(i, svalue);
     if (DBG) {
       Log.d(LCAT, "Index: " + i + " value: " + v + " type: " + v.getClass().getName());
     }
     a[i] = toNative(v, Object.class);
   }
   return a;
 }
 @Override
 public void put(String name, Scriptable start, Object value) {
   if (wrapped == null) {
     super.put(name, this, value);
   } else {
     if (properties.has(name, start)) {
       properties.put(name, properties, value);
     } else {
       wrapped.put(name, wrapped, value);
     }
   }
 }
 @Override
 public void delete(String name) {
   if (wrapped == null) {
     super.delete(name);
   } else {
     if (properties.has(name, properties)) {
       properties.delete(name);
     } else {
       wrapped.delete(name);
     }
   }
 }
Example #15
0
 public static void help(Context cx, Scriptable thisObj, Object[] args, Function funObj) {
   if (args.length == 0) {
     final Object[] objects = thisObj.getIds();
     for (Object object : objects) {
       System.out.println("jai." + object + " = " + thisObj.get(object.toString(), thisObj));
     }
     printOperatorList();
   } else {
     for (Object arg : args) {
       printOperatorUsage(Context.toString(arg));
     }
   }
 }
Example #16
0
  public static void finishInit(
      Scriptable scope, FunctionObject constructor, Scriptable prototype) {
    // Create and make these properties in the prototype visible
    Context cx = Context.getCurrentContext();

    Scriptable jars = cx.newArray(prototype, 0);
    jars.put(0, jars, cx.newArray(jars, 0));

    defineProperty(prototype, "params", cx.newArray(prototype, 0), ScriptableObject.PERMANENT);
    defineProperty(prototype, "auth", cx.newArray(prototype, 0), ScriptableObject.PERMANENT);
    defineProperty(prototype, "jars", jars, ScriptableObject.PERMANENT);
    defineProperty(prototype, "headers", cx.newArray(prototype, 0), ScriptableObject.PERMANENT);
  }
Example #17
0
 @Function(
     doc = "Read the certificate object from the keystore.",
     parameters = @Parameter(name = "name", type = "string", doc = "The certificate's name"),
     returns = "The certificate object.")
 public Object getCertificate(String name)
     throws KeyStoreException, IllegalArgumentException, IllegalAccessException,
         InvocationTargetException {
   Scriptable ret = Context.getCurrentContext().newObject(global);
   X509Certificate cer = (X509Certificate) keystore.getCertificate(name);
   for (PropertyDescriptor pc : BeanUtils.getPropertyDescriptors(X509Certificate.class)) {
     ret.put(pc.getName(), ret, String.valueOf(pc.getReadMethod().invoke(cer)));
   }
   return ret;
 }
Example #18
0
 /**
  * Take an object from the scripting layer and convert it to an int.
  *
  * @param obj
  * @return int
  */
 private int toInt(Object obj) {
   int result = 0;
   if (obj instanceof String) {
     result = Integer.parseInt((String) obj);
   } else if (obj instanceof Number) {
     result = ((Number) obj).intValue();
   } else if (obj instanceof Scriptable) {
     Scriptable sobj = (Scriptable) obj;
     if (sobj.getClassName().equals("Number")) {
       result = (int) ScriptRuntime.toNumber(sobj);
     }
   }
   return result;
 }
Example #19
0
 private List<Issue> readErrors(String systemId) {
   ArrayList<Issue> issues = new ArrayList<Issue>();
   Scriptable JSLINT = (Scriptable) scope.get("JSLINT", scope);
   Scriptable errors = (Scriptable) JSLINT.get("errors", JSLINT);
   int count = Util.intValue("length", errors);
   for (int i = 0; i < count; i++) {
     Scriptable err = (Scriptable) errors.get(i, errors);
     // JSLINT spits out a null when it cannot proceed.
     // TODO Should probably turn i-1th issue into a "fatal".
     if (err != null) {
       issues.add(IssueBuilder.fromJavaScript(systemId, err));
     }
   }
   return issues;
 }
  @Override
  public Object execIdCall(
      IdFunctionObject f, Context cx, Scriptable scope, Scriptable thisObj, Object[] args) {
    if (!f.hasTag(CLASS_TAG)) {
      return super.execIdCall(f, cx, scope, thisObj, args);
    }

    while (thisObj != null && !(thisObj instanceof AttrProxyPrototype)) {
      thisObj = thisObj.getPrototype();
    }

    AttrProxyPrototype proxy = (AttrProxyPrototype) thisObj;
    int id = f.methodId();
    switch (id) {
      case Id_constructor:
        return jsConstructor(scope, args);
      case Id_getValue:
        return getValue(cx, thisObj, args);

      case Id_getName:
        return getName(cx, thisObj, args);

      case Id_getOwnerElement:
        return getOwnerElement(cx, thisObj, args);

      case Id_getSpecified:
        return getSpecified(cx, thisObj, args);

      case Id_setValue:
        setValue(cx, thisObj, args);
        return Undefined.instance;
      default:
        throw new IllegalArgumentException(String.valueOf(id));
    }
  }
Example #21
0
File: DB.java Project: o-nix/Kafra
  protected Map<String, Object> convertParams(Scriptable params) {
    Map<String, Object> paramsMap = new HashMap<>();

    if (params != null && !(params instanceof Undefined)) {
      for (Object id : params.getIds()) {
        String castId = id instanceof String ? (String) id : String.valueOf(id);
        Object value = params.get(castId, params);

        if (value instanceof NativeJavaObject) value = ((NativeJavaObject) value).unwrap();

        paramsMap.put(castId, value);
      }
    }

    return paramsMap;
  }
 @Override
 public Object get(String name, Scriptable start) {
   if (wrapped == null) {
     return super.get(name, start);
   }
   if (name.startsWith("super$")) {
     return wrapped.get(name.substring(6), wrapped);
   }
   if (properties != null) {
     Object value = properties.get(name, properties);
     if (value != NOT_FOUND) {
       return value;
     }
   }
   return wrapped.get(name, wrapped);
 }
Example #23
0
  public void exec(Scriptable xdcO, Session ses) {
    this.xdcO = xdcO;
    this.ses = ses;
    om = (Value.Obj) xdcO.get("om", null);

    Object o = om.geto("$name");
    String s = o instanceof String ? (String) o : null;
    isCFG = s != null && s.equals("cfg");
    isROV = s != null && s.equals("rov");

    $$IMPORTS();
    $$OBJECTS();
    Platform$$OBJECTS();
    SunSparc7$$OBJECTS();
    Platform$$CONSTS();
    SunSparc7$$CONSTS();
    Platform$$CREATES();
    SunSparc7$$CREATES();
    Platform$$FUNCTIONS();
    SunSparc7$$FUNCTIONS();
    Platform$$SIZES();
    SunSparc7$$SIZES();
    Platform$$TYPES();
    SunSparc7$$TYPES();
    if (isROV) {
      Platform$$ROV();
      SunSparc7$$ROV();
    } // isROV
    $$SINGLETONS();
    Platform$$SINGLETONS();
    SunSparc7$$SINGLETONS();
    $$INITIALIZATION();
  }
Example #24
0
 public static Object eval(String source, Map<String, Scriptable> bindings) {
   Context cx = ContextFactory.getGlobal().enterContext();
   try {
     Scriptable scope = cx.initStandardObjects();
     if (bindings != null) {
       for (String id : bindings.keySet()) {
         Scriptable object = bindings.get(id);
         object.setParentScope(scope);
         scope.put(id, scope, object);
       }
     }
     return cx.evaluateString(scope, source, "source", 1, null);
   } finally {
     Context.exit();
   }
 }
Example #25
0
 /**
  * Executes an arbitrary expression.<br>
  * It fails if the expression throws a JsAssertException.<br>
  * It fails if the expression throws a RhinoException.<br>
  * Code from JsTester (http://jstester.sf.net/)
  */
 public Object eval(String expr) {
   Object value = null;
   try {
     value = context.evaluateString(globalScope, expr, "", 1, null);
   } catch (JavaScriptException jse) {
     Scriptable jsAssertException = (Scriptable) globalScope.get("currentException", globalScope);
     jse.printStackTrace();
     String message = (String) jsAssertException.get("message", jsAssertException);
     if (message != null) {
       fail(message);
     }
   } catch (RhinoException re) {
     fail(re.getMessage());
   }
   return value;
 }
  @Override
  protected void setInstanceIdValue(int id, Scriptable start, Object value) {
    Scriptable original = start;
    ProgressBarProxyPrototype proxy = this;
    while (start != null && !(start instanceof ProgressBarProxyPrototype)) {
      start = start.getPrototype();
    }

    if (start instanceof ProgressBarProxyPrototype) {
      proxy = (ProgressBarProxyPrototype) start;
    }

    switch (id) {
      case Id_min:
        proxy.setProperty("min", value);
        proxy.onPropertyChanged("min", value);
        break;
      case Id_max:
        proxy.setProperty("max", value);
        proxy.onPropertyChanged("max", value);
        break;
      case Id_value:
        proxy.setProperty("value", value);
        proxy.onPropertyChanged("value", value);
        break;
      case Id_message:
        proxy.setProperty("message", value);
        proxy.onPropertyChanged("message", value);
        break;
      default:
        throw new IllegalArgumentException(String.valueOf(id));
    }
  }
  @Override
  protected void setInstanceIdValue(int id, Scriptable start, Object value) {
    Scriptable original = start;
    AttrProxyPrototype proxy = this;
    while (start != null && !(start instanceof AttrProxyPrototype)) {
      start = start.getPrototype();
    }

    if (start instanceof AttrProxyPrototype) {
      proxy = (AttrProxyPrototype) start;
    }

    switch (id) {
      case Id_specified:
        proxy.setProperty("specified", value);
        proxy.onPropertyChanged("specified", value);
        break;
      case Id_ownerElement:
        proxy.setProperty("ownerElement", value);
        proxy.onPropertyChanged("ownerElement", value);
        break;
      case Id_name:
        proxy.setProperty("name", value);
        proxy.onPropertyChanged("name", value);
        break;
      case Id_value:
        proxy.setter_value(value);
        break;
      default:
        throw new IllegalArgumentException(String.valueOf(id));
    }
  }
Example #28
0
File: DB.java Project: o-nix/Kafra
  public Object jsFunction_create(String collection, Scriptable defaults) {
    ODocument document = collection != null ? new ODocument(collection) : new ODocument();
    ScriptableObject result = (ScriptableObject) wrap(document);

    if (defaults != null && !(defaults instanceof Undefined)) {
      for (Object rawId : defaults.getIds()) {
        String id = (String) rawId;
        Object value = defaults.get(id, defaults);

        if (value instanceof NativeJavaObject) value = jsFunction_objectToDocument(value);

        result.put(id, result, value);
      }
    }

    return result;
  }
Example #29
0
 public String execute(String methodName, Object... params) throws Throwable {
   if (cx == null) {
     throw new IllegalStateException("Can't execute a function, context is null!");
   }
   Object fObj = scope.get(methodName, scope);
   Function f = (Function) fObj;
   return Context.toString(f.call(cx, scope, scope, params));
 }
 /**
  * Sets the value of the indexed property, creating it if need be.
  *
  * @param index the numeric index for the property
  * @param start the object whose property is being set
  * @param value value to set the property to
  */
 public void put(int index, Scriptable start, Object value) {
   if (start == this) {
     synchronized (this) {
       indexedProps.put(new Integer(index), value);
     }
   } else {
     start.put(index, start, value);
   }
 }