KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.xb;
23
24 import org.jboss.logging.Logger;
25 import org.w3c.dom.Element JavaDoc;
26 import org.w3c.dom.NamedNodeMap JavaDoc;
27 import org.w3c.dom.Node JavaDoc;
28
29 import javax.xml.namespace.QName JavaDoc;
30
31 /**
32  * A QName builder that discovers the namespaceURI for a given prefix by walking
33  * up the document tree.
34  *
35  * The combined name is of the form [prefix:]localPart
36  *
37  * @author Thomas.Diesler@jboss.org
38  * @version $Revision: 1958 $
39  */

40 public final class QNameBuilder
41 {
42    private static Logger log = Logger.getLogger(QNameBuilder.class);
43
44    /**
45     * Build a QName from a combined name
46     * @param element The current element
47     * @param combinedName A name of form prefix:localPart
48     * @return A QName, or null
49     */

50    public static QName JavaDoc buildQName(Element JavaDoc element, String JavaDoc combinedName)
51    {
52       if (combinedName == null)
53          return null;
54
55       int colonIndex = combinedName.indexOf(":");
56       if (colonIndex < 0)
57          return new QName JavaDoc(combinedName);
58
59       String JavaDoc prefix = combinedName.substring(0, colonIndex);
60       String JavaDoc localPart = combinedName.substring(colonIndex + 1);
61
62       Node JavaDoc currNode = element;
63       String JavaDoc namespaceURI = getNamespaceURI(currNode, prefix);
64       while (namespaceURI == null && currNode != null)
65       {
66          Node JavaDoc 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 JavaDoc(namespaceURI, localPart, prefix);
78
79       log.warn("Cannot find namespaceURI for name: " + combinedName);
80       return new QName JavaDoc(localPart);
81    }
82
83    /**
84     * Get the namespaceURI from a given prefix from the current node.
85     */

86    private static String JavaDoc getNamespaceURI(Node JavaDoc node, String JavaDoc prefix)
87    {
88       String JavaDoc namespaceURI = null;
89       NamedNodeMap JavaDoc attrs = node.getAttributes();
90       if (attrs != null)
91       {
92          for (int i = 0; namespaceURI == null && i < attrs.getLength(); i++)
93          {
94             Node JavaDoc 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