001/*
002 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
003 * 
004 * This software is open source. 
005 * See the bottom of this file for the licence.
006 * 
007 * $Id: DefaultNamespaceContext.java,v 1.2 2001/11/11 09:27:03 jstrachan Exp $
008 */
009
010package org.dom4j.xpath;
011
012import org.dom4j.Document;
013import org.dom4j.Element;
014import org.dom4j.Namespace;
015import org.dom4j.Node;
016
017import org.jaxen.NamespaceContext;
018
019
020/** <p><code>DefaultNamespaceContext</code> implements a Jaxen 
021  * NamespaceContext such that a context node is used
022  * to determine the current XPath namespace prefixes and namespace URIs 
023  * available.</p>
024  *
025  * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
026  */
027public class DefaultNamespaceContext implements NamespaceContext {
028
029    private final Element element;
030    
031    public DefaultNamespaceContext(Element element) {
032        this.element = element;
033    }
034
035    public static DefaultNamespaceContext create(Object node) {
036        Element element = null;
037        if ( node instanceof Element ) {
038            element = (Element) node;
039        }
040        else if ( node instanceof Document ) {
041            Document doc = (Document) node;
042            element = doc.getRootElement();
043        }
044        else if ( node instanceof Node ) {
045            element = ((Node) node).getParent();
046        }
047        if (element != null) {
048            return new DefaultNamespaceContext(element);
049        }
050        return null;
051    }
052    
053    public String translateNamespacePrefixToUri(String prefix) {
054        if ( prefix != null && prefix.length() > 0 ) {
055            Namespace ns = element.getNamespaceForPrefix( prefix );
056            if ( ns != null ) {
057                return ns.getURI();
058            }
059        }
060        return null;
061    }
062}
063
064
065
066
067/*
068 * Redistribution and use of this software and associated documentation
069 * ("Software"), with or without modification, are permitted provided
070 * that the following conditions are met:
071 *
072 * 1. Redistributions of source code must retain copyright
073 *    statements and notices.  Redistributions must also contain a
074 *    copy of this document.
075 *
076 * 2. Redistributions in binary form must reproduce the
077 *    above copyright notice, this list of conditions and the
078 *    following disclaimer in the documentation and/or other
079 *    materials provided with the distribution.
080 *
081 * 3. The name "DOM4J" must not be used to endorse or promote
082 *    products derived from this Software without prior written
083 *    permission of MetaStuff, Ltd.  For written permission,
084 *    please contact dom4j-info@metastuff.com.
085 *
086 * 4. Products derived from this Software may not be called "DOM4J"
087 *    nor may "DOM4J" appear in their names without prior written
088 *    permission of MetaStuff, Ltd. DOM4J is a registered
089 *    trademark of MetaStuff, Ltd.
090 *
091 * 5. Due credit should be given to the DOM4J Project
092 *    (http://dom4j.org/).
093 *
094 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
095 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
096 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
097 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
098 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
099 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
100 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
101 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
102 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
103 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
104 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
105 * OF THE POSSIBILITY OF SUCH DAMAGE.
106 *
107 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
108 *
109 * $Id: DefaultNamespaceContext.java,v 1.2 2001/11/11 09:27:03 jstrachan Exp $
110 */