Ejemplo n.º 1
0
  private void processResponse(RubyValue res) {
    if (res != null && res != RubyConstant.QNIL && res instanceof RubyHash) {
      RubyHash resHash = (RubyHash) res;
      RubyValue resBody = null;

      RubyArray arKeys = resHash.keys();
      RubyArray arValues = resHash.values();
      for (int i = 0; i < arKeys.size(); i++) {
        String strKey = arKeys.get(i).toString();
        if (strKey.equals("request-body")) resBody = arValues.get(i);
        else if (strKey.equals("status")) responseCode = arValues.get(i).toInt();
        else if (strKey.equals("message")) responseMsg = arValues.get(i).toString();
        else resHeaders.addProperty(strKey, arValues.get(i).toString());
      }
      String strBody = "";

      if (resBody != null && resBody != RubyConstant.QNIL)
        strBody = resBody.toRubyString().toString();

      if (!RHOCONF().getBool("log_skip_post")) LOG.TRACE(strBody);

      try {
        responseData = new ByteArrayInputStream(strBody.getBytes("UTF-8"));
      } catch (java.io.UnsupportedEncodingException exc) {
        LOG.ERROR("Error getting utf-8 body :", exc);
      }

      if (responseData != null)
        contentLength = Integer.parseInt(resHeaders.getPropertyIgnoreCase("Content-Length"));
    }
  }
Ejemplo n.º 2
0
  // @RubyLevelMethod(name="split")
  public RubyValue split(RubyArray args) {
    RubyValue r = (null == args) ? GlobalVariables.get("$;") : args.get(0);

    Collection /*<String>*/ splitResult;
    boolean bSkipFirstEmptyItem = false;
    if (r == RubyConstant.QNIL) {
      splitResult = split(this, " ");
    } else if (r instanceof RubyRegexp) {
      RubyRegexp reg = (RubyRegexp) r;
      splitResult = split(this, reg, args);

      if (reg.getPattern().getPattern().startsWith("(?=")) bSkipFirstEmptyItem = true;
    } else if (r instanceof RubyString) {
      splitResult = split(this, ((RubyString) r).toString());
    } else {
      throw new RubyException(
          RubyRuntime.ArgumentErrorClass,
          "wrong argument type " + r.getRubyClass() + " (expected Regexp)");
    }

    RubyArray a = new RubyArray(splitResult.size());
    int i = 0;
    //        for (String str : splitResult) {
    for (Iterator iter = splitResult.iterator(); iter.hasNext(); ) {
      String str = (String) iter.next();
      if (!(bSkipFirstEmptyItem && 0 == i && (str == null || str.equals("")))) {
        // To conform ruby's behavior, discard the first empty element
        a.add(ObjectFactory.createString(str));
      }
      ++i;
    }
    return a;
  }
Ejemplo n.º 3
0
  private void processResponse(RubyValue res) {
    if (res != null && res != RubyConstant.QNIL && res instanceof RubyHash) {
      RubyHash resHash = (RubyHash) res;
      RubyValue resBody = null;

      RubyArray arKeys = resHash.keys();
      RubyArray arValues = resHash.values();
      for (int i = 0; i < arKeys.size(); i++) {
        String strKey = arKeys.get(i).toString();
        if (strKey.equals("request-body")) resBody = arValues.get(i);
        else if (strKey.equals("status")) responseCode = arValues.get(i).toInt();
        else if (strKey.equals("message")) responseMsg = arValues.get(i).toString();
        else resHeaders.addProperty(strKey, arValues.get(i).toString());
      }
      String strBody = "";

      if (resBody != null && resBody != RubyConstant.QNIL)
        strBody = resBody.toRubyString().toString();

      log(strBody);

      responseData = new ByteArrayInputStream(strBody.getBytes());
      if (responseData != null)
        contentLength = Integer.parseInt(resHeaders.getPropertyIgnoreCase("Content-Length"));
    }
  }
Ejemplo n.º 4
0
  // RHO_COMMENT
  public boolean equals(Object o, boolean bConvToAry) {
    if (this == o) {
      return true;
    }

    if (o instanceof RubyArray) {
      RubyArray that = (RubyArray) o;
      int size = array_.size();
      if (size != that.size()) {
        return false;
      }

      for (int i = 0; i < size; ++i) {
        if (!this.get(i).equals(that.get(i))) {
          return false;
        }
      }

      return true;
    } else if (o instanceof RubyValue && bConvToAry) {
      RubyValue v = (RubyValue) o;
      if (!v.respondTo(RubyID.toAryID)) {
        return false;
      } else {
        return v.equals(this);
      }
    } else {
      return false;
    }
  }
