KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 1999-2002,2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.dom;
18
19 import org.apache.xerces.xs.XSSimpleTypeDefinition;
20 import org.apache.xerces.xs.XSTypeDefinition;
21 import org.apache.xerces.impl.dv.xs.XSSimpleTypeDecl;
22 import org.apache.xerces.impl.xs.XSComplexTypeDecl;
23 import org.apache.xerces.util.URI;
24 import org.apache.xerces.xni.NamespaceContext;
25 import org.w3c.dom.Attr JavaDoc;
26 import org.w3c.dom.DOMException JavaDoc;
27
28
29
30 /**
31  * ElementNSImpl inherits from ElementImpl and adds namespace support.
32  * <P>
33  * The qualified name is the node name, and we store localName which is also
34  * used in all queries. On the other hand we recompute the prefix when
35  * necessary.
36  *
37  * @xerces.internal
38  *
39  * @author Elena litani, IBM
40  * @author Neeraj Bajaj, Sun Microsystems
41  * @version $Id: ElementNSImpl.java,v 1.48 2005/05/10 15:36:42 ankitp Exp $
42  */

43 public class ElementNSImpl
44     extends ElementImpl {
45
46     //
47
// Constants
48
//
49

50     /** Serialization version. */
51     static final long serialVersionUID = -9142310625494392642L;
52     static final String JavaDoc xmlURI = "http://www.w3.org/XML/1998/namespace";
53
54     //
55
// Data
56
//
57

58     /** DOM2: Namespace URI. */
59     protected String JavaDoc namespaceURI;
60
61     /** DOM2: localName. */
62     protected String JavaDoc localName;
63
64     /** DOM3: type information */
65     // REVISIT: we are losing the type information in DOM during serialization
66
transient XSTypeDefinition type;
67
68     protected ElementNSImpl() {
69         super();
70     }
71     /**
72      * DOM2: Constructor for Namespace implementation.
73      */

74     protected ElementNSImpl(CoreDocumentImpl ownerDocument,
75                             String JavaDoc namespaceURI,
76                             String JavaDoc qualifiedName)
77         throws DOMException JavaDoc
78     {
79         super(ownerDocument, qualifiedName);
80         setName(namespaceURI, qualifiedName);
81     }
82
83     private void setName(String JavaDoc namespaceURI, String JavaDoc qname) {
84
85         String JavaDoc prefix;
86         // DOM Level 3: namespace URI is never empty string.
87
this.namespaceURI = namespaceURI;
88         if (namespaceURI != null) {
89             //convert the empty string to 'null'
90
this.namespaceURI = (namespaceURI.length() == 0) ? null : namespaceURI;
91         }
92
93         int colon1, colon2 ;
94
95         //NAMESPACE_ERR:
96
//1. if the qualified name is 'null' it is malformed.
97
//2. or if the qualifiedName is null and the namespaceURI is different from null,
98
// We dont need to check for namespaceURI != null, if qualified name is null throw DOMException.
99
if(qname == null){
100                 String JavaDoc msg =
101                     DOMMessageFormatter.formatMessage(
102                         DOMMessageFormatter.DOM_DOMAIN,
103                         "NAMESPACE_ERR",
104                         null);
105                 throw new DOMException JavaDoc(DOMException.NAMESPACE_ERR, msg);
106         }
107         else{
108             colon1 = qname.indexOf(':');
109             colon2 = qname.lastIndexOf(':');
110         }
111
112         ownerDocument.checkNamespaceWF(qname, colon1, colon2);
113         if (colon1 < 0) {
114             // there is no prefix
115
localName = qname;
116             if (ownerDocument.errorChecking) {
117                 ownerDocument.checkQName(null, localName);
118                 if (qname.equals("xmlns")
119                     && (namespaceURI == null
120                     || !namespaceURI.equals(NamespaceContext.XMLNS_URI))
121                     || (namespaceURI!=null && namespaceURI.equals(NamespaceContext.XMLNS_URI)
122                     && !qname.equals("xmlns"))) {
123                     String JavaDoc msg =
124                         DOMMessageFormatter.formatMessage(
125                                 DOMMessageFormatter.DOM_DOMAIN,
126                                 "NAMESPACE_ERR",
127                                 null);
128                     throw new DOMException JavaDoc(DOMException.NAMESPACE_ERR, msg);
129                 }
130             }
131         }//there is a prefix
132
else {
133             prefix = qname.substring(0, colon1);
134             localName = qname.substring(colon2 + 1);
135             
136             //NAMESPACE_ERR:
137
//1. if the qualifiedName has a prefix and the namespaceURI is null,
138

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

142             if (ownerDocument.errorChecking) {
143                 if( namespaceURI == null || ( prefix.equals("xml") && !namespaceURI.equals(NamespaceContext.XML_URI) )){
144                     String JavaDoc msg =
145                         DOMMessageFormatter.formatMessage(
146                                 DOMMessageFormatter.DOM_DOMAIN,
147                                 "NAMESPACE_ERR",
148                                 null);
149                     throw new DOMException JavaDoc(DOMException.NAMESPACE_ERR, msg);
150                 }
151                 
152                 ownerDocument.checkQName(prefix, localName);
153                 ownerDocument.checkDOMNSErr(prefix, namespaceURI);
154             }
155         }
156     }
157
158     // when local name is known
159
protected ElementNSImpl(CoreDocumentImpl ownerDocument,
160                             String JavaDoc namespaceURI, String JavaDoc qualifiedName,
161                             String JavaDoc localName)
162         throws DOMException JavaDoc
163     {
164         super(ownerDocument, qualifiedName);
165
166         this.localName = localName;
167         this.namespaceURI = namespaceURI;
168     }
169
170     // for DeferredElementImpl
171
protected ElementNSImpl(CoreDocumentImpl ownerDocument,
172                             String JavaDoc value) {
173         super(ownerDocument, value);
174     }
175
176     // Support for DOM Level 3 renameNode method.
177
// Note: This only deals with part of the pb. CoreDocumentImpl
178
// does all the work.
179
void rename(String JavaDoc namespaceURI, String JavaDoc qualifiedName)
180     {
181         if (needsSyncData()) {
182             synchronizeData();
183         }
184         this.name = qualifiedName;
185         setName(namespaceURI, qualifiedName);
186         reconcileDefaultAttributes();
187     }
188
189     /**
190      * NON-DOM: resets this node and sets specified values for the node
191      *
192      * @param ownerDocument
193      * @param namespaceURI
194      * @param qualifiedName
195      * @param localName
196      */

197     protected void setValues (CoreDocumentImpl ownerDocument,
198                             String JavaDoc namespaceURI, String JavaDoc qualifiedName,
199                             String JavaDoc localName){
200
201         // remove children first
202
firstChild = null;
203         previousSibling = null;
204         nextSibling = null;
205         fNodeListCache = null;
206
207         // set owner document
208
attributes = null;
209         super.flags = 0;
210         setOwnerDocument(ownerDocument);
211
212         // synchronizeData will initialize attributes
213
needsSyncData(true);
214         super.name = qualifiedName;
215         this.localName = localName;
216         this.namespaceURI = namespaceURI;
217
218     }
219
220     //
221
// Node methods
222
//
223

224
225
226     //
227
//DOM2: Namespace methods.
228
//
229

230     /**
231      * Introduced in DOM Level 2. <p>
232      *
233      * The namespace URI of this node, or null if it is unspecified.<p>
234      *
235      * This is not a computed value that is the result of a namespace lookup based on
236      * an examination of the namespace declarations in scope. It is merely the
237      * namespace URI given at creation time.<p>
238      *
239      * For nodes created with a DOM Level 1 method, such as createElement
240      * from the Document interface, this is null.
241      * @since WD-DOM-Level-2-19990923
242      */

243     public String JavaDoc getNamespaceURI()
244     {
245         if (needsSyncData()) {
246             synchronizeData();
247         }
248         return namespaceURI;
249     }
250
251     /**
252      * Introduced in DOM Level 2. <p>
253      *
254      * The namespace prefix of this node, or null if it is unspecified. <p>
255      *
256      * For nodes created with a DOM Level 1 method, such as createElement
257      * from the Document interface, this is null. <p>
258      *
259      * @since WD-DOM-Level-2-19990923
260      */

261     public String JavaDoc getPrefix()
262     {
263
264         if (needsSyncData()) {
265             synchronizeData();
266         }
267         int index = name.indexOf(':');
268         return index < 0 ? null : name.substring(0, index);
269     }
270
271     /**
272      * Introduced in DOM Level 2. <p>
273      *
274      * Note that setting this attribute changes the nodeName attribute, which holds the
275      * qualified name, as well as the tagName and name attributes of the Element
276      * and Attr interfaces, when applicable.<p>
277      *
278      * @param prefix The namespace prefix of this node, or null(empty string) if it is unspecified.
279      *
280      * @exception INVALID_CHARACTER_ERR
281      * Raised if the specified
282      * prefix contains an invalid character.
283      * @exception DOMException
284      * @since WD-DOM-Level-2-19990923
285      */

286     public void setPrefix(String JavaDoc prefix)
287         throws DOMException JavaDoc
288     {
289         if (needsSyncData()) {
290             synchronizeData();
291         }
292         if (ownerDocument.errorChecking) {
293             if (isReadOnly()) {
294                 String JavaDoc msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null);
295                 throw new DOMException JavaDoc(
296                                      DOMException.NO_MODIFICATION_ALLOWED_ERR,
297                                      msg);
298             }
299             if (prefix != null && prefix.length() != 0) {
300                 if (!CoreDocumentImpl.isXMLName(prefix,ownerDocument.isXML11Version())) {
301                     String JavaDoc msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_CHARACTER_ERR", null);
302                     throw new DOMException JavaDoc(DOMException.INVALID_CHARACTER_ERR, msg);
303                 }
304                 if (namespaceURI == null || prefix.indexOf(':') >=0) {
305                     String JavaDoc msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NAMESPACE_ERR", null);
306                     throw new DOMException JavaDoc(DOMException.NAMESPACE_ERR, msg);
307                 } else if (prefix.equals("xml")) {
308                      if (!namespaceURI.equals(xmlURI)) {
309                          String JavaDoc msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NAMESPACE_ERR", null);
310                          throw new DOMException JavaDoc(DOMException.NAMESPACE_ERR, msg);
311                      }
312                 }
313             }
314
315         }
316         // update node name with new qualifiedName
317
if (prefix !=null && prefix.length() != 0) {
318             name = prefix + ":" + localName;
319         }
320         else {
321             name = localName;
322         }
323     }
324
325     /**
326      * Introduced in DOM Level 2. <p>
327      *
328      * Returns the local part of the qualified name of this node.
329      * @since WD-DOM-Level-2-19990923
330      */

