2 Copyright (C) 1999,2000,2001,2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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
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
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. */
40 import javax.xml.XMLConstants;
41 import org.w3c.dom.DOMException;
44 * <p> Abstract implemention of namespace support. This facilitates
45 * sharing code for attribute and element nodes.
47 * @author David Brownell
48 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
50 public abstract class DomNsNode
55 private String namespace;
56 private String prefix;
57 private String localName;
60 * Constructs a node associated with the specified document, and
61 * with the specified namespace information.
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
71 DomNsNode(short nodeType, DomDocument owner, String namespaceURI, String name)
73 super(nodeType, owner);
75 setNamespaceURI(namespaceURI);
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)}.
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}.
95 DomNsNode(short nodeType, DomDocument owner, String namespaceURI, String name,
96 String prefix, String localName)
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);
107 * Returns the node's name, including any namespace prefix.
109 public final String getNodeName()
114 final void setNodeName(String name)
116 this.name = name.intern();
117 int index = name.indexOf(':');
121 localName = this.name;
125 prefix = name.substring(0, index).intern();
126 localName = name.substring(index + 1).intern();
132 * Returns the node's namespace URI
133 * <em>or null</em> if the node name is not namespace scoped.
135 public final String getNamespaceURI()
140 final void setNamespaceURI(String namespaceURI)
142 if ("".equals(namespaceURI))
146 namespace = (namespaceURI == null) ? null : namespaceURI.intern();
151 * Returns any prefix part of the node's name (before any colon).
153 public final String getPrefix()
160 * Assigns the prefix part of the node's name (before any colon).
162 public final void setPrefix(String prefix)
166 throw new DomDOMException(DOMException.NO_MODIFICATION_ALLOWED_ERR);
174 else if (namespace == null)
176 throw new DomDOMException(DOMException.NAMESPACE_ERR,
177 "can't set prefix, node has no namespace URI",
181 DomDocument.checkName(prefix, "1.1".equals(owner.getXmlVersion()));
182 if (prefix.indexOf (':') != -1)
184 throw new DomDOMException(DOMException.NAMESPACE_ERR,
185 "illegal prefix " + prefix, this, 0);
188 if (XMLConstants.XML_NS_PREFIX.equals(prefix)
189 && !XMLConstants.XML_NS_URI.equals(namespace))
191 throw new DomDOMException(DOMException.NAMESPACE_ERR,
192 "xml namespace is always " +
193 XMLConstants.XML_NS_URI, this, 0);
196 if (XMLConstants.XMLNS_ATTRIBUTE.equals(prefix))
198 if (namespace != null || getNodeType() != ATTRIBUTE_NODE)
200 throw new DomDOMException(DOMException.NAMESPACE_ERR,
201 "xmlns attribute prefix is reserved",
205 else if (getNodeType () == ATTRIBUTE_NODE
206 && (XMLConstants.XMLNS_ATTRIBUTE.equals(name) ||
207 name.startsWith("xmlns:")))
209 throw new DomDOMException(DOMException.NAMESPACE_ERR,
210 "namespace declarations can't change names",
214 this.prefix = prefix.intern();
219 * Returns the local part of the node's name (after any colon).
221 public final String getLocalName()