KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > apache > xerces > dom > AttrNSImpl


1 /* $Id: AttrNSImpl.java,v 1.1.1.1 2003/03/10 16:34:28 taweili Exp $ */
2 /*
3  * The Apache Software License, Version 1.1
4  *
5  *
6  * Copyright (c) 1999 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Xerces" and "Apache Software Foundation" must
29  * not be used to endorse or promote products derived from this
30  * software without prior written permission. For written
31  * permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * nor may "Apache" appear in their name, without prior written
35  * permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation and was
53  * originally based on software copyright (c) 1999, International
54  * Business Machines, Inc., http://www.apache.org. For more
55  * information on the Apache Software Foundation, please see
56  * <http://www.apache.org/>.
57  */

58
59 package org.enhydra.apache.xerces.dom;
60
61 import org.w3c.dom.DOMException JavaDoc;
62
63 /**
64  * AttrNSImpl inherits from AttrImpl and adds namespace support.
65  * <P>
66  * The qualified name is the node name, and we store localName which is also
67  * used in all queries. On the other hand we recompute the prefix when
68  * necessary.
69  * @author Arnaud Le Hors, IBM
70  * @author Andy Clark, IBM
71  * @author Ralf Pfeiffer, IBM
72  */

73 public class AttrNSImpl
74     extends AttrImpl {
75
76     //
77
// Constants
78
//
79

80     /** Serialization version. */
81     static final long serialVersionUID = -781906615369795414L;
82     static final String JavaDoc xmlnsURI = "http://www.w3.org/2000/xmlns/";
83     static final String JavaDoc xmlURI = "http://www.w3.org/XML/1998/namespace";
84
85     //
86
// Data
87
//
88

89     /** DOM2: Namespace URI. */
90     protected String JavaDoc namespaceURI;
91   
92     /** DOM2: localName. */
93     protected String JavaDoc localName;
94     /**
95      * DOM2: Constructor for Namespace implementation.
96      */

97     protected AttrNSImpl(CoreDocumentImpl ownerDocument,
98              String JavaDoc namespaceURI,
99              String JavaDoc qualifiedName) {
100
101         super(ownerDocument, qualifiedName);
102
103         int index = qualifiedName.indexOf(':');
104         String JavaDoc prefix;
105         if (index < 0) {
106             prefix = null;
107             localName = qualifiedName;
108
109             if (ownerDocument.errorChecking &&
110                 qualifiedName.equals("xmlns") &&
111                 (namespaceURI == null || !namespaceURI.equals(xmlnsURI))) {
112
113                 throw new DOMException JavaDoc(DOMException.NAMESPACE_ERR,
114                        "DOM003 Namespace error");
115             }
116         }
117         else {
118             prefix = qualifiedName.substring(0, index);
119             localName = qualifiedName.substring(index+1);
120         
121             if (ownerDocument.errorChecking) {
122                 if (namespaceURI == null
123                     || (localName.length() == 0)
124                     || (localName.indexOf(':') >= 0)) {
125                     throw new DOMException JavaDoc(DOMException.NAMESPACE_ERR,
126                                            "DOM003 Namespace error");
127                 } else if (prefix.equals("xml")) {
128                     if (!namespaceURI.equals(xmlURI)) {
129                         throw new DOMException JavaDoc(DOMException.NAMESPACE_ERR,
130                                                "DOM003 Namespace error");
131                     }
132                 } else if (prefix.equals("xmlns")) {
133                     if (!namespaceURI.equals(xmlnsURI)) {
134                         throw new DOMException JavaDoc(DOMException.NAMESPACE_ERR,
135                                                "DOM003 Namespace error");
136                     }
137                 } else if (index == 0) {
138                     throw new DOMException JavaDoc(DOMException.NAMESPACE_ERR,
139                                            "DOM003 Namespace error");
140                 }
141             }
142         }
143     this.namespaceURI = namespaceURI;
144     }
145
146     // for DeferredAttrImpl
147
protected AttrNSImpl(CoreDocumentImpl ownerDocument,
148              String JavaDoc value) {
149     super(ownerDocument, value);
150     }
151
152     //
153
// DOM2: Namespace methods
154
//
155

156     /**
157      * Introduced in DOM Level 2. <p>
158      *
159      * The namespace URI of this node, or null if it is unspecified.<p>
160      *
161      * This is not a computed value that is the result of a namespace lookup
162      * based on an examination of the namespace declarations in scope. It is
163      * merely the namespace URI given at creation time.<p>
164      *
165      * For nodes created with a DOM Level 1 method, such as createElement
166      * from the Document interface, this is null.
167      * @since WD-DOM-Level-2-19990923
168      */

169     public String JavaDoc getNamespaceURI()
170     {
171         if (needsSyncData()) {
172             synchronizeData();
173         }
174         // REVIST: This code could/should be done at a lower-level, such that
175
// the namespaceURI is set properly upon creation. However, there still
176
// seems to be some DOM spec interpretation grey-area.
177
return namespaceURI;
178     }
179     
180     /**
181      * Introduced in DOM Level 2. <p>
182      *
183      * The namespace prefix of this node, or null if it is unspecified. <p>
184      *
185      * For nodes created with a DOM Level 1 method, such as createElement
186      * from the Document interface, this is null. <p>
187      *
188      * @since WD-DOM-Level-2-19990923
189      */

190     public String JavaDoc getPrefix()
191     {
192         if (needsSyncData()) {
193             synchronizeData();
194         }
195         int index = name.indexOf(':');
196         return index < 0 ? null : name.substring(0, index);
197     }
198     
199     /**
200      * Introduced in DOM Level 2. <p>
201      *
202      * Note that setting this attribute changes the nodeName attribute, which
203      * holds the qualified name, as well as the tagName and name attributes of
204      * the Element and Attr interfaces, when applicable.<p>
205      *
206      * @throws INVALID_CHARACTER_ERR Raised if the specified
207      * prefix contains an invalid character.
208      *
209      * @since WD-DOM-Level-2-19990923
210      */

211     public void setPrefix(String JavaDoc prefix)
212         throws DOMException JavaDoc
213     {
214         if (needsSyncData()) {
215             synchronizeData();
216         }
217     if (ownerDocument().errorChecking) {
218             if (isReadOnly()) {
219                 throw new DOMException JavaDoc(
220                                      DOMException.NO_MODIFICATION_ALLOWED_ERR,
221                                      "DOM001 Modification not allowed");
222             }
223             if (!CoreDocumentImpl.isXMLName(prefix)) {
224                 throw new DOMException JavaDoc(DOMException.INVALID_CHARACTER_ERR,
225                                        "DOM002 Illegal character");
226             }
227             if (namespaceURI == null || prefix.indexOf(':') >=0) {
228                 throw new DOMException JavaDoc(DOMException.NAMESPACE_ERR,
229                        "DOM003 Namespace error");
230             } else if (prefix != null) {
231                 if (prefix.equals("xmlns")) {
232                     if (!namespaceURI.equals(xmlnsURI)){
233                         throw new DOMException JavaDoc(DOMException.NAMESPACE_ERR,
234                                                "DOM003 Namespace error");
235                     }
236                 } else if (prefix.equals("xml")) {
237                     if (!namespaceURI.equals(xmlURI)) {
238                         throw new DOMException JavaDoc(DOMException.NAMESPACE_ERR,
239                                                "DOM003 Namespace error");
240                     }
241                 } else if (name.equals("xmlns")) {
242                         throw new DOMException JavaDoc(DOMException.NAMESPACE_ERR,
243                                     "DOM003 Namespace error");
244                 }
245             }
246         }
247         // update node name with new qualifiedName
248
name = prefix + ":" + localName;
249     }
250                                         
251     /**
252      * Introduced in DOM Level 2. <p>
253      *
254      * Returns the local part of the qualified name of this node.
255      * @since WD-DOM-Level-2-19990923
256      */

257     public String JavaDoc getLocalName()
258     {
259         if (needsSyncData()) {
260             synchronizeData();
261         }
262         return localName;
263     }
264 }
265
Popular Tags