1 17 package org.apache.servicemix.jbi.jaxp; 18 19 23 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import javax.xml.namespace.NamespaceContext ; 29 30 import org.w3c.dom.Attr ; 31 import org.w3c.dom.Element ; 32 import org.w3c.dom.NamedNodeMap ; 33 import org.w3c.dom.Node ; 34 35 public class W3CNamespaceContext implements NamespaceContext 36 { 37 private Element currentNode; 38 39 public String getNamespaceURI(String prefix) 40 { 41 String name = prefix; 42 if (name.length() == 0) name = "xmlns"; 43 else name = "xmlns:" + prefix; 44 45 return getNamespaceURI(currentNode, name); 46 } 47 48 private String getNamespaceURI(Element e, String name) 49 { 50 Attr attr = e.getAttributeNode(name); 51 if (attr == null) 52 { 53 Node n = e.getParentNode(); 54 if (n instanceof Element && n != e) 55 { 56 return getNamespaceURI((Element ) n, name); 57 } 58 } 59 else 60 { 61 return attr.getValue(); 62 } 63 64 return null; 65 } 66 67 public String getPrefix(String uri) 68 { 69 return getPrefix(currentNode, uri); 70 } 71 72 private String getPrefix(Element e, String uri) 73 { 74 NamedNodeMap attributes = e.getAttributes(); 75 for (int i = 0; i < attributes.getLength(); i++) 76 { 77 Attr a = (Attr ) attributes.item(i); 78 79 String val = a.getValue(); 80 if (val != null && val.equals(uri)) 81 { 82 String name = a.getNodeName(); 83 if (name.equals("xmlns")) return ""; 84 else return name.substring(6); 85 } 86 } 87 88 Node n = e.getParentNode(); 89 if (n instanceof Element && n != e) 90 { 91 return getPrefix((Element ) n, uri); 92 } 93 94 return null; 95 } 96 97 public Iterator getPrefixes(String uri) 98 { 99 List prefixes = new ArrayList (); 100 101 String prefix = getPrefix(uri); 102 if (prefix != null) prefixes.add(prefix); 103 104 return prefixes.iterator(); 105 } 106 107 public Element getElement() 108 { 109 return currentNode; 110 } 111 112 public void setElement(Element currentNode) 113 { 114 this.currentNode = currentNode; 115 } 116 } 117 | Popular Tags |