⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 expr.java

📁 linux下建立JAVA虚拟机的源码KAFFE
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* Expr.java --    Copyright (C) 2004,2006 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. */package gnu.xml.xpath;import java.io.IOException;import java.text.DecimalFormat;import java.text.DecimalFormatSymbols;import java.util.ArrayList;import java.util.Collection;import java.util.Collections;import java.util.Comparator;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Locale;import java.util.Set;import java.util.StringTokenizer;import javax.xml.namespace.QName;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import javax.xml.xpath.XPathConstants;import javax.xml.xpath.XPathExpression;import javax.xml.xpath.XPathExpressionException;import org.w3c.dom.Document;import org.w3c.dom.Node;import org.xml.sax.InputSource;import org.xml.sax.SAXException;/** * An XPath expression. * This can be evaluated in the context of a node to produce a result. * * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> */public abstract class Expr  implements XPathExpression{  protected static final Comparator documentOrderComparator =    new DocumentOrderComparator();  protected static final DecimalFormat decimalFormat =    new DecimalFormat("####################################################" +                      ".####################################################",                      new DecimalFormatSymbols(Locale.US));  public Object evaluate(Object item, QName returnType)    throws XPathExpressionException  {    Object ret = null;    Node context = null;    if (item instanceof Node)      {        context = (Node) item;        ret = evaluate(context, 1, 1);        if (XPathConstants.STRING == returnType &&            !(ret instanceof String))          {            ret = _string(context, ret);          }        else if (XPathConstants.NUMBER == returnType &&                 !(ret instanceof Double))          {            ret = new Double(_number(context, ret));          }        else if (XPathConstants.BOOLEAN == returnType &&                 !(ret instanceof Boolean))          {            ret = _boolean(context, ret) ? Boolean.TRUE : Boolean.FALSE;          }        else if (XPathConstants.NODE == returnType)          {            if (ret instanceof Collection)              {                Collection ns = (Collection) ret;                switch (ns.size())                  {                  case 0:                    ret = null;                    break;                  case 1:                    ret = (Node) ns.iterator().next();                    break;                  default:                    throw new XPathExpressionException("multiple nodes in node-set");                  }              }            else if (ret != null)              {                throw new XPathExpressionException("return value is not a node-set");              }          }        else if (XPathConstants.NODESET == returnType)          {            if (ret != null && !(ret instanceof Collection))              {                throw new XPathExpressionException("return value is not a node-set");              }          }      }    return ret;  }  public String evaluate(Object item)    throws XPathExpressionException  {    return (String) evaluate(item, XPathConstants.STRING);   }  public Object evaluate(InputSource source, QName returnType)    throws XPathExpressionException  {    try      {        DocumentBuilderFactory factory =          new gnu.xml.dom.JAXPFactory();        DocumentBuilder builder = factory.newDocumentBuilder();        Document doc = builder.parse(source);        return evaluate(doc, returnType);      }    catch (ParserConfigurationException e)      {        throw new XPathExpressionException(e);       }    catch (SAXException e)      {        throw new XPathExpressionException(e);       }    catch (IOException e)      {        throw new XPathExpressionException(e);       }  }  public String evaluate(InputSource source)    throws XPathExpressionException  {    return (String) evaluate(source, XPathConstants.STRING);  }  public abstract Object evaluate(Node context, int pos, int len);  public abstract Expr clone(Object context);  public abstract boolean references(QName var);    /* -- 4.1 Node Set Functions -- */  /**   * The id function selects elements by their unique ID.   * When the argument to id is of type node-set, then the result is   * the union of the result of applying id to the string-value of each of   * the nodes in the argument node-set. When the argument to id is of any   * other type, the argument is converted to a string as if by a call to   * the string function; the string is split into a whitespace-separated   * list of tokens (whitespace is any sequence of characters matching the   * production S); the result is a node-set containing the elements in the   * same document as the context node that have a unique ID equal to any of   * the tokens in the list.   */  public static Collection _id(Node context, Object object)  {    Set ret = new HashSet();    if (object instanceof Collection)      {        Collection nodeSet = (Collection) object;        for (Iterator i = nodeSet.iterator(); i.hasNext(); )          {            String string = stringValue((Node) i.next());            ret.addAll(_id (context, string));          }      }    else      {        Document doc = (context instanceof Document) ? (Document) context :          context.getOwnerDocument();        String string = _string(context, object);        StringTokenizer st = new StringTokenizer(string, " \t\r\n");        while (st.hasMoreTokens())          {            Node element = doc.getElementById(st.nextToken());            if (element != null)              {                ret.add(element);              }          }      }    return ret;  }  /**   * The local-name function returns the local part of the expanded-name of   * the node in the argument node-set that is first in document order. If   * the argument node-set is empty or the first node has no expanded-name,   * an empty string is returned. If the argument is omitted, it defaults to   * a node-set with the context node as its only member.   */  public static String _local_name(Node context, Collection nodeSet)  {    if (nodeSet == null || nodeSet.isEmpty())      return "";    Node node = firstNode(nodeSet);    String ret = node.getLocalName();    return (ret == null) ? "" : ret;  }  /**   * The namespace-uri function returns the namespace URI of the   * expanded-name of the node in the argument node-set that is first in   * document order. If the argument node-set is empty, the first node has   * no expanded-name, or the namespace URI of the expanded-name is null, an   * empty string is returned. If the argument is omitted, it defaults to a

⌨️ 快捷键说明

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