Ejemplo n.º 1
0
 @Override
 public String getTidyOutput(Object obj) {
   if (obj == null) {
     return "";
   }
   if (obj instanceof Undefined) {
     return "";
   } else if (obj instanceof String) {
     return (String) obj;
   } else if (obj instanceof Integer) {
     return ((Integer) obj).toString();
   } else if (obj instanceof Boolean) {
     return ((Boolean) obj).toString();
   } else if (obj instanceof NativeArray) {
     String ret = "[";
     for (Object object : ((NativeArray) obj).getAllIds()) {
       ret += getTidyOutput(((NativeArray) obj).get(object)) + ", ";
     }
     return ret + "]";
   } else if (obj instanceof NativeJavaObject) {
     NativeJavaObject natObj = (NativeJavaObject) obj;
     if (!(natObj.unwrap() instanceof String)) {
       return "JavaObject [" + natObj.unwrap().getClass().getName() + "]";
     } else {
       return (String) natObj.unwrap();
     }
   } else {
     return obj.toString();
   }
 }
  private Object unwrap(Object obj) {
    if (obj instanceof NativeJavaObject) {
      NativeJavaObject res = (NativeJavaObject) obj;
      return res.unwrap();
    }

    if (obj != null) {
      System.err.println(
          "Cannot handle Javascript result of type " + obj.getClass().getCanonicalName());
    }
    return null;
  }
Ejemplo n.º 3
0
 public static Object map(Context cx, Scriptable thisObj, Object[] args, Function funObj) {
   NativeJavaObject njo = (NativeJavaObject) args[0];
   NativeArray array = (NativeArray) cx.newArray(thisObj, 0);
   Function f = (Function) args[1];
   Collection list = (Collection) njo.unwrap();
   int i = 0;
   for (Object o : list) {
     Object[] argx = {o, Context.javaToJS(i, f), njo};
     Object r = f.call(cx, thisObj, f, argx);
     array.put((int) array.getLength(), array, Context.javaToJS(r, f));
     i++;
   }
   return array;
 }
Ejemplo n.º 4
0
 public static Object find(Context cx, Scriptable thisObj, Object[] args, Function funObj) {
   NativeJavaObject njo = (NativeJavaObject) args[0];
   Function f = (Function) args[1];
   Collection list = (Collection) njo.unwrap();
   int i = 0;
   for (Object o : list) {
     Object[] argx = {o, i, njo};
     Object r = f.call(cx, thisObj, f, argx);
     if (r != null && r.getClass() != Undefined.class && !r.equals(false)) {
       return Context.javaToJS(o, f);
     }
   }
   return null;
 }
Ejemplo n.º 5
0
  public static Object filter(Context cx, Scriptable thisObj, Object[] args, Function funObj) {
    NativeArray array = (NativeArray) cx.newArray(thisObj, 0);
    NativeJavaObject njo = (NativeJavaObject) args[0];
    Function f = (Function) args[1];
    Collection list = (Collection) njo.unwrap();
    int i = 0;
    for (Object o : list) {
      Object[] argx = {o, i, njo};
      Object r = f.call(cx, thisObj, f, argx);
      if (r != null && r.getClass() != Undefined.class && !r.equals(false)) {
        array.put((int) array.getLength(), array, o);
      }
    }

    return array;
  }
Ejemplo n.º 6
0
 public static Object reduce(Context cx, Scriptable thisObj, Object[] args, Function funObj) {
   NativeJavaObject njo = (NativeJavaObject) args[0];
   Function f = (Function) args[1];
   Collection list = (Collection) njo.unwrap();
   int i = 0;
   Object or = null;
   for (Object o : list) {
     if (i == 1) {
       or = o;
       continue;
     }
     Object[] argx = {or, o, i, njo};
     or = f.call(cx, thisObj, f, argx);
     i++;
   }
   return or;
 }
  /**
   * sorts the date into a prerequisite format based on the index.
   *
   * <p>currently does not work corretly with javascript method calls, if this method was commented
   * in, it would be called, even for calls to printd(String,Scritable) <br>
   * returns a javascript string.
   */
  public Object printd(final int index, final org.mozilla.javascript.Scriptable obj) {
    if (DebugDefaultJavascript) {
      System.out.println("JpedalDefaultJavascript.printd(int,Scriptable)");
    }

    Calendar date = null;
    if (org.mozilla.javascript.NativeJavaObject.canConvert(obj, Calendar.class)) {
      date = (java.util.Calendar) org.mozilla.javascript.Context.jsToJava(obj, Calendar.class);
    }

    if (date == null) {
      org.mozilla.javascript.Context.throwAsScriptRuntimeEx(new RuntimeException("Not a Date()"));
    }

    // <start-demo><end-demo>

    final SimpleDateFormat df = new SimpleDateFormat(format[index]);

    return context.newObject(scope, "String", new Object[] {df.format(date)});
  }
