expr.java

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

JAVA
690
字号
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT.  See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the *   Free SoftwareFoundation, Inc. *   59 Temple Place, Suite 330 *   Boston, MA 02111-1307  USA * * @author Scott Ferguson */package com.caucho.xpath;import com.caucho.util.CharBuffer;import com.caucho.xml.XmlChar;import com.caucho.xml.XmlUtil;import com.caucho.xpath.expr.ObjectVar;import com.caucho.xpath.expr.Var;import com.caucho.xpath.pattern.AbstractPattern;import com.caucho.xpath.pattern.FromExpr;import com.caucho.xpath.pattern.NodeArrayListIterator;import com.caucho.xpath.pattern.NodeIterator;import com.caucho.xpath.pattern.NodeListIterator;import com.caucho.xpath.pattern.SingleNodeIterator;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import java.util.ArrayList;import java.util.Iterator;/** * Compute values from nodes.  Because the expressions themselves are * untyped, the class provides methods for creating the type of the * desired result. */abstract public class Expr {  protected static final int CONST = 0;  protected static final int NODE_SET = CONST + 1;  protected static final int ID = NODE_SET + 1;  protected static final int OR = ID + 1;  protected static final int AND = OR + 1;  protected static final int EQ = AND + 1;  protected static final int NEQ = EQ + 1;  protected static final int LT = NEQ + 1;  protected static final int LE = LT + 1;  protected static final int GT = LE + 1;  protected static final int GE = GT + 1;    protected static final int BOOLEAN_EQ = GE + 1;  protected static final int BOOLEAN_NEQ = BOOLEAN_EQ + 1;    protected static final int NUMBER_EQ = BOOLEAN_NEQ + 1;  protected static final int NUMBER_NEQ = NUMBER_EQ + 1;  protected static final int NUMBER_LT = NUMBER_NEQ + 1;  protected static final int NUMBER_LE = NUMBER_LT + 1;  protected static final int NUMBER_GT = NUMBER_LE + 1;  protected static final int NUMBER_GE = NUMBER_GT + 1;    protected static final int STRING_EQ = NUMBER_GE + 1;  protected static final int STRING_NEQ = STRING_EQ + 1;  protected static final int NEG = STRING_NEQ + 1;  protected static final int ADD = NEG + 1;  protected static final int SUB = ADD + 1;  protected static final int MUL = SUB + 1;  protected static final int DIV = MUL + 1;  protected static final int QUO = DIV + 1;  protected static final int MOD = QUO + 1;  protected static final int TRUE = MOD + 1;  protected static final int FALSE = TRUE + 1;  protected static final int NOT = FALSE + 1;  protected static final int BOOLEAN = NOT + 1;  protected static final int LANG = BOOLEAN + 1;  protected static final int NUMBER = LANG + 1;  protected static final int SUM = NUMBER + 1;  protected static final int FLOOR = SUM + 1;  protected static final int CEILING = FLOOR + 1;  protected static final int ROUND = CEILING + 1;  public static final int POSITION = ROUND + 1;  protected static final int COUNT = POSITION + 1;  protected static final int LAST = COUNT + 1;  protected static final int STRING = LAST + 1;  protected static final int CONCAT = STRING + 1;  protected static final int STARTS_WITH = CONCAT + 1;  protected static final int CONTAINS = STARTS_WITH + 1;  protected static final int SUBSTRING = CONTAINS + 1;  protected static final int SUBSTRING_BEFORE = SUBSTRING + 1;  protected static final int SUBSTRING_AFTER = SUBSTRING_BEFORE + 1;  protected static final int STRING_LENGTH = SUBSTRING_AFTER + 1;  protected static final int NORMALIZE = STRING_LENGTH + 1;  protected static final int TRANSLATE = NORMALIZE + 1;  protected static final int FORMAT_NUMBER = TRANSLATE + 1;  protected static final int LOCAL_PART = FORMAT_NUMBER + 1;  protected static final int NAMESPACE = LOCAL_PART + 1;  protected static final int QNAME = NAMESPACE + 1;  protected static final int GENERATE_ID = QNAME + 1;  protected static final int FUNCTION_AVAILABLE = GENERATE_ID + 1;  protected static final int SYSTEM_PROPERTY = FUNCTION_AVAILABLE + 1;  protected static final int IF = SYSTEM_PROPERTY + 1;    protected static final int SELF = IF + 1;  protected static final int SELF_NAME = SELF + 1;  protected static final int ATTRIBUTE = SELF_NAME + 1;  protected static final int ELEMENT = ATTRIBUTE + 1;    protected static final int BASE_URI = ELEMENT + 1;  protected static final int LAST_FUN = BASE_URI + 1;  private AbstractPattern listContext;    protected Expr() {}  public void setListContext(AbstractPattern listContext)  {    this.listContext = listContext;  }  public AbstractPattern getListContext()  {    return listContext;  }    /**   * true if the expression prefers to return a number.   */  public boolean isNumber()  {    return false;  }    /**   * Evaluates the expression as a double using the node as a context.   *   * @param node the node to evaluate and use as a context   *   * @return the numeric value.   */  public double evalNumber(Node node)    throws XPathException  {    Env env = XPath.createEnv();    env.setCurrentNode(node);    env.setContextNode(node);    double result = evalNumber(node, env);    XPath.freeEnv(env);    return result;  }  /**   * Evaluates the expression as a number.   *   * @param node the current node.   * @param env variable environment.   *   * @return the numeric value.   */  public abstract double evalNumber(Node node, ExprEnvironment env)    throws XPathException;  /**   * true if the expression prefers to return a boolean.   */  public boolean isBoolean()  {    return false;  }  /**   * Returns the boolean value of the node.   *   * @param node the node to evaluate and use as a context   *   * @return the boolean value   */  public boolean evalBoolean(Node node)    throws XPathException  {    Env env = XPath.createEnv();    env.setCurrentNode(node);    env.setContextNode(node);    boolean result = evalBoolean(node, env);    XPath.freeEnv(env);    return result;  }  /**   * Returns the boolean value of the node.   *   * @param node the node to evaluate and use as a context   * @param env variable environment.   *   * @return the boolean value.   */  public abstract boolean evalBoolean(Node node, ExprEnvironment env)    throws XPathException;  /**   * Returns the expression evaluated as a string.   *   * @param node the node to evaluate and use as a context   *   * @return the string value of the expression.   */  public String evalString(Node node)    throws XPathException  {    Env env = XPath.createEnv();    env.setCurrentNode(node);    env.setContextNode(node);    String result = evalString(node, env);    XPath.freeEnv(env);    return result;  }  /**   * true if the expression prefers to return a string.   */  public boolean isString()  {    return false;  }  /**   * Returns the string value of the node.   *   * @param node the node to evaluate and use as a context   * @param env variable environment.   */  public abstract String evalString(Node node, ExprEnvironment env)    throws XPathException;    /**   * Fills a char buffer with the evaluated string results.   *   * @param cb the buffer containing the results.   * @param node the node to evaluate and use as a context   */  public void evalString(CharBuffer cb, Node node)    throws XPathException  {    Env env = XPath.createEnv();    env.setCurrentNode(node);    env.setContextNode(node);    evalString(cb, node, env);    XPath.freeEnv(env);  }    /**   * Fills a char buffer with the evaluated string results.   *   * @param cb the buffer containing the results.   * @param node the node to evaluate and use as a context   * @param env the variable environment   */  public void evalString(CharBuffer cb, Node node, ExprEnvironment env)    throws XPathException  {    cb.append(evalString(node, env));  }  /**   * true if the expression prefers to return a node set.   */  public boolean isNodeSet()  {    return false;  }  /**   * Returns an iterator of matching nodes   *   * @param node the node to evaluate and use as a context   *   * @return the value as a node iterator.   */  public NodeIterator evalNodeSet(Node node)    throws XPathException  {    Env env = XPath.createEnv();    env.setCurrentNode(node);    env.setContextNode(node);    NodeIterator result = evalNodeSet(node, env);    XPath.freeEnv(env);    return result;  }  /**   * Returns an iterator of matching nodes   *   * @param node the node to evaluate and use as a context   * @param env variable environment.   *   * @return the value as a node iterator.   */  public NodeIterator evalNodeSet(Node node, ExprEnvironment env)    throws XPathException  {    Object obj = evalObject(node, env);    if (obj instanceof Node)      return new SingleNodeIterator(env, (Node) obj);    else if (obj instanceof NodeList)      return new NodeListIterator(env, (NodeList) obj);

⌨️ 快捷键说明

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