001: // Namespaces.java - Analyze namespace nodes in a DOM tree
002:
003: /*
004: * Copyright 2001-2004 The Apache Software Foundation or its licensors,
005: * as applicable.
006: *
007: * Licensed under the Apache License, Version 2.0 (the "License");
008: * you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: package com.sun.org.apache.xml.internal.resolver.helpers;
021:
022: import org.w3c.dom.*;
023:
024: /**
025: * Static Namespace query methods.
026: *
027: * <p>This class defines a set of static methods that can be called
028: * to analyze the namespace properties of DOM nodes.</p>
029: *
030: * @author Norman Walsh
031: * <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
032: *
033: * @version 1.0
034: */
035: public class Namespaces {
036: /**
037: * Returns the "prefix" part of a QName or the empty string (not
038: * null) if the name has no prefix.
039: *
040: * @param element The QName of an element.
041: * @return The prefix part of the element name.
042: */
043: public static String getPrefix(Element element) {
044: String name = element.getTagName();
045: String prefix = "";
046:
047: if (name.indexOf(':') > 0) {
048: prefix = name.substring(0, name.indexOf(':'));
049: }
050:
051: return prefix;
052: }
053:
054: /**
055: * Returns the "localname" part of a QName, which is the whole
056: * name if it has no prefix.
057: *
058: * @param element The QName of an element.
059: * @return The local part of a QName.
060: */
061: public static String getLocalName(Element element) {
062: String name = element.getTagName();
063:
064: if (name.indexOf(':') > 0) {
065: name = name.substring(name.indexOf(':') + 1);
066: }
067:
068: return name;
069: }
070:
071: /**
072: * Returns the namespace URI for the specified prefix at the
073: * specified context node.
074: *
075: * @param node The context node.
076: * @param prefix The prefix.
077: * @return The namespace URI associated with the prefix, or
078: * null if no namespace declaration exists for the prefix.
079: */
080: public static String getNamespaceURI(Node node, String prefix) {
081: if (node == null || node.getNodeType() != Node.ELEMENT_NODE) {
082: return null;
083: }
084:
085: if (prefix.equals("")) {
086: if (((Element) node).hasAttribute("xmlns")) {
087: return ((Element) node).getAttribute("xmlns");
088: }
089: } else {
090: String nsattr = "xmlns:" + prefix;
091: if (((Element) node).hasAttribute(nsattr)) {
092: return ((Element) node).getAttribute(nsattr);
093: }
094: }
095:
096: return getNamespaceURI(node.getParentNode(), prefix);
097: }
098:
099: /**
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 getNamespaceURI(Element element) {
108: String prefix = getPrefix(element);
109: return getNamespaceURI(element, prefix);
110: }
111: }
|