jaxp/src/com/sun/org/apache/xalan/internal/lib/Extensions.java
author joehw
Tue, 22 Oct 2013 13:15:50 -0700
changeset 22416 5473b73ce396
parent 12458 d601e4bba306
child 22417 278570df46ad
permissions -rw-r--r--
8025018: Enhance JAX-P set up Reviewed-by: alanb, dfuchs, lancea, ahgross
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
7f561c08de6b Initial load
duke
parents:
diff changeset
     1
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
7f561c08de6b Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
7f561c08de6b Initial load
duke
parents:
diff changeset
     4
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
     5
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
     6
 * Copyright 1999-2004 The Apache Software Foundation.
7f561c08de6b Initial load
duke
parents:
diff changeset
     7
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
     8
 * Licensed under the Apache License, Version 2.0 (the "License");
7f561c08de6b Initial load
duke
parents:
diff changeset
     9
 * you may not use this file except in compliance with the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    10
 * You may obtain a copy of the License at
7f561c08de6b Initial load
duke
parents:
diff changeset
    11
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    12
 *     http://www.apache.org/licenses/LICENSE-2.0
7f561c08de6b Initial load
duke
parents:
diff changeset
    13
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    14
 * Unless required by applicable law or agreed to in writing, software
7f561c08de6b Initial load
duke
parents:
diff changeset
    15
 * distributed under the License is distributed on an "AS IS" BASIS,
7f561c08de6b Initial load
duke
parents:
diff changeset
    16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
7f561c08de6b Initial load
duke
parents:
diff changeset
    17
 * See the License for the specific language governing permissions and
7f561c08de6b Initial load
duke
parents:
diff changeset
    18
 * limitations under the License.
