variablemodule.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 798 行 · 第 1/2 页

JAVA
798
字号
  {    return v.isNull();  }  /**   * Returns true for numeric   *   * @param env the calling environment   * @param v the value to test   *   * @return true for numeric   */  public static boolean is_numeric(Env env, @ReadOnly Value v)  {    return v.isNumeric();  }  /**   * Returns true for an object   *   * @param env the calling environment   * @param v the value to test   *   * @return true for object   */  public static boolean is_object(Env env, @ReadOnly Value v)  {    return v.isObject();  }  /**   * Returns true for a real   *   * @param v the value to test   *   * @return true for a real   */  public static boolean is_real(@ReadOnly Value v)  {    return is_float(v);  }  /**   * Returns true if the value is a resource   */  public boolean is_resource(@ReadOnly Value value)  {    return value.isResource();  }  /**   * Returns true for a scalar   *   * @param v the value to test   *   * @return true for a scalar   */  public static boolean is_scalar(@ReadOnly Value v)  {    if (v == null)      return false;    Value value = v.toValue();        return (value instanceof DoubleValue            || value instanceof StringValue            || value instanceof LongValue            || value instanceof BooleanValue);  }  /**   * Returns true if the value is a string   */  public boolean is_string(@ReadOnly Value value)  {    return value.isString();  }  // XXX: is_unicode  /**   * Returns the type string for the variable   */  public static boolean isset(@ReadOnly Value v)  {    return v.isset();  }  /**   * Prints a value.  If isReturn is true, then returns what was supposed   * to be printed as a string instead.   *   * @param env the quercus calling environment   * @param v the variable to print   * @param isReturn set to true if returning instead of printing value   * @return the string that was supposed to be printed, or true   */  public static Value print_r(Env env,			      @ReadOnly Value v,			      @Optional boolean isReturn)  {    try {      WriteStream out;            if (isReturn) {        StringWriter writer = new StringWriter();        out = writer.openWrite();                out.setNewlineString("\n");                v.printR(env, out, 0, new IdentityHashMap<Value, String>());                return env.createString(writer.getString());      }      else {        out = env.getOut();                v.printR(env, out, 0, new IdentityHashMap<Value, String>());                return BooleanValue.TRUE;      }    } catch (IOException e) {      throw new QuercusModuleException(e);    }  }  private static void printDepth(WriteStream out, int depth)    throws IOException  {    for (int i = 0; i < depth; i++)      out.print(' ');  }  /**   * Serializes the value to a string.   */  public static String serialize(@ReadOnly Value v)  {    StringBuilder sb = new StringBuilder();    v.serialize(sb, new SerializeMap());    return sb.toString();  }  /**   * Converts the variable to a specified tyep.   */  public static boolean settype(Env env,				@Reference Value var,				String type)  {    Value value = var.toValue();    if ("null".equals(type)) {      var.set(NullValue.NULL);      return true;    }    else if ("boolean".equals(type) || "bool".equals(type)) {      var.set(value.toBoolean() ? BooleanValue.TRUE : BooleanValue.FALSE);      return true;    }    else if ("string".equals(type)) {      var.set(value.toStringValue());      return true;    }    else if ("int".equals(type) || "integer".equals(type)) {      var.set(new LongValue(value.toLong()));      return true;    }    else if ("float".equals(type) || "double".equals(type)) {      var.set(new DoubleValue(value.toDouble()));      return true;    }    else if ("object".equals(type)) {      var.set(value.toObject(env));      return true;    }    else if ("array".equals(type)) {      if (value.isArray())        var.set(value);      else {        ArrayValueImpl array = new ArrayValueImpl();        var.set(array);                if (! value.isNull())          array.append(value);      }      return true;    }    else      return false;  }  /**   * Converts to a string   *   * @param env the quercus calling environment   * @param v the variable to convert   * @return the double value   */  public static Value strval(Env env, @ReadOnly Value v)  {    if (v instanceof StringValue)      return (StringValue) v;    else      return v.toString(env);  }  /**   * Unserializes the value from a string.   */  public static Value unserialize(Env env, StringValue s)  {    Value v = null;    UnserializeKey key = new UnserializeKey(s);        UnserializeCacheEntry entry = _unserializeCache.get(key);    if (entry != null) {      v = entry.getValue(env);      if (v != null)	return v;    }    UnserializeReader is = null;    try {      is = new UnserializeReader(s);            v = is.unserialize(env);    } catch (IOException e) {      log.log(Level.FINE, e.toString(), e);      env.notice(e.toString());      v = BooleanValue.FALSE;    }    if (is != null && ! is.useReference()) {      entry = new UnserializeCacheEntry(v);            _unserializeCache.put(key, entry);      return entry.getValue(env);    }    return v;  }  // XXX: unset  /**   * Prints a debug version of the variable   *   * @param env the quercus calling environment   * @param v the variable to print   * @return the escaped stringPhp   */  public static Value var_dump(Env env, @ReadOnly Value v, Value []args)  {    try {      if (v == null)	env.getOut().print("NULL#java");      else {	v.varDump(env, env.getOut(), 0,  new IdentityHashMap<Value,String>());          	env.getOut().println();      }            if (args != null) {	for (Value value : args) {	  if (value == null)	    env.getOut().print("NULL#java");	  else {	    value.varDump(env, env.getOut(), 0,			  new IdentityHashMap<Value,String>());	    	    env.getOut().println();	  }	}      }      return NullValue.NULL;    } catch (IOException e) {      throw new QuercusModuleException(e);    }  }  /**   * Serializes the value to a string.   */  public static Value var_export(Env env,				 @ReadOnly Value v,				 @Optional boolean isReturn)  {    StringBuilder sb = new StringBuilder();    v.varExport(sb);    if (isReturn)      return env.createString(sb.toString());    else {      env.print(sb);      return NullValue.NULL;    }  }  private static void debug_impl(Env env, Value v, int depth)    throws IOException  {    WriteStream out = env.getOut();    if (v instanceof Var)      out.print("&");    v = v.toValue();    if (v instanceof ArrayValue) {      ArrayValue array = (ArrayValue) v;      out.println("Array");      printDepth(out, 2 * depth);      out.println("(");      for (Map.Entry<Value,Value> entry : array.entrySet()) {        printDepth(out, 2 * depth);        out.print("    [");        out.print(entry.getKey());        out.print("] => ");        debug_impl(env, entry.getValue(), depth + 1); // XXX: recursion      }      printDepth(out, 2 * depth);      out.println(")");    }    else if (v instanceof BooleanValue) {      if (v.toBoolean())	out.print("bool(true)");      else	out.print("bool(false)");    }    else if (v instanceof LongValue) {      out.print("int(" + v.toLong() + ")");    }    else if (v instanceof DoubleValue) {      out.print("float(" + v.toDouble() + ")");    }    else if (v instanceof StringValue) {      out.print("string(" + v.toString() + ")");    }    else if (v instanceof NullValue) {      out.print("NULL");    }    else {      v.print(env);    }  }  static class UnserializeKey {    private final SoftReference<StringValue> _stringRef;    private int _hash;    UnserializeKey(StringValue string)    {      _hash = string.hashCode();      _stringRef = new SoftReference<StringValue>(string);    }    public int hashCode()    {      return _hash;    }    public boolean equals(Object o)    {      if (this == o)	return true;      else if (! (o instanceof UnserializeKey))	return false;      UnserializeKey key = (UnserializeKey) o;      StringValue a = _stringRef.get();      StringValue b = key._stringRef.get();      if (a == null || b == null)	return false;      return a.equals(b);    }  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?