/**
   * Evaluates the expression.
   *
   * @param env the calling environment.
   * @return the expression value.
   */
  @Override
  public Value eval(Env env) {
    Value qThis = env.getThis();

    QuercusClass cls = qThis.getQuercusClass();

    if (cls == null) {
      env.error(L.l("no calling class found"), getLocation());

      return NullValue.NULL;
    }

    StringValue methodName = _methodName.evalStringValue(env);
    int hash = methodName.hashCodeCaseInsensitive();

    Value[] values = evalArgs(env, _args);

    env.pushCall(this, cls, values);

    try {
      env.checkTimeout();

      return cls.callMethod(env, qThis, methodName, hash, values);
    } finally {
      env.popCall();
    }
  }
  /**
   * Converts node tree to a valid xml string.
   *
   * @return xml string
   */
  public final Value asXML(Env env, @Optional Value filename) {
    Value value = toXML(env);

    if (!value.isString()) {
      return value;
    }

    StringValue str = value.toStringValue(env);

    if (filename.isDefault()) {
      return str;
    } else {
      Path path = env.lookupPwd(filename);

      OutputStream os = null;

      try {
        os = path.openWrite();

        str.writeTo(os);

        return BooleanValue.TRUE;
      } catch (IOException e) {
        env.warning(e);

        return BooleanValue.FALSE;
      } finally {
        if (os != null) {
          IoUtil.close(os);
        }
      }
    }
  }
  public ClassField(
      StringValue name,
      String declaringClassName,
      Expr initValue,
      FieldVisibility visibility,
      String comment,
      boolean isTraitField) {
    _name = name;
    _declaringClassName = declaringClassName;

    _initValue = initValue;

    _comment = comment;

    _isTraitField = isTraitField;

    if (visibility.isProtected()) {
      StringValue sb = name.createStringBuilder();

      _canonicalName = createProtectedCanonicalName(sb, name);
    } else if (visibility.isPrivate()) {
      StringValue sb = name.createStringBuilder();

      _canonicalName = createPrivateCanonicalName(sb, name, declaringClassName);
    } else {
      _canonicalName = name;
    }
  }
Beispiel #4
0
  @JsonEncode
  public void jsonEncode(Env env, StringValue sb) {
    sb.append('{');

    jsonEncodeImpl(env, sb, true);

    sb.append('}');
  }
  public static StringValue createProtectedCanonicalName(StringValue sb, StringValue name) {
    sb.append('\u0000');
    sb.append('*');
    sb.append('\u0000');
    sb.append(name);

    return sb;
  }
Beispiel #6
0
  /** Reads in a string until NULL or EOF encountered. */
  private StringValue readOriginalString() throws IOException {
    StringValue sb = _env.createUnicodeBuilder();

    for (int ch = _in.read(); ch > 0; ch = _in.read()) {
      sb.append((char) ch);
    }

    return sb;
  }
  /** Reads into a binary builder. */
  @Override
  public StringValue read(int length) throws IOException {
    if (_is == null) return null;

    StringValue bb = _env.createBinaryBuilder();

    if (bb.appendRead(_is, length) > 0) return bb;
    else return null;
  }
  public static StringValue getOrdinaryName(StringValue canonicalName) {
    int p = canonicalName.lastIndexOf('\u0000');

    if (p >= 0) {
      return canonicalName.substring(p + 1);
    } else {
      return canonicalName;
    }
  }
  public static StringValue createPrivateCanonicalName(
      StringValue sb, StringValue name, String declaringClass) {
    sb.append('\u0000');
    sb.append(declaringClass);
    sb.append('\u0000');

    sb.append(name);

    return sb;
  }