7f561c08de6b Initial load
duke
parents:
diff changeset
    19
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    20
/*
7f561c08de6b Initial load
duke
parents:
diff changeset
    21
 * $Id: Extensions.java,v 1.2.4.1 2005/09/10 18:53:32 jeffsuttor Exp $
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
package com.sun.org.apache.xalan.internal.lib;
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
import java.util.Hashtable;
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
import java.util.StringTokenizer;
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import javax.xml.parsers.DocumentBuilder;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import javax.xml.parsers.DocumentBuilderFactory;
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
import javax.xml.parsers.ParserConfigurationException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
import com.sun.org.apache.xalan.internal.extensions.ExpressionContext;
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
import com.sun.org.apache.xalan.internal.xslt.EnvironmentCheck;
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
import com.sun.org.apache.xpath.internal.NodeSet;
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
import com.sun.org.apache.xpath.internal.objects.XBoolean;
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
import com.sun.org.apache.xpath.internal.objects.XNumber;
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
import com.sun.org.apache.xpath.internal.objects.XObject;
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
    38
import com.sun.org.apache.xalan.internal.utils.ObjectFactory;
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
import org.w3c.dom.Document;
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
import org.w3c.dom.DocumentFragment;
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
import org.w3c.dom.Node;
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
import org.w3c.dom.NodeList;
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
import org.w3c.dom.Text;
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
import org.w3c.dom.traversal.NodeIterator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
import org.xml.sax.SAXNotSupportedException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
 * This class contains many of the Xalan-supplied extensions.
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
 * It is accessed by specifying a namespace URI as follows:
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
 * <pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
 *    xmlns:xalan="http://xml.apache.org/xalan"
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
 * </pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
 * @xsl.usage general
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
public class Extensions
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
{
22416
5473b73ce396 8025018: Enhance JAX-P set up
joehw
parents: 12458
diff changeset
    59
    static final String JDK_DEFAULT_DOM = "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl";
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
   * Constructor Extensions
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
  private Extensions(){}  // Make sure class cannot be instantiated
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
   * This method is an extension that implements as a Xalan extension
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
   * the node-set function also found in xt and saxon.
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
   * If the argument is a Result Tree Fragment, then <code>nodeset</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
   * returns a node-set consisting of a single root node as described in
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
   * section 11.1 of the XSLT 1.0 Recommendation.  If the argument is a
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
   * node-set, <code>nodeset</code> returns a node-set.  If the argument
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
   * is a string, number, or boolean, then <code>nodeset</code> returns
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
   * a node-set consisting of a single root node with a single text node
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
   * child that is the result of calling the XPath string() function on the
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
   * passed parameter.  If the argument is anything else, then a node-set
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
   * is returned consisting of a single root node with a single text node
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
   * child that is the result of calling the java <code>toString()</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
   * method on the passed argument.
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
   * Most of the
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
   * actual work here is done in <code>MethodResolver</code> and
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
   * <code>XRTreeFrag</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
   * @param myProcessor Context passed by the extension processor
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
   * @param rtf Argument in the stylesheet to the nodeset extension function
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
   * NEEDSDOC ($objectName$) @return
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
  public static NodeSet nodeset(ExpressionContext myProcessor, Object rtf)
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
    String textNodeValue;
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
    if (rtf instanceof NodeIterator)
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
      return new NodeSet((NodeIterator) rtf);
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
    else
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
      if (rtf instanceof String)
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
        textNodeValue = (String) rtf;
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
      else if (rtf instanceof Boolean)
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
        textNodeValue = new XBoolean(((Boolean) rtf).booleanValue()).str();
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
      else if (rtf instanceof Double)
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
        textNodeValue = new XNumber(((Double) rtf).doubleValue()).str();
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
      else
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
        textNodeValue = rtf.toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
      // This no longer will work right since the DTM.
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
      // Document myDoc = myProcessor.getContextNode().getOwnerDocument();
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
      try
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
        DocumentBuilder db = dbf.newDocumentBuilder();
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
        Document myDoc = db.newDocument();
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
        Text textNode = myDoc.createTextNode(textNodeValue);
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
        DocumentFragment docFrag = myDoc.createDocumentFragment();
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
        docFrag.appendChild(textNode);
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
        return new NodeSet(docFrag);
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
      catch(ParserConfigurationException pce)
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
        throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(pce);
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
   * Returns the intersection of two node-sets.
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
   * @param nl1 NodeList for first node-set
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
   * @param nl2 NodeList for second node-set
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
   * @return a NodeList containing the nodes in nl1 that are also in nl2
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
   * Note: The usage of this extension function in the xalan namespace
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
   * is deprecated. Please use the same function in the EXSLT sets extension
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
   * (http://exslt.org/sets).
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
  public static NodeList intersection(NodeList nl1, NodeList nl2)
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
    return ExsltSets.intersection(nl1, nl2);
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
   * Returns the difference between two node-sets.
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
   * @param nl1 NodeList for first node-set
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
   * @param nl2 NodeList for second node-set
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
   * @return a NodeList containing the nodes in nl1 that are not in nl2
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
   * Note: The usage of this extension function in the xalan namespace
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
   * is deprecated. Please use the same function in the EXSLT sets extension
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
   * (http://exslt.org/sets).
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
  public static NodeList difference(NodeList nl1, NodeList nl2)
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
    return ExsltSets.difference(nl1, nl2);
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
   * Returns node-set containing distinct string values.
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
   * @param nl NodeList for node-set
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
   * @return a NodeList with nodes from nl containing distinct string values.
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
   * In other words, if more than one node in nl contains the same string value,
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
   * only include the first such node found.
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
   * Note: The usage of this extension function in the xalan namespace
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
   * is deprecated. Please use the same function in the EXSLT sets extension
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
   * (http://exslt.org/sets).
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
  public static NodeList distinct(NodeList nl)
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
    return ExsltSets.distinct(nl);
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
   * Returns true if both node-sets contain the same set of nodes.
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
   * @param nl1 NodeList for first node-set
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
   * @param nl2 NodeList for second node-set
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
   * @return true if nl1 and nl2 contain exactly the same set of nodes.
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
  public static boolean hasSameNodes(NodeList nl1, NodeList nl2)
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
    NodeSet ns1 = new NodeSet(nl1);
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
    NodeSet ns2 = new NodeSet(nl2);
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
    if (ns1.getLength() != ns2.getLength())
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
      return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
    for (int i = 0; i < ns1.getLength(); i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
      Node n = ns1.elementAt(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
      if (!ns2.contains(n))
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
        return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
    return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
   * Returns the result of evaluating the argument as a string containing
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
   * an XPath expression.  Used where the XPath expression is not known until
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
   * run-time.  The expression is evaluated as if the run-time value of the
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
   * argument appeared in place of the evaluate function call at compile time.
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
   * @param myContext an <code>ExpressionContext</code> passed in by the
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
   *                  extension mechanism.  This must be an XPathContext.
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
   * @param xpathExpr The XPath expression to be evaluated.
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
   * @return the XObject resulting from evaluating the XPath
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
   * @throws SAXNotSupportedException
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
   * Note: The usage of this extension function in the xalan namespace
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
   * is deprecated. Please use the same function in the EXSLT dynamic extension
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
   * (http://exslt.org/dynamic).
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
  public static XObject evaluate(ExpressionContext myContext, String xpathExpr)
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
         throws SAXNotSupportedException
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
    return ExsltDynamic.evaluate(myContext, xpathExpr);
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
   * Returns a NodeSet containing one text node for each token in the first argument.
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
   * Delimiters are specified in the second argument.
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
   * Tokens are determined by a call to <code>StringTokenizer</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
   * If the first argument is an empty string or contains only delimiters, the result
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
   * will be an empty NodeSet.
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
   * Contributed to XalanJ1 by <a href="mailto:benoit.cerrina@writeme.com">Benoit Cerrina</a>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
   * @param toTokenize The string to be split into text tokens.
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
   * @param delims The delimiters to use.
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
   * @return a NodeSet as described above.
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
  public static NodeList tokenize(String toTokenize, String delims)
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
    Document doc = DocumentHolder.m_doc;
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
    StringTokenizer lTokenizer = new StringTokenizer(toTokenize, delims);
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
    NodeSet resultSet = new NodeSet();
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
    synchronized (doc)
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
      while (lTokenizer.hasMoreTokens())
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
        resultSet.addNode(doc.createTextNode(lTokenizer.nextToken()));
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
    return resultSet;
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
   * Returns a NodeSet containing one text node for each token in the first argument.
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
   * Delimiters are whitespace.  That is, the delimiters that are used are tab (&#x09),
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
   * linefeed (&#x0A), return (&#x0D), and space (&#x20).
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
   * Tokens are determined by a call to <code>StringTokenizer</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
   * If the first argument is an empty string or contains only delimiters, the result
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
   * will be an empty NodeSet.
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
   * Contributed to XalanJ1 by <a href="mailto:benoit.cerrina@writeme.com">Benoit Cerrina</a>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
   * @param toTokenize The string to be split into text tokens.
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
   * @return a NodeSet as described above.
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
  public static NodeList tokenize(String toTokenize)
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
    return tokenize(toTokenize, " \t\n\r");
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
7f561c08de6b Initial load
duke
parents:
diff changeset
   288
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   289
   * Return a Node of basic debugging information from the
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
   * EnvironmentCheck utility about the Java environment.
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
   * <p>Simply calls the {@link com.sun.org.apache.xalan.internal.xslt.EnvironmentCheck}
7f561c08de6b Initial load
duke
parents:
diff changeset
   293
   * utility to grab info about the Java environment and CLASSPATH,
7f561c08de6b Initial load
duke
parents:
diff changeset
   294
   * etc., and then returns the resulting Node.  Stylesheets can
7f561c08de6b Initial load
duke
parents:
diff changeset
   295
   * then maniuplate this data or simply xsl:copy-of the Node.  Note
7f561c08de6b Initial load
duke
parents:
diff changeset
   296
   * that we first attempt to load the more advanced
7f561c08de6b Initial load
duke
parents:
diff changeset
   297
   * org.apache.env.Which utility by reflection; only if that fails
7f561c08de6b Initial load
duke
parents:
diff changeset
   298
   * to we still use the internal version.  Which is available from
7f561c08de6b Initial load
duke
parents:
diff changeset
   299
   * <a href="http://xml.apache.org/commons/">http://xml.apache.org/commons/</a>.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   300
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   301
   * <p>We throw a WrappedRuntimeException in the unlikely case
7f561c08de6b Initial load
duke
parents:
diff changeset
   302
   * that reading information from the environment throws us an
7f561c08de6b Initial load
duke
parents:
diff changeset
   303
   * exception. (Is this really the best thing to do?)</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
   304
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   305
   * @param myContext an <code>ExpressionContext</code> passed in by the
7f561c08de6b Initial load
duke
parents:
diff changeset
   306
   *                  extension mechanism.  This must be an XPathContext.
7f561c08de6b Initial load
duke
parents:
diff changeset
   307
   * @return a Node as described above.
7f561c08de6b Initial load
duke
parents:
diff changeset
   308
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   309
  public static Node checkEnvironment(ExpressionContext myContext)
7f561c08de6b Initial load
duke
parents:
diff changeset
   310
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   311
7f561c08de6b Initial load
duke
parents:
diff changeset
   312
    Document factoryDocument;
7f561c08de6b Initial load
duke
parents:
diff changeset
   313
    try
7f561c08de6b Initial load
duke
parents:
diff changeset
   314
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   315
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
7f561c08de6b Initial load
duke
parents:
diff changeset
   316
      DocumentBuilder db = dbf.newDocumentBuilder();
7f561c08de6b Initial load
duke
parents:
diff changeset
   317
      factoryDocument = db.newDocument();
7f561c08de6b Initial load
duke
parents:
diff changeset
   318
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   319
    catch(ParserConfigurationException pce)
7f561c08de6b Initial load
duke
parents:
diff changeset
   320
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   321
      throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(pce);
7f561c08de6b Initial load
duke
parents:
diff changeset
   322
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   323
7f561c08de6b Initial load
duke
parents:
diff changeset
   324
    Node resultNode = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   325
    try
7f561c08de6b Initial load
duke
parents:
diff changeset
   326
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   327
      // First use reflection to try to load Which, which is a
7f561c08de6b Initial load
duke
parents:
diff changeset
   328
      //  better version of EnvironmentCheck
7f561c08de6b Initial load
duke
parents:
diff changeset
   329
      resultNode = checkEnvironmentUsingWhich(myContext, factoryDocument);
7f561c08de6b Initial load
duke
parents:
diff changeset
   330
7f561c08de6b Initial load
duke
parents:
diff changeset
   331
      if (null != resultNode)
7f561c08de6b Initial load
duke
parents:
diff changeset
   332
        return resultNode;
7f561c08de6b Initial load
duke
parents:
diff changeset
   333
7f561c08de6b Initial load
duke
parents:
diff changeset
   334
      // If reflection failed, fallback to our internal EnvironmentCheck
7f561c08de6b Initial load
duke
parents:
diff changeset
   335
      EnvironmentCheck envChecker = new EnvironmentCheck();
7f561c08de6b Initial load
duke
parents:
diff changeset
   336
      Hashtable h = envChecker.getEnvironmentHash();
7f561c08de6b Initial load
duke
parents:
diff changeset
   337
      resultNode = factoryDocument.createElement("checkEnvironmentExtension");
7f561c08de6b Initial load
duke
parents:
diff changeset
   338
      envChecker.appendEnvironmentReport(resultNode, factoryDocument, h);
7f561c08de6b Initial load
duke
parents:
diff changeset
   339
      envChecker = null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   340
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   341
    catch(Exception e)
7f561c08de6b Initial load
duke
parents:
diff changeset
   342
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   343
      throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(e);
7f561c08de6b Initial load
duke
parents:
diff changeset
   344
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   345
7f561c08de6b Initial load
duke
parents:
diff changeset
   346
    return resultNode;
7f561c08de6b Initial load
duke
parents:
diff changeset
   347
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   348
7f561c08de6b Initial load
duke
parents:
diff changeset
   349
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   350
   * Private worker method to attempt to use org.apache.env.Which.
7f561c08de6b Initial load
duke
parents:
diff changeset
   351
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   352
   * @param myContext an <code>ExpressionContext</code> passed in by the
7f561c08de6b Initial load
duke
parents:
diff changeset
   353
   *                  extension mechanism.  This must be an XPathContext.
7f561c08de6b Initial load
duke
parents:
diff changeset
   354
   * @param factoryDocument providing createElement services, etc.
7f561c08de6b Initial load
duke
parents:
diff changeset
   355
   * @return a Node with environment info; null if any error
7f561c08de6b Initial load
duke
parents:
diff changeset
   356
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   357
  private static Node checkEnvironmentUsingWhich(ExpressionContext myContext,
7f561c08de6b Initial load
duke
parents:
diff changeset
   358
        Document factoryDocument)
7f561c08de6b Initial load
duke
parents:
diff changeset
   359
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   360
    final String WHICH_CLASSNAME = "org.apache.env.Which";
7f561c08de6b Initial load
duke
parents:
diff changeset
   361
    final String WHICH_METHODNAME = "which";
7f561c08de6b Initial load
duke
parents:
diff changeset
   362
    final Class WHICH_METHOD_ARGS[] = { java.util.Hashtable.class,
7f561c08de6b Initial load
duke
parents:
diff changeset
   363
                                        java.lang.String.class,
7f561c08de6b Initial load
duke
parents:
diff changeset
   364
                                        java.lang.String.class };
7f561c08de6b Initial load
duke
parents:
diff changeset
   365
    try
7f561c08de6b Initial load
duke
parents:
diff changeset
   366
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   367
      // Use reflection to try to find xml-commons utility 'Which'
12458
d601e4bba306 7160380: Sync JDK8 with JAXP 1.4.5
joehw
parents: 12457
diff changeset
   368
      Class clazz = ObjectFactory.findProviderClass(WHICH_CLASSNAME, true);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   369
      if (null == clazz)
7f561c08de6b Initial load
duke
parents:
diff changeset
   370
        return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   371
7f561c08de6b Initial load
duke
parents:
diff changeset
   372
      // Fully qualify names since this is the only method they're used in
7f561c08de6b Initial load
duke
parents:
diff changeset
   373
      java.lang.reflect.Method method = clazz.getMethod(WHICH_METHODNAME, WHICH_METHOD_ARGS);
7f561c08de6b Initial load
duke
parents:
diff changeset
   374
      Hashtable report = new Hashtable();
7f561c08de6b Initial load
duke
parents:
diff changeset
   375
7f561c08de6b Initial load
duke
parents:
diff changeset
   376
      // Call the method with our Hashtable, common options, and ignore return value
7f561c08de6b Initial load
duke
parents:
diff changeset
   377
      Object[] methodArgs = { report, "XmlCommons;Xalan;Xerces;Crimson;Ant", "" };
7f561c08de6b Initial load
duke
parents:
diff changeset
   378
      Object returnValue = method.invoke(null, methodArgs);
7f561c08de6b Initial load
duke
parents:
diff changeset
   379
7f561c08de6b Initial load
duke
parents:
diff changeset
   380
      // Create a parent to hold the report and append hash to it
7f561c08de6b Initial load
duke
parents:
diff changeset
   381
      Node resultNode = factoryDocument.createElement("checkEnvironmentExtension");
7f561c08de6b Initial load
duke
parents:
diff changeset
   382
      com.sun.org.apache.xml.internal.utils.Hashtree2Node.appendHashToNode(report, "whichReport",
7f561c08de6b Initial load
duke
parents:
diff changeset
   383
            resultNode, factoryDocument);
7f561c08de6b Initial load
duke
parents:
diff changeset
   384
7f561c08de6b Initial load
duke
parents:
diff changeset
   385
      return resultNode;
7f561c08de6b Initial load
duke
parents:
diff changeset
   386
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   387
    catch (Throwable t)
7f561c08de6b Initial load
duke
parents:
diff changeset
   388
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   389
      // Simply return null; no need to report error
7f561c08de6b Initial load
duke
parents:
diff changeset
   390
      return null;
7f561c08de6b Initial load
duke
parents:
diff changeset
   391
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   392
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   393
7f561c08de6b Initial load
duke
parents:
diff changeset
   394
    /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   395
     * This class is not loaded until first referenced (see Java Language
7f561c08de6b Initial load
duke
parents:
diff changeset
   396
     * Specification by Gosling/Joy/Steele, section 12.4.1)
7f561c08de6b Initial load
duke
parents:
diff changeset
   397
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   398
     * The static members are created when this class is first referenced, as a
7f561c08de6b Initial load
duke
parents:
diff changeset
   399
     * lazy initialization not needing checking against null or any
7f561c08de6b Initial load
duke
parents:
diff changeset
   400
     * synchronization.
7f561c08de6b Initial load
duke
parents:
diff changeset
   401
     *
7f561c08de6b Initial load
duke
parents:
diff changeset
   402
     */
