001: /* $Id: ElementNSImpl.java,v 1.16.2.1 2001/11/05 13:10:18 elena Exp $ */
002: /*
003: * The Apache Software License, Version 1.1
004: *
005: *
006: * Copyright (c) 1999 The Apache Software Foundation. All rights
007: * reserved.
008: *
009: * Redistribution and use in source and binary forms, with or without
010: * modification, are permitted provided that the following conditions
011: * are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution,
022: * if any, must include the following acknowledgment:
023: * "This product includes software developed by the
024: * Apache Software Foundation (http://www.apache.org/)."
025: * Alternately, this acknowledgment may appear in the software itself,
026: * if and wherever such third-party acknowledgments normally appear.
027: *
028: * 4. The names "Xerces" and "Apache Software Foundation" must
029: * not be used to endorse or promote products derived from this
030: * software without prior written permission. For written
031: * permission, please contact apache@apache.org.
032: *
033: * 5. Products derived from this software may not be called "Apache",
034: * nor may "Apache" appear in their name, without prior written
035: * permission of the Apache Software Foundation.
036: *
037: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
038: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
039: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
040: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
041: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
042: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
043: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
044: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
045: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
046: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
047: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
048: * SUCH DAMAGE.
049: * ====================================================================
050: *
051: * This software consists of voluntary contributions made by many
052: * individuals on behalf of the Apache Software Foundation and was
053: * originally based on software copyright (c) 1999, International
054: * Business Machines, Inc., http://www.apache.org. For more
055: * information on the Apache Software Foundation, please see
056: * <http://www.apache.org/>.
057: */
058:
059: package org.apache.xerces.dom;
060:
061: import org.w3c.dom.DOMException;
062:
063: /**
064: * ElementNSImpl inherits from ElementImpl and adds namespace support.
065: * <P>
066: * The qualified name is the node name, and we store localName which is also
067: * used in all queries. On the other hand we recompute the prefix when
068: * necessary.
069: */
070: public class ElementNSImpl extends ElementImpl {
071:
072: //
073: // Constants
074: //
075:
076: /** Serialization version. */
077: static final long serialVersionUID = -9142310625494392642L;
078: static final String xmlURI = "http://www.w3.org/XML/1998/namespace";
079:
080: //
081: // Data
082: //
083:
084: /** DOM2: Namespace URI. */
085: protected String namespaceURI;
086:
087: /** DOM2: localName. */
088: protected String localName;
089:
090: /**
091: * DOM2: Constructor for Namespace implementation.
092: */
093: protected ElementNSImpl(CoreDocumentImpl ownerDocument,
094: String namespaceURI, String qualifiedName)
095: throws DOMException {
096: super (ownerDocument, qualifiedName);
097:
098: int index = qualifiedName.indexOf(':');
099: String prefix;
100: if (index < 0) {
101: prefix = null;
102: localName = qualifiedName;
103: } else {
104: prefix = qualifiedName.substring(0, index);
105: localName = qualifiedName.substring(index + 1);
106:
107: if (ownerDocument.errorChecking) {
108: if (namespaceURI == null || (localName.length() == 0)
109: || (localName.indexOf(':') >= 0)) {
110: throw new DOMException(DOMException.NAMESPACE_ERR,
111: "DOM003 Namespace error");
112: } else if (prefix.equals("xml")) {
113: if (!namespaceURI.equals(xmlURI)) {
114: throw new DOMException(
115: DOMException.NAMESPACE_ERR,
116: "DOM003 Namespace error");
117: }
118: } else if (index == 0) {
119: throw new DOMException(DOMException.NAMESPACE_ERR,
120: "DOM003 Namespace error");
121: }
122: }
123: }
124: this .namespaceURI = namespaceURI;
125: }
126:
127: // for DeferredElementImpl
128: protected ElementNSImpl(CoreDocumentImpl ownerDocument, String value) {
129: super (ownerDocument, value);
130: }
131:
132: //
133: // Node methods
134: //
135:
136: //
137: //DOM2: Namespace methods.
138: //
139:
140: /**
141: * Introduced in DOM Level 2. <p>
142: *
143: * The namespace URI of this node, or null if it is unspecified.<p>
144: *
145: * This is not a computed value that is the result of a namespace lookup based on
146: * an examination of the namespace declarations in scope. It is merely the
147: * namespace URI given at creation time.<p>
148: *
149: * For nodes created with a DOM Level 1 method, such as createElement
150: * from the Document interface, this is null.
151: * @since WD-DOM-Level-2-19990923
152: */
153: public String getNamespaceURI() {
154: if (needsSyncData()) {
155: synchronizeData();
156: }
157: return namespaceURI;
158: }
159:
160: /**
161: * Introduced in DOM Level 2. <p>
162: *
163: * The namespace prefix of this node, or null if it is unspecified. <p>
164: *
165: * For nodes created with a DOM Level 1 method, such as createElement
166: * from the Document interface, this is null. <p>
167: *
168: * @since WD-DOM-Level-2-19990923
169: */
170: public String getPrefix() {
171: if (needsSyncData()) {
172: synchronizeData();
173: }
174: int index = name.indexOf(':');
175: return index < 0 ? null : name.substring(0, index);
176: }
177:
178: /**
179: * Introduced in DOM Level 2. <p>
180: *
181: * Note that setting this attribute changes the nodeName attribute, which holds the
182: * qualified name, as well as the tagName and name attributes of the Element
183: * and Attr interfaces, when applicable.<p>
184: *
185: * @throws INVALID_CHARACTER_ERR Raised if the specified
186: * prefix contains an invalid character.
187: *
188: * @since WD-DOM-Level-2-19990923
189: */
190: public void setPrefix(String prefix) throws DOMException {
191: if (needsSyncData()) {
192: synchronizeData();
193: }
194: if (ownerDocument().errorChecking) {
195: if (isReadOnly()) {
196: throw new DOMException(
197: DOMException.NO_MODIFICATION_ALLOWED_ERR,
198: "DOM001 Modification not allowed");
199: }
200: if (!CoreDocumentImpl.isXMLName(prefix)) {
201: throw new DOMException(
202: DOMException.INVALID_CHARACTER_ERR,
203: "DOM002 Illegal character");
204: }
205: if (namespaceURI == null || prefix.indexOf(':') >= 0) {
206: throw new DOMException(DOMException.NAMESPACE_ERR,
207: "DOM003 Namespace error");
208: } else if (prefix != null) {
209: if (prefix.equals("xml")) {
210: if (!namespaceURI.equals(xmlURI)) {
211: throw new DOMException(
212: DOMException.NAMESPACE_ERR,
213: "DOM003 Namespace error");
214: }
215: }
216: }
217: }
218: // update node name with new qualifiedName
219: name = prefix + ":" + localName;
220: }
221:
222: /**
223: * Introduced in DOM Level 2. <p>
224: *
225: * Returns the local part of the qualified name of this node.
226: * @since WD-DOM-Level-2-19990923
227: */
228: public String getLocalName() {
229: if (needsSyncData()) {
230: synchronizeData();
231: }
232: return localName;
233: }
234: }
|