Ejemplo n.º 5
0
  // @RubyLevelMethod(name="[]", alias="slice")
  public RubyValue aref(RubyValue arg) {
    if (arg instanceof RubyFixnum) {
      return this.get(arg.toInt());
    }

    if (arg instanceof RubySymbol) {
      throw new RubyException(RubyRuntime.TypeErrorClass, "Symbol as array index");
    }

    if (arg instanceof RubyRange) {
      RubyRange range = (RubyRange) arg;
      int begin = range.getLeft().toInt();
      int end = range.getRight().toInt();
      if (begin < 0) {
        begin = this.size() + begin;
      }
      if (end < 0) {
        end = this.size() + end;
      }

      if (!range.isExcludeEnd()) {
        ++end;
      }

      RubyArray resultValue = this.subarray(begin, end - begin);
      if (null == resultValue) return RubyConstant.QNIL;
      return resultValue;
      // return (null == resultValue ? RubyConstant.QNIL : resultValue);
    }

    return this.get(arg.toInt());
  }
 private static boolean hasMethod(RubyValue receiver, RubyID mid, boolean include_private) {
   if (include_private) {
     return (null != receiver.findMethod(mid));
   } else {
     return (null != receiver.findPublicMethod(mid));
   }
 }
 @RubyLevelMethod(name = "eval", module = true)
 public static RubyValue eval(RubyValue receiver, RubyValue arg0, RubyValue arg1) {
   if (arg1 instanceof RubyBinding) {
     return eval(arg0.toStr(), (RubyBinding) arg1);
   } else {
     return eval(arg0.toStr());
   }
 }
 @RubyLevelMethod(name = "p", module = true)
 public static RubyValue p(RubyValue receiver, RubyValue arg) {
   RubyValue str = RubyAPI.callNoArgMethod(arg, null, RubyID.inspectID);
   RubyString value = str.toRubyString();
   value.appendString("\n");
   System.out.print(value.toString());
   return RubyConstant.QNIL;
 }
Ejemplo n.º 9
0
 public int hashCode() {
   int hash = 0;
   //        for (RubyValue v : array_) {
   for (Iterator iter = array_.iterator(); iter.hasNext(); ) {
     RubyValue v = (RubyValue) iter.next();
     hash += v.hashCode();
   }
   return hash;
 }
Ejemplo n.º 10
0
  public RubyString appendString(RubyValue v) {
    if (v instanceof RubyString) {
      return appendString((RubyString) v);
    } else {
      RubyValue r = RubyAPI.callPublicNoArgMethod(v, null, RubyID.toSID);
      if (r instanceof RubyString) return appendString((RubyString) r);

      return ObjectFactory.createString(r.toString());
    }
  }
Ejemplo n.º 11
0
  // @RubyLevelMethod(name="[]", alias="slice")
  public RubyValue aref(RubyValue begin, RubyValue length) {
    if (begin instanceof RubySymbol) {
      throw new RubyException(RubyRuntime.TypeErrorClass, "Symbol as array index");
    }

    RubyArray resultValue = this.subarray(begin.toInt(), length.toInt());
    if (null == resultValue) return RubyConstant.QNIL;
    return resultValue;
    //        return (null == resultValue ? RubyConstant.QNIL : resultValue);
  }
Ejemplo n.º 12
0
 // @RubyLevelMethod(name="delete_if")
 public RubyValue delete_if(RubyBlock block) {
   for (int i = 0; i < array_.size(); ) {
     RubyValue r = block.invoke(this, (RubyValue) array_.get(i));
     if (r.isTrue()) {
       array_.remove(i);
     } else {
       ++i;
     }
   }
   return this;
 }
Ejemplo n.º 13
0
  // @RubyLevelMethod(name="concat", alias="<<")
  public RubyString concat(RubyValue v) {
    if (v instanceof RubyFixnum) {
      int i = v.toInt();
      if (i >= 0 && i <= 0xff) {
        this.sb_.append((char) i);
        return this;
      }
    }

    this.sb_.append(v.toRubyString().sb_);
    return this;
  }
