FileDocCategorySizeDatePackage
Extensions.javaAPI DocJava SE 6 API14588Tue Jun 10 00:22:24 BST 2008com.sun.org.apache.xalan.internal.lib

Extensions

public class Extensions extends Object
This class contains many of the Xalan-supplied extensions. It is accessed by specifying a namespace URI as follows:
xmlns:xalan="http://xml.apache.org/xalan"
xsl.usage
general

Fields Summary
Constructors Summary
private Extensions()
Constructor Extensions

Methods Summary
public static org.w3c.dom.NodecheckEnvironment(com.sun.org.apache.xalan.internal.extensions.ExpressionContext myContext)
Return a Node of basic debugging information from the EnvironmentCheck utility about the Java environment.

Simply calls the {@link com.sun.org.apache.xalan.internal.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 http://xml.apache.org/commons/.

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?)

param
myContext an ExpressionContext passed in by the extension mechanism. This must be an XPathContext.
return
a Node as described above.


    Document factoryDocument;
    try
    {
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = dbf.newDocumentBuilder();
      factoryDocument = db.newDocument();
    }
    catch(ParserConfigurationException pce)
    {
      throw new com.sun.org.apache.xml.internal.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 com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(e);
    }

    return resultNode;
  
private static org.w3c.dom.NodecheckEnvironmentUsingWhich(com.sun.org.apache.xalan.internal.extensions.ExpressionContext myContext, org.w3c.dom.Document factoryDocument)
Private worker method to attempt to use org.apache.env.Which.

param
myContext an ExpressionContext 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

    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'
      Class clazz = ObjectFactory.findProviderClass(
        WHICH_CLASSNAME, ObjectFactory.findClassLoader(), true);
      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");
      com.sun.org.apache.xml.internal.utils.Hashtree2Node.appendHashToNode(report, "whichReport", 
            resultNode, factoryDocument);

      return resultNode;
    }
    catch (Throwable t)
    {
      // Simply return null; no need to report error
      return null;
    }
  
public static org.w3c.dom.NodeListdifference(org.w3c.dom.NodeList nl1, org.w3c.dom.NodeList nl2)
Returns the difference between two node-sets.

param
nl1 NodeList for first node-set
param
nl2 NodeList for second node-set
return
a NodeList containing the nodes in nl1 that are not in nl2 Note: The usage of this extension function in the xalan namespace is deprecated. Please use the same function in the EXSLT sets extension (http://exslt.org/sets).

    return ExsltSets.difference(nl1, nl2);
  
public static org.w3c.dom.NodeListdistinct(org.w3c.dom.NodeList nl)
Returns node-set containing distinct string values.

param
nl NodeList for node-set
return
a NodeList with nodes from nl containing distinct string values. In other words, if more than one node in nl contains the same string value, only include the first such node found. Note: The usage of this extension function in the xalan namespace is deprecated. Please use the same function in the EXSLT sets extension (http://exslt.org/sets).

    return ExsltSets.distinct(nl);
  
public static com.sun.org.apache.xpath.internal.objects.XObjectevaluate(com.sun.org.apache.xalan.internal.extensions.ExpressionContext myContext, java.lang.String xpathExpr)
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 ExpressionContext passed in by the extension mechanism. This must be an XPathContext.
param
xpathExpr 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).

    return ExsltDynamic.evaluate(myContext, xpathExpr);
  
public static booleanhasSameNodes(org.w3c.dom.NodeList nl1, org.w3c.dom.NodeList nl2)
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.


    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;
  
public static org.w3c.dom.NodeListintersection(org.w3c.dom.NodeList nl1, org.w3c.dom.NodeList nl2)
Returns the intersection of two node-sets.

param
nl1 NodeList for first node-set
param
nl2 NodeList for second node-set
return
a NodeList containing the nodes in nl1 that are also in nl2 Note: The usage of this extension function in the xalan namespace is deprecated. Please use the same function in the EXSLT sets extension (http://exslt.org/sets).

    return ExsltSets.intersection(nl1, nl2);
  
public static com.sun.org.apache.xpath.internal.NodeSetnodeset(com.sun.org.apache.xalan.internal.extensions.ExpressionContext myProcessor, java.lang.Object rtf)
This method is an extension that implements as a Xalan extension the node-set function also found in xt and saxon. If the argument is a Result Tree Fragment, then nodeset returns a node-set consisting of a single root node as described in section 11.1 of the XSLT 1.0 Recommendation. If the argument is a node-set, nodeset returns a node-set. If the argument is a string, number, or boolean, then nodeset returns a node-set consisting of a single root node with a single text node child that is the result of calling the XPath string() function on the passed parameter. If the argument is anything else, then a node-set is returned consisting of a single root node with a single text node child that is the result of calling the java toString() method on the passed argument. Most of the actual work here is done in MethodResolver and XRTreeFrag.

param
myProcessor Context passed by the extension processor
param
rtf Argument in the stylesheet to the nodeset extension function NEEDSDOC ($objectName$) @return


    String textNodeValue;

    if (rtf instanceof NodeIterator)
    {
      return new NodeSet((NodeIterator) rtf);
    }
    else
    {
      if (rtf instanceof String)
      {
        textNodeValue = (String) rtf;
      }
      else if (rtf instanceof Boolean)
      {
        textNodeValue = new XBoolean(((Boolean) rtf).booleanValue()).str();
      }
      else if (rtf instanceof Double)
      {
        textNodeValue = new XNumber(((Double) rtf).doubleValue()).str();
      }
      else
      {
        textNodeValue = rtf.toString();
      }

      // This no longer will work right since the DTM.
      // Document myDoc = myProcessor.getContextNode().getOwnerDocument();
      try
      {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document myDoc = db.newDocument();
        
        Text textNode = myDoc.createTextNode(textNodeValue);
        DocumentFragment docFrag = myDoc.createDocumentFragment();
  
        docFrag.appendChild(textNode);
  
        return new NodeSet(docFrag);
      }
      catch(ParserConfigurationException pce)
      {
        throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(pce);
      }
    }
  
public static org.w3c.dom.NodeListtokenize(java.lang.String toTokenize, java.lang.String delims)
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 StringTokenizer. If the first argument is an empty string or contains only delimiters, the result will be an empty NodeSet. Contributed to XalanJ1 by Benoit Cerrina.

param
toTokenize The string to be split into text tokens.
param
delims The delimiters to use.
return
a NodeSet as described above.


    Document doc = DocumentHolder.m_doc;


    StringTokenizer lTokenizer = new StringTokenizer(toTokenize, delims);
    NodeSet resultSet = new NodeSet();

    synchronized (doc)
    {
      while (lTokenizer.hasMoreTokens())
      {
        resultSet.addNode(doc.createTextNode(lTokenizer.nextToken()));
      }
    }

    return resultSet;
  
public static org.w3c.dom.NodeListtokenize(java.lang.String toTokenize)
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 StringTokenizer. If the first argument is an empty string or contains only delimiters, the result will be an empty NodeSet. Contributed to XalanJ1 by Benoit Cerrina.

param
toTokenize The string to be split into text tokens.
return
a NodeSet as described above.

    return tokenize(toTokenize, " \t\n\r");