Beispiel #10
0
  public Value receive(Env env, @Optional("1") long timeout) throws JMSException {
    Message message = _consumer.receive(timeout);

    if (message == null) return BooleanValue.FALSE;

    if (message instanceof ObjectMessage) {
      Object object = ((ObjectMessage) message).getObject();

      return env.wrapJava(object);
    } else if (message instanceof TextMessage) {
      return env.createString(((TextMessage) message).getText());
    } else if (message instanceof StreamMessage) {
      Object object = ((StreamMessage) message).readObject();

      return env.wrapJava(object);
    } else if (message instanceof BytesMessage) {
      BytesMessage bytesMessage = (BytesMessage) message;
      int length = (int) bytesMessage.getBodyLength();

      StringValue bb = env.createBinaryBuilder(length);

      TempBuffer tempBuffer = TempBuffer.allocate();
      int sublen;

      while (true) {
        sublen = bytesMessage.readBytes(tempBuffer.getBuffer());

        if (sublen > 0) bb.append(tempBuffer.getBuffer(), 0, sublen);
        else break;
      }

      TempBuffer.free(tempBuffer);

      return bb;
    } else if (message instanceof MapMessage) {
      MapMessage mapMessage = (MapMessage) message;

      Enumeration mapNames = mapMessage.getMapNames();

      ArrayValue array = new ArrayValueImpl();

      while (mapNames.hasMoreElements()) {
        String name = mapNames.nextElement().toString();

        Object object = mapMessage.getObject(name);

        array.put(env.createString(name), env.wrapJava(object));
      }

      return array;
    } else {
      return BooleanValue.FALSE;
    }
  }
Beispiel #11
0
  /**
   * Converts node tree to a valid xml string.
   *
   * @return xml string
   */
  @ReturnNullAsFalse
  public StringValue asXML(Env env) {
    if (_parent == null) {
      StringValue sb = env.createBinaryBuilder();

      sb.append("<?xml version=\"1.0\"?>\n");
      toXMLImpl(sb);
      sb.append("\n");

      return sb;
    } else return toXML(env);
  }
  public BinaryStream fopen(Env env, StringValue path, StringValue mode, LongValue options) {
    boolean useIncludePath = (options.toLong() & StreamModule.STREAM_USE_PATH) != 0;

    Value pathComponent = UrlModule.parse_url(env, path, UrlModule.PHP_URL_PATH);

    if (!pathComponent.isset()) {
      log.info(L.l("no path component found in '{0}'", path.toString()));
      return null;
    }

    return ZlibModule.gzopen(
        env, pathComponent.toStringValue(env), mode.toString(), useIncludePath);
  }
  protected Value toXML(Env env) {
    StringValue sb = env.createBinaryBuilder();

    if (_parent == null) {
      sb.append("<?xml version=\"1.0\"?>\n");
    }

    toXMLImpl(sb);

    if (_parent == null) {
      sb.append("\n");
    }

    return sb;
  }
Beispiel #14
0
  /** var_dump() implementation */
  public void varDumpImpl(
      Env env, Value obj, WriteStream out, int depth, IdentityHashMap<Value, String> valueSet)
      throws IOException {
    String name = "SimpleXMLElement";

    if (obj != null) name = obj.getClassName();

    // php/1x33
    if (_text != null && _children == null && _attributes == null) {
      if (depth > 0) {
        _text.varDump(env, out, depth, valueSet);
        return;
      }

      out.println("object(" + name + ") (1) {");
      printDepth(out, 2 * (depth + 1));
      out.println("[0]=>");

      printDepth(out, 2 * (depth + 1));
      _text.varDump(env, out, depth, valueSet);
      out.println();

      printDepth(out, 2 * depth);
      out.print("}");

      return;
    }

    Set<Map.Entry<Value, Value>> entrySet = entrySet();
    out.println("object(" + name + ") (" + entrySet.size() + ") {");

    for (Map.Entry<Value, Value> entry : entrySet) {
      printDepth(out, 2 * (depth + 1));
      out.print("[");

      if (entry.getKey().isString()) out.print("\"" + entry.getKey() + "\"");
      else out.print(entry.getKey());

      out.println("]=>");

      printDepth(out, 2 * (depth + 1));
      entry.getValue().varDump(env, out, depth + 1, valueSet);
      out.println();
    }

    printDepth(out, 2 * depth);
    out.print('}');
  }
