expr.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 690 行 · 第 1/2 页
JAVA
690 行
else if (obj instanceof NodeIterator) return (NodeIterator) obj; else if (obj instanceof ArrayList) return new NodeArrayListIterator(env, (ArrayList) obj); else { return new SingleNodeIterator(env, null); } } /** * Returns the object value of the node. * * @param node the node to evaluate and use as a context */ public Object evalObject(Node node) throws XPathException { Env env = XPath.createEnv(); env.setCurrentNode(node); env.setContextNode(node); Object result = evalObject(node, env); XPath.freeEnv(env); return result; } /** * Returns the object value of the node. * * @param node the node to evaluate and use as a context * @param env variable environment. */ public abstract Object evalObject(Node node, ExprEnvironment env) throws XPathException; /** * Evaluates to a variable. * * @param node the node to evaluate and use as a context. * @param env the variable environment. * * @return a variable containing the value. */ public Var evalVar(Node node, ExprEnvironment env) throws XPathException { Object obj = evalObject(node, env); return new ObjectVar(obj); } /** * Adds a variable with the expression's value. */ public void addVar(Env newEnv, String name, Node node, Env env) throws XPathException { Var var = evalVar(node, env); newEnv.addVar(name, var); } /** * Sets a variable with the expression's value. */ public void setVar(String name, Node node, Env env) throws XPathException { env.setVar(name, evalVar(node, env)); } /** * Adds a param with the expression's value. */ public void addParam(Env newEnv, String name, Node node, Env env) throws XPathException { Var var = env.getVar(name); if (var == null) newEnv.addVar(name, evalVar(node, env)); else newEnv.addVar(name, var); } /** * Convert a Java object to a boolean using the XPath rules. */ public static boolean toBoolean(Object value) throws XPathException { if (value instanceof Node) value = XmlUtil.textValue((Node) value); else if (value instanceof NodeList) { NodeList list = (NodeList) value; return list.item(0) != null; } else if (value instanceof ArrayList) { ArrayList list = (ArrayList) value; return list.size() > 0; } else if (value instanceof Iterator) { return ((Iterator) value).hasNext(); } if (value == null) return false; else if (value instanceof Double) { Double d = (Double) value; return d.doubleValue() != 0; } else if (value instanceof Boolean) { Boolean b = (Boolean) value; return b.booleanValue(); } else if (value instanceof String) { String string = (String) value; return string != null && string.length() > 0; } else return true; } /** * Convert a Java object to a double using the XPath rules. */ public static double toDouble(Object value) throws XPathException { if (value instanceof Node) { String string = XmlUtil.textValue((Node) value); if (string == null) return 0; else return stringToNumber(string); } else if (value instanceof NodeList) { NodeList list = (NodeList) value; value = list.item(0); } else if (value instanceof ArrayList) { ArrayList list = (ArrayList) value; if (list.size() > 0) value = list.get(0); else value = null; } else if (value instanceof NodeIterator) { value = ((NodeIterator) value).nextNode(); } if (value instanceof Node) value = XmlUtil.textValue((Node) value); if (value == null) return 0; if (value instanceof Number) { Number d = (Number) value; return d.doubleValue(); } else if (value instanceof Boolean) { Boolean b = (Boolean) value; return b.booleanValue() ? 1 : 0; } else if (value instanceof String) { return stringToNumber((String) value); } else return 0; } /** * Convert a Java object to a string using the XPath rules. */ public static String toString(Object value) throws XPathException { if (value instanceof Node) { String s = XmlUtil.textValue((Node) value); if (s == null) return ""; else return s; } else if (value instanceof NodeList) { NodeList list = (NodeList) value; value = list.item(0); } else if (value instanceof ArrayList) { ArrayList list = (ArrayList) value; if (list.size() > 0) value = list.get(0); else value = null; } else if (value instanceof Iterator) { value = ((Iterator) value).next(); } if (value instanceof Node) value = XmlUtil.textValue((Node) value); else if (value instanceof Double) { double d = ((Double) value).doubleValue(); if ((int) d == d) return String.valueOf((int) d); else return String.valueOf(d); } if (value == null) return ""; else return value.toString(); } /** * Convert a Java object to a node using the XPath rules. */ public static Node toNode(Object value) throws XPathException { if (value instanceof Node) return (Node) value; else if (value instanceof NodeList) { NodeList list = (NodeList) value; value = list.item(0); } else if (value instanceof ArrayList) { ArrayList list = (ArrayList) value; if (list.size() > 0) value = list.get(0); else value = null; } else if (value instanceof NodeIterator) { value = ((NodeIterator) value).nextNode(); } if (value instanceof Node) return (Node) value; else return null; } /** * Convert a string to a double following XPath. * * @param string string to be treated as a double. * @return the double value. */ static protected double stringToNumber(String string) throws XPathException { int i = 0; int length = string.length(); boolean isNumber = false; for (; i < length && XmlChar.isWhitespace(string.charAt(i)); i++) { } if (i >= length) return 0; int ch = string.charAt(i);; int sign = 1; if (ch == '-') { sign = -1; for (i++; i < length && XmlChar.isWhitespace(string.charAt(i)); i++) { } } double value = 0; double exp = 1; for (; i < length && (ch = string.charAt(i)) >= '0' && ch <= '9'; i++) { value = 10 * value + ch - '0'; isNumber = true; } if (ch == '.') { for (i++; i < length && (ch = string.charAt(i)) >= '0' && ch <= '9'; i++) { value = 10 * value + ch - '0'; isNumber = true; exp = 10 * exp; } } double pexp = 1.0; if (ch == 'e' || ch == 'E') { int eSign = 1; i++; if (i >= length) return Double.NaN; if (string.charAt(i) == '-') { eSign = -1; i++; } else if (string.charAt(i) == '+') { i++; } int v = 0; for (; i < length && (ch = string.charAt(i)) >= '0' && ch <= '9'; i++) { v = v * 10 + ch - '0'; } pexp = Math.pow(10, eSign * v); } for (; i < length && XmlChar.isWhitespace(string.charAt(i)); i++) { } if (i < length || ! isNumber) return Double.NaN; else return sign * value * pexp / exp; } /** * Convert from an expression to a pattern. */ protected AbstractPattern toNodeList() { return new FromExpr(null, this); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?