1 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 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 103 | Popular Tags |