KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: ElementNSImpl.java,v 1.1.1.1 2003/03/10 16:34:30 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 /**
65  * ElementNSImpl inherits from ElementImpl 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  */

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

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

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

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

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

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

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

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

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

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