extensions.java
来自「java jdk 1.4的源码」· Java 代码 · 共 442 行 · 第 1/2 页
JAVA
442 行
return ExsltSets.distinct(nl); } /** * Returns true if both node-sets contain the same set of nodes. * * @param nl1 NodeList for first node-set * @param nl2 NodeList for second node-set * @return true if nl1 and nl2 contain exactly the same set of nodes. */ public static boolean hasSameNodes(NodeList nl1, NodeList nl2) { NodeSet ns1 = new NodeSet(nl1); NodeSet ns2 = new NodeSet(nl2); if (ns1.getLength() != ns2.getLength()) return false; for (int i = 0; i < ns1.getLength(); i++) { Node n = ns1.elementAt(i); if (!ns2.contains(n)) return false; } return true; } /** * Returns the result of evaluating the argument as a string containing * an XPath expression. Used where the XPath expression is not known until * run-time. The expression is evaluated as if the run-time value of the * argument appeared in place of the evaluate function call at compile time. * * @param myContext an <code>ExpressionContext</code> passed in by the * extension mechanism. This must be an XPathContext. * @param xpathExtr The XPath expression to be evaluated. * @return the XObject resulting from evaluating the XPath * * @throws SAXNotSupportedException * * Note: The usage of this extension function in the xalan namespace * is deprecated. Please use the same function in the EXSLT dynamic extension * (http://exslt.org/dynamic). */ public static XObject evaluate(ExpressionContext myContext, String xpathExpr) throws SAXNotSupportedException { return ExsltDynamic.evaluate(myContext, xpathExpr); } /** * Returns a NodeSet containing one text node for each token in the first argument. * Delimiters are specified in the second argument. * Tokens are determined by a call to <code>StringTokenizer</code>. * If the first argument is an empty string or contains only delimiters, the result * will be an empty NodeSet. * * Contributed to XalanJ1 by <a href="mailto:benoit.cerrina@writeme.com">Benoit Cerrina</a>. * * @param myContext an <code>ExpressionContext</code> passed in by the * extension mechanism. This must be an XPathContext. * @param toTokenize The string to be split into text tokens. * @param delims The delimiters to use. * @return a NodeSet as described above. */ public static NodeList tokenize(String toTokenize, String delims) { try { if (lDoc == null) lDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument(); } catch(ParserConfigurationException pce) { throw new org.apache.xml.utils.WrappedRuntimeException(pce); } StringTokenizer lTokenizer = new StringTokenizer(toTokenize, delims); NodeSet resultSet = new NodeSet(); while (lTokenizer.hasMoreTokens()) { resultSet.addNode(lDoc.createTextNode(lTokenizer.nextToken())); } return resultSet; } /** * Returns a NodeSet containing one text node for each token in the first argument. * Delimiters are whitespace. That is, the delimiters that are used are tab (	), * linefeed (
), return (
), and space ( ). * Tokens are determined by a call to <code>StringTokenizer</code>. * If the first argument is an empty string or contains only delimiters, the result * will be an empty NodeSet. * * Contributed to XalanJ1 by <a href="mailto:benoit.cerrina@writeme.com">Benoit Cerrina</a>. * * @param myContext an <code>ExpressionContext</code> passed in by the * extension mechanism. This must be an XPathContext. * @param toTokenize The string to be split into text tokens. * @return a NodeSet as described above. */ public static NodeList tokenize(String toTokenize) { return tokenize(toTokenize, " \t\n\r"); } /** * Return a Node of basic debugging information from the * EnvironmentCheck utility about the Java environment. * * <p>Simply calls the {@link org.apache.xalan.xslt.EnvironmentCheck} * utility to grab info about the Java environment and CLASSPATH, * etc., and then returns the resulting Node. Stylesheets can * then maniuplate this data or simply xsl:copy-of the Node. Note * that we first attempt to load the more advanced * org.apache.env.Which utility by reflection; only if that fails * to we still use the internal version. Which is available from * <a href="http://xml.apache.org/commons/">http://xml.apache.org/commons/</a>.</p> * * <p>We throw a WrappedRuntimeException in the unlikely case * that reading information from the environment throws us an * exception. (Is this really the best thing to do?)</p> * * @param myContext an <code>ExpressionContext</code> passed in by the * extension mechanism. This must be an XPathContext. * @return a Node as described above. */ public static Node checkEnvironment(ExpressionContext myContext) { Document factoryDocument; try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); factoryDocument = db.newDocument(); } catch(ParserConfigurationException pce) { throw new org.apache.xml.utils.WrappedRuntimeException(pce); } Node resultNode = null; try { // First use reflection to try to load Which, which is a // better version of EnvironmentCheck resultNode = checkEnvironmentUsingWhich(myContext, factoryDocument); if (null != resultNode) return resultNode; // If reflection failed, fallback to our internal EnvironmentCheck EnvironmentCheck envChecker = new EnvironmentCheck(); Hashtable h = envChecker.getEnvironmentHash(); resultNode = factoryDocument.createElement("checkEnvironmentExtension"); envChecker.appendEnvironmentReport(resultNode, factoryDocument, h); envChecker = null; } catch(Exception e) { throw new org.apache.xml.utils.WrappedRuntimeException(e); } return resultNode; } /** * Private worker method to attempt to use org.apache.env.Which. * * @param myContext an <code>ExpressionContext</code> passed in by the * extension mechanism. This must be an XPathContext. * @param factoryDocument providing createElement services, etc. * @return a Node with environment info; null if any error */ private static Node checkEnvironmentUsingWhich(ExpressionContext myContext, Document factoryDocument) { final String WHICH_CLASSNAME = "org.apache.env.Which"; final String WHICH_METHODNAME = "which"; final Class WHICH_METHOD_ARGS[] = { java.util.Hashtable.class, java.lang.String.class, java.lang.String.class }; try { // Use reflection to try to find xml-commons utility 'Which' // Classloader note: if anyone really cares, we could try to // use the context classloader instead Class clazz = Class.forName(WHICH_CLASSNAME); if (null == clazz) return null; // Fully qualify names since this is the only method they're used in java.lang.reflect.Method method = clazz.getMethod(WHICH_METHODNAME, WHICH_METHOD_ARGS); Hashtable report = new Hashtable(); // Call the method with our Hashtable, common options, and ignore return value Object[] methodArgs = { report, "XmlCommons;Xalan;Xerces;Crimson;Ant", "" }; Object returnValue = method.invoke(null, methodArgs); // Create a parent to hold the report and append hash to it Node resultNode = factoryDocument.createElement("checkEnvironmentExtension"); org.apache.xml.utils.Hashtree2Node.appendHashToNode(report, "whichReport", resultNode, factoryDocument); return resultNode; } catch (Throwable t) { // Simply return null; no need to report error return null; } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?