Ejemplo n.º 14
0
  // @RubyLevelMethod(name="force_encoding")
  public RubyValue force_encoding(RubyValue arg) {
    if ((m_valEncoding != null && !m_valEncoding.toString().equalsIgnoreCase("UTF-8"))
        && arg.toString().equalsIgnoreCase("UTF-8")) {
      try {
        byte bytes[] = sb_.toString().getBytes("ISO8859_1");
        String str1 = new String(bytes, "UTF-8");
        sb_ = new StringBuffer(str1);
      } catch (java.io.UnsupportedEncodingException exc) {
        sb_ = new StringBuffer("");
      }
    }

    m_valEncoding = arg;
    return this;
  }
  private static RubyValue objSingletonMethod(RubyValue receiver, boolean all) {
    RubyArray a = new RubyArray();
    if (receiver.getRubyClass().isSingleton()) {
      RubyClass rubyClass = receiver.getRubyClass();
      rubyClass.collectOwnMethodNames(a, RubyMethod.PUBLIC);
      rubyClass = rubyClass.getSuperClass();

      if (all) {
        while (rubyClass != null && rubyClass.isSingleton()) {
          rubyClass.collectOwnMethodNames(a, RubyMethod.PUBLIC);
          rubyClass = rubyClass.getSuperClass();
        }
      }
    }
    return a;
  }
 @RubyLevelMethod(name = "methods")
 public static RubyValue objMethods(RubyValue receiver) {
   RubyArray a = new RubyArray();
   RubyClass klass = receiver.getRubyClass();
   klass.collectClassMethodNames(a, RubyMethod.ALL);
   return a;
 }
 @RubyLevelMethod(name = "method")
 public static RubyValue objMethod(RubyValue receiver, RubyValue arg) {
   String method_name = arg.toStr();
   RubyID mid = RubyID.intern(method_name);
   RubyMethod m = receiver.findMethod(mid);
   if (null == m) {
     throw new RubyException(
         RubyRuntime.NameErrorClass,
         "public method '"
             + method_name
             + "' can not be found in '"
             + receiver.getRubyClass().getName()
             + "'");
   }
   return ObjectFactory.createMethod(receiver, method_name, m);
 }
  @RubyLevelMethod(name = "__load_with_reflection__", module = true)
  public static RubyValue loadWithReflection(RubyValue receiver, RubyValue arg, RubyBlock block) {
    String required_file = arg.toStr();
    String name = NameFactory.createMainClassName(required_file);
    try {
      Class c = Class.forName(name);
      Object o = c.newInstance();
      RubyProgram p = (RubyProgram) o;

      // $".push(file_name) unless $".include?(file_name)
      RubyArray a = (RubyArray) GlobalVariables.get("$\"");
      if (a.include(arg) == RubyConstant.QFALSE) {
        a.push(arg);
      }

      p.invoke();
      return RubyConstant.QTRUE;
    } catch (ClassNotFoundException e) {
      return RubyConstant.QFALSE;
    } catch (InstantiationException e) {
      return RubyConstant.QFALSE;
    } catch (IllegalAccessException e) {
      return RubyConstant.QFALSE;
    }
  }
Ejemplo n.º 19
0
  @RubyLevelMethod(name = "&")
  public RubyValue opAnd(RubyValue v) {
    if (v instanceof RubyBignum) {
      return ((RubyBignum) v).op_band(this);
    }

    return RubyBignum.bignorm(this.value_ & v.toInt());
  }
Ejemplo n.º 20
0
  @RubyLevelMethod(name = "^")
  public RubyValue opXor(RubyValue v) {
    if (v instanceof RubyBignum) {
      return ((RubyBignum) v).op_bxor(this);
    }

    return RubyBignum.bignorm(this.value_ ^ v.toInt());
  }
 @RubyLevelMethod(name = "method_missing", module = true)
 public static RubyValue methodMissing(RubyValue receiver, RubyArray args) {
   RubySymbol method_name = (RubySymbol) args.get(0);
   RubyClass klass = receiver.getRubyClass();
   klass = (klass != null) ? klass.getRealClass() : null;
   throw new RubyException(
       RubyRuntime.NoMethodErrorClass,
       "undefined method '" + method_name.toString() + "' for " + klass.getName());
 }
 @RubyLevelMethod(
     name = "object_id",
     alias = {"__id__", "hash"})
 public static RubyValue objectId(RubyValue receiver) {
   // Object.hashCode() javadoc:
   // As much as is reasonably practical, the hashCode method defined
   // by class Object does return distinct integers for distinct objects.
   return ObjectFactory.createFixnum(receiver.hashCode());
 }
 @RubyLevelMethod(name = "throw", module = true)
 public static RubyValue throwMethod(RubyValue receiver, RubyArray args) {
   RubyExceptionValue e;
   RubyValue tag = args.get(0);
   if (tag instanceof RubySymbol || tag instanceof RubyString) {
     e = new RubyExceptionValueForThrow(tag, args.get(1));
   } else if (tag instanceof RubyExceptionValue) {
     e = (RubyExceptionValue) tag;
   } else if (tag instanceof RubyClass) {
     RubyClass c = (RubyClass) tag;
     e = new RubyExceptionValue(c, c.getName() + " is not a symbol");
   } else {
     e =
         new RubyExceptionValue(
             RubyRuntime.ArgumentErrorClass, tag.toString() + " is not a symbol");
   }
   throw new RubyException(e);
 }
