示例#1
0
  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();
  }
示例#2
0
  private Object parseMessage() {
    // Consume SOAP message, if any
    // TODO: Add a SOAP handler in Parser.java
    if (soapAction != null) {
      try {
        MimeHeaders mime_headers = new MimeHeaders();

        for (Object k : headers.getIds()) {
          if (k instanceof String) {
            String name = (String) k;
            String value = Context.toString(ScriptableObject.getProperty(headers, name));

            mime_headers.addHeader(name, value);
          }
        }

        return MessageFactory.newInstance(SOAPConstants.DYNAMIC_SOAP_PROTOCOL)
            .createMessage(mime_headers, request.getInputStream());
      } catch (IOException ex) {
        throw new ESXXException("Unable to read SOAP message stream: " + ex.getMessage());
      } catch (SOAPException ex) {
        throw new ESXXException(400 /* Bad Request */, "Invalid SOAP message: " + ex.getMessage());
      } finally {
        try {
          request.getInputStream().close();
        } catch (Exception ignored) {
        }
      }
    } else if (contentType != null && contentLength > 0) {
      try {
        ESXX esxx = ESXX.getInstance();
        return esxx.parseStream(
            contentType,
            request.getInputStream(),
            URI.create("urn:x-esxx:incoming-request-entity"),
            null,
            new java.io.PrintWriter(request.getErrorWriter()),
            Context.getCurrentContext(),
            this);
      } catch (Exception ex) {
        throw new ESXXException(
            400 /* Bad Request */, "Unable to parse request entity: " + ex.getMessage(), ex);
      } finally {
        try {
          request.getInputStream().close();
        } catch (Exception ignored) {
        }
      }
    } else {
      // Return a dummy object
      return Context.getCurrentContext().newObject(this);
    }
  }
示例#3
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));
     }
   }
 }
示例#4
0
文件: DB.java 项目: 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;
  }
示例#5
0
文件: DB.java 项目: 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;
  }
  void listCommands() {
    if (ServiceManager.Current != null) {
      Scriptable scope = ServiceManager.Current.getScope();

      Object[] objs = scope.getIds();

      for (int i = 0; i < objs.length; i++) {
        Object obj = objs[i];

        try {
          int pos = commanddoc.getLength();

          commanddoc.insertString(pos, obj.toString() + "\n", responseStyle);
        } catch (BadLocationException ex) {
          Exceptions.printStackTrace(ex);
        }
      }
    }
  }
示例#7
0
  private void enumerateProperty(
      Context cx, String name, PropEnumerator pe, URI candidate, String realm, String mechanism) {
    String uri = candidate.toString();
    String scheme = candidate.getScheme();
    String user = candidate.getUserInfo();
    String host = candidate.getHost();
    Integer port = candidate.getPort();
    String path = candidate.getPath();

    Object p = ScriptableObject.getProperty(this, name);

    if (p instanceof Scriptable) {
      Scriptable params = (Scriptable) p;

      for (Object key : params.getIds()) {
        if (key instanceof Integer) {
          p = params.get((Integer) key, params);
        } else {
          p = params.get((String) key, params);
        }

        if (p instanceof Scriptable) {
          Scriptable param = (Scriptable) p;

          int score = 0;

          score += filterProperty(cx, param, "realm", realm) * 1;
          score += filterProperty(cx, param, "mechanism", mechanism) * 2;
          score += filterProperty(cx, param, "scheme", scheme) * 4;
          score += filterProperty(cx, param, "path", path) * 8;
          score += filterProperty(cx, param, "port", port) * 16;
          score += filterProperty(cx, param, "host", host) * 32;
          score += filterProperty(cx, param, "user-info", user) * 64;
          score += filterProperty(cx, param, "uri", uri) * 128;

          if (score >= 0) {
            pe.handleProperty(param, score);
          }
        }
      }
    }
  }
