DefaultNamespaceContext xref

View Javadoc

    1   /*
    2    * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    3    *
    4    * This software is open source.
    5    * See the bottom of this file for the licence.
    6    */
    7
    8   package org.dom4j.xpath;
    9
    10  import java.io.Serializable;
    11
    12  import org.dom4j.Document;
    13  import org.dom4j.Element;
    14  import org.dom4j.Namespace;
    15  import org.dom4j.Node;
    16
    17  import org.jaxen.NamespaceContext;
    18
    19  /***
    20   * <p>
    21   * <code>DefaultNamespaceContext</code> implements a Jaxen NamespaceContext
    22   * such that a context node is used to determine the current XPath namespace
    23   * prefixes and namespace URIs available.
    24   * </p>
    25   *
    26   * @author <a href="mailto:jstrachan@apache.org">James Strachan </a>
    27   */
    28  public class DefaultNamespaceContext implements NamespaceContext, Serializable {
    29      private final Element element;
    30
    31      public DefaultNamespaceContext(Element element) {
    32          this.element = element;
    33      }
    34
    35      public static DefaultNamespaceContext create(Object node) {
    36          Element element = null;
    37
    38          if (node instanceof Element) {
    39              element = (Element) node;
    40          } else if (node instanceof Document) {
    41              Document doc = (Document) node;
    42              element = doc.getRootElement();
    43          } else if (node instanceof Node) {
    44              element = ((Node) node).getParent();
    45          }
    46
    47          if (element != null) {
    48              return new DefaultNamespaceContext(element);
    49          }
    50
    51          return null;
    52      }
    53
    54      public String translateNamespacePrefixToUri(String prefix) {
    55          if ((prefix != null) && (prefix.length() > 0)) {
    56              Namespace ns = element.getNamespaceForPrefix(prefix);
    57
    58              if (ns != null) {
    59                  return ns.getURI();
    60              }
    61          }
    62
    63          return null;
    64      }
    65  }
    66
    67  /*
    68   * Redistribution and use of this software and associated documentation
    69   * ("Software"), with or without modification, are permitted provided that the
    70   * following conditions are met:
    71   *
    72   * 1. Redistributions of source code must retain copyright statements and
    73   * notices. Redistributions must also contain a copy of this document.
    74   *
    75   * 2. Redistributions in binary form must reproduce the above copyright notice,
    76   * this list of conditions and the following disclaimer in the documentation
    77   * and/or other materials provided with the distribution.
    78   *
    79   * 3. The name "DOM4J" must not be used to endorse or promote products derived
    80   * from this Software without prior written permission of MetaStuff, Ltd. For
    81   * written permission, please contact dom4j-info@metastuff.com.
    82   *
    83   * 4. Products derived from this Software may not be called "DOM4J" nor may
    84   * "DOM4J" appear in their names without prior written permission of MetaStuff,
    85   * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
    86   *
    87   * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
    88   *
    89   * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
    90   * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    91   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    92   * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
    93   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    94   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    95   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    96   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    97   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    98   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    99   * POSSIBILITY OF SUCH DAMAGE.
    100  *
    101  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    102  */