7f561c08de6b Initial load
duke
parents:
diff changeset
   403
    private static class DocumentHolder
7f561c08de6b Initial load
duke
parents:
diff changeset
   404
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   405
        // Reuse the Document object to reduce memory usage.
7f561c08de6b Initial load
duke
parents:
diff changeset
   406
        private static final Document m_doc;
7f561c08de6b Initial load
duke
parents:
diff changeset
   407
        static
7f561c08de6b Initial load
duke
parents:
diff changeset
   408
        {
7f561c08de6b Initial load
duke
parents:
diff changeset
   409
            try
7f561c08de6b Initial load
duke
parents:
diff changeset
   410
            {
22416
5473b73ce396 8025018: Enhance JAX-P set up
joehw
parents: 12458
diff changeset
   411
                if (System.getSecurityManager() == null) {
5473b73ce396 8025018: Enhance JAX-P set up
joehw
parents: 12458
diff changeset
   412
                    m_doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
5473b73ce396 8025018: Enhance JAX-P set up
joehw
parents: 12458
diff changeset
   413
                } else {
5473b73ce396 8025018: Enhance JAX-P set up
joehw
parents: 12458
diff changeset
   414
                    m_doc = DocumentBuilderFactory.newInstance(JDK_DEFAULT_DOM, null).newDocumentBuilder().newDocument();
5473b73ce396 8025018: Enhance JAX-P set up
joehw
parents: 12458
diff changeset
   415
                }
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   416
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   417
7f561c08de6b Initial load
duke
parents:
diff changeset
   418
            catch(ParserConfigurationException pce)
7f561c08de6b Initial load
duke
parents:
diff changeset
   419
            {
7f561c08de6b Initial load
duke
parents:
diff changeset
   420
                  throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(pce);
7f561c08de6b Initial load
duke
parents:
diff changeset
   421
            }
7f561c08de6b Initial load
duke
parents:
diff changeset
   422
7f561c08de6b Initial load
duke
parents:
diff changeset
   423
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   424
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   425
}