View Javadoc

    1   package org.jaxen;
    2
    3   /*
    4    $Id: Context.java,v 1.14 2005/06/01 11:19:23 elharo Exp $
    5
    6    Copyright 2003 (C) The Werken Company. All Rights Reserved.
    7
    8    Redistribution and use of this software and associated documentation
    9    ("Software"), with or without modification, are permitted provided
    10   that the following conditions are met:
    11
    12   1. Redistributions of source code must retain copyright
    13      statements and notices.  Redistributions must also contain a
    14      copy of this document.
    15
    16   2. Redistributions in binary form must reproduce the
    17      above copyright notice, this list of conditions and the
    18      following disclaimer in the documentation and/or other
    19      materials provided with the distribution.
    20
    21   3. The name "jaxen" must not be used to endorse or promote
    22      products derived from this Software without prior written
    23      permission of The Werken Company.  For written permission,
    24      please contact bob@werken.com.
    25
    26   4. Products derived from this Software may not be called "jaxen"
    27      nor may "jaxen" appear in their names without prior written
    28      permission of The Werken Company. "jaxen" is a registered
    29      trademark of The Werken Company.
    30
    31   5. Due credit should be given to The Werken Company.
    32      (http://jaxen.werken.com/).
    33
    34   THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS
    35   ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
    36   NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
    37   FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
    38   THE WERKEN COMPANY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
    39   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    40   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    41   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    42   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    43   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    44   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
    45   OF THE POSSIBILITY OF SUCH DAMAGE.
    46
    47   */
    48
    49  import java.io.Serializable;
    50  import java.util.ArrayList;
    51  import java.util.Collections;
    52  import java.util.List;
    53
    54  /*** Wrapper around implementation-specific objects used
    55   *  as the context of an expression evaluation.
    56   *
    57   *  <p>
    58   *  <strong>NOTE:</strong> This class is not typically used directly,
    59   *  but is exposed for writers of implementation-specific
    60   *  XPath packages.
    61   *  </p>
    62   *
    63   *  <p>
    64   *  The <code>Context</code> bundles utilities together
    65   *  for evaluation of the expression.  It wraps the provided
    66   *  objects for ease-of-passage through the expression AST.
    67   *  </p>
    68   *
    69   *  @see ContextSupport
    70   *  @see BaseXPath
    71   *  @see org.jaxen.dom4j.Dom4jXPath XPath for dom4j
    72   *  @see org.jaxen.jdom.JDOMXPath  XPath for JDOM
    73   *  @see org.jaxen.dom.DOMXPath   XPath for W3C DOM
    74   *
    75   *  @author <a href="mailto:bob@werken.com">bob mcwhirter</a>
    76   */
    77  public class Context
    78      implements Serializable
    79  {
    80      // ----------------------------------------------------------------------
    81      //     Instance members
    82      // ----------------------------------------------------------------------
    83
    84      /*** Context-support */
    85      private ContextSupport contextSupport;
    86
    87      /*** Context node-set */
    88      private List nodeSet;
    89
    90      /*** Current context size */
    91      private int size;
    92
    93      /*** Current context position */
    94      private int position;
    95
    96      // ----------------------------------------------------------------------
    97      //     Constructors
    98      // ----------------------------------------------------------------------
    99
    100     /*** Create a new context.
    101      *
    102      *  @param contextSupport the context-support
    103      */
    104     public Context(ContextSupport contextSupport)
    105     {
    106         this.contextSupport = contextSupport;
    107         this.nodeSet        = Collections.EMPTY_LIST;
    108     }
    109
    110     // ----------------------------------------------------------------------
    111     //     Instance methods
    112     // ----------------------------------------------------------------------
    113
    114     /*** Set the context node-set.
    115      *
    116      *  @param nodeSet the context node-set
    117      */
    118     public void setNodeSet(List nodeSet)
    119     {
    120         this.nodeSet = nodeSet;
    121     }
    122
    123     /*** Retrieve the context node-set.
    124      *
    125      *  @return the context node-set
    126      */
    127     public List getNodeSet()
    128     {
    129         return this.nodeSet;
    130     }
    131
    132     /*** Set the <code>ContextSupport</code>.
    133      *
    134      *  @param contextSupport the context-support
    135      */
    136     public void setContextSupport(ContextSupport contextSupport)
    137     {
    138         this.contextSupport = contextSupport;
    139     }
    140
    141     /*** Retrieve the <code>ContextSupport</code>.
    142      *
    143      *  @return the context-support
    144      */
    145     public ContextSupport getContextSupport()
    146     {
    147         return this.contextSupport;
    148     }
    149
    150     /*** Retrieve the current <code>Navigator</code>.
    151      *
    152      *  @return the navigator
    153      */
    154     public Navigator getNavigator()
    155     {
    156         return getContextSupport().getNavigator();
    157     }
    158
    159     /*** Translate a namespace prefix to its URI.
    160      *
    161      *  @param prefix the prefix
    162      *
    163      *  @return the namespace URI mapped to the prefix
    164      */
    165     public String translateNamespacePrefixToUri(String prefix)
    166     {
    167         return getContextSupport().translateNamespacePrefixToUri( prefix );
    168     }
    169
    170     /*** Retrieve a variable value.
    171      *
    172      *  @param namespaceURI the function namespace URI
    173      *  @param prefix the function prefix
    174      *  @param localName the function name
    175      *
    176      *  @return the variable value
    177      *
    178      *  @throws UnresolvableException if unable to locate a bound variable
    179      */
    180     public Object getVariableValue(String namespaceURI,
    181                                    String prefix,
    182                                    String localName)
    183         throws UnresolvableException
    184     {
    185         return getContextSupport().getVariableValue( namespaceURI,
    186                                                      prefix,
    187                                                      localName );
    188     }
    189
    190     /*** Retrieve a <code>Function</code>.
    191      *
    192      *  @param namespaceURI the function namespace URI
    193      *  @param prefix the function prefix
    194      *  @param localName the function name
    195      *
    196      *  @return the function object
    197      *
    198      *  @throws UnresolvableException if unable to locate a bound function
    199      */
    200     public Function getFunction(String namespaceURI,
    201                                 String prefix,
    202                                 String localName)
    203         throws UnresolvableException
    204     {
    205         return getContextSupport().getFunction( namespaceURI,
    206                                                 prefix,
    207                                                 localName );
    208     }
    209
    210     // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    211     //     Properties
    212     // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    213
    214     /*** Set the current size in the context node-set.
    215      *
    216      *  @param size the size
    217      */
    218     public void setSize(int size)
    219     {
    220         this.size = size;
    221     }
    222
    223     /*** Retrieve the size of the context node-set.
    224      *
    225      *  @return the size
    226      */
    227     public int getSize()
    228     {
    229         return this.size;
    230     }
    231
    232     /*** Set the current position in the context node-set.
    233      *
    234      *  @param position the position
    235      */
    236     public void setPosition(int position)
    237     {
    238         this.position = position;
    239     }
    240
    241     /*** Retrieve current position in the context node-set.
    242      *
    243      *  @return the current position
    244      */
    245     public int getPosition()
    246     {
    247         return this.position;
    248     }
    249
    250     // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    251     //     Helpers
    252     // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    253
    254     /*** Create a type-safe shallow copy.
    255      *
    256      *  @return the duplicate
    257      */
    258     public Context duplicate()
    259     {
    260         Context dupe = new Context( getContextSupport() );
    261
    262         List thisNodeSet = getNodeSet();
    263
    264         if ( thisNodeSet != null )
    265         {
    266             List dupeNodeSet = new ArrayList( thisNodeSet.size() );
    267             dupeNodeSet.addAll( thisNodeSet );
    268             dupe.setNodeSet( dupeNodeSet );
    269         }
    270
    271         return dupe;
    272     }
    273 }