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