1 7 package org.jboss.xml; 8 9 11 import org.jboss.logging.Logger; 12 import org.w3c.dom.Element ; 13 import org.w3c.dom.NamedNodeMap ; 14 import org.w3c.dom.Node ; 15 16 import javax.xml.namespace.QName ; 17 18 27 public final class QNameBuilder 28 { 29 private static Logger log = Logger.getLogger(QNameBuilder.class); 30 31 37 public static QName buildQName(Element element, String combinedName) 38 { 39 if (combinedName == null) 40 return null; 41 42 int colonIndex = combinedName.indexOf(":"); 43 if (colonIndex < 0) 44 return new QName (combinedName); 45 46 String prefix = combinedName.substring(0, colonIndex); 47 String localPart = combinedName.substring(colonIndex + 1); 48 49 Node currNode = element; 50 String namespaceURI = getNamespaceURI (currNode, prefix); 51 while (namespaceURI == null && currNode != null) 52 { 53 Node parentNode = currNode.getParentNode(); 54 if (parentNode != null && parentNode != currNode) 55 namespaceURI = getNamespaceURI (parentNode, prefix); 56 57 if (parentNode == currNode) 58 break; 59 60 currNode = parentNode; 61 } 62 63 if (namespaceURI != null) 64 return new QName (namespaceURI, localPart); 65 66 log.warn ("Cannot find namespaceURI for name: " + combinedName); 67 return new QName (localPart); 68 } 69 70 73 private static String getNamespaceURI(Node node, String prefix) 74 { 75 String namespaceURI = null; 76 NamedNodeMap attrs = node.getAttributes(); 77 if (attrs != null) 78 { 79 for (int i=0; namespaceURI == null && i < attrs.getLength(); i++) 80 { 81 Node attr = attrs.item(i); 82 if (prefix.equals(attr.getLocalName())) 83 namespaceURI = attr.getNodeValue(); 84 } 85 } 86 return namespaceURI; 87 } 88 } 89 | Popular Tags |