KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > resolver > helpers > Namespaces


1 // Namespaces.java - Analyze namespace nodes in a DOM tree
2

3 /*
4  * Copyright 2001-2004 The Apache Software Foundation or its licensors,
5  * as applicable.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19
20 package com.sun.org.apache.xml.internal.resolver.helpers;
21
22 import org.w3c.dom.*;
23
24 /**
25  * Static Namespace query methods.
26  *
27  * <p>This class defines a set of static methods that can be called
28  * to analyze the namespace properties of DOM nodes.</p>
29  *
30  * @author Norman Walsh
31  * <a HREF="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
32  *
33  * @version 1.0
34  */

35 public class Namespaces {
36     /**
37      * Returns the "prefix" part of a QName or the empty string (not
38      * null) if the name has no prefix.
39      *
40      * @param element The QName of an element.
41      * @return The prefix part of the element name.
42      */

43     public static String JavaDoc getPrefix(Element element) {
44     String JavaDoc name = element.getTagName();
45     String JavaDoc prefix = "";
46
47     if (name.indexOf(':') > 0) {
48         prefix = name.substring(0, name.indexOf(':'));
49     }
50
51     return prefix;
52     }
53
54     /**
55      * Returns the "localname" part of a QName, which is the whole
56      * name if it has no prefix.
57      *
58      * @param element The QName of an element.
59      * @return The local part of a QName.
60      */

61     public static String JavaDoc getLocalName(Element element) {
62     String JavaDoc name = element.getTagName();
63
64     if (name.indexOf(':') > 0) {
65         name = name.substring(name.indexOf(':')+1);
66     }
67
68     return name;
69     }
70
71     /**
72      * Returns the namespace URI for the specified prefix at the
73      * specified context node.
74      *
75      * @param node The context node.
76      * @param prefix The prefix.
77      * @return The namespace URI associated with the prefix, or
78      * null if no namespace declaration exists for the prefix.
79      */

80     public static String JavaDoc getNamespaceURI(Node node, String JavaDoc prefix) {
81     if (node == null || node.getNodeType() != Node.ELEMENT_NODE) {
82         return null;
83     }
84
85     if (prefix.equals("")) {
86         if (((Element) node).hasAttribute("xmlns")) {
87         return ((Element) node).getAttribute("xmlns");
88         }
89     } else {
90         String JavaDoc nsattr = "xmlns:" + prefix;
91         if (((Element) node).hasAttribute(nsattr)) {
92         return ((Element) node).getAttribute(nsattr);
93         }
94     }
95
96     return getNamespaceURI(node.getParentNode(), prefix);
97     }
98
99     /**
100      * Returns the namespace URI for the namespace to which the
101      * element belongs.
102      *
103      * @param element The element.
104      * @return The namespace URI associated with the namespace of the
105      * element, or null if no namespace declaration exists for it.
106      */

107     public static String JavaDoc getNamespaceURI(Element element) {
108     String JavaDoc prefix = getPrefix(element);
109     return getNamespaceURI(element, prefix);
110     }
111 }
112
Popular Tags