KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > dom > ElementNSImpl


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

57
58 package com.sun.org.apache.xerces.internal.dom;
59
60 import com.sun.org.apache.xerces.internal.xs.XSTypeDefinition;
61 import com.sun.org.apache.xerces.internal.util.URI;
62 import com.sun.org.apache.xerces.internal.xni.NamespaceContext;
63 import org.w3c.dom.Attr JavaDoc;
64 import org.w3c.dom.DOMException JavaDoc;
65 import org.w3c.dom.TypeInfo JavaDoc;
66
67
68
69 /**
70  * ElementNSImpl inherits from ElementImpl and adds namespace support.
71  * <P>
72  * The qualified name is the node name, and we store localName which is also
73  * used in all queries. On the other hand we recompute the prefix when
74  * necessary.
75  * @author Elena litani, IBM
76  * @author Neeraj Bajaj, Sun Microsystems
77  * @version $Id: ElementNSImpl.java,v 1.41 2004/01/29 20:59:52 elena Exp $
78  */

79 public class ElementNSImpl
80     extends ElementImpl {
81
82     //
83
// Constants
84
//
85

86     /** Serialization version. */
87     static final long serialVersionUID = -9142310625494392642L;
88     static final String JavaDoc xmlURI = "http://www.w3.org/XML/1998/namespace";
89
90     //
91
// Data
92
//
93

94     /** DOM2: Namespace URI. */
95     protected String JavaDoc namespaceURI;
96   
97     /** DOM2: localName. */
98     protected String JavaDoc localName;
99
100     protected ElementNSImpl() {
101         super();
102     }
103     /**
104      * DOM2: Constructor for Namespace implementation.
105      */

106     protected ElementNSImpl(CoreDocumentImpl ownerDocument,
107                             String JavaDoc namespaceURI,
108                             String JavaDoc qualifiedName)
109         throws DOMException JavaDoc
110     {
111         super(ownerDocument, qualifiedName);
112         setName(namespaceURI, qualifiedName);
113     }
114
115     private void setName(String JavaDoc namespaceURI, String JavaDoc qname) {
116         
117         String JavaDoc prefix;
118         // DOM Level 3: namespace URI is never empty string.
119
this.namespaceURI = namespaceURI;
120         if (namespaceURI != null) {
121             //convert the empty string to 'null'
122
this.namespaceURI = (namespaceURI.length() == 0) ? null : namespaceURI;
123          }
124
125         int colon1, colon2 ;
126
127         //NAMESPACE_ERR:
128
//1. if the qualified name is 'null' it is malformed.
129
//2. or if the qualifiedName is null and the namespaceURI is different from null,
130
// We dont need to check for namespaceURI != null, if qualified name is null throw DOMException.
131
if(qname == null){
132                  String JavaDoc msg =
133                      DOMMessageFormatter.formatMessage(
134                          DOMMessageFormatter.DOM_DOMAIN,
135                          "NAMESPACE_ERR",
136                          null);
137                  throw new DOMException JavaDoc(DOMException.NAMESPACE_ERR, msg);
138          }
139          else{
140              colon1 = qname.indexOf(':');
141              colon2 = qname.lastIndexOf(':');
142         }
143         ownerDocument().checkNamespaceWF(qname, colon1, colon2);
144         if (colon1 < 0) {
145             // there is no prefix
146
localName = qname;
147             ownerDocument().checkQName(null, localName);
148             if (qname.equals("xmlns")
149                 && (namespaceURI == null
150                     || !namespaceURI.equals(NamespaceContext.XMLNS_URI))
151                 || (namespaceURI!=null && namespaceURI.equals(NamespaceContext.XMLNS_URI)
152                     && !qname.equals("xmlns"))) {
153                 String JavaDoc msg =
154                     DOMMessageFormatter.formatMessage(
155                         DOMMessageFormatter.DOM_DOMAIN,
156                         "NAMESPACE_ERR",
157                         null);
158                 throw new DOMException JavaDoc(DOMException.NAMESPACE_ERR, msg);
159             }
160         }//there is a prefix
161
else {
162             prefix = qname.substring(0, colon1);
163
164             //NAMESPACE_ERR:
165
//1. if the qualifiedName has a prefix and the namespaceURI is null,
166

167             //2. or if the qualifiedName has a prefix that is "xml" and the namespaceURI
168
//is different from " http://www.w3.org/XML/1998/namespace"
169

170             if( namespaceURI == null || ( prefix.equals("xml") && !namespaceURI.equals(NamespaceContext.XML_URI) )){
171                 String JavaDoc msg =
172                     DOMMessageFormatter.formatMessage(
173                         DOMMessageFormatter.DOM_DOMAIN,
174                         "NAMESPACE_ERR",
175                         null);
176                 throw new DOMException JavaDoc(DOMException.NAMESPACE_ERR, msg);
177             }
178
179             localName = qname.substring(colon2 + 1);
180             ownerDocument().checkQName(prefix, localName);
181             ownerDocument().checkDOMNSErr(prefix, namespaceURI);
182         }
183     }
184
185     // when local name is known
186
protected ElementNSImpl(CoreDocumentImpl ownerDocument,
187                             String JavaDoc namespaceURI, String JavaDoc qualifiedName,
188                             String JavaDoc localName)
189         throws DOMException JavaDoc
190     {
191         super(ownerDocument, qualifiedName);
192
193         this.localName = localName;
194         this.namespaceURI = namespaceURI;
195     }
196
197     // for DeferredElementImpl
198
protected ElementNSImpl(CoreDocumentImpl ownerDocument,
199                             String JavaDoc value) {
200         super(ownerDocument, value);
201     }
202
203     // Support for DOM Level 3 renameNode method.
204
// Note: This only deals with part of the pb. CoreDocumentImpl
205
// does all the work.
206
void rename(String JavaDoc namespaceURI, String JavaDoc qualifiedName)
207     {
208         if (needsSyncData()) {
209             synchronizeData();
210         }
211     this.name = qualifiedName;
212         setName(namespaceURI, qualifiedName);
213         reconcileDefaultAttributes();
214     }
215
216     /**
217      * NON-DOM: resets this node and sets specified values for the node
218      *
219      * @param ownerDocument
220      * @param namespaceURI
221      * @param qualifiedName
222      * @param localName
223      */

224     protected void setValues (CoreDocumentImpl ownerDocument,
225                             String JavaDoc namespaceURI, String JavaDoc qualifiedName,
226                             String JavaDoc localName){
227         
228         // remove children first
229
firstChild = null;
230         previousSibling = null;
231         nextSibling = null;
232         fNodeListCache = null;
233         
234         // set owner document
235
attributes = null;
236         super.flags = 0;
237         setOwnerDocument(ownerDocument);
238
239         // synchronizeData will initialize attributes
240
needsSyncData(true);
241         super.name = qualifiedName;
242         this.localName = localName;
243         this.namespaceURI = namespaceURI;
244
245     }
246
247     //
248
// Node methods
249
//
250

251
252     
253     //
254
//DOM2: Namespace methods.
255
//
256

257     /**
258      * Introduced in DOM Level 2. <p>
259      *
260      * The namespace URI of this node, or null if it is unspecified.<p>
261      *
262      * This is not a computed value that is the result of a namespace lookup based on
263      * an examination of the namespace declarations in scope. It is merely the
264      * namespace URI given at creation time.<p>
265      *
266      * For nodes created with a DOM Level 1 method, such as createElement
267      * from the Document interface, this is null.
268      * @since WD-DOM-Level-2-19990923
269      */

270     public String JavaDoc getNamespaceURI()
271     {
272         if (needsSyncData()) {
273             synchronizeData();
274         }
275         return namespaceURI;
276     }
277     
278     /**
279      * Introduced in DOM Level 2. <p>
280      *
281      * The namespace prefix of this node, or null if it is unspecified. <p>
282      *
283      * For nodes created with a DOM Level 1 method, such as createElement
284      * from the Document interface, this is null. <p>
285      *
286      * @since WD-DOM-Level-2-19990923
287      */

288     public String JavaDoc getPrefix()
289     {
290         
291         if (needsSyncData()) {
292             synchronizeData();
293         }
294         int index = name.indexOf(':');
295         return index < 0 ? null : name.substring(0, index);
296     }
297     
298     /**
299      * Introduced in DOM Level 2. <p>
300      *
301      * Note that setting this attribute changes the nodeName attribute, which holds the
302      * qualified name, as well as the tagName and name attributes of the Element
303      * and Attr interfaces, when applicable.<p>
304      *
305      * @param prefix The namespace prefix of this node, or null(empty string) if it is unspecified.
306      *
307      * @exception INVALID_CHARACTER_ERR
308      * Raised if the specified
309      * prefix contains an invalid character.
310      * @exception DOMException
311      * @since WD-DOM-Level-2-19990923
312      */

313     public void setPrefix(String JavaDoc prefix)
314         throws DOMException JavaDoc
315     {
316         if (needsSyncData()) {
317             synchronizeData();
318         }
319         if (ownerDocument().errorChecking) {
320             if (isReadOnly()) {
321                 String JavaDoc msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null);
322                 throw new DOMException JavaDoc(
323                                      DOMException.NO_MODIFICATION_ALLOWED_ERR,
324                                      msg);
325             }
326             if (prefix != null && prefix.length() != 0) {
327                 if (!CoreDocumentImpl.isXMLName(prefix,ownerDocument().isXML11Version())) {
328                     String JavaDoc msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_CHARACTER_ERR", null);
329                     throw new DOMException JavaDoc(DOMException.INVALID_CHARACTER_ERR, msg);
330                 }
331                 if (namespaceURI == null || prefix.indexOf(':') >=0) {
332                     String JavaDoc msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NAMESPACE_ERR", null);
333                     throw new DOMException JavaDoc(DOMException.NAMESPACE_ERR, msg);
334                 } else if (prefix.equals("xml")) {
335                      if (!namespaceURI.equals(xmlURI)) {
336                          String JavaDoc msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NAMESPACE_ERR", null);
337                          throw new DOMException JavaDoc(DOMException.NAMESPACE_ERR, msg);
338                      }
339                 }
340             }
341         }
342         // update node name with new qualifiedName
343
if (prefix !=null && prefix.length() != 0) {
344             name = prefix + ":" + localName;
345         }
346         else {
347             name = localName;
348         }
349     }
350                                         
351     /**
352      * Introduced in DOM Level 2. <p>
353      *
354      * Returns the local part of the qualified name of this node.
355      * @since WD-DOM-Level-2-19990923
356      */

357     public String JavaDoc getLocalName()
358     {
359         if (needsSyncData()) {
360             synchronizeData();
361         }
362         return localName;
363     }
364
365
366    /**
367      * DOM Level 3 WD - Experimental.
368      * Retrieve baseURI
369      */

370     public String JavaDoc getBaseURI() {
371
372         if (needsSyncData()) {
373             synchronizeData();
374         }
375
376         //Absolute base URI is computed according to XML Base (http://www.w3.org/TR/xmlbase/#granularity)
377

378         //1. the base URI specified by an xml:base attribute on the element, if one exists
379

380         if (attributes != null) {
381             Attr JavaDoc attrNode = (Attr JavaDoc)attributes.getNamedItemNS("http://www.w3.org/XML/1998/namespace", "base");
382             if (attrNode != null) {
383                 String JavaDoc uri = attrNode.getNodeValue();
384                 if (uri.length() != 0 ) {// attribute value is always empty string
385
try {
386                        uri = new URI(uri).toString();
387                     }
388                     catch (com.sun.org.apache.xerces.internal.util.URI.MalformedURIException e){
389                         // REVISIT: what should happen in this case?
390
return null;
391                     }
392                     return uri;
393                 }
394             }
395         }
396
397         //2.the base URI of the element's parent element within the document or external entity,
398
//if one exists
399
String JavaDoc parentElementBaseURI = (this.parentNode() != null) ? this.parentNode().getBaseURI() : null ;
400         //base URI of parent element is not null
401
if(parentElementBaseURI != null){
402             try {
403                 //return valid absolute base URI
404
return new URI(parentElementBaseURI).toString();
405             }
406             catch (com.sun.org.apache.xerces.internal.util.URI.MalformedURIException e){
407                 // REVISIT: what should happen in this case?
408
return null;
409             }
410         }
411         //3. the base URI of the document entity or external entity containing the element
412

413         String JavaDoc baseURI = (this.ownerNode != null) ? this.ownerNode.getBaseURI() : null ;
414
415         if(baseURI != null){
416             try {
417                 //return valid absolute base URI
418
return new URI(baseURI).toString();
419             }
420             catch (com.sun.org.apache.xerces.internal.util.URI.MalformedURIException e){
421                 // REVISIT: what should happen in this case?
422
return null;
423             }
424         }
425
426         return null;
427     }
428     
429     /**
430      * NON-DOM: setting type used by the DOM parser
431      * @see NodeImpl#setReadOnly
432      */

433     public void setType(TypeInfo JavaDoc type) {
434         this.type = type;
435     }
436     
437         
438 }
439
Popular Tags