jaxp/src/com/sun/org/apache/xpath/internal/CachedXPathAPI.java
author joehw
Thu, 12 Apr 2012 08:38:26 -0700
changeset 12457 c348e06f0e82
parent 6 jaxp/src/share/classes/com/sun/org/apache/xpath/internal/CachedXPathAPI.java@7f561c08de6b
permissions -rw-r--r--
7160496: Rename JDK8 JAXP source directory Summary: moving src/share/classes to src Reviewed-by: ohair
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: CachedXPathAPI.java,v 1.2.4.1 2005/09/10 03:47:42 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
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    40
 * These functions tend to be a little slow, since a number of objects must be
7f561c08de6b Initial load
duke
parents:
diff changeset
    41
 * created for each evaluation.  A faster way is to precompile the
7f561c08de6b Initial load
duke
parents:
diff changeset
    42
 * XPaths using the low-level API, and then just use the XPaths
7f561c08de6b Initial load
duke
parents:
diff changeset
    43
 * over and over.
7f561c08de6b Initial load
duke
parents:
diff changeset
    44
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    45
 * This is an alternative for the old XPathAPI class, which provided
7f561c08de6b Initial load
duke
parents:
diff changeset
    46
 * static methods for the purpose but had the drawback of
7f561c08de6b Initial load
duke
parents:
diff changeset
    47
 * instantiating a new XPathContext (and thus building a new DTMManager,
7f561c08de6b Initial load
duke
parents:
diff changeset
    48
 * and new DTMs) each time it was called. XPathAPIObject instead retains
7f561c08de6b Initial load
duke
parents:
diff changeset
    49
 * its context as long as the object persists, reusing the DTMs. This
7f561c08de6b Initial load
duke
parents:
diff changeset
    50
 * does have a downside: if you've changed your source document, you should
7f561c08de6b Initial load
duke
parents:
diff changeset
    51
 * obtain a new XPathAPIObject to continue searching it, since trying to use
7f561c08de6b Initial load
duke
parents:
diff changeset
    52
 * the old DTMs will probably yield bad results or malfunction outright... and
7f561c08de6b Initial load
duke
parents:
diff changeset
    53
 * the cached DTMs may consume memory until this object and its context are
7f561c08de6b Initial load
duke
parents:
diff changeset
    54
 * returned to the heap. Essentially, it's the caller's responsibility to
7f561c08de6b Initial load
duke
parents:
diff changeset
    55
 * decide when to discard the cache.
7f561c08de6b Initial load
duke
parents:
diff changeset
    56
 *
7f561c08de6b Initial load
duke
parents:
diff changeset
    57
 * @see <a href="http://www.w3.org/TR/xpath">XPath Specification</a>
7f561c08de6b Initial load
duke
parents:
diff changeset
    58
 * */
7f561c08de6b Initial load
duke
parents:
diff changeset
    59