示例#8
0
 static ExhibitDescriptor toExhibitDescriptor(Scriptable res, Set<String> existing) {
   if (res == null) {
     throw new IllegalStateException("Null return values are not permitted");
   }
   List<ObsDescriptor.Field> fields = Lists.newArrayList();
   Map<String, FieldType> vecs = Maps.newHashMap();
   Map<String, ObsDescriptor> frames = Maps.newHashMap();
   for (Object id : res.getIds()) {
     if (!existing.contains(id) && !"INIT".equals(id)) {
       Object v = res.get(id.toString(), res);
       if (isCastableToFrame(v)) {
         frames.put(id.toString(), toFrameDescriptor(v));
       } else if (isCastableToVec(v)) {
         vecs.put(id.toString(), toVectorType(v));
       } else if (isCastableToField(v)) {
         fields.add(new ObsDescriptor.Field(id.toString(), toFieldType(v)));
       }
     }
   }
   return new ExhibitDescriptor(new SimpleObsDescriptor(fields), frames, vecs);
 }
 public static String scriptableToString(Scriptable scriptable) {
   StringBuilder builder = new StringBuilder();
   for (Object propid : scriptable.getIds()) {
     String propIdString = Context.toString(propid);
     int propIntKey = -1;
     try {
       propIntKey = Integer.parseInt(propIdString);
     } catch (NumberFormatException nfe) {
       // dummy.
     }
     String propValue;
     if (propIntKey >= 0) {
       propValue = Context.toString(scriptable.get(propIntKey, scriptable));
     } else {
       propValue = Context.toString(scriptable.get(propIdString, scriptable));
     }
     builder.append(propIdString);
     builder.append(": ");
     builder.append(propValue);
     builder.append("; ");
   }
   return builder.toString();
 }
示例#10
0
  protected Object toNative(Object value, Class<?> target) {
    Object o = null;

    if (value instanceof String) {
      o = Context.jsToJava(value, target);
    } else if (value instanceof Double || value instanceof Integer) {
      o = Context.jsToJava(value, target);
    } else if (value instanceof Boolean) {
      o = Context.jsToJava(value, target);
    } else if (value instanceof Function) {
      if (DBG) {
        Log.i(LCAT, "Is a Function");
      }
      o = new KrollCallback(weakKrollContext.get(), this, (Function) value);
    } else if (value == null) {
      o = null;
    } else if (value instanceof Scriptable) {
      Scriptable svalue = (Scriptable) value;
      if (isArrayLike(svalue)) {
        o = toArray(svalue);
      } else if (value instanceof KrollObject) {
        o = ((KrollObject) value).target;
      } else if (svalue.getClassName().equals("Date")) {
        double time = (Double) ScriptableObject.callMethod(svalue, "getTime", new Object[0]);
        o = new Date((long) time);
      } else {
        TiDict args = new TiDict();
        o = args;

        Scriptable so = (Scriptable) value;
        for (Object key : so.getIds()) {
          Object v = so.get((String) key, so);
          v = toNative(v, Object.class);
          //					if (v instanceof Scriptable && isArrayLike((Scriptable) v)) {
          //						v = toArray((Scriptable) v);
          //					}
          if (DBG) {
            Log.i(LCAT, "Key: " + key + " value: " + v + " type: " + v.getClass().getName());
          }
          args.put((String) key, v);
        }
        // Log.w(LCAT, "Unhandled type conversion of Scriptable: value: " + value.toString() + "
        // type: " + value.getClass().getName());
      }
    } else {
      if (value.getClass().isArray()) {
        Object[] values = (Object[]) value;
        Object[] newValues = new Object[values.length];
        for (int i = 0; i < values.length; i++) {
          newValues[i] = toNative(values[i], Object.class);
        }
        o = newValues;
      } else {
        Log.w(
            LCAT,
            "Unhandled type conversion: value: "
                + value.toString()
                + " type: "
                + value.getClass().getName());
      }
    }

    return o;
  }
  public void Serialize(Scriptable start, Object obj, String loc, int tab) {
    if (obj == null) {
      pr.write("null");
    } else if (set.containsKey(obj)) {
      pr.write("/* [" + obj.getClass().getName() + " " + set.get(obj) + "] */");
    } else if (obj instanceof String) {
      pr.write("\"" + obj.toString() + "\"");
    } else if (obj instanceof Number) {
      pr.write(obj.toString());
    } else if (obj instanceof UniqueTag) {
      UniqueTag u = (UniqueTag) obj;

      if (u.equals(UniqueTag.NULL_VALUE)) {
        pr.write("null");
      } else if (u.equals(UniqueTag.NOT_FOUND)) {
        pr.write("__undefined__");
      } else if (u.equals(UniqueTag.DOUBLE_MARK)) {
        pr.write("__double_mark__");
      }
      /*
      			set.put(obj,loc);

      			Serialize(u.readResolve());
      */
    } else if (obj instanceof Scriptable) {
      Scriptable s = (Scriptable) obj;
      String className = s.getClassName();

      Object[] ids = s.getIds();

      pr.write("{\n");
      writeTabs(tab);

      for (Object o : ids) {
        Object val = null;
        String nloc = loc + "/";
        if (o instanceof String) {
          val = s.get((String) o, s);
          pr.write("\"" + o.toString() + "\":");
          nloc += (String) o;
        } else {
          val = s.get((Integer) o, s);
          pr.write("" + o.toString() + ":");
          nloc += o.toString();
        }

        set.put(obj, loc);

        if (val == null) {
          pr.write("null,\n");
          writeTabs(tab);
        } else {
          Serialize(start, val, nloc, tab + 1);
          pr.write(",\n");
          writeTabs(tab);
        }
      }
      pr.write("}\n");
      writeTabs(tab);
    } else {
      pr.write("/* " + obj.getClass().getName() + " */\n");
      writeTabs(tab);
    }
  }
