001: /* $Id: AttrNSImpl.java,v 1.21.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: * AttrNSImpl inherits from AttrImpl 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: * @author Arnaud Le Hors, IBM
070: * @author Andy Clark, IBM
071: * @author Ralf Pfeiffer, IBM
072: */
073: public class AttrNSImpl extends AttrImpl {
074:
075: //
076: // Constants
077: //
078:
079: /** Serialization version. */
080: static final long serialVersionUID = -781906615369795414L;
081: static final String xmlnsURI = "http://www.w3.org/2000/xmlns/";
082: static final String xmlURI = "http://www.w3.org/XML/1998/namespace";
083:
084: //
085: // Data
086: //
087:
088: /** DOM2: Namespace URI. */
089: protected String namespaceURI;
090:
091: /** DOM2: localName. */
092: protected String localName;
093:
094: /**
095: * DOM2: Constructor for Namespace implementation.
096: */
097: protected AttrNSImpl(CoreDocumentImpl ownerDocument,
098: String namespaceURI, String qualifiedName) {
099:
100: super (ownerDocument, qualifiedName);
101:
102: int index = qualifiedName.indexOf(':');
103: String prefix;
104: if (index < 0) {
105: prefix = null;
106: localName = qualifiedName;
107:
108: if (ownerDocument.errorChecking
109: && qualifiedName.equals("xmlns")
110: && (namespaceURI == null || !namespaceURI
111: .equals(xmlnsURI))) {
112:
113: throw new DOMException(DOMException.NAMESPACE_ERR,
114: "DOM003 Namespace error");
115: }
116: } else {
117: prefix = qualifiedName.substring(0, index);
118: localName = qualifiedName.substring(index + 1);
119:
120: if (ownerDocument.errorChecking) {
121: if (namespaceURI == null || (localName.length() == 0)
122: || (localName.indexOf(':') >= 0)) {
123: throw new DOMException(DOMException.NAMESPACE_ERR,
124: "DOM003 Namespace error");
125: } else if (prefix.equals("xml")) {
126: if (!namespaceURI.equals(xmlURI)) {
127: throw new DOMException(
128: DOMException.NAMESPACE_ERR,
129: "DOM003 Namespace error");
130: }
131: } else if (prefix.equals("xmlns")) {
132: if (!namespaceURI.equals(xmlnsURI)) {
133: throw new DOMException(
134: DOMException.NAMESPACE_ERR,
135: "DOM003 Namespace error");
136: }
137: } else if (index == 0) {
138: throw new DOMException(DOMException.NAMESPACE_ERR,
139: "DOM003 Namespace error");
140: }
141: }
142: }
143: this .namespaceURI = namespaceURI;
144: }
145:
146: // for DeferredAttrImpl
147: protected AttrNSImpl(CoreDocumentImpl ownerDocument, String value) {
148: super (ownerDocument, value);
149: }
150:
151: //
152: // DOM2: Namespace methods
153: //
154:
155: /**
156: * Introduced in DOM Level 2. <p>
157: *
158: * The namespace URI of this node, or null if it is unspecified.<p>
159: *
160: * This is not a computed value that is the result of a namespace lookup
161: * based on an examination of the namespace declarations in scope. It is
162: * merely the namespace URI given at creation time.<p>
163: *
164: * For nodes created with a DOM Level 1 method, such as createElement
165: * from the Document interface, this is null.
166: * @since WD-DOM-Level-2-19990923
167: */
168: public String getNamespaceURI() {
169: if (needsSyncData()) {
170: synchronizeData();
171: }
172: // REVIST: This code could/should be done at a lower-level, such that
173: // the namespaceURI is set properly upon creation. However, there still
174: // seems to be some DOM spec interpretation grey-area.
175: return namespaceURI;
176: }
177:
178: /**
179: * Introduced in DOM Level 2. <p>
180: *
181: * The namespace prefix of this node, or null if it is unspecified. <p>
182: *
183: * For nodes created with a DOM Level 1 method, such as createElement
184: * from the Document interface, this is null. <p>
185: *
186: * @since WD-DOM-Level-2-19990923
187: */
188: public String getPrefix() {
189: if (needsSyncData()) {
190: synchronizeData();
191: }
192: int index = name.indexOf(':');
193: return index < 0 ? null : name.substring(0, index);
194: }
195:
196: /**
197: * Introduced in DOM Level 2. <p>
198: *
199: * Note that setting this attribute changes the nodeName attribute, which
200: * holds the qualified name, as well as the tagName and name attributes of
201: * the Element and Attr interfaces, when applicable.<p>
202: *
203: * @throws INVALID_CHARACTER_ERR Raised if the specified
204: * prefix contains an invalid character.
205: *
206: * @since WD-DOM-Level-2-19990923
207: */
208: public void setPrefix(String prefix) throws DOMException {
209: if (needsSyncData()) {
210: synchronizeData();
211: }
212: if (ownerDocument().errorChecking) {
213: if (isReadOnly()) {
214: throw new DOMException(
215: DOMException.NO_MODIFICATION_ALLOWED_ERR,
216: "DOM001 Modification not allowed");
217: }
218: if (!CoreDocumentImpl.isXMLName(prefix)) {
219: throw new DOMException(
220: DOMException.INVALID_CHARACTER_ERR,
221: "DOM002 Illegal character");
222: }
223: if (namespaceURI == null || prefix.indexOf(':') >= 0) {
224: throw new DOMException(DOMException.NAMESPACE_ERR,
225: "DOM003 Namespace error");
226: } else if (prefix != null) {
227: if (prefix.equals("xmlns")) {
228: if (!namespaceURI.equals(xmlnsURI)) {
229: throw new DOMException(
230: DOMException.NAMESPACE_ERR,
231: "DOM003 Namespace error");
232: }
233: } else if (prefix.equals("xml")) {
234: if (!namespaceURI.equals(xmlURI)) {
235: throw new DOMException(
236: DOMException.NAMESPACE_ERR,
237: "DOM003 Namespace error");
238: }
239: } else if (name.equals("xmlns")) {
240: throw new DOMException(DOMException.NAMESPACE_ERR,
241: "DOM003 Namespace error");
242: }
243: }
244: }
245: // update node name with new qualifiedName
246: name = prefix + ":" + localName;
247: }
248:
249: /**
250: * Introduced in DOM Level 2. <p>
251: *
252: * Returns the local part of the qualified name of this node.
253: * @since WD-DOM-Level-2-19990923
254: */
255: public String getLocalName() {
256: if (needsSyncData()) {
257: synchronizeData();
258: }
259: return localName;
260: }
261: }
|