1 22 package org.jboss.xb; 23 24 import org.jboss.logging.Logger; 25 import org.w3c.dom.Element ; 26 import org.w3c.dom.NamedNodeMap ; 27 import org.w3c.dom.Node ; 28 29 import javax.xml.namespace.QName ; 30 31 40 public final class QNameBuilder 41 { 42 private static Logger log = Logger.getLogger(QNameBuilder.class); 43 44 50 public static QName buildQName(Element element, String combinedName) 51 { 52 if (combinedName == null) 53 return null; 54 55 int colonIndex = combinedName.indexOf(":"); 56 if (colonIndex < 0) 57 return new QName (combinedName); 58 59 String prefix = combinedName.substring(0, colonIndex); 60 String localPart = combinedName.substring(colonIndex + 1); 61 62 Node currNode = element; 63 String namespaceURI = getNamespaceURI(currNode, prefix); 64 while (namespaceURI == null && currNode != null) 65 { 66 Node parentNode = currNode.getParentNode(); 67 if (parentNode != null && parentNode != currNode) 68 namespaceURI = getNamespaceURI(parentNode, prefix); 69 70 if (parentNode == currNode) 71 break; 72 73 currNode = parentNode; 74 } 75 76 if (namespaceURI != null) 77 return new QName (namespaceURI, localPart, prefix); 78 79 log.warn("Cannot find namespaceURI for name: " + combinedName); 80 return new QName (localPart); 81 } 82 83 86 private static String getNamespaceURI(Node node, String prefix) 87 { 88 String namespaceURI = null; 89 NamedNodeMap attrs = node.getAttributes(); 90 if (attrs != null) 91 { 92 for (int i = 0; namespaceURI == null && i < attrs.getLength(); i++) 93 { 94 Node attr = attrs.item(i); 95 if (("xmlns:" + prefix).equals(attr.getNodeName())) 96 namespaceURI = attr.getNodeValue(); 97 } 98 } 99 return namespaceURI; 100 } 101 } 102 | Popular Tags |