286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 1999-2004 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A/*
286N/A * $Id: Extensions.java,v 1.2.4.1 2005/09/10 18:53:32 jeffsuttor Exp $
286N/A */
286N/Apackage com.sun.org.apache.xalan.internal.lib;
286N/A
286N/Aimport java.util.Hashtable;
286N/Aimport java.util.StringTokenizer;
286N/A
286N/Aimport javax.xml.parsers.DocumentBuilder;
286N/Aimport javax.xml.parsers.DocumentBuilderFactory;
286N/Aimport javax.xml.parsers.ParserConfigurationException;
286N/A
286N/Aimport com.sun.org.apache.xalan.internal.extensions.ExpressionContext;
286N/Aimport com.sun.org.apache.xalan.internal.xslt.EnvironmentCheck;
286N/Aimport com.sun.org.apache.xpath.internal.NodeSet;
286N/Aimport com.sun.org.apache.xpath.internal.objects.XBoolean;
286N/Aimport com.sun.org.apache.xpath.internal.objects.XNumber;
286N/Aimport com.sun.org.apache.xpath.internal.objects.XObject;
286N/Aimport com.sun.org.apache.xalan.internal.utils.ObjectFactory;
286N/A
286N/Aimport org.w3c.dom.Document;
286N/Aimport org.w3c.dom.DocumentFragment;
286N/Aimport org.w3c.dom.Node;
286N/Aimport org.w3c.dom.NodeList;
286N/Aimport org.w3c.dom.Text;
286N/Aimport org.w3c.dom.traversal.NodeIterator;
286N/A
286N/Aimport org.xml.sax.SAXNotSupportedException;
286N/A
286N/A/**
286N/A * This class contains many of the Xalan-supplied extensions.
286N/A * It is accessed by specifying a namespace URI as follows:
286N/A * <pre>
286N/A * xmlns:xalan="http://xml.apache.org/xalan"
286N/A * </pre>
286N/A * @xsl.usage general
286N/A */
286N/Apublic class Extensions
286N/A{
286N/A /**
286N/A * Constructor Extensions
286N/A *
286N/A */
286N/A private Extensions(){} // Make sure class cannot be instantiated
286N/A
286N/A /**
286N/A * This method is an extension that implements as a Xalan extension
286N/A * the node-set function also found in xt and saxon.
286N/A * If the argument is a Result Tree Fragment, then <code>nodeset</code>
286N/A * returns a node-set consisting of a single root node as described in
286N/A * section 11.1 of the XSLT 1.0 Recommendation. If the argument is a
286N/A * node-set, <code>nodeset</code> returns a node-set. If the argument
286N/A * is a string, number, or boolean, then <code>nodeset</code> returns
286N/A * a node-set consisting of a single root node with a single text node
286N/A * child that is the result of calling the XPath string() function on the
286N/A * passed parameter. If the argument is anything else, then a node-set
286N/A * is returned consisting of a single root node with a single text node
286N/A * child that is the result of calling the java <code>toString()</code>
286N/A * method on the passed argument.
286N/A * Most of the
286N/A * actual work here is done in <code>MethodResolver</code> and
286N/A * <code>XRTreeFrag</code>.
286N/A * @param myProcessor Context passed by the extension processor
286N/A * @param rtf Argument in the stylesheet to the nodeset extension function
286N/A *
286N/A * NEEDSDOC ($objectName$) @return
286N/A */
286N/A public static NodeSet nodeset(ExpressionContext myProcessor, Object rtf)
286N/A {
286N/A
286N/A String textNodeValue;
286N/A
286N/A if (rtf instanceof NodeIterator)
286N/A {
286N/A return new NodeSet((NodeIterator) rtf);
286N/A }
286N/A else
286N/A {
286N/A if (rtf instanceof String)
286N/A {
286N/A textNodeValue = (String) rtf;
286N/A }
286N/A else if (rtf instanceof Boolean)
286N/A {
286N/A textNodeValue = new XBoolean(((Boolean) rtf).booleanValue()).str();
286N/A }
286N/A else if (rtf instanceof Double)
286N/A {
286N/A textNodeValue = new XNumber(((Double) rtf).doubleValue()).str();
286N/A }
286N/A else
286N/A {
286N/A textNodeValue = rtf.toString();
286N/A }
286N/A
286N/A // This no longer will work right since the DTM.
286N/A // Document myDoc = myProcessor.getContextNode().getOwnerDocument();
286N/A try
286N/A {
286N/A DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
286N/A DocumentBuilder db = dbf.newDocumentBuilder();
286N/A Document myDoc = db.newDocument();
286N/A
286N/A Text textNode = myDoc.createTextNode(textNodeValue);
286N/A DocumentFragment docFrag = myDoc.createDocumentFragment();
286N/A
286N/A docFrag.appendChild(textNode);
286N/A
286N/A return new NodeSet(docFrag);
286N/A }
286N/A catch(ParserConfigurationException pce)
286N/A {
286N/A throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(pce);
286N/A }
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * Returns the intersection of two node-sets.
286N/A *
286N/A * @param nl1 NodeList for first node-set
286N/A * @param nl2 NodeList for second node-set
286N/A * @return a NodeList containing the nodes in nl1 that are also in nl2
286N/A *
286N/A * Note: The usage of this extension function in the xalan namespace
286N/A * is deprecated. Please use the same function in the EXSLT sets extension
286N/A * (http://exslt.org/sets).
286N/A */
286N/A public static NodeList intersection(NodeList nl1, NodeList nl2)
286N/A {
286N/A return ExsltSets.intersection(nl1, nl2);
286N/A }
286N/A
286N/A /**
286N/A * Returns the difference between two node-sets.
286N/A *
286N/A * @param nl1 NodeList for first node-set
286N/A * @param nl2 NodeList for second node-set
286N/A * @return a NodeList containing the nodes in nl1 that are not in nl2
286N/A *
286N/A * Note: The usage of this extension function in the xalan namespace
286N/A * is deprecated. Please use the same function in the EXSLT sets extension
286N/A * (http://exslt.org/sets).
286N/A */
286N/A public static NodeList difference(NodeList nl1, NodeList nl2)
286N/A {
286N/A return ExsltSets.difference(nl1, nl2);
286N/A }
286N/A
286N/A /**
286N/A * Returns node-set containing distinct string values.
286N/A *
286N/A * @param nl NodeList for node-set
286N/A * @return a NodeList with nodes from nl containing distinct string values.
286N/A * In other words, if more than one node in nl contains the same string value,
286N/A * only include the first such node found.
286N/A *
286N/A * Note: The usage of this extension function in the xalan namespace
286N/A * is deprecated. Please use the same function in the EXSLT sets extension
286N/A * (http://exslt.org/sets).
286N/A */
286N/A public static NodeList distinct(NodeList nl)
286N/A {
286N/A return ExsltSets.distinct(nl);
286N/A }
286N/A
286N/A /**
286N/A * Returns true if both node-sets contain the same set of nodes.
286N/A *
286N/A * @param nl1 NodeList for first node-set
286N/A * @param nl2 NodeList for second node-set
286N/A * @return true if nl1 and nl2 contain exactly the same set of nodes.
286N/A */
286N/A public static boolean hasSameNodes(NodeList nl1, NodeList nl2)
286N/A {
286N/A
286N/A NodeSet ns1 = new NodeSet(nl1);
286N/A NodeSet ns2 = new NodeSet(nl2);
286N/A
286N/A if (ns1.getLength() != ns2.getLength())
286N/A return false;
286N/A
286N/A for (int i = 0; i < ns1.getLength(); i++)
286N/A {
286N/A Node n = ns1.elementAt(i);
286N/A
286N/A if (!ns2.contains(n))
286N/A return false;
286N/A }
286N/A
286N/A return true;
286N/A }
286N/A
286N/A /**
286N/A * Returns the result of evaluating the argument as a string containing
286N/A * an XPath expression. Used where the XPath expression is not known until
286N/A * run-time. The expression is evaluated as if the run-time value of the
286N/A * argument appeared in place of the evaluate function call at compile time.
286N/A *
286N/A * @param myContext an <code>ExpressionContext</code> passed in by the
286N/A * extension mechanism. This must be an XPathContext.
286N/A * @param xpathExpr The XPath expression to be evaluated.
286N/A * @return the XObject resulting from evaluating the XPath
286N/A *
286N/A * @throws SAXNotSupportedException
286N/A *
286N/A * Note: The usage of this extension function in the xalan namespace
286N/A * is deprecated. Please use the same function in the EXSLT dynamic extension
286N/A * (http://exslt.org/dynamic).
286N/A */
286N/A public static XObject evaluate(ExpressionContext myContext, String xpathExpr)
286N/A throws SAXNotSupportedException
286N/A {
286N/A return ExsltDynamic.evaluate(myContext, xpathExpr);
286N/A }
286N/A
286N/A /**
286N/A * Returns a NodeSet containing one text node for each token in the first argument.
286N/A * Delimiters are specified in the second argument.
286N/A * Tokens are determined by a call to <code>StringTokenizer</code>.
286N/A * If the first argument is an empty string or contains only delimiters, the result
286N/A * will be an empty NodeSet.
286N/A *
286N/A * Contributed to XalanJ1 by <a href="mailto:benoit.cerrina@writeme.com">Benoit Cerrina</a>.
286N/A *
286N/A * @param toTokenize The string to be split into text tokens.
286N/A * @param delims The delimiters to use.
286N/A * @return a NodeSet as described above.
286N/A */
286N/A public static NodeList tokenize(String toTokenize, String delims)
286N/A {
286N/A
286N/A Document doc = DocumentHolder.m_doc;
286N/A
286N/A
286N/A StringTokenizer lTokenizer = new StringTokenizer(toTokenize, delims);
286N/A NodeSet resultSet = new NodeSet();
286N/A
286N/A synchronized (doc)
286N/A {
286N/A while (lTokenizer.hasMoreTokens())
286N/A {
286N/A resultSet.addNode(doc.createTextNode(lTokenizer.nextToken()));
286N/A }
286N/A }
286N/A
286N/A return resultSet;
286N/A }
286N/A
286N/A /**
286N/A * Returns a NodeSet containing one text node for each token in the first argument.
286N/A * Delimiters are whitespace. That is, the delimiters that are used are tab (&#x09),
286N/A * linefeed (&#x0A), return (&#x0D), and space (&#x20).
286N/A * Tokens are determined by a call to <code>StringTokenizer</code>.
286N/A * If the first argument is an empty string or contains only delimiters, the result
286N/A * will be an empty NodeSet.
286N/A *
286N/A * Contributed to XalanJ1 by <a href="mailto:benoit.cerrina@writeme.com">Benoit Cerrina</a>.
286N/A *
286N/A * @param toTokenize The string to be split into text tokens.
286N/A * @return a NodeSet as described above.
286N/A */
286N/A public static NodeList tokenize(String toTokenize)
286N/A {
286N/A return tokenize(toTokenize, " \t\n\r");
286N/A }
286N/A
286N/A /**
286N/A * Return a Node of basic debugging information from the
286N/A * EnvironmentCheck utility about the Java environment.
286N/A *
286N/A * <p>Simply calls the {@link com.sun.org.apache.xalan.internal.xslt.EnvironmentCheck}
286N/A * utility to grab info about the Java environment and CLASSPATH,
286N/A * etc., and then returns the resulting Node. Stylesheets can
286N/A * then maniuplate this data or simply xsl:copy-of the Node. Note
286N/A * that we first attempt to load the more advanced
286N/A * org.apache.env.Which utility by reflection; only if that fails
286N/A * to we still use the internal version. Which is available from
286N/A * <a href="http://xml.apache.org/commons/">http://xml.apache.org/commons/</a>.</p>
286N/A *
286N/A * <p>We throw a WrappedRuntimeException in the unlikely case
286N/A * that reading information from the environment throws us an
286N/A * exception. (Is this really the best thing to do?)</p>
286N/A *
286N/A * @param myContext an <code>ExpressionContext</code> passed in by the
286N/A * extension mechanism. This must be an XPathContext.
286N/A * @return a Node as described above.
286N/A */
286N/A public static Node checkEnvironment(ExpressionContext myContext)
286N/A {
286N/A
286N/A Document factoryDocument;
286N/A try
286N/A {
286N/A DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
286N/A DocumentBuilder db = dbf.newDocumentBuilder();
286N/A factoryDocument = db.newDocument();
286N/A }
286N/A catch(ParserConfigurationException pce)
286N/A {
286N/A throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(pce);
286N/A }
286N/A
286N/A Node resultNode = null;
286N/A try
286N/A {
286N/A // First use reflection to try to load Which, which is a
286N/A // better version of EnvironmentCheck
286N/A resultNode = checkEnvironmentUsingWhich(myContext, factoryDocument);
286N/A
286N/A if (null != resultNode)
286N/A return resultNode;
286N/A
286N/A // If reflection failed, fallback to our internal EnvironmentCheck
286N/A EnvironmentCheck envChecker = new EnvironmentCheck();
286N/A Hashtable h = envChecker.getEnvironmentHash();
286N/A resultNode = factoryDocument.createElement("checkEnvironmentExtension");
286N/A envChecker.appendEnvironmentReport(resultNode, factoryDocument, h);
286N/A envChecker = null;
286N/A }
286N/A catch(Exception e)
286N/A {
286N/A throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(e);
286N/A }
286N/A
286N/A return resultNode;
286N/A }
286N/A
286N/A /**
286N/A * Private worker method to attempt to use org.apache.env.Which.
286N/A *
286N/A * @param myContext an <code>ExpressionContext</code> passed in by the
286N/A * extension mechanism. This must be an XPathContext.
286N/A * @param factoryDocument providing createElement services, etc.
286N/A * @return a Node with environment info; null if any error
286N/A */
286N/A private static Node checkEnvironmentUsingWhich(ExpressionContext myContext,
286N/A Document factoryDocument)
286N/A {
286N/A final String WHICH_CLASSNAME = "org.apache.env.Which";
286N/A final String WHICH_METHODNAME = "which";
286N/A final Class WHICH_METHOD_ARGS[] = { java.util.Hashtable.class,
286N/A java.lang.String.class,
286N/A java.lang.String.class };
286N/A try
286N/A {
286N/A // Use reflection to try to find xml-commons utility 'Which'
286N/A Class clazz = ObjectFactory.findProviderClass(WHICH_CLASSNAME, true);
286N/A if (null == clazz)
286N/A return null;
286N/A
286N/A // Fully qualify names since this is the only method they're used in
286N/A java.lang.reflect.Method method = clazz.getMethod(WHICH_METHODNAME, WHICH_METHOD_ARGS);
286N/A Hashtable report = new Hashtable();
286N/A
286N/A // Call the method with our Hashtable, common options, and ignore return value
286N/A Object[] methodArgs = { report, "XmlCommons;Xalan;Xerces;Crimson;Ant", "" };
286N/A Object returnValue = method.invoke(null, methodArgs);
286N/A
286N/A // Create a parent to hold the report and append hash to it
286N/A Node resultNode = factoryDocument.createElement("checkEnvironmentExtension");
286N/A com.sun.org.apache.xml.internal.utils.Hashtree2Node.appendHashToNode(report, "whichReport",
286N/A resultNode, factoryDocument);
286N/A
286N/A return resultNode;
286N/A }
286N/A catch (Throwable t)
286N/A {
286N/A // Simply return null; no need to report error
286N/A return null;
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * This class is not loaded until first referenced (see Java Language
286N/A * Specification by Gosling/Joy/Steele, section 12.4.1)
286N/A *
286N/A * The static members are created when this class is first referenced, as a
286N/A * lazy initialization not needing checking against null or any
286N/A * synchronization.
286N/A *
286N/A */
286N/A private static class DocumentHolder
286N/A {
286N/A // Reuse the Document object to reduce memory usage.
286N/A private static final Document m_doc;
286N/A static
286N/A {
286N/A try
286N/A {
286N/A m_doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
286N/A }
286N/A
286N/A catch(ParserConfigurationException pce)
286N/A {
286N/A throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(pce);
286N/A }
286N/A
286N/A }
286N/A }
286N/A}