1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package com.sun.org.apache.xml.internal.resolver.helpers; 19 20import org.w3c.dom.*; 21 22/** 23 * Static Namespace query methods. 24 * 25 * <p>This class defines a set of static methods that can be called 26 * to analyze the namespace properties of DOM nodes.</p> 27 * 28 * @author Norman Walsh 29 * <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a> 30 * 31 */ 32public class Namespaces { 33 /** 34 * Returns the "prefix" part of a QName or the empty string (not 35 * null) if the name has no prefix. 36 * 37 * @param element The QName of an element. 38 * @return The prefix part of the element name. 39 */ 40 public static String getPrefix(Element element) { 41 String name = element.getTagName(); 42 String prefix = ""; 43 44 final int indexOfColon = name.indexOf(':'); 45 if (indexOfColon > 0) { 46 prefix = name.substring(0, indexOfColon); 47 } 48 49 return prefix; 50 } 51 52 /** 53 * Returns the "localname" part of a QName, which is the whole 54 * name if it has no prefix. 55 * 56 * @param element The QName of an element. 57 * @return The local part of a QName. 58 */ 59 public static String getLocalName(Element element) { 60 String name = element.getTagName(); 61 62 final int indexOfColon = name.indexOf(':'); 63 if (indexOfColon > 0) { 64 name = name.substring(indexOfColon + 1); 65 } 66 67 return name; 68 } 69 70 /** 71 * Returns the namespace URI for the specified prefix at the 72 * specified context node. 73 * 74 * @param node The context node. 75 * @param prefix The prefix. 76 * @return The namespace URI associated with the prefix, or 77 * null if no namespace declaration exists for the prefix. 78 */ 79 public static String getNamespaceURI(Node node, String prefix) { 80 if (node == null || node.getNodeType() != Node.ELEMENT_NODE) { 81 return null; 82 } 83 84 if (prefix.length() == 0) { 85 if (((Element) node).hasAttribute("xmlns")) { 86 return ((Element) node).getAttribute("xmlns"); 87 } 88 } else { 89 String nsattr = "xmlns:" + prefix; 90 if (((Element) node).hasAttribute(nsattr)) { 91 return ((Element) node).getAttribute(nsattr); 92 } 93 } 94 95 return getNamespaceURI(node.getParentNode(), prefix); 96 } 97 98 /** 99 * Returns the namespace URI for the namespace to which the 100 * element belongs. 101 * 102 * @param element The element. 103 * @return The namespace URI associated with the namespace of the 104 * element, or null if no namespace declaration exists for it. 105 */ 106 public static String getNamespaceURI(Element element) { 107 String prefix = getPrefix(element); 108 return getNamespaceURI(element, prefix); 109 } 110} 111