1 /* DomNsNode.java -- 2 Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc. 3 4 This file is part of GNU Classpath. 5 6 GNU Classpath is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2, or (at your option) 9 any later version. 10 11 GNU Classpath is distributed in the hope that it will be useful, but 12 WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with GNU Classpath; see the file COPYING. If not, write to the 18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19 02110-1301 USA. 20 21 Linking this library statically or dynamically with other modules is 22 making a combined work based on this library. Thus, the terms and 23 conditions of the GNU General Public License cover the whole 24 combination. 25 26 As a special exception, the copyright holders of this library give you 27 permission to link this library with independent modules to produce an 28 executable, regardless of the license terms of these independent 29 modules, and to copy and distribute the resulting executable under 30 terms of your choice, provided that you also meet, for each linked 31 independent module, the terms and conditions of the license of that 32 module. An independent module is a module which is not derived from 33 or based on this library. If you modify this library, you may extend 34 this exception to your version of the library, but you are not 35 obligated to do so. If you do not wish to do so, delete this 36 exception statement from your version. */ 37 38 package gnu.xml.dom; 39 40 import javax.xml.XMLConstants; 41 import org.w3c.dom.DOMException; 42 43 /** 44 * <p> Abstract implemention of namespace support. This facilitates 45 * sharing code for attribute and element nodes. 46 * 47 * @author David Brownell 48 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> 49 */ 50 public abstract class DomNsNode 51 extends DomNode 52 { 53 54 private String name; 55 private String namespace; 56 private String prefix; 57 private String localName; 58 59 /** 60 * Constructs a node associated with the specified document, and 61 * with the specified namespace information. 62 * 63 * @param owner The document with which this entity is associated 64 * @param namespaceURI Combined with the local part of the name, 65 * this identifies a type of element or attribute; may be null. 66 * If this is the empty string, it is reassigned as null so that 67 * applications only need to test that case. 68 * @param name Name of this node, which may include a prefix 69 */ 70 // package private DomNsNode(short nodeType, DomDocument owner, String namespaceURI, String name)71 DomNsNode(short nodeType, DomDocument owner, String namespaceURI, String name) 72 { 73 super(nodeType, owner); 74 setNodeName(name); 75 setNamespaceURI(namespaceURI); 76 } 77 78 /** 79 * Constructs a node associated with the specified document, and 80 * with the specified namespace information. The prefix and local part 81 * are given explicitly rather than being computed. This allows them 82 * to be explicitly set to {@code null} as required by 83 * {@link Document#createElement(String)}. 84 * 85 * @param owner The document with which this entity is associated 86 * @param namespaceURI Combined with the local part of the name, 87 * this identifies a type of element or attribute; may be null. 88 * If this is the empty string, it is reassigned as null so that 89 * applications only need to test that case. 90 * @param name Name of this node, which may include a prefix 91 * @param prefix the namespace prefix of the name. May be {@code null}. 92 * @param localName the local part of the name. May be {@code null}. 93 */ 94 // package private DomNsNode(short nodeType, DomDocument owner, String namespaceURI, String name, String prefix, String localName)95 DomNsNode(short nodeType, DomDocument owner, String namespaceURI, String name, 96 String prefix, String localName) 97 { 98 super(nodeType, owner); 99 this.name = name.intern(); 100 this.prefix = prefix == null ? null : prefix.intern(); 101 this.localName = localName == null ? null : localName.intern(); 102 setNamespaceURI(namespaceURI); 103 } 104 105 /** 106 * <b>DOM L1</b> 107 * Returns the node's name, including any namespace prefix. 108 */ getNodeName()109 public final String getNodeName() 110 { 111 return name; 112 } 113 setNodeName(String name)114 final void setNodeName(String name) 115 { 116 this.name = name.intern(); 117 int index = name.indexOf(':'); 118 if (index == -1) 119 { 120 prefix = null; 121 localName = this.name; 122 } 123 else 124 { 125 prefix = name.substring(0, index).intern(); 126 localName = name.substring(index + 1).intern(); 127 } 128 } 129 130 /** 131 * <b>DOM L2</b> 132 * Returns the node's namespace URI 133 * <em>or null</em> if the node name is not namespace scoped. 134 */ getNamespaceURI()135 public final String getNamespaceURI() 136 { 137 return namespace; 138 } 139 setNamespaceURI(String namespaceURI)140 final void setNamespaceURI(String namespaceURI) 141 { 142 if ("".equals(namespaceURI)) 143 { 144 namespaceURI = null; 145 } 146 namespace = (namespaceURI == null) ? null : namespaceURI.intern(); 147 } 148 149 /** 150 * <b>DOM L2</b> 151 * Returns any prefix part of the node's name (before any colon). 152 */ getPrefix()153 public final String getPrefix() 154 { 155 return prefix; 156 } 157 158 /** 159 * <b>DOM L2</b> 160 * Assigns the prefix part of the node's name (before any colon). 161 */ setPrefix(String prefix)162 public final void setPrefix(String prefix) 163 { 164 if (readonly) 165 { 166 throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR); 167 } 168 169 if (prefix == null) 170 { 171 name = localName; 172 return; 173 } 174 else if (namespace == null) 175 { 176 throw new DomDOMException(DOMException.NAMESPACE_ERR, 177 "can't set prefix, node has no namespace URI", 178 this, 0); 179 } 180 181 DomDocument.checkName(prefix, "1.1".equals(owner.getXmlVersion())); 182 if (prefix.indexOf (':') != -1) 183 { 184 throw new DomDOMException(DOMException.NAMESPACE_ERR, 185 "illegal prefix " + prefix, this, 0); 186 } 187 188 if (XMLConstants.XML_NS_PREFIX.equals(prefix) 189 && !XMLConstants.XML_NS_URI.equals(namespace)) 190 { 191 throw new DomDOMException(DOMException.NAMESPACE_ERR, 192 "xml namespace is always " + 193 XMLConstants.XML_NS_URI, this, 0); 194 } 195 196 if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix)) 197 { 198 if (namespace != null || getNodeType() != ATTRIBUTE_NODE) 199 { 200 throw new DomDOMException(DOMException.NAMESPACE_ERR, 201 "xmlns attribute prefix is reserved", 202 this, 0); 203 } 204 } 205 else if (getNodeType () == ATTRIBUTE_NODE 206 && (XMLConstants.XMLNS_ATTRIBUTE.equals(name) || 207 name.startsWith("xmlns:"))) 208 { 209 throw new DomDOMException(DOMException.NAMESPACE_ERR, 210 "namespace declarations can't change names", 211 this, 0); 212 } 213 214 this.prefix = prefix.intern(); 215 } 216 217 /** 218 * <b>DOM L2</b> 219 * Returns the local part of the node's name (after any colon). 220 */ getLocalName()221 public final String getLocalName() 222 { 223 return localName; 224 } 225 226 } 227