Ejemplo n.º 24
0
  // @RubyLevelMethod(name="last")
  public RubyValue last(RubyValue v) {
    int n = v.toInt();
    int size = this.array_.size();
    if (n > size) {
      n = size;
    }

    return new RubyArray(this.array_.subList(size - n, size));
  }
Ejemplo n.º 25
0
  @RubyLevelMethod(name = "to_s")
  public RubyString to_s(RubyValue v) {
    int radix = v.toInt();
    if (radix < 2 || radix > 36) {
      throw new RubyException(RubyRuntime.ArgumentErrorClass, "illegal radix " + radix);
    }

    return ObjectFactory.createString(this.toString(radix));
  }
Ejemplo n.º 26
0
  // @RubyLevelMethod(name="[]")
  public RubyValue array_access(RubyArray args) {
    String string = toString();
    if (args.size() == 1) {
      RubyValue arg = args.get(0);
      if (arg instanceof RubyString) {
        String str = ((RubyString) arg).toString();
        if (string.indexOf(str) >= 0) {
          return ObjectFactory.createString(str);
        } else {
          return RubyConstant.QNIL;
        }
      } else if (arg instanceof RubyRange) {
        RubyRange range = (RubyRange) arg;
        int start = range.getLeft().toInt();
        int end = range.getRight().toInt();
        return substring(string, start, end, range.isExcludeEnd());
      } else if (arg instanceof RubyRegexp) {
        RubyRegexp regexp = (RubyRegexp) arg;
        RubyMatchData match = regexp.match(string);
        if (match != null) {
          return ObjectFactory.createString(match.toString());
        } else {
          return RubyConstant.QNIL;
        }
      } else {
        int index = arg.toInt();
        if (index < 0) {
          index = string.length() + index;
        }

        if (index < 0 || index >= string.length()) {
          return RubyConstant.QNIL;
        } else {
          return ObjectFactory.createFixnum(string.charAt(index));
        }
      }
    } else {
      int start = args.get(0).toInt();
      int length = args.get(1).toInt() - 1;

      return substring(string, start, start + length, false);
    }
  }
Ejemplo n.º 27
0
  @RubyLevelMethod(name = "quo")
  public RubyFloat quo(RubyValue v) {
    if (v instanceof RubyFixnum) {
      return ObjectFactory.createFloat(this.value_ / ((RubyFixnum) v).value_);
    }

    // FIXME: should be coerced.
    throw new RubyException(
        RubyRuntime.TypeErrorClass, v.getRubyClass().getName() + " can't be coersed into Fixnum");
  }
Ejemplo n.º 28
0
  @RubyLevelMethod(name = ">")
  public RubyValue opGt(RubyValue v) {
    if (v instanceof RubyFixnum) {
      return ObjectFactory.createBoolean(this.value_ > ((RubyFixnum) v).value_);
    } else if (v instanceof RubyFloat) {
      return ObjectFactory.createBoolean(this.value_ > v.toFloat());
    }

    return coerceRelop(RubyID.gtID, v);
  }
Ejemplo n.º 29
0
  // @RubyLevelMethod(name="==")
  public RubyValue opEqual(RubyValue v) {
    if (this == v) {
      return RubyConstant.QTRUE;
    }

    if (v instanceof RubyString) {
      RubyString str = ((RubyString) v);
      if ((this.sb_.length() == str.sb_.length() && this.cmp(str) == 0)) {
        return RubyConstant.QTRUE;
      } else {
        return RubyConstant.QFALSE;
      }
    }

    if (v.respondTo(RubyID.toStrID)) {
      return ObjectFactory.createBoolean(v.equals(this));
    } else {
      return RubyConstant.QFALSE;
    }
  }
Ejemplo n.º 30
0
 // @RubyLevelMethod(name="*")
 public RubyValue operator_star(RubyValue arg) {
   String string = toString();
   int count = arg.toInt();
   if (count < 0) {
     throw new RubyException(RubyRuntime.ArgumentErrorClass, "negative argument");
   }
   StringBuffer result = new StringBuffer();
   for (int i = 0; i < count; ++i) {
     result.append(string);
   }
   return ObjectFactory.createString(result);
 }