1 16 19 package org.apache.xml.utils; 20 21 import org.w3c.dom.NamedNodeMap ; 22 import org.w3c.dom.Node ; 23 24 30 public class PrefixResolverDefault implements PrefixResolver 31 { 32 33 37 Node m_context; 38 39 49 public PrefixResolverDefault(Node xpathExpressionContext) 50 { 51 m_context = xpathExpressionContext; 52 } 53 54 62 public String getNamespaceForPrefix(String prefix) 63 { 64 return getNamespaceForPrefix(prefix, m_context); 65 } 66 67 77 public String getNamespaceForPrefix(String prefix, 78 org.w3c.dom.Node namespaceContext) 79 { 80 81 Node parent = namespaceContext; 82 String namespace = null; 83 84 if (prefix.equals("xml")) 85 { 86 namespace = Constants.S_XMLNAMESPACEURI; 87 } 88 else 89 { 90 int type; 91 92 while ((null != parent) && (null == namespace) 93 && (((type = parent.getNodeType()) == Node.ELEMENT_NODE) 94 || (type == Node.ENTITY_REFERENCE_NODE))) 95 { 96 if (type == Node.ELEMENT_NODE) 97 { 98 if (parent.getNodeName().indexOf(prefix+":") == 0) 99 return parent.getNamespaceURI(); 100 NamedNodeMap nnm = parent.getAttributes(); 101 102 for (int i = 0; i < nnm.getLength(); i++) 103 { 104 Node attr = nnm.item(i); 105 String aname = attr.getNodeName(); 106 boolean isPrefix = aname.startsWith("xmlns:"); 107 108 if (isPrefix || aname.equals("xmlns")) 109 { 110 int index = aname.indexOf(':'); 111 String p = isPrefix ? aname.substring(index + 1) : ""; 112 113 if (p.equals(prefix)) 114 { 115 namespace = attr.getNodeValue(); 116 117 break; 118 } 119 } 120 } 121 } 122 123 parent = parent.getParentNode(); 124 } 125 } 126 127 return namespace; 128 } 129 130 135 public String getBaseIdentifier() 136 { 137 return null; 138 } 139 142 public boolean handlesNullPrefixes() { 143 return false; 144 } 145 146 } 147 | Popular Tags |