示例#12
0
  /**
   * Register the script content for current script handler.
   *
   * @param sScriptContent This is either the JavaSciprt code content or a full class name which has
   *     implemented <code>IChartItemScriptHandler</code>
   */
  @SuppressWarnings("unchecked")
  public final void register(String sScriptName, String sScriptContent) throws ChartException {
    try {
      getLogger()
          .log(
              ILogger.INFORMATION, Messages.getString("Info.try.load.java.handler")); // $NON-NLS-1$

      Class<?> handlerClass = null;

      try {
        handlerClass = Class.forName(sScriptContent);
      } catch (ClassNotFoundException ex) {
        if (iscl != null) {
          handlerClass =
              iscl.loadClass(
                  sScriptContent,
                  SecurityUtil.getClassLoader(AbstractScriptHandler.this.getClass()));
        } else {
          throw ex;
        }
      }

      if (getEventHandlerClass().isAssignableFrom(handlerClass)) {
        try {
          javahandler = (T) SecurityUtil.newClassInstance(handlerClass);
        } catch (InstantiationException e) {
          throw new ChartException(ChartEnginePlugin.ID, BirtException.ERROR, e);
        } catch (IllegalAccessException e) {
          throw new ChartException(ChartEnginePlugin.ID, BirtException.ERROR, e);
        }

        getLogger()
            .log(
                ILogger.INFORMATION,
                Messages.getString(
                    "Info.java.handler.loaded", //$NON-NLS-1$
                    handlerClass,
                    ULocale.getDefault()));
      } else {
        getLogger()
            .log(
                ILogger.WARNING,
                Messages.getString(
                    "Info.invalid.java.handler", //$NON-NLS-1$
                    handlerClass,
                    ULocale.getDefault()));
      }
    } catch (ClassNotFoundException e) {
      // Not a Java class name, so this must be JavaScript code
      javahandler = null;

      getLogger()
          .log(
              ILogger.INFORMATION,
              Messages.getString("Info.try.register.javascript.content")); // $NON-NLS-1$

      final Context cx = Context.enter();
      try {
        cx.evaluateString(
            scope,
            sScriptContent,
            sScriptName == null ? "<cmd>" : sScriptName,
            1,
            null); //$NON-NLS-1$

        getLogger()
            .log(
                ILogger.INFORMATION,
                Messages.getString("Info.javascript.content.registered")); // $NON-NLS-1$

        // prepare function name cache.
        Object[] objs = scope.getIds();

        if (objs != null) {
          javaScriptFunctionNamesCache = new ArrayList<String>();
          for (int i = 0; i < objs.length; i++) {
            javaScriptFunctionNamesCache.add(String.valueOf(objs[i]));
          }
        } else {
          javaScriptFunctionNamesCache = null;
        }

      } catch (RhinoException jsx) {
        throw convertException(jsx);
      } finally {
        Context.exit();
      }
    }
  }