Beispiel #15
0
  protected void jsonEncodeImpl(Env env, StringValue sb, boolean isTop) {
    if (!isTop) {
      sb.append('"');
      sb.append(getName());
      sb.append('"');

      sb.append(':');
    }

    if (_attributes == null
        && _children != null
        && _children.size() == 1
        && !_children.get(0).isElement()) {
      _children.get(0).jsonEncodeImpl(env, sb, false);
    } else {
      int length = 0;

      boolean hasChildren = _attributes != null && _children != null;

      if (hasChildren) sb.append('{');

      if (_attributes != null) {
        length++;

        sb.append("\"@attributes\"");
        sb.append(':');
        sb.append('{');

        for (SimpleXMLElement attribute : _attributes) {
          attribute.jsonEncodeImpl(env, sb, false);
        }

        sb.append('}');
      }

      if (_children != null) {
        for (SimpleXMLElement child : _children) {
          if (!child.isElement()) continue;

          if (length++ > 0) sb.append(',');

          child.jsonEncodeImpl(env, sb, false);
        }
      }

      if (hasChildren) sb.append('}');
    }
  }
Beispiel #16
0
  /**
   * Retrieves the plural translation for msgid1.
   * 
   * @param env
   * @param domain
   * @param category
   * @param msgid1
   * @param msgid2
   *
   * @return translation found, else msgid1 if n == 1, else msgid2
   */
  private StringValue translate(Env env,
                                GettextDomain domain,
                                CharSequence category,
                                StringValue msgid1,
                                StringValue msgid2,
                                int quantity,
                                Value []args)
  {
    Locale locale = env.getLocaleInfo().getMessages().getLocale();

    GettextResource resource = getResource(env,
                                           domain.getPath(),
                                           locale,
                                           category,
                                           domain.getName());

    StringValue unicodeTranslation
      = resource.getTranslation(msgid1, quantity);

    if (unicodeTranslation == null)
      unicodeTranslation = errorReturn(msgid1, msgid2, quantity);

    StringValue translation = msgid1.create(env, unicodeTranslation, domain.getCharset());
    
    return format(env, translation, args);
  }
Beispiel #17
0
  /** Creates a function call expression */
  @Override
  public Expr createCall(QuercusParser parser, Location location, ArrayList<Expr> args)
      throws IOException {
    Expr var = parser.createVar(_varName.toString());
    ExprFactory factory = parser.getExprFactory();

    return factory.createClassMethodCall(location, _className, var, args);
  }
Beispiel #18
0
  /** Reads in translated plurals forms that are separated by NULL. */
  private ArrayList<StringValue> readPluralForms(int length) throws IOException {
    ArrayList<StringValue> list = new ArrayList<StringValue>();
    StringValue sb = new UnicodeBuilderValue();

    for (; length > 0; length--) {
      int ch = _in.readChar();

      if (ch > 0) sb.append((char) ch);
      else if (ch == 0) {
        list.add(sb);
        sb = new UnicodeBuilderValue();
      } else break;
    }

    list.add(sb);
    return list;
  }
Beispiel #19
0
  /** Returns the id for a constant */
  public int addLowerConstantId(StringValue name) {
    int id = getConstantId(name);

    int lowerId = getConstantId(name.toLowerCase());

    _constantLowerMap[id] = lowerId;

    return id;
  }
  public static StringValue getDeclaringClass(StringValue sb, StringValue canonicalName) {
    if (canonicalName.charAt(0) != '\u0000') {
      return sb;
    }

    int len = canonicalName.length();
    for (int i = 1; i < len; i++) {
      char ch = canonicalName.charAt(i);

      if (ch == '\u0000') {
        break;
      } else {
        sb.append(ch);
      }
    }

    return sb;
  }
Beispiel #21
0
    public boolean equals(Object o) {
      if (!(o instanceof IncludeKey)) return false;

      IncludeKey key = (IncludeKey) o;

      return (_include.equals(key._include)
          && _includePath.equals(key._includePath)
          && _pwd.equals(key._pwd)
          && _scriptPwd.equals(key._scriptPwd));
    }
Beispiel #22
0
    public int hashCode() {
      int hash = 37;

      hash = 65537 * hash + _include.hashCode();
      hash = 65537 * hash + _includePath.hashCode();
      hash = 65537 * hash + _pwd.hashCode();
      hash = 65537 * hash + _scriptPwd.hashCode();

      return hash;
    }