public class CachedXPathAPI
7f561c08de6b Initial load
duke
parents:
diff changeset
    60
{
7f561c08de6b Initial load
duke
parents:
diff changeset
    61
  /** XPathContext, and thus the document model system (DTMs), persists through multiple
7f561c08de6b Initial load
duke
parents:
diff changeset
    62
      calls to this object. This is set in the constructor.
7f561c08de6b Initial load
duke
parents:
diff changeset
    63
  */
7f561c08de6b Initial load
duke
parents:
diff changeset
    64
  protected XPathContext xpathSupport;
7f561c08de6b Initial load
duke
parents:
diff changeset
    65
7f561c08de6b Initial load
duke
parents:
diff changeset
    66
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    67
   * <p>Default constructor. Establishes its own {@link XPathContext}, and hence
7f561c08de6b Initial load
duke
parents:
diff changeset
    68
   * its own {@link com.sun.org.apache.xml.internal.dtm.DTMManager}.
7f561c08de6b Initial load
duke
parents:
diff changeset
    69
   * Good choice for simple uses.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    70
   * <p>Note that any particular instance of {@link CachedXPathAPI} must not be
7f561c08de6b Initial load
duke
parents:
diff changeset
    71
   * operated upon by multiple threads without synchronization; we do
7f561c08de6b Initial load
duke
parents:
diff changeset
    72
   * not currently support multithreaded access to a single
7f561c08de6b Initial load
duke
parents:
diff changeset
    73
   * {@link com.sun.org.apache.xml.internal.dtm.DTM}.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    74
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    75
  public CachedXPathAPI()
7f561c08de6b Initial load
duke
parents:
diff changeset
    76
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    77
    xpathSupport = new XPathContext();
7f561c08de6b Initial load
duke
parents:
diff changeset
    78
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    79
7f561c08de6b Initial load
duke
parents:
diff changeset
    80
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
    81
   * <p>This constructor shares its {@link XPathContext} with a pre-existing
7f561c08de6b Initial load
duke
parents:
diff changeset
    82
   * {@link CachedXPathAPI}.  That allows sharing document models
7f561c08de6b Initial load
duke
parents:
diff changeset
    83
   * ({@link com.sun.org.apache.xml.internal.dtm.DTM}) and previously established location
7f561c08de6b Initial load
duke
parents:
diff changeset
    84
   * state.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    85
   * <p>Note that the original {@link CachedXPathAPI} and the new one should
7f561c08de6b Initial load
duke
parents:
diff changeset
    86
   * not be operated upon concurrently; we do not support multithreaded access
7f561c08de6b Initial load
duke
parents:
diff changeset
    87
   * to a single {@link com.sun.org.apache.xml.internal.dtm.DTM} at this time.  Similarly,
7f561c08de6b Initial load
duke
parents:
diff changeset
    88
   * any particular instance of {@link CachedXPathAPI} must not be operated
7f561c08de6b Initial load
duke
parents:
diff changeset
    89
   * upon by multiple threads without synchronization.</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    90
   * <p>%REVIEW% Should this instead do a clone-and-reset on the XPathSupport object?</p>
7f561c08de6b Initial load
duke
parents:
diff changeset
    91
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
    92
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
    93
  public CachedXPathAPI(CachedXPathAPI priorXPathAPI)
7f561c08de6b Initial load
duke
parents:
diff changeset
    94
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
    95
    xpathSupport = priorXPathAPI.xpathSupport;
7f561c08de6b Initial load
duke
parents:
diff changeset
    96
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
    97
7f561c08de6b Initial load
duke
parents:
diff changeset
    98
7f561c08de6b Initial load
duke
parents:
diff changeset
    99
  /** Returns the XPathSupport object used in this CachedXPathAPI
7f561c08de6b Initial load
duke
parents:
diff changeset
   100
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   101
   * %REVIEW% I'm somewhat concerned about the loss of encapsulation
7f561c08de6b Initial load
duke
parents:
diff changeset
   102
   * this causes, but the xml-security folks say they need it.
7f561c08de6b Initial load
duke
parents:
diff changeset
   103
   * */
7f561c08de6b Initial load
duke
parents:
diff changeset
   104
  public XPathContext getXPathContext()
7f561c08de6b Initial load
duke
parents:
diff changeset
   105
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   106
    return this.xpathSupport;
7f561c08de6b Initial load
duke
parents:
diff changeset
   107
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   108
7f561c08de6b Initial load
duke
parents:
diff changeset
   109
7f561c08de6b Initial load
duke
parents:
diff changeset
   110
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   111
   * Use an XPath string to select a single node. XPath namespace
7f561c08de6b Initial load
duke
parents:
diff changeset
   112
   * prefixes are resolved from the context node, which may not
7f561c08de6b Initial load
duke
parents:
diff changeset
   113
   * be what you want (see the next method).
7f561c08de6b Initial load
duke
parents:
diff changeset
   114
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   115
   * @param contextNode The node to start searching from.
7f561c08de6b Initial load
duke
parents:
diff changeset
   116
   * @param str A valid XPath string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   117
   * @return The first node found that matches the XPath, or null.
7f561c08de6b Initial load
duke
parents:
diff changeset
   118
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   119
   * @throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   120
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   121
  public  Node selectSingleNode(Node contextNode, String str)
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
    return selectSingleNode(contextNode, str, contextNode);
7f561c08de6b Initial load
duke
parents:
diff changeset
   125
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   126
7f561c08de6b Initial load
duke
parents:
diff changeset
   127
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   128
   * Use an XPath string to select a single node.
7f561c08de6b Initial load
duke
parents:
diff changeset
   129
   * XPath namespace prefixes are resolved from the namespaceNode.
7f561c08de6b Initial load
duke
parents:
diff changeset
   130
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   131
   * @param contextNode The node to start searching from.
7f561c08de6b Initial load
duke
parents:
diff changeset
   132
   * @param str A valid XPath string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   133
   * @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
7f561c08de6b Initial load
duke
parents:
diff changeset
   134
   * @return The first node found that matches the XPath, or null.
7f561c08de6b Initial load
duke
parents:
diff changeset
   135
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   136
   * @throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   137
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   138
  public  Node selectSingleNode(
7f561c08de6b Initial load
duke
parents:
diff changeset
   139
          Node contextNode, String str, Node namespaceNode)
7f561c08de6b Initial load
duke
parents:
diff changeset
   140
            throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   141
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   142
7f561c08de6b Initial load
duke
parents:
diff changeset
   143
    // Have the XObject return its result as a NodeSetDTM.
7f561c08de6b Initial load
duke
parents:
diff changeset
   144
    NodeIterator nl = selectNodeIterator(contextNode, str, namespaceNode);
7f561c08de6b Initial load
duke
parents:
diff changeset
   145
7f561c08de6b Initial load
duke
parents:
diff changeset
   146
    // Return the first node, or null
7f561c08de6b Initial load
duke
parents:
diff changeset
   147
    return nl.nextNode();
7f561c08de6b Initial load
duke
parents:
diff changeset
   148
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   149
7f561c08de6b Initial load
duke
parents:
diff changeset
   150
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   151
   *  Use an XPath string to select a nodelist.
7f561c08de6b Initial load
duke
parents:
diff changeset
   152
   *  XPath namespace prefixes are resolved from the contextNode.
7f561c08de6b Initial load
duke
parents:
diff changeset
   153
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   154
   *  @param contextNode The node to start searching from.
7f561c08de6b Initial load
duke
parents:
diff changeset
   155
   *  @param str A valid XPath string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   156
   *  @return A NodeIterator, should never be null.
7f561c08de6b Initial load
duke
parents:
diff changeset
   157
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   158
   * @throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   159
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   160
  public  NodeIterator selectNodeIterator(Node contextNode, String str)
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
    return selectNodeIterator(contextNode, str, contextNode);
7f561c08de6b Initial load
duke
parents:
diff changeset
   164
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   165
7f561c08de6b Initial load
duke
parents:
diff changeset
   166
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   167
   *  Use an XPath string to select a nodelist.
7f561c08de6b Initial load
duke
parents:
diff changeset
   168
   *  XPath namespace prefixes are resolved from the namespaceNode.
7f561c08de6b Initial load
duke
parents:
diff changeset
   169
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   170
   *  @param contextNode The node to start searching from.
7f561c08de6b Initial load
duke
parents:
diff changeset
   171
   *  @param str A valid XPath string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   172
   *  @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
7f561c08de6b Initial load
duke
parents:
diff changeset
   173
   *  @return A NodeIterator, should never be null.
7f561c08de6b Initial load
duke
parents:
diff changeset
   174
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   175
   * @throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   176
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   177
  public  NodeIterator selectNodeIterator(
7f561c08de6b Initial load
duke
parents:
diff changeset
   178
          Node contextNode, String str, Node namespaceNode)
7f561c08de6b Initial load
duke
parents:
diff changeset
   179
            throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   180
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   181
7f561c08de6b Initial load
duke
parents:
diff changeset
   182
    // Execute the XPath, and have it return the result
7f561c08de6b Initial load
duke
parents:
diff changeset
   183
    XObject list = eval(contextNode, str, namespaceNode);
7f561c08de6b Initial load
duke
parents:
diff changeset
   184
7f561c08de6b Initial load
duke
parents:
diff changeset
   185
    // Have the XObject return its result as a NodeSetDTM.
7f561c08de6b Initial load
duke
parents:
diff changeset
   186
    return list.nodeset();
7f561c08de6b Initial load
duke
parents:
diff changeset
   187
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   188
7f561c08de6b Initial load
duke
parents:
diff changeset
   189
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   190
   *  Use an XPath string to select a nodelist.
7f561c08de6b Initial load
duke
parents:
diff changeset
   191
   *  XPath namespace prefixes are resolved from the contextNode.
7f561c08de6b Initial load
duke
parents:
diff changeset
   192
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   193
   *  @param contextNode The node to start searching from.
7f561c08de6b Initial load
duke
parents:
diff changeset
   194
   *  @param str A valid XPath string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   195
   *  @return A NodeIterator, should never be null.
7f561c08de6b Initial load
duke
parents:
diff changeset
   196
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   197
   * @throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   198
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   199
  public  NodeList selectNodeList(Node contextNode, String str)
7f561c08de6b Initial load
duke
parents:
diff changeset
   200
          throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   201
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   202
    return selectNodeList(contextNode, str, contextNode);
7f561c08de6b Initial load
duke
parents:
diff changeset
   203
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   204
7f561c08de6b Initial load
duke
parents:
diff changeset
   205
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   206
   *  Use an XPath string to select a nodelist.
7f561c08de6b Initial load
duke
parents:
diff changeset
   207
   *  XPath namespace prefixes are resolved from the namespaceNode.
7f561c08de6b Initial load
duke
parents:
diff changeset
   208
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   209
   *  @param contextNode The node to start searching from.
7f561c08de6b Initial load
duke
parents:
diff changeset
   210
   *  @param str A valid XPath string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   211
   *  @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
7f561c08de6b Initial load
duke
parents:
diff changeset
   212
   *  @return A NodeIterator, should never be null.
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  NodeList selectNodeList(
7f561c08de6b Initial load
duke
parents:
diff changeset
   217
          Node contextNode, String str, Node namespaceNode)
7f561c08de6b Initial load
duke
parents:
diff changeset
   218
            throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   219
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   220
7f561c08de6b Initial load
duke
parents:
diff changeset
   221
    // Execute the XPath, and have it return the result
7f561c08de6b Initial load
duke
parents:
diff changeset
   222
    XObject list = eval(contextNode, str, namespaceNode);
7f561c08de6b Initial load
duke
parents:
diff changeset
   223
7f561c08de6b Initial load
duke
parents:
diff changeset
   224
    // Return a NodeList.
7f561c08de6b Initial load
duke
parents:
diff changeset
   225
    return list.nodelist();
7f561c08de6b Initial load
duke
parents:
diff changeset
   226
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   227
7f561c08de6b Initial load
duke
parents:
diff changeset
   228
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   229
   *  Evaluate XPath string to an XObject.  Using this method,
7f561c08de6b Initial load
duke
parents:
diff changeset
   230
   *  XPath namespace prefixes will be resolved from the namespaceNode.
7f561c08de6b Initial load
duke
parents:
diff changeset
   231
   *  @param contextNode The node to start searching from.
7f561c08de6b Initial load
duke
parents:
diff changeset
   232
   *  @param str A valid XPath string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   233
   *  @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
   234
   *  @see com.sun.org.apache.xpath.internal.objects.XObject
7f561c08de6b Initial load
duke
parents:
diff changeset
   235
   *  @see com.sun.org.apache.xpath.internal.objects.XNull
7f561c08de6b Initial load
duke
parents:
diff changeset
   236
   *  @see com.sun.org.apache.xpath.internal.objects.XBoolean
7f561c08de6b Initial load
duke
parents:
diff changeset
   237
   *  @see com.sun.org.apache.xpath.internal.objects.XNumber
7f561c08de6b Initial load
duke
parents:
diff changeset
   238
   *  @see com.sun.org.apache.xpath.internal.objects.XString
7f561c08de6b Initial load
duke
parents:
diff changeset
   239
   *  @see com.sun.org.apache.xpath.internal.objects.XRTreeFrag
7f561c08de6b Initial load
duke
parents:
diff changeset
   240
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   241
   * @throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   242
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   243
  public  XObject eval(Node contextNode, String str)
7f561c08de6b Initial load
duke
parents:
diff changeset
   244
          throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   245
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   246
    return eval(contextNode, str, contextNode);
7f561c08de6b Initial load
duke
parents:
diff changeset
   247
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   248
7f561c08de6b Initial load
duke
parents:
diff changeset
   249
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   250
   *  Evaluate XPath string to an XObject.
7f561c08de6b Initial load
duke
parents:
diff changeset
   251
   *  XPath namespace prefixes are resolved from the namespaceNode.
7f561c08de6b Initial load
duke
parents:
diff changeset
   252
   *  The implementation of this is a little slow, since it creates
7f561c08de6b Initial load
duke
parents:
diff changeset
   253
   *  a number of objects each time it is called.  This could be optimized
7f561c08de6b Initial load
duke
parents:
diff changeset
   254
   *  to keep the same objects around, but then thread-safety issues would arise.
7f561c08de6b Initial load
duke
parents:
diff changeset
   255
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   256
   *  @param contextNode The node to start searching from.
7f561c08de6b Initial load
duke
parents:
diff changeset
   257
   *  @param str A valid XPath string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   258
   *  @param namespaceNode The node from which prefixes in the XPath will be resolved to namespaces.
7f561c08de6b Initial load
duke
parents:
diff changeset
   259
   *  @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
   260
   *  @see com.sun.org.apache.xpath.internal.objects.XObject
7f561c08de6b Initial load
duke
parents:
diff changeset
   261
   *  @see com.sun.org.apache.xpath.internal.objects.XNull
7f561c08de6b Initial load
duke
parents:
diff changeset
   262
   *  @see com.sun.org.apache.xpath.internal.objects.XBoolean
7f561c08de6b Initial load
duke
parents:
diff changeset
   263
   *  @see com.sun.org.apache.xpath.internal.objects.XNumber
7f561c08de6b Initial load
duke
parents:
diff changeset
   264
   *  @see com.sun.org.apache.xpath.internal.objects.XString
7f561c08de6b Initial load
duke
parents:
diff changeset
   265
   *  @see com.sun.org.apache.xpath.internal.objects.XRTreeFrag
7f561c08de6b Initial load
duke
parents:
diff changeset
   266
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   267
   * @throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   268
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   269
  public  XObject eval(Node contextNode, String str, Node namespaceNode)
7f561c08de6b Initial load
duke
parents:
diff changeset
   270
          throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   271
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   272
7f561c08de6b Initial load
duke
parents:
diff changeset
   273
    // Since we don't have a XML Parser involved here, install some default support
7f561c08de6b Initial load
duke
parents:
diff changeset
   274
    // for things like namespaces, etc.
7f561c08de6b Initial load
duke
parents:
diff changeset
   275
    // (Changed from: XPathContext xpathSupport = new XPathContext();
7f561c08de6b Initial load
duke
parents:
diff changeset
   276
    //    because XPathContext is weak in a number of areas... perhaps
7f561c08de6b Initial load
duke
parents:
diff changeset
   277
    //    XPathContext should be done away with.)
7f561c08de6b Initial load
duke
parents:
diff changeset
   278
7f561c08de6b Initial load
duke
parents:
diff changeset
   279
    // Create an object to resolve namespace prefixes.
7f561c08de6b Initial load
duke
parents:
diff changeset
   280
    // XPath namespaces are resolved from the input context node's document element
7f561c08de6b Initial load
duke
parents:
diff changeset
   281
    // if it is a root node, or else the current context node (for lack of a better
7f561c08de6b Initial load
duke
parents:
diff changeset
   282
    // resolution space, given the simplicity of this sample code).
7f561c08de6b Initial load
duke
parents:
diff changeset
   283
    PrefixResolverDefault prefixResolver = new PrefixResolverDefault(
7f561c08de6b Initial load
duke
parents:
diff changeset
   284
      (namespaceNode.getNodeType() == Node.DOCUMENT_NODE)
7f561c08de6b Initial load
duke
parents:
diff changeset
   285
      ? ((Document) namespaceNode).getDocumentElement() : namespaceNode);
7f561c08de6b Initial load
duke
parents:
diff changeset
   286
7f561c08de6b Initial load
duke
parents:
diff changeset
   287
    // Create the XPath object.
7f561c08de6b Initial load
duke
parents:
diff changeset
   288
    XPath xpath = new XPath(str, null, prefixResolver, XPath.SELECT, null);
7f561c08de6b Initial load
duke
parents:
diff changeset
   289
7f561c08de6b Initial load
duke
parents:
diff changeset
   290
    // Execute the XPath, and have it return the result
7f561c08de6b Initial load
duke
parents:
diff changeset
   291
    // return xpath.execute(xpathSupport, contextNode, prefixResolver);
7f561c08de6b Initial load
duke
parents:
diff changeset
   292
    int ctxtNode = xpathSupport.getDTMHandleFromNode(contextNode);
7f561c08de6b Initial load
duke
parents:
diff changeset
   293
7f561c08de6b Initial load
duke
parents:
diff changeset
   294
    return xpath.execute(xpathSupport, ctxtNode, prefixResolver);
7f561c08de6b Initial load
duke
parents:
diff changeset
   295
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   296
7f561c08de6b Initial load
duke
parents:
diff changeset
   297
  /**
7f561c08de6b Initial load
duke
parents:
diff changeset
   298
   *   Evaluate XPath string to an XObject.
7f561c08de6b Initial load
duke
parents:
diff changeset
   299
   *   XPath namespace prefixes are resolved from the namespaceNode.
7f561c08de6b Initial load
duke
parents:
diff changeset
   300
   *   The implementation of this is a little slow, since it creates
7f561c08de6b Initial load
duke
parents:
diff changeset
   301
   *   a number of objects each time it is called.  This could be optimized
7f561c08de6b Initial load
duke
parents:
diff changeset
   302
   *   to keep the same objects around, but then thread-safety issues would arise.
7f561c08de6b Initial load
duke
parents:
diff changeset
   303
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   304
   *   @param contextNode The node to start searching from.
7f561c08de6b Initial load
duke
parents:
diff changeset
   305
   *   @param str A valid XPath string.
7f561c08de6b Initial load
duke
parents:
diff changeset
   306
   *   @param prefixResolver Will be called if the parser encounters namespace
7f561c08de6b Initial load
duke
parents:
diff changeset
   307
   *                         prefixes, to resolve the prefixes to URLs.
7f561c08de6b Initial load
duke
parents:
diff changeset
   308
   *   @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
   309
   *   @see com.sun.org.apache.xpath.internal.objects.XObject
7f561c08de6b Initial load
duke
parents:
diff changeset
   310
   *   @see com.sun.org.apache.xpath.internal.objects.XNull
7f561c08de6b Initial load
duke
parents:
diff changeset
   311
   *   @see com.sun.org.apache.xpath.internal.objects.XBoolean
7f561c08de6b Initial load
duke
parents:
diff changeset
   312
   *   @see com.sun.org.apache.xpath.internal.objects.XNumber
7f561c08de6b Initial load
duke
parents:
diff changeset
   313
   *   @see com.sun.org.apache.xpath.internal.objects.XString
7f561c08de6b Initial load
duke
parents:
diff changeset
   314
   *   @see com.sun.org.apache.xpath.internal.objects.XRTreeFrag
7f561c08de6b Initial load
duke
parents:
diff changeset
   315
   *
7f561c08de6b Initial load
duke
parents:
diff changeset
   316
   * @throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   317
   */
7f561c08de6b Initial load
duke
parents:
diff changeset
   318
  public  XObject eval(
7f561c08de6b Initial load
duke
parents:
diff changeset
   319
          Node contextNode, String str, PrefixResolver prefixResolver)
7f561c08de6b Initial load
duke
parents:
diff changeset
   320
            throws TransformerException
7f561c08de6b Initial load
duke
parents:
diff changeset
   321
  {
7f561c08de6b Initial load
duke
parents:
diff changeset
   322
7f561c08de6b Initial load
duke
parents:
diff changeset
   323
    // Since we don't have a XML Parser involved here, install some default support
7f561c08de6b Initial load
duke
parents:
diff changeset
   324
    // for things like namespaces, etc.
7f561c08de6b Initial load
duke
parents:
diff changeset
   325
    // (Changed from: XPathContext xpathSupport = new XPathContext();
7f561c08de6b Initial load
duke
parents:
diff changeset
   326
    //    because XPathContext is weak in a number of areas... perhaps
7f561c08de6b Initial load
duke
parents:
diff changeset
   327
    //    XPathContext should be done away with.)
7f561c08de6b Initial load
duke
parents:
diff changeset
   328
    // Create the XPath object.
7f561c08de6b Initial load
duke
parents:
diff changeset
   329
    XPath xpath = new XPath(str, null, prefixResolver, XPath.SELECT, null);
7f561c08de6b Initial load
duke
parents:
diff changeset
   330
7f561c08de6b Initial load
duke
parents:
diff changeset
   331
    // Execute the XPath, and have it return the result
7f561c08de6b Initial load
duke
parents:
diff changeset
   332
    XPathContext xpathSupport = new XPathContext();
7f561c08de6b Initial load
duke
parents:
diff changeset
   333
    int ctxtNode = xpathSupport.getDTMHandleFromNode(contextNode);
7f561c08de6b Initial load
duke
parents:
diff changeset
   334
7f561c08de6b Initial load
duke
parents:
diff changeset
   335
    return xpath.execute(xpathSupport, ctxtNode, prefixResolver);
7f561c08de6b Initial load
duke
parents:
diff changeset
   336
  }
7f561c08de6b Initial load
duke
parents:
diff changeset
   337
}