Ejemplo n.º 8
0
 public static Scriptable createAdapterWrapper(Scriptable obj, Object adapter) {
   Scriptable scope = ScriptableObject.getTopLevelScope(obj);
   NativeJavaObject res = new NativeJavaObject(scope, adapter, null, true);
   res.setPrototype(obj);
   return res;
 }
  /**
   * sorts the date into the string format specified
   *
   * <p>longMonth = "mmmm", shortMonth = "mmm", lead0NumberedMonth = "mm", nonLead0NumberedMonth =
   * "m", longDay = "dddd", shortDay= "ddd", lead0NumberedDay = "dd", nonLead0NumberedDay = "d",
   * longYear = "yyyy", shortYear = "yy", lead024Hour = "HH", nonLead024Hour = "H", lead012Hour =
   * "hh", nonLead012Hour ="h", lead0Mins = "MM", nonLead0Mins = "M", lead0Secs = "ss", nonLead0Secs
   * = "s", amPm = "tt", singleDigitAmPm = "t", escapeChar = "\"; <br>
   * returns a javascript string.
   */
  public Object printd(final String format, final org.mozilla.javascript.Scriptable obj) {
    if (DebugDefaultJavascript) {
      System.out.println("JpedalDefaultJavascript.printd(String,Scriptable)");
    }

    // get the Date class from the Scriptable value passed in as
    // Rhino does not allow NativeDate to be used externally, even though this is what is passed
    // here.
    Date jsDate = null;
    if (org.mozilla.javascript.NativeJavaObject.canConvert(obj, Date.class)) {
      jsDate = (java.util.Date) org.mozilla.javascript.Context.jsToJava(obj, Date.class);
    }
    if (jsDate == null) {
      org.mozilla.javascript.Context.throwAsScriptRuntimeEx(new RuntimeException("Not a Date()"));
    }

    Calendar date = Calendar.getInstance();
    date.setTime(jsDate);
    // jsDate = null;//delete the variable so it does not get used

    // <start-demo><end-demo>

    // replace the javascript format string with a java format string to produce the required
    // output.
    // we need to do this as some chars represent different types of data in each
    final char[] formatVal = format.toCharArray();
    final StringBuffer retVal = new StringBuffer();
    for (int i = 0; i < formatVal.length; i++) {
      if (formatVal[i] == 'm') { // replace JS month with java month
        retVal.append('M');
      } else if (formatVal[i] == 't') {
        if (formatVal[i + 1] == 't') { // if tt replace with char for AM/PM
          retVal.append('a');
          i++;
        } else { // single t special case, replace with either A/P
          if (date.get(Calendar.HOUR_OF_DAY) > 12) {
            retVal.append("'P'");
          } else {
            retVal.append("'A'");
          }
        }
      } else if (formatVal[i] == 'M') { // replace JS min for java min
        retVal.append('m');
      } else if (formatVal[i] == 'd' && formatVal[i + 1] == 'd' && formatVal[i + 2] == 'd') {
        // if word for day is needed then change to java word of day
        if (formatVal[i + 3] == 'd') { // long JS day replaced with long java day
          retVal.append("EEEE");
          i += 3;
        } else { // short JS day replaced with short java day
          retVal.append("EEE");
          i += 2;
        }
      } else if (formatVal[i] == '\\') {
        retVal.append('\'');
        retVal.append(formatVal[++i]);
        retVal.append('\'');
      } else {
        retVal.append(formatVal[i]);
      }
    }

    // create the formatter object with the required format
    final SimpleDateFormat df = new SimpleDateFormat(new String(retVal));
    final String newDate = df.format(date.getTime());

    if (DebugDefaultJavascript) {
      System.out.println("returning String=" + newDate);
    }

    return context.newObject(scope, "String", new Object[] {newDate});
  }