Beispiel #23
0
  /**
   * Evaluates the expression.
   *
   * @param env the calling environment.
   * @param ctx
   * @return the expression value.
   */
  @Override
  @Nonnull
  protected V<? extends ValueOrVar> _eval(Env env, FeatureExpr ctx) {
    QuercusClass cl = env.findClass(_className);

    if (cl == null) {
      env.error(L.l("no matching class {0}", _className), getLocation());
    }

    // qa/0954 - static calls pass the current $this
    Value qThis = env.getThis();

    StringValue methodName = _nameExpr.evalStringValue(env, ctx).getOne();

    V<? extends ValueOrVar>[] args = evalArgs(env, _args, ctx);
    int hash = methodName.hashCodeCaseInsensitive();

    return cl.callStaticMethod(env, ctx, qThis, methodName, hash, args).map((a) -> a.toValue());
  }
Beispiel #24
0
  private static StringValue formatImpl(Env env,
                                        StringValue msg,
                                        Value []args,
                                        StringValue sb)
  {
    int i = 0;
    int length = msg.length();

    while (i < length) {
      char ch = msg.charAt(i);

      if (ch != '[' || i + 4 > length) {
        sb.append(ch);
        i++;
      }
      else if (msg.charAt(i + 1) != '_') {
        sb.append(ch);
        i++;
      }
      else if (msg.charAt(i + 3) != ']') {
        sb.append(ch);
        i++;
      }
      else {
        ch = msg.charAt(i + 2);
        int argIndex = ch - '0';

        if (0 <= argIndex && argIndex < args.length) {
          sb.append(args[argIndex]);
          i += 4;
        }
        else {
          sb.append('[');
          i++;
        }
      }
    }
    
    return sb;
  }
Beispiel #25
0
 private static StringValue format(Env env,
                             StringValue msg,
                             Value []args)
 {
   if (args.length == 0)
     return msg;
   
   StringValue sb;
   
   if (msg.isUnicode())
     sb = env.createUnicodeBuilder();
   else
     sb = env.createBinaryBuilder();
   
   return formatImpl(env, msg, args, sb);
 }
  @Override
  protected void toXMLImpl(StringValue sb) {
    sb.append(" ");

    if (_prefix != null && !"".equals(_prefix)) {
      sb.append(_prefix);
      sb.append(":");
    }

    sb.append(_name);
    sb.append("=\"");
    if (_text != null) sb.append(_text);
    sb.append("\"");
  }
 /** Appends to a string builder. */
 @Override
 public StringValue appendTo(StringValue builder) throws IOException {
   if (_is != null) return builder.append(_is);
   else return builder;
 }
Beispiel #28
0
  protected void toXMLImpl(StringValue sb) {
    sb.append("<");

    boolean hasPrefix = false;

    if (_prefix != null && !"".equals(_prefix) && getNamespace(_prefix) != null) hasPrefix = true;

    if (hasPrefix) {
      sb.append(_prefix);
      sb.append(":");
    }

    sb.append(_name);

    /*
    if (_namespaceMap != null) {
      for (Map.Entry<String,String> entry : _namespaceMap.entrySet()) {
        if (! "".equals(entry.getKey())) {
          sb.append(" xmlns:");
          sb.append(entry.getKey());
        }
        else
          sb.append(" xmlns");
        sb.append("=\"");
        sb.append(entry.getValue());
        sb.append("\"");
      }
    }
    */

    // add attributes, if any
    if (_attributes != null) {
      int size = _attributes.size();

      for (int i = 0; i < size; i++) {
        SimpleXMLElement attr = _attributes.get(i);

        attr.toXMLImpl(sb);
      }
    }

    // add children, if any
    if (_children != null) {
      sb.append(">");

      int size = _children.size();

      for (int i = 0; i < size; i++) {
        SimpleXMLElement child = _children.get(i);

        child.toXMLImpl(sb);
      }
    } else if (_text == null || _text.length() == 0) {
      sb.append("/>");
      return;
    } else {
      sb.append(">");

      sb.append(_text);
    }

    // add closing tag
    sb.append("</");

    if (hasPrefix) {
      sb.append(_prefix);
      sb.append(":");
    }

    sb.append(_name);

    sb.append(">");
  }
Beispiel #29
0
  protected void addText(StringValue text) {
    if (_text == null) _text = text.createStringBuilder();

    _text = _text.append(text);
  }
Beispiel #30
0
 /** Returns true if the variable is a superglobal. */
 public static boolean isSuperGlobal(StringValue name) {
   return _superGlobals.contains(name.toString());
 }