KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 1999-2004 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.impl.dv.xs.XSSimpleTypeDecl;
61 import com.sun.org.apache.xerces.internal.xni.NamespaceContext;
62 import org.w3c.dom.DOMException JavaDoc;
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  * @version $Id: AttrNSImpl.java,v 1.43 2004/02/16 05:34:38 mrglavas Exp $
74  */

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

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

92     /** DOM2: Namespace URI. */
93     protected String JavaDoc namespaceURI;
94   
95     /** DOM2: localName. */
96     protected String JavaDoc localName;
97
98     /*
99      * Default constructor
100      */

101     public AttrNSImpl(){}
102         
103    /**
104      * DOM2: Constructor for Namespace implementation.
105      */

106     protected AttrNSImpl(CoreDocumentImpl ownerDocument,
107                          String JavaDoc namespaceURI,
108                          String JavaDoc qualifiedName) {
109
110         super(ownerDocument, qualifiedName);
111         setName(namespaceURI, qualifiedName);
112     }
113
114     private void setName(String JavaDoc namespaceURI, String JavaDoc qname){
115         
116         String JavaDoc prefix;
117         // DOM Level 3: namespace URI is never empty string.
118
this.namespaceURI = namespaceURI;
119         if (namespaceURI !=null) {
120             this.namespaceURI = (namespaceURI.length() == 0)? null
121                                  : namespaceURI;
122
123         }
124         int colon1 = qname.indexOf(':');
125         int colon2 = qname.lastIndexOf(':');
126         ownerDocument().checkNamespaceWF(qname, colon1, colon2);
127         if (colon1 < 0) {
128             // there is no prefix
129
localName = qname;
130             ownerDocument().checkQName(null, localName);
131             if (ownerDocument().errorChecking) {
132                 if (qname.equals("xmlns")
133                     && (namespaceURI == null
134                         || !namespaceURI.equals(NamespaceContext.XMLNS_URI))
135                     || (namespaceURI!=null && namespaceURI.equals(NamespaceContext.XMLNS_URI)
136                         && !qname.equals("xmlns"))) {
137                     String JavaDoc msg =
138                         DOMMessageFormatter.formatMessage(
139                             DOMMessageFormatter.DOM_DOMAIN,
140                             "NAMESPACE_ERR",
141                             null);
142                     throw new DOMException JavaDoc(DOMException.NAMESPACE_ERR, msg);
143                 }
144             }
145         }
146         else {
147             prefix = qname.substring(0, colon1);
148             localName = qname.substring(colon2+1);
149             ownerDocument().checkQName(prefix, localName);
150             ownerDocument().checkDOMNSErr(prefix, namespaceURI);
151         }
152     }
153
154     // when local name is known
155
public AttrNSImpl(CoreDocumentImpl ownerDocument,
156                          String JavaDoc namespaceURI,
157                          String JavaDoc qualifiedName,
158                          String JavaDoc localName) {
159         super(ownerDocument, qualifiedName);
160         
161         this.localName = localName;
162         this.namespaceURI = namespaceURI;
163     }
164     
165     // for DeferredAttrImpl
166
protected AttrNSImpl(CoreDocumentImpl ownerDocument,
167                          String JavaDoc value) {
168         super(ownerDocument, value);
169     }
170
171     // Support for DOM Level 3 renameNode method.
172
// Note: This only deals with part of the pb. It is expected to be
173
// called after the Attr has been detached for one thing.
174
// CoreDocumentImpl does all the work.
175
void rename(String JavaDoc namespaceURI, String JavaDoc qualifiedName) {
176         if (needsSyncData()) {
177             synchronizeData();
178         }
179     this.name = qualifiedName;
180         setName(namespaceURI, qualifiedName);
181     }
182
183     /**
184      * NON-DOM: resets this node and sets specified values for the node
185      *
186      * @param ownerDocument
187      * @param namespaceURI
188      * @param qualifiedName
189      * @param localName
190      */

