KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 1999-2004 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.impl.dv.xs.XSSimpleTypeDecl;
20 import org.apache.xerces.xni.NamespaceContext;
21 import org.apache.xerces.xs.XSSimpleTypeDefinition;
22 import org.w3c.dom.DOMException JavaDoc;
23
24 /**
25  * AttrNSImpl inherits from AttrImpl and adds namespace support.
26  * <P>
27  * The qualified name is the node name, and we store localName which is also
28  * used in all queries. On the other hand we recompute the prefix when
29  * necessary.
30  *
31  * @xerces.internal
32  *
33  * @author Arnaud Le Hors, IBM
34  * @author Andy Clark, IBM
35  * @author Ralf Pfeiffer, IBM
36  * @version $Id: AttrNSImpl.java,v 1.48 2005/05/02 22:02:22 mrglavas Exp $
37  */

38 public class AttrNSImpl
39     extends AttrImpl {
40
41     //
42
// Constants
43
//
44

45     /** Serialization version. */
46     static final long serialVersionUID = -781906615369795414L;
47     
48     static final String JavaDoc xmlnsURI = "http://www.w3.org/2000/xmlns/";
49     static final String JavaDoc xmlURI = "http://www.w3.org/XML/1998/namespace";
50
51     //
52
// Data
53
//
54

55     /** DOM2: Namespace URI. */
56     protected String JavaDoc namespaceURI;
57   
58     /** DOM2: localName. */
59     protected String JavaDoc localName;
60
61     /*
62      * Default constructor
63      */

64     public AttrNSImpl(){}
65         
66    /**
67      * DOM2: Constructor for Namespace implementation.
68      */

69     protected AttrNSImpl(CoreDocumentImpl ownerDocument,
70                          String JavaDoc namespaceURI,
71                          String JavaDoc qualifiedName) {
72
73         super(ownerDocument, qualifiedName);
74         setName(namespaceURI, qualifiedName);
75     }
76
77     private void setName(String JavaDoc namespaceURI, String JavaDoc qname){
78         CoreDocumentImpl ownerDocument = ownerDocument();
79         String JavaDoc prefix;
80         // DOM Level 3: namespace URI is never empty string.
81
this.namespaceURI = namespaceURI;
82         if (namespaceURI !=null) {
83             this.namespaceURI = (namespaceURI.length() == 0)? null
84                     : namespaceURI;
85             
86         }
87         int colon1 = qname.indexOf(':');
88         int colon2 = qname.lastIndexOf(':');
89         ownerDocument.checkNamespaceWF(qname, colon1, colon2);
90         if (colon1 < 0) {
91             // there is no prefix
92
localName = qname;
93             if (ownerDocument.errorChecking) {
94                 ownerDocument.checkQName(null, localName);
95                 
96                 if (qname.equals("xmlns") && (namespaceURI == null
97                     || !namespaceURI.equals(NamespaceContext.XMLNS_URI))
98                     || (namespaceURI!=null && namespaceURI.equals(NamespaceContext.XMLNS_URI)
99                     && !qname.equals("xmlns"))) {
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             }
108         }
109         else {
110             prefix = qname.substring(0, colon1);
111             localName = qname.substring(colon2+1);
112             ownerDocument.checkQName(prefix, localName);
113             ownerDocument.checkDOMNSErr(prefix, namespaceURI);
114         }
115     }
116
117     // when local name is known
118
public AttrNSImpl(CoreDocumentImpl ownerDocument,
119                          String JavaDoc namespaceURI,
120                          String JavaDoc qualifiedName,
121                          String JavaDoc localName) {
122         super(ownerDocument, qualifiedName);
123         
124         this.localName = localName;
125         this.namespaceURI = namespaceURI;
126     }
127     
128     // for DeferredAttrImpl
129
protected AttrNSImpl(CoreDocumentImpl ownerDocument,
130                          String JavaDoc value) {
131         super(ownerDocument, value);
132     }
133
134     // Support for DOM Level 3 renameNode method.
135
// Note: This only deals with part of the pb. It is expected to be
136
// called after the Attr has been detached for one thing.
137
// CoreDocumentImpl does all the work.
138
void rename(String JavaDoc namespaceURI, String JavaDoc qualifiedName) {
139         if (needsSyncData()) {
140             synchronizeData();
141         }
142         this.name = qualifiedName;
143         setName(namespaceURI, qualifiedName);
144     }
145
146     /**
147      * NON-DOM: resets this node and sets specified values for the node
148      *
149      * @param ownerDocument
150      * @param namespaceURI
151      * @param qualifiedName
152      * @param localName
153      */

154     public void setValues (CoreDocumentImpl ownerDocument,
155                          String JavaDoc namespaceURI,
156                          String JavaDoc qualifiedName,
157                          String JavaDoc localName){
158
159         super.textNode = null;
160         super.flags = 0;
161         isSpecified(true);
162         hasStringValue(true);
163         super.setOwnerDocument(ownerDocument);
164         this.localName = localName;
165         this.namespaceURI = namespaceURI;
166         super.name = qualifiedName;
167         super.value = null;
168     }
169
170     //
171
// DOM2: Namespace methods
172
//
173

174     /**
175      * Introduced in DOM Level 2. <p>
176      *
177      * The namespace URI of this node, or null if it is unspecified.<p>
178      *
179      * This is not a computed value that is the result of a namespace lookup
180      * based on an examination of the namespace declarations in scope. It is
181      * merely the namespace URI given at creation time.<p>
182      *
183      * For nodes created with a DOM Level 1 method, such as createElement
184      * from the Document interface, this is null.
185      * @since WD-DOM-Level-2-19990923
186      */

187     public String JavaDoc getNamespaceURI()
188     {
189         if (needsSyncData()) {
190             synchronizeData();
191         }
192         // REVIST: This code could/should be done at a lower-level, such that
193
// the namespaceURI is set properly upon creation. However, there still
194
// seems to be some DOM spec interpretation grey-area.
195
return namespaceURI;
196     }
197     
198     /**
199      * Introduced in DOM Level 2. <p>
200      *
201      * The namespace prefix of this node, or null if it is unspecified. <p>
202      *
203      * For nodes created with a DOM Level 1 method, such as createElement
204      * from the Document interface, this is null. <p>
205      *
206      * @since WD-DOM-Level-2-19990923
207      */

208     public String JavaDoc getPrefix()
209     {
210         if (needsSyncData()) {
211             synchronizeData();
212         }
213         int index = name.indexOf(':');
214         return index < 0 ? null : name.substring(0, index);
215     }
216     
217     /**
218      * Introduced in DOM Level 2. <p>
219      *
220      * Note that setting this attribute changes the nodeName attribute, which
221      * holds the qualified name, as well as the tagName and name attributes of
222      * the Element and Attr interfaces, when applicable.<p>
223      *
224      * @param prefix The namespace prefix of this node, or null(empty string) if it is unspecified.
225      *
226      * @exception INVALID_CHARACTER_ERR
227      * Raised if the specified
228      * prefix contains an invalid character.
229      * @exception DOMException
230      * @since WD-DOM-Level-2-19990923
231      */

232     public void setPrefix(String JavaDoc prefix)
233         throws DOMException JavaDoc
234     {
235         if (needsSyncData()) {
236             synchronizeData();
237         }
238         if (ownerDocument().errorChecking) {
239             if (isReadOnly()) {
240                 String JavaDoc msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NO_MODIFICATION_ALLOWED_ERR", null);
241                 throw new DOMException JavaDoc(DOMException.NO_MODIFICATION_ALLOWED_ERR, msg);
242             }
243             if (prefix != null && prefix.length() != 0) {
244
245                 if (!CoreDocumentImpl.isXMLName(prefix,ownerDocument().isXML11Version())) {
246                     String JavaDoc msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "INVALID_CHARACTER_ERR", null);
247                     throw new DOMException JavaDoc(DOMException.INVALID_CHARACTER_ERR, msg);
248                 }
249                 if (namespaceURI == null || prefix.indexOf(':') >=0) {
250                     String JavaDoc msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NAMESPACE_ERR", null);
251                     throw new DOMException JavaDoc(DOMException.NAMESPACE_ERR, msg);
252                
253                 }
254                if (prefix.equals("xmlns")) {
255                     if (!namespaceURI.equals(xmlnsURI)){
256                         String JavaDoc msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NAMESPACE_ERR", null);
257                         throw new DOMException JavaDoc(DOMException.NAMESPACE_ERR, msg);
258                     }
259                 } else if (prefix.equals("xml")) {
260                     if (!namespaceURI.equals(xmlURI)) {
261                         String JavaDoc msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NAMESPACE_ERR", null);
262                         throw new DOMException JavaDoc(DOMException.NAMESPACE_ERR, msg);
263                     }
264                 }else if (name.equals("xmlns")) {
265                     String JavaDoc msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.DOM_DOMAIN, "NAMESPACE_ERR", null);
266                     throw new DOMException JavaDoc(DOMException.NAMESPACE_ERR, msg);
267                 }
268             }
269         }
270
271         // update node name with new qualifiedName
272
if (prefix !=null && prefix.length() != 0) {
273             name = prefix + ":" + localName;
274         }
275         else {
276             name = localName;
277         }
278     }
279                                         
280     /**
281      * Introduced in DOM Level 2. <p>
282      *
283      * Returns the local part of the qualified name of this node.
284      * @since WD-DOM-Level-2-19990923
285      */

286     public String JavaDoc getLocalName()
287     {
288         if (needsSyncData()) {
289             synchronizeData();
290         }
291         return localName;
292     }
293     
294     
295     /**
296      * @see org.w3c.dom.TypeInfo#getTypeName()
297      */

298     public String JavaDoc getTypeName() {
299         if (type !=null){
300             if (type instanceof XSSimpleTypeDecl){
301                 return ((XSSimpleTypeDecl)type).getName();
302             }
303             return (String JavaDoc)type;
304         }
305         return null;
306     }
307
308     /**
309      * Introduced in DOM Level 3. <p>
310      * Checks if a type is derived from another by restriction. See:
311      * http://www.w3.org/TR/DOM-Level-3-Core/core.html#TypeInfo-isDerivedFrom
312      *
313      * @param ancestorNS
314      * The namspace of the ancestor type declaration
315      * @param ancestorName
316      * The name of the ancestor type declaration
317      * @param type
318      * The reference type definition
319      *
320      * @return boolean True if the type is derived by restriciton for the
321      * reference type
322      */

323     public boolean isDerivedFrom(String JavaDoc typeNamespaceArg,
324                                  String JavaDoc typeNameArg,
325                                  int derivationMethod) {
326         if (type != null) {
327             if (type instanceof XSSimpleTypeDefinition) {
328                 return ((XSSimpleTypeDecl) type).isDOMDerivedFrom(
329                         typeNamespaceArg, typeNameArg, derivationMethod);
330             }
331         }
332         return false;
333     }
334
335     /**
336      * @see org.w3c.dom.TypeInfo#getTypeNamespace()
337      */

338     public String JavaDoc getTypeNamespace() {
339         if (type !=null) {
340             if (type instanceof XSSimpleTypeDecl){
341                 return ((XSSimpleTypeDecl)type).getNamespace();
342             }
343             return DTD_URI;
344         }
345         return null;
346     }
347     
348 }
349
Popular Tags