jaxp/src/java.xml/share/classes/com/sun/org/apache/xalan/internal/lib/Extensions.java
author dfuchs
Tue, 30 Jun 2015 12:04:27 +0200
changeset 31497 4a6b2e733c0d
parent 25868 686eef1e7a79
child 44797 8b3b3b911b8a
permissions -rw-r--r--
8130051: Cleanup usage of reflection in jaxp Summary: replaced usage of reflection with direct access where possible, removed obsolete code where possible. Reviewed-by: joehw
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.StringTokenizer;
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import javax.xml.parsers.DocumentBuilderFactory;
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import javax.xml.parsers.ParserConfigurationException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
import com.sun.org.apache.xalan.internal.extensions.ExpressionContext;
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
import com.sun.org.apache.xpath.internal.NodeSet;
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
import com.sun.org.apache.xpath.internal.objects.XBoolean;
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
import com.sun.org.apache.xpath.internal.objects.XNumber;
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
import com.sun.org.apache.xpath.internal.objects.XObject;
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
import org.w3c.dom.Document;
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
import org.w3c.dom.DocumentFragment;
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
import org.w3c.dom.Node;
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
import org.w3c.dom.NodeList;
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
import org.w3c.dom.Text;
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
import org.w3c.dom.traversal.NodeIterator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
import org.xml.sax.SAXNotSupportedException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
 * This class contains many of the Xalan-supplied extensions.
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
 * It is accessed by specifying a namespace URI as follows:
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
 * <pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
 *    xmlns:xalan="http://xml.apache.org/xalan"
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
 * </pre>
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
 * @xsl.usage general
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
public class Extensions
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
{
22416
5473b73ce396 8025018: Enhance JAX-P set up
joehw
parents: 12458
diff changeset
    55
    static final String JDK_DEFAULT_DOM = "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl";
6
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
   * Constructor Extensions
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
  private Extensions(){}  // Make sure class cannot be instantiated
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
   * This method is an extension that implements as a Xalan extension
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
   * the node-set function also found in xt and saxon.
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
   * If the argument is a Result Tree Fragment, then <code>nodeset</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
   * returns a node-set consisting of a single root node as described in
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
   * section 11.1 of the XSLT 1.0 Recommendation.  If the argument is a
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
   * node-set, <code>nodeset</code> returns a node-set.  If the argument
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
   * is a string, number, or boolean, then <code>nodeset</code> returns
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
   * a node-set consisting of a single root node with a single text node
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
   * child that is the result of calling the XPath string() function on the
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
   * passed parameter.  If the argument is anything else, then a node-set
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
   * is returned consisting of a single root node with a single text node
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
   * child that is the result of calling the java <code>toString()</code>
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
   * method on the passed argument.
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
   * Most of the
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
   * actual work here is done in <code>MethodResolver</code> and
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
   * <code>XRTreeFrag</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
   * @param myProcessor Context passed by the extension processor
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
   * @param rtf Argument in the stylesheet to the nodeset extension function
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
   * NEEDSDOC ($objectName$) @return
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
  public static NodeSet nodeset(ExpressionContext myProcessor, Object rtf)
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
    String textNodeValue;
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
    if (rtf instanceof NodeIterator)
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
      return new NodeSet((NodeIterator) rtf);
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
    else
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
      if (rtf instanceof String)
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
        textNodeValue = (String) rtf;
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
      else if (rtf instanceof Boolean)
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
        textNodeValue = new XBoolean(((Boolean) rtf).booleanValue()).str();
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
      else if (rtf instanceof Double)
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
        textNodeValue = new XNumber(((Double) rtf).doubleValue()).str();
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
      else
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
        textNodeValue = rtf.toString();
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
      // This no longer will work right since the DTM.
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
      // Document myDoc = myProcessor.getContextNode().getOwnerDocument();
22417
278570df46ad 8027201: Enhance JAX-P set up
joehw
parents: 22416
diff changeset
   114
      Document myDoc = getDocument();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
        Text textNode = myDoc.createTextNode(textNodeValue);
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
        DocumentFragment docFrag = myDoc.createDocumentFragment();
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
        docFrag.appendChild(textNode);
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
22417
278570df46ad 8027201: Enhance JAX-P set up
joehw
parents: 22416
diff changeset
   121
      return new NodeSet(docFrag);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
   * Returns the intersection of two node-sets.
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
   * @param nl1 NodeList for first node-set
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
   * @param nl2 NodeList for second node-set
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
   * @return a NodeList containing the nodes in nl1 that are also in nl2
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
   * Note: The usage of this extension function in the xalan namespace
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
   * is deprecated. Please use the same function in the EXSLT sets extension
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
   * (http://exslt.org/sets).
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
  public static NodeList intersection(NodeList nl1, NodeList nl2)
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
    return ExsltSets.intersection(nl1, nl2);
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
   * Returns the difference between two node-sets.
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
   * @param nl1 NodeList for first node-set
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
   * @param nl2 NodeList for second node-set
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
   * @return a NodeList containing the nodes in nl1 that are not in nl2
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
   * Note: The usage of this extension function in the xalan namespace
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
   * is deprecated. Please use the same function in the EXSLT sets extension
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
   * (http://exslt.org/sets).
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
  public static NodeList difference(NodeList nl1, NodeList nl2)
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
    return ExsltSets.difference(nl1, nl2);
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
   * Returns node-set containing distinct string values.
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
   * @param nl NodeList for node-set
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
   * @return a NodeList with nodes from nl containing distinct string values.
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
   * In other words, if more than one node in nl contains the same string value,
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
   * only include the first such node found.
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
   * Note: The usage of this extension function in the xalan namespace
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
   * is deprecated. Please use the same function in the EXSLT sets extension
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
   * (http://exslt.org/sets).
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
  public static NodeList distinct(NodeList nl)
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
    return ExsltSets.distinct(nl);
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
   * Returns true if both node-sets contain the same set of nodes.
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
   * @param nl1 NodeList for first node-set
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
   * @param nl2 NodeList for second node-set
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
   * @return true if nl1 and nl2 contain exactly the same set of nodes.
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
  public static boolean hasSameNodes(NodeList nl1, NodeList nl2)
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
    NodeSet ns1 = new NodeSet(nl1);
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
    NodeSet ns2 = new NodeSet(nl2);
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
    if (ns1.getLength() != ns2.getLength())
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
      return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
    for (int i = 0; i < ns1.getLength(); i++)
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
      Node n = ns1.elementAt(i);
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
      if (!ns2.contains(n))
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
        return false;
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
    return true;
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
   * Returns the result of evaluating the argument as a string containing
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
   * an XPath expression.  Used where the XPath expression is not known until
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
   * run-time.  The expression is evaluated as if the run-time value of the
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
   * argument appeared in place of the evaluate function call at compile time.
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
   * @param myContext an <code>ExpressionContext</code> passed in by the
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
   *                  extension mechanism.  This must be an XPathContext.
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
   * @param xpathExpr The XPath expression to be evaluated.
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
   * @return the XObject resulting from evaluating the XPath
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
   * @throws SAXNotSupportedException
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
   * Note: The usage of this extension function in the xalan namespace
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
   * is deprecated. Please use the same function in the EXSLT dynamic extension
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
   * (http://exslt.org/dynamic).
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
  public static XObject evaluate(ExpressionContext myContext, String xpathExpr)
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
         throws SAXNotSupportedException
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
    return ExsltDynamic.evaluate(myContext, xpathExpr);
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
   * Returns a NodeSet containing one text node for each token in the first argument.
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
   * Delimiters are specified in the second argument.
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
   * Tokens are determined by a call to <code>StringTokenizer</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
   * If the first argument is an empty string or contains only delimiters, the result
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
   * will be an empty NodeSet.
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
   * Contributed to XalanJ1 by <a href="mailto:benoit.cerrina@writeme.com">Benoit Cerrina</a>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
   * @param toTokenize The string to be split into text tokens.
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
   * @param delims The delimiters to use.
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
   * @return a NodeSet as described above.
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
  public static NodeList tokenize(String toTokenize, String delims)
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
22417
278570df46ad 8027201: Enhance JAX-P set up
joehw
parents: 22416
diff changeset
   240
    Document doc = getDocument();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
    StringTokenizer lTokenizer = new StringTokenizer(toTokenize, delims);
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
    NodeSet resultSet = new NodeSet();
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
    synchronized (doc)
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
    {
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
      while (lTokenizer.hasMoreTokens())
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
      {
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
        resultSet.addNode(doc.createTextNode(lTokenizer.nextToken()));
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
      }
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
    return resultSet;
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
   * Returns a NodeSet containing one text node for each token in the first argument.
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
   * Delimiters are whitespace.  That is, the delimiters that are used are tab (&#x09),
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
   * linefeed (&#x0A), return (&#x0D), and space (&#x20).
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
   * Tokens are determined by a call to <code>StringTokenizer</code>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
   * If the first argument is an empty string or contains only delimiters, the result
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
   * will be an empty NodeSet.
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
   * Contributed to XalanJ1 by <a href="mailto:benoit.cerrina@writeme.com">Benoit Cerrina</a>.
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
   * @param toTokenize The string to be split into text tokens.
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
   * @return a NodeSet as described above.
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
  public static NodeList tokenize(String toTokenize)
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
    return tokenize(toTokenize, " \t\n\r");
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
    /**
22417
278570df46ad 8027201: Enhance JAX-P set up
joehw
parents: 22416
diff changeset
   275
   * @return an instance of DOM Document
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
     */
22417
278570df46ad 8027201: Enhance JAX-P set up
joehw
parents: 22416
diff changeset
   277
   private static Document getDocument()
278570df46ad 8027201: Enhance JAX-P set up
joehw
parents: 22416
diff changeset
   278
   {
278570df46ad 8027201: Enhance JAX-P set up
joehw
parents: 22416
diff changeset
   279
        try
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
        {
22417
278570df46ad 8027201: Enhance JAX-P set up
joehw
parents: 22416
diff changeset
   281
            if (System.getSecurityManager() == null) {
278570df46ad 8027201: Enhance JAX-P set up
joehw
parents: 22416
diff changeset
   282
                return DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
278570df46ad 8027201: Enhance JAX-P set up
joehw
parents: 22416
diff changeset
   283
            } else {
278570df46ad 8027201: Enhance JAX-P set up
joehw
parents: 22416
diff changeset
   284
                return DocumentBuilderFactory.newInstance(JDK_DEFAULT_DOM, null).newDocumentBuilder().newDocument();
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
            }
22417
278570df46ad 8027201: Enhance JAX-P set up
joehw
parents: 22416
diff changeset
   286
        }
278570df46ad 8027201: Enhance JAX-P set up
joehw
parents: 22416
diff changeset
   287
        catch(ParserConfigurationException pce)
278570df46ad 8027201: Enhance JAX-P set up
joehw
parents: 22416
diff changeset
   288
        {
278570df46ad 8027201: Enhance JAX-P set up
joehw
parents: 22416
diff changeset
   289
            throw new com.sun.org.apache.xml.internal.utils.WrappedRuntimeException(pce);
6
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
        }
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
    }
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
}