191     public void setValues (CoreDocumentImpl ownerDocument,
192                          String JavaDoc namespaceURI,
193                          String JavaDoc qualifiedName,
194                          String JavaDoc localName){
195
196         super.textNode = null;
197         super.flags = 0;
198         isSpecified(true);
199         hasStringValue(true);
200         super.setOwnerDocument(ownerDocument);
201         this.localName = localName;
202         this.namespaceURI = namespaceURI;
203         super.name = qualifiedName;
204         super.value = null;
205     }
206
207     //
208
// DOM2: Namespace methods
209
//
210

211     /**
212      * Introduced in DOM Level 2. <p>
213      *
214      * The namespace URI of this node, or null if it is unspecified.<p>
215      *
216      * This is not a computed value that is the result of a namespace lookup
217      * based on an examination of the namespace declarations in scope. It is
218      * merely the namespace URI given at creation time.<p>
219      *
220      * For nodes created with a DOM Level 1 method, such as createElement
221      * from the Document interface, this is null.
222      * @since WD-DOM-Level-2-19990923
223      */

224     public String JavaDoc getNamespaceURI()
225     {
226         if (needsSyncData()) {
227             synchronizeData();
228         }
229         // REVIST: This code could/should be done at a lower-level, such that
230
// the namespaceURI is set properly upon creation. However, there still
231
// seems to be some DOM spec interpretation grey-area.
232
return namespaceURI;
233     }
234     
235     /**
236      * Introduced in DOM Level 2. <p>
237      *
238      * The namespace prefix of this node, or null if it is unspecified. <p>
239      *
240      * For nodes created with a DOM Level 1 method, such as createElement
241      * from the Document interface, this is null. <p>
242      *
243      * @since WD-DOM-Level-2-19990923
244      */

245     public String JavaDoc getPrefix()
246     {
247         if (needsSyncData()) {
248             synchronizeData();
249         }
250         int index = name.indexOf(':');
251         return index < 0 ? null : name.substring(0, index);
252     }
253     
254     /**
255      * Introduced in DOM Level 2. <p>
256      *
257      * Note that setting this attribute changes the nodeName attribute, which
258      * holds the qualified name, as well as the tagName and name attributes of
259      * the Element and Attr interfaces, when applicable.<p>
260      *
261      * @param prefix The namespace prefix of this node, or null(empty string) if it is unspecified.
262      *
263      * @exception INVALID_CHARACTER_ERR
264      * Raised if the specified
265      * prefix contains an invalid character.
266      * @exception DOMException
267      * @since WD-DOM-Level-2-19990923
268      */

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

323     public String JavaDoc getLocalName()
324     {
325         if (needsSyncData()) {
326             synchronizeData();
327         }
328         return localName;
329     }
330
331     /**
332      * DOM Level 3 Experimental
333      *
334      * @see org.w3c.dom.TypeInfo#isDerivedFrom()
335      */

336     public boolean isDerivedFrom(String JavaDoc typeNamespaceArg,
337                                  String JavaDoc typeNameArg,
338                                  int derivationMethod) {
339         
340         //REVISIT: XSSimpleTypeDecl.derivedFrom and
341
//derivationMethod constants in DOM vs Xerces
342
if (type !=null){
343             if (type instanceof XSSimpleTypeDecl){
344                 return ((XSSimpleTypeDecl)type).derivedFrom(typeNamespaceArg,typeNameArg,(short)derivationMethod);
345             }
346         }
347         return false;
348     }
349
350     /**
351      * @see org.w3c.dom.TypeInfo#getTypeNamespace()
352      */

353     public String JavaDoc getTypeNamespace() {
354         if (type !=null) {
355             if (type instanceof XSSimpleTypeDecl){
356                 return ((XSSimpleTypeDecl)type).getNamespace();
357             }
358             return DTD_URI;
359         }
360         return null;
361     }
362     
363 }
364
Popular Tags