KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > xml > QNameBuilder


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.xml;
8
9 // $Id: QNameBuilder.java,v 1.1.2.3 2005/06/26 04:42:04 starksm Exp $
10

11 import org.jboss.logging.Logger;
12 import org.w3c.dom.Element JavaDoc;
13 import org.w3c.dom.NamedNodeMap JavaDoc;
14 import org.w3c.dom.Node JavaDoc;
15
16 import javax.xml.namespace.QName JavaDoc;
17
18 /**
19  * A QName builder that discovers the namespaceURI for a given prefix by walking
20  * up the document tree.
21  *
22  * The combined name is of the form [prefix:]localPart
23  *
24  * @author Thomas.Diesler@jboss.org
25  * @version $Revision: 1.1.2.3 $
26  */

27 public final class QNameBuilder
28 {
29    private static Logger log = Logger.getLogger(QNameBuilder.class);
30
31    /**
32     * Build a QName from a combined name
33     * @param element The current element
34     * @param combinedName A name of form prefix:localPart
35     * @return A QName, or null
36     */

37    public static QName JavaDoc buildQName(Element JavaDoc element, String JavaDoc combinedName)
38    {
39       if (combinedName == null)
40          return null;
41
42       int colonIndex = combinedName.indexOf(":");
43       if (colonIndex < 0)
44          return new QName JavaDoc(combinedName);
45
46       String JavaDoc prefix = combinedName.substring(0, colonIndex);
47       String JavaDoc localPart = combinedName.substring(colonIndex + 1);
48
49       Node JavaDoc currNode = element;
50       String JavaDoc namespaceURI = getNamespaceURI (currNode, prefix);
51       while (namespaceURI == null && currNode != null)
52       {
53          Node JavaDoc 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 JavaDoc(namespaceURI, localPart);
65
66       log.warn ("Cannot find namespaceURI for name: " + combinedName);
67       return new QName JavaDoc(localPart);
68    }
69
70    /**
71     * Get the namespaceURI from a given prefix from the current node.
72     */

73    private static String JavaDoc getNamespaceURI(Node JavaDoc node, String JavaDoc prefix)
74    {
75       String JavaDoc namespaceURI = null;
76       NamedNodeMap JavaDoc attrs = node.getAttributes();
77       if (attrs != null)
78       {
79          for (int i=0; namespaceURI == null && i < attrs.getLength(); i++)
80          {
81             Node JavaDoc attr = attrs.item(i);
82             if (prefix.equals(attr.getLocalName()))
83                namespaceURI = attr.getNodeValue();
84          }
85       }
86       return namespaceURI;
87    }
88 }
89
Popular Tags