jaxp/src/com/sun/org/apache/xpath/internal/XPathAPI.java
author lana
Tue, 18 Mar 2014 17:49:48 -0700 (2014-03-19)
changeset 23377 2af1ddf102a4
parent 12457 c348e06f0e82
permissions -rw-r--r--
Merge
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: XPathAPI.java,v 1.2.4.1 2005/09/10 18:18:23 jeffsuttor Exp $
7f561c08de6b Initial load
duke
parents:
diff changeset
    22
 */
7f561c08de6b Initial load
duke
parents:
diff changeset
    23
package com.sun.org.apache.xpath.internal;
7f561c08de6b Initial load
duke
parents:
diff changeset
    24
7f561c08de6b Initial load
duke
parents:
diff changeset
    25
import javax.xml.transform.TransformerException;
7f561c08de6b Initial load
duke
parents:
diff changeset
    26
7f561c08de6b Initial load
duke
parents:
diff changeset
    27
import com.sun.org.apache.xml.internal.utils.PrefixResolver;
7f561c08de6b Initial load
duke
parents:
diff changeset
    28
import com.sun.org.apache.xml.internal.utils.PrefixResolverDefault;
7f561c08de6b Initial load
duke
parents:
diff changeset
    29
import com.sun.org.apache.xpath.internal.objects.XObject;
7f561c08de6b Initial load
duke
parents:
diff changeset
    30
7f561c08de6b Initial load
duke
parents:
diff changeset
    31
import org.w3c.dom.Document;
7f561c08de6b Initial load
duke
parents:
diff changeset
    32
import org.w3c.dom.Node;
7f561c08de6b Initial load
duke
parents:
diff changeset
    33
import org.w3c.dom.NodeList;
7f561c08de6b Initial load
duke
parents:
diff changeset
    34
import org.w3c.dom.traversal.NodeIterator;
7f561c08de6b Initial load
duke
parents:
diff changeset
    35
7f561c08de6b Initial load
duke
parents:
diff changeset
    36
/**
7f561c08de6b Initial load
duke
parents:
diff changeset
    37
 * The methods in this class are convenience methods into the
7f561c08de6b Initial load
duke
parents:
diff changeset
    38
 * low-level XPath API.
7f561c08de6b Initial load
duke
parents:
diff changeset
    39
 * These functions tend to be a little slow, since a number of objects must be
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
 * created for each evaluation.  A faster way is to precompile the
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
 * XPaths using the low-level API, and then just use the XPaths
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
 * over and over.
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
 * NOTE: In particular, each call to this method will create a new
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
 * XPathContext, a new DTMManager... and thus a new DTM. That's very
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
 * safe, since it guarantees that you're always processing against a
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
 * fully up-to-date view of your document. But it's also portentially
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
 * very expensive, since you're rebuilding the DTM every time. You should
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
 * consider using an instance of CachedXPathAPI rather than these static
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
 * methods.
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
 * @see <a href="http://www.w3.org/TR/xpath">XPath Specification</a>
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
 * */
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
public class XPathAPI
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
{
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
   * Use an XPath string to select a single node. XPath namespace
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
   * prefixes are resolved from the context node, which may not
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
   * be what you want (see the next method).
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
   * @param contextNode The node to start searching from.
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
   * @param str A valid XPath string.
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
   * @return The first node found that matches the XPath, or null.
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
   * @throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
  public static Node selectSingleNode(Node contextNode, String str)
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
          throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
    return selectSingleNode(contextNode, str, contextNode);
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
   * Use an XPath string to select a single node.
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
   * XPath namespace prefixes are resolved from the namespaceNode.
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
   * @param contextNode The node to start searching from.
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
   * @param str A valid XPath string.
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
   * @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
   * @return The first node found that matches the XPath, or null.
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
   * @throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
  public static Node selectSingleNode(
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
          Node contextNode, String str, Node namespaceNode)
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
            throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
    // Have the XObject return its result as a NodeSetDTM.
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
    NodeIterator nl = selectNodeIterator(contextNode, str, namespaceNode);
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
    // Return the first node, or null
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
    return nl.nextNode();
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
   *  Use an XPath string to select a nodelist.
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
   *  XPath namespace prefixes are resolved from the contextNode.
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
   *  @param contextNode The node to start searching from.
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
   *  @param str A valid XPath string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
   *  @return A NodeIterator, should never be null.
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
   * @throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
  public static NodeIterator selectNodeIterator(Node contextNode, String str)
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
          throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
    return selectNodeIterator(contextNode, str, contextNode);
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
   *  Use an XPath string to select a nodelist.
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
   *  XPath namespace prefixes are resolved from the namespaceNode.
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
   *  @param contextNode The node to start searching from.
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
   *  @param str A valid XPath string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
   *  @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
   *  @return A NodeIterator, should never be null.
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   122
   * @throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   123
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   124
  public static NodeIterator selectNodeIterator(
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
          Node contextNode, String str, Node namespaceNode)
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
            throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
    // Execute the XPath, and have it return the result
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
    XObject list = eval(contextNode, str, namespaceNode);
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
    // Have the XObject return its result as a NodeSetDTM.
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
    return list.nodeset();
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
   *  Use an XPath string to select a nodelist.
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
   *  XPath namespace prefixes are resolved from the contextNode.
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
   *  @param contextNode The node to start searching from.
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
   *  @param str A valid XPath string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
   *  @return A NodeIterator, should never be null.
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
   * @throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
  public static NodeList selectNodeList(Node contextNode, String str)
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
          throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
    return selectNodeList(contextNode, str, contextNode);
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
   *  Use an XPath string to select a nodelist.
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
   *  XPath namespace prefixes are resolved from the namespaceNode.
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
   *  @param contextNode The node to start searching from.
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
   *  @param str A valid XPath string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
   *  @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
   *  @return A NodeIterator, should never be null.
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   161
   * @throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   162
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   163
  public static NodeList selectNodeList(
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
          Node contextNode, String str, Node namespaceNode)
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
            throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
    // Execute the XPath, and have it return the result
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
    XObject list = eval(contextNode, str, namespaceNode);
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
    // Return a NodeList.
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
    return list.nodelist();
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
   *  Evaluate XPath string to an XObject.  Using this method,
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
   *  XPath namespace prefixes will be resolved from the namespaceNode.
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
   *  @param contextNode The node to start searching from.
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
   *  @param str A valid XPath string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
   *  @return An XObject, which can be used to obtain a string, number, nodelist, etc, should never be null.
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
   *  @see com.sun.org.apache.xpath.internal.objects.XObject
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
   *  @see com.sun.org.apache.xpath.internal.objects.XNull
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
   *  @see com.sun.org.apache.xpath.internal.objects.XBoolean
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
   *  @see com.sun.org.apache.xpath.internal.objects.XNumber
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
   *  @see com.sun.org.apache.xpath.internal.objects.XString
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
   *  @see com.sun.org.apache.xpath.internal.objects.XRTreeFrag
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
   * @throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
  public static XObject eval(Node contextNode, String str)
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
          throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
    return eval(contextNode, str, contextNode);
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
   *  Evaluate XPath string to an XObject.
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
   *  XPath namespace prefixes are resolved from the namespaceNode.
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
   *  The implementation of this is a little slow, since it creates
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
   *  a number of objects each time it is called.  This could be optimized
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
   *  to keep the same objects around, but then thread-safety issues would arise.
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
   *  @param contextNode The node to start searching from.
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
   *  @param str A valid XPath string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
   *  @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
   *  @return An XObject, which can be used to obtain a string, number, nodelist, etc, should never be null.
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
   *  @see com.sun.org.apache.xpath.internal.objects.XObject
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
   *  @see com.sun.org.apache.xpath.internal.objects.XNull
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
   *  @see com.sun.org.apache.xpath.internal.objects.XBoolean
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
   *  @see com.sun.org.apache.xpath.internal.objects.XNumber
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
   *  @see com.sun.org.apache.xpath.internal.objects.XString
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
   *  @see com.sun.org.apache.xpath.internal.objects.XRTreeFrag
7f561c08de6b Initial load
duke
parents:
diff changeset
   213
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   214
   * @throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   215
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   216
  public static XObject eval(Node contextNode, String str, Node namespaceNode)
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
          throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
    // Since we don't have a XML Parser involved here, install some default support
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
    // for things like namespaces, etc.
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
    // (Changed from: XPathContext xpathSupport = new XPathContext();
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
    //    because XPathContext is weak in a number of areas... perhaps
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
    //    XPathContext should be done away with.)
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
    XPathContext xpathSupport = new XPathContext();
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
    // Create an object to resolve namespace prefixes.
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
    // XPath namespaces are resolved from the input context node's document element
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
    // if it is a root node, or else the current context node (for lack of a better
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
    // resolution space, given the simplicity of this sample code).
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
    PrefixResolverDefault prefixResolver = new PrefixResolverDefault(
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
      (namespaceNode.getNodeType() == Node.DOCUMENT_NODE)
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
      ? ((Document) namespaceNode).getDocumentElement() : namespaceNode);
7f561c08de6b Initial load
duke
parents:
diff changeset
   234
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
    // Create the XPath object.
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
    XPath xpath = new XPath(str, null, prefixResolver, XPath.SELECT, null);
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
    // Execute the XPath, and have it return the result
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
    // return xpath.execute(xpathSupport, contextNode, prefixResolver);
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
    int ctxtNode = xpathSupport.getDTMHandleFromNode(contextNode);
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
    return xpath.execute(xpathSupport, ctxtNode, prefixResolver);
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
   *   Evaluate XPath string to an XObject.
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
   *   XPath namespace prefixes are resolved from the namespaceNode.
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
   *   The implementation of this is a little slow, since it creates
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
   *   a number of objects each time it is called.  This could be optimized
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
   *   to keep the same objects around, but then thread-safety issues would arise.
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
   *   @param contextNode The node to start searching from.
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
   *   @param str A valid XPath string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
   *   @param prefixResolver Will be called if the parser encounters namespace
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
   *                         prefixes, to resolve the prefixes to URLs.
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
   *   @return An XObject, which can be used to obtain a string, number, nodelist, etc, should never be null.
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
   *   @see com.sun.org.apache.xpath.internal.objects.XObject
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
   *   @see com.sun.org.apache.xpath.internal.objects.XNull
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
   *   @see com.sun.org.apache.xpath.internal.objects.XBoolean
7f561c08de6b Initial load
duke
parents:
diff changeset
   260
   *   @see com.sun.org.apache.xpath.internal.objects.XNumber
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
   *   @see com.sun.org.apache.xpath.internal.objects.XString
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
   *   @see com.sun.org.apache.xpath.internal.objects.XRTreeFrag
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
   * @throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
  public static XObject eval(
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
          Node contextNode, String str, PrefixResolver prefixResolver)
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
            throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
    // Since we don't have a XML Parser involved here, install some default support
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
    // for things like namespaces, etc.
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
    // (Changed from: XPathContext xpathSupport = new XPathContext();
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
    //    because XPathContext is weak in a number of areas... perhaps
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
    //    XPathContext should be done away with.)
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
    // Create the XPath object.
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
    XPath xpath = new XPath(str, null, prefixResolver, XPath.SELECT, null);
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
    // Execute the XPath, and have it return the result
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
    XPathContext xpathSupport = new XPathContext();
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
    int ctxtNode = xpathSupport.getDTMHandleFromNode(contextNode);
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
    return xpath.execute(xpathSupport, ctxtNode, prefixResolver);
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
}