331     public String JavaDoc getLocalName()
332     {
333         if (needsSyncData()) {
334             synchronizeData();
335         }
336         return localName;
337     }
338
339
340    /**
341      * DOM Level 3 WD - Experimental.
342      * Retrieve baseURI
343      */

344     public String JavaDoc getBaseURI() {
345
346         if (needsSyncData()) {
347             synchronizeData();
348         }
349         // Absolute base URI is computed according to XML Base (http://www.w3.org/TR/xmlbase/#granularity)
350

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

353         if (attributes != null) {
354             Attr JavaDoc attrNode = (Attr JavaDoc)attributes.getNamedItemNS("http://www.w3.org/XML/1998/namespace", "base");
355             if (attrNode != null) {
356                 String JavaDoc uri = attrNode.getNodeValue();
357                 if (uri.length() != 0 ) {// attribute value is always empty string
358
try {
359                         uri = new URI(uri).toString();
360                     }
361                     catch (org.apache.xerces.util.URI.MalformedURIException e) {
362                         // This may be a relative URI.
363

364                         // Start from the base URI of the parent, or if this node has no parent, the owner node.
365
NodeImpl parentOrOwner = (parentNode() != null) ? parentNode() : ownerNode;
366                         
367                         // Make any parentURI into a URI object to use with the URI(URI, String) constructor.
368
String JavaDoc parentBaseURI = (parentOrOwner != null) ? parentOrOwner.getBaseURI() : null;
369                         
370                         if (parentBaseURI != null) {
371                             try {
372                                 uri = new URI(new URI(parentBaseURI), uri).toString();
373                             }
374                             catch (org.apache.xerces.util.URI.MalformedURIException ex){
375                                 // This should never happen: parent should have checked the URI and returned null if invalid.
376
return null;
377                             }
378                             return uri;
379                         }
380                         // REVISIT: what should happen in this case?
381
return null;
382                     }
383                     return uri;
384                 }
385             }
386         }
387
388         //2.the base URI of the element's parent element within the document or external entity,
389
//if one exists
390
String JavaDoc parentElementBaseURI = (this.parentNode() != null) ? this.parentNode().getBaseURI() : null ;
391         //base URI of parent element is not null
392
if(parentElementBaseURI != null){
393             try {
394                 //return valid absolute base URI
395
return new URI(parentElementBaseURI).toString();
396             }
397             catch (org.apache.xerces.util.URI.MalformedURIException e){
398                 // REVISIT: what should happen in this case?
399
return null;
400             }
401         }
402         //3. the base URI of the document entity or external entity containing the element
403

404         String JavaDoc baseURI = (this.ownerNode != null) ? this.ownerNode.getBaseURI() : null ;
405
406         if(baseURI != null){
407             try {
408                 //return valid absolute base URI
409
return new URI(baseURI).toString();
410             }
411             catch (org.apache.xerces.util.URI.MalformedURIException e){
412                 // REVISIT: what should happen in this case?
413
return null;
414             }
415         }
416
417         return null;
418
419     }
420
421
422     /**
423      * @see org.w3c.dom.TypeInfo#getTypeName()
424      */

425     public String JavaDoc getTypeName() {
426         if (type !=null){
427             if (type instanceof XSSimpleTypeDefinition) {
428                 return ((XSSimpleTypeDecl) type).getTypeName();
429             } else {
430                 return ((XSComplexTypeDecl) type).getTypeName();
431             }
432         }
433         return null;
434     }
435
436     /**
437      * @see org.w3c.dom.TypeInfo#getTypeNamespace()
438      */

439     public String JavaDoc getTypeNamespace() {
440         if (type !=null){
441             return type.getNamespace();
442         }
443         return null;
444     }
445
446     /**
447      * Introduced in DOM Level 2. <p>
448      * Checks if a type is derived from another by restriction. See:
449      * http://www.w3.org/TR/DOM-Level-3-Core/core.html#TypeInfo-isDerivedFrom
450      *
451      * @param ancestorNS
452      * The namspace of the ancestor type declaration
453      * @param ancestorName
454      * The name of the ancestor type declaration
455      * @param type
456      * The reference type definition
457      *
458      * @return boolean True if the type is derived by restriciton for the
459      * reference type
460      */

461     public boolean isDerivedFrom(String JavaDoc typeNamespaceArg, String JavaDoc typeNameArg,
462             int derivationMethod) {
463         if(needsSyncData()) {
464             synchronizeData();
465         }
466         if (type != null) {
467             if (type instanceof XSSimpleTypeDefinition) {
468                 return ((XSSimpleTypeDecl) type).isDOMDerivedFrom(
469                         typeNamespaceArg, typeNameArg, derivationMethod);
470             } else {
471                 return ((XSComplexTypeDecl) type).isDOMDerivedFrom(
472                         typeNamespaceArg, typeNameArg, derivationMethod);
473             }
474         }
475         return false;
476     }
477
478     /**
479      * NON-DOM: setting type used by the DOM parser
480      * @see NodeImpl#setReadOnly
481      */

482     public void setType(XSTypeDefinition type) {
483         this.type = type;
484     }
485 }
486
Popular Tags