1 16 18 package com.sun.org.apache.xpath.internal.jaxp; 19 20 import org.w3c.dom.Node ; 21 import org.w3c.dom.NamedNodeMap ; 22 import com.sun.org.apache.xml.internal.utils.PrefixResolver; 23 24 import javax.xml.namespace.NamespaceContext ; 25 26 33 public class JAXPPrefixResolver implements PrefixResolver 34 { 35 36 private NamespaceContext namespaceContext; 37 38 39 public JAXPPrefixResolver ( NamespaceContext nsContext ) { 40 this.namespaceContext = nsContext; 41 } 42 43 44 public String getNamespaceForPrefix( String prefix ) { 45 return namespaceContext.getNamespaceURI( prefix ); 46 } 47 48 53 public String getBaseIdentifier() { 54 return null; 55 } 56 57 60 public boolean handlesNullPrefixes() { 61 return false; 62 } 63 64 65 69 70 public static final String S_XMLNAMESPACEURI = 71 "http://www.w3.org/XML/1998/namespace"; 72 73 74 84 public String getNamespaceForPrefix(String prefix, 85 org.w3c.dom.Node namespaceContext) { 86 Node parent = namespaceContext; 87 String namespace = null; 88 89 if (prefix.equals("xml")) { 90 namespace = S_XMLNAMESPACEURI; 91 } else { 92 int type; 93 94 while ((null != parent) && (null == namespace) 95 && (((type = parent.getNodeType()) == Node.ELEMENT_NODE) 96 || (type == Node.ENTITY_REFERENCE_NODE))) { 97 98 if (type == Node.ELEMENT_NODE) { 99 NamedNodeMap nnm = parent.getAttributes(); 100 101 for (int i = 0; i < nnm.getLength(); i++) { 102 Node attr = nnm.item(i); 103 String aname = attr.getNodeName(); 104 boolean isPrefix = aname.startsWith("xmlns:"); 105 106 if (isPrefix || aname.equals("xmlns")) { 107 int index = aname.indexOf(':'); 108 String p =isPrefix ?aname.substring(index + 1) :""; 109 110 if (p.equals(prefix)) { 111 namespace = attr.getNodeValue(); 112 break; 113 } 114 } 115 } 116 } 117 118 parent = parent.getParentNode(); 119 } 120 } 121 return namespace; 122 } 123 124 } 125 126 | Popular Tags |