KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: ElementNSImpl.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  * ElementNSImpl inherits from ElementImpl 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  */

70 public class ElementNSImpl
71     extends ElementImpl {
72
73     //
74
// Constants
75
//
76

77     /** Serialization version. */
78     static final long serialVersionUID = -9142310625494392642L;
79     static final String JavaDoc xmlURI = "http://www.w3.org/XML/1998/namespace";
80
81     //
82
// Data
83
//
84

85     /** DOM2: Namespace URI. */
86     protected String JavaDoc namespaceURI;
87   
88     /** DOM2: localName. */
89     protected String JavaDoc localName;
90
91     
92     /**
93      * DOM2: Constructor for Namespace implementation.
94      */

95     protected ElementNSImpl(DocumentImpl ownerDocument,
96                 String JavaDoc namespaceURI,
97                 String JavaDoc qualifiedName)
98         throws DOMException JavaDoc
99     {
100         super(ownerDocument, qualifiedName);
101
102         int index = qualifiedName.indexOf(':');
103         String JavaDoc prefix;
104         if (index < 0) {
105             prefix = null;
106             localName = qualifiedName;
107         }
108         else {
109             prefix = qualifiedName.substring(0, index);
110             localName = qualifiedName.substring(index+1);
111         
112             if (ownerDocument.errorChecking) {
113                 if (namespaceURI == null
114                     || (localName.length() == 0)
115                     || (localName.indexOf(':') >= 0)) {
116                     throw new DOMException JavaDoc(DOMException.NAMESPACE_ERR,
117                                            "DOM003 Namespace error");
118                 }
119                 else if (prefix.equals("xml")) {
120                     if (!namespaceURI.equals(xmlURI)) {
121                         throw new DOMException JavaDoc(DOMException.NAMESPACE_ERR,
122                                                "DOM003 Namespace error");
123                     }
124                 } else if (index == 0) {
125                     throw new DOMException JavaDoc(DOMException.NAMESPACE_ERR,
126                                            "DOM003 Namespace error");
127                 }
128             }
129         }
130     this.namespaceURI = namespaceURI;
131     }
132
133     // for DeferredElementImpl
134
protected ElementNSImpl(DocumentImpl ownerDocument,
135                 String JavaDoc value) {
136     super(ownerDocument, value);
137     }
138
139     //
140
// Node methods
141
//
142

143     
144     //
145
//DOM2: Namespace methods.
146
//
147

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

161     public String JavaDoc getNamespaceURI()
162     {
163         if (needsSyncData()) {
164             synchronizeData();
165         }
166         return namespaceURI;
167     }
168     
169     /**
170      * Introduced in DOM Level 2. <p>
171      *
172      * The namespace prefix of this node, or null if it is unspecified. <p>
173      *
174      * For nodes created with a DOM Level 1 method, such as createElement
175      * from the Document interface, this is null. <p>
176      *
177      * @since WD-DOM-Level-2-19990923
178      */

179     public String JavaDoc getPrefix()
180     {
181         if (needsSyncData()) {
182             synchronizeData();
183         }
184         int index = name.indexOf(':');
185         return index < 0 ? null : name.substring(0, index);
186     }
187     
188     /**
189      * Introduced in DOM Level 2. <p>
190      *
191      * Note that setting this attribute changes the nodeName attribute, which holds the
192      * qualified name, as well as the tagName and name attributes of the Element
193      * and Attr interfaces, when applicable.<p>
194      *
195      * @throws INVALID_CHARACTER_ERR Raised if the specified
196      * prefix contains an invalid character.
197      *
198      * @since WD-DOM-Level-2-19990923
199      */

200     public void setPrefix(String JavaDoc prefix)
201         throws DOMException JavaDoc
202     {
203         if (needsSyncData()) {
204             synchronizeData();
205         }
206     if (ownerDocument().errorChecking) {
207             if (isReadOnly()) {
208                 throw new DOMException JavaDoc(
209                                      DOMException.NO_MODIFICATION_ALLOWED_ERR,
210                                      "DOM001 Modification not allowed");
211             }
212             if (!DocumentImpl.isXMLName(prefix)) {
213                 throw new DOMException JavaDoc(DOMException.INVALID_CHARACTER_ERR,
214                                        "DOM002 Illegal character");
215             }
216             if (namespaceURI == null) {
217                   throw new DOMException JavaDoc(DOMException.NAMESPACE_ERR,
218                                          "DOM003 Namespace error");
219             } else if (prefix != null) {
220                 if (prefix.equals("xml")) {
221                     if (!namespaceURI.equals(xmlURI)) {
222                         throw new DOMException JavaDoc(DOMException.NAMESPACE_ERR,
223                                                "DOM003 Namespace error");
224                     }
225                 }
226             }
227         }
228         // update node name with new qualifiedName
229
name = prefix + ":" + localName;
230     }
231                                         
232     /**
233      * Introduced in DOM Level 2. <p>
234      *
235      * Returns the local part of the qualified name of this node.
236      * @since WD-DOM-Level-2-19990923
237      */

238     public String JavaDoc getLocalName()
239     {
240         if (needsSyncData()) {
241             synchronizeData();
242         }
243         return localName;
244     }
245 }
246
Popular Tags