KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > xpath > datamodel > xerces > dom > AttrNSImpl


1 /* $Id: AttrNSImpl.java,v 1.1 2003/10/01 16:48:50 lars 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.xquark.xpath.datamodel.xerces.dom;
60
61 import org.w3c.dom.DOMException JavaDoc;
62
63
64 /**
65  * AttrNSImpl inherits from AttrImpl and adds namespace support.
66  * <P>
67  * The qualified name is the node name, and we store localName which is also
68  * used in all queries. On the other hand we recompute the prefix when
69  * necessary.
70  * @author Arnaud Le Hors, IBM
71  * @author Andy Clark, IBM
72  * @author Ralf Pfeiffer, IBM
73  */

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

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

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

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

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

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

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

212     public void setPrefix(String JavaDoc prefix)
213         throws DOMException JavaDoc
214     {
215         if (needsSyncData()) {
216             synchronizeData();
217         }
218     if (ownerDocument().errorChecking) {
219             if (isReadOnly()) {
220                 throw new DOMException JavaDoc(
221                                      DOMException.NO_MODIFICATION_ALLOWED_ERR,
222                                      "DOM001 Modification not allowed");
223             }
224             if (!DocumentImpl.isXMLName(prefix)) {
225                 throw new DOMException JavaDoc(DOMException.INVALID_CHARACTER_ERR,
226                                        "DOM002 Illegal character");
227             }
228             if (namespaceURI == null) {
229                 throw new DOMException JavaDoc(DOMException.NAMESPACE_ERR,
230                        "DOM003 Namespace error");
231             } else if (prefix != null) {
232                 if (prefix.equals("xmlns")) {
233                     if (!namespaceURI.equals(xmlnsURI)){
234                         throw new DOMException JavaDoc(DOMException.NAMESPACE_ERR,
235                                                "DOM003 Namespace error");
236                     }
237                 } else if (prefix.equals("xml")) {
238                     if (!namespaceURI.equals(xmlURI)) {
239                         throw new DOMException JavaDoc(DOMException.NAMESPACE_ERR,
240                                                "DOM003 Namespace error");
241                     }
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 JavaDoc getLocalName()
256     {
257         if (needsSyncData()) {
258             synchronizeData();
259         }
260         return localName;
261     }
262 }
263
Popular Tags