KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > impl > dtd > XMLNSDTDValidator


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 1999-2002 The Apache Software Foundation.
6  * All rights 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.impl.dtd;
59
60 import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter;
61 import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter;
62 import com.sun.org.apache.xerces.internal.util.XMLSymbols;
63 import com.sun.org.apache.xerces.internal.xni.Augmentations;
64 import com.sun.org.apache.xerces.internal.xni.NamespaceContext;
65 import com.sun.org.apache.xerces.internal.xni.QName;
66 import com.sun.org.apache.xerces.internal.xni.XMLAttributes;
67 import com.sun.org.apache.xerces.internal.xni.XNIException;
68
69
70 /**
71  * The DTD validator. The validator implements a document
72  * filter: receiving document events from the scanner; validating
73  * the content and structure; augmenting the InfoSet, if applicable;
74  * and notifying the parser of the information resulting from the
75  * validation process.
76  * <p> Formerly, this component also handled DTD events and grammar construction.
77  * To facilitate the development of a meaningful DTD grammar caching/preparsing
78  * framework, this functionality has been moved into the XMLDTDLoader
79  * class. Therefore, this class no longer implements the DTDFilter
80  * or DTDContentModelFilter interfaces.
81  * <p>
82  * This component requires the following features and properties from the
83  * component manager that uses it:
84  * <ul>
85  * <li>http://xml.org/sax/features/namespaces</li>
86  * <li>http://xml.org/sax/features/validation</li>
87  * <li>http://apache.org/xml/features/validation/dynamic</li>
88  * <li>http://apache.org/xml/properties/internal/symbol-table</li>
89  * <li>http://apache.org/xml/properties/internal/error-reporter</li>
90  * <li>http://apache.org/xml/properties/internal/grammar-pool</li>
91  * <li>http://apache.org/xml/properties/internal/datatype-validator-factory</li>
92  * </ul>
93  *
94  * @author Elena Litani, IBM
95  *
96  * @version $Id: XMLNSDTDValidator.java,v 1.7 2003/10/10 18:25:40 mrglavas Exp $
97  */

98 public class XMLNSDTDValidator
99               extends XMLDTDValidator{
100
101     /** Attribute QName. */
102     private QName fAttributeQName = new QName();
103
104
105     /** Bind namespaces */
106     protected final void startNamespaceScope (QName element, XMLAttributes attributes,
107                                       Augmentations augs) throws XNIException {
108         
109         // add new namespace context
110
fNamespaceContext.pushContext();
111
112         if (element.prefix == XMLSymbols.PREFIX_XMLNS) {
113             fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN,
114                                        "ElementXMLNSPrefix",
115                                        new Object JavaDoc[]{element.rawname},
116                                        XMLErrorReporter.SEVERITY_FATAL_ERROR);
117         }
118         
119         // search for new namespace bindings
120
int length = attributes.getLength();
121         for (int i = 0; i < length; i++) {
122             String JavaDoc localpart = attributes.getLocalName(i);
123             String JavaDoc prefix = attributes.getPrefix(i);
124             // when it's of form xmlns="..." or xmlns:prefix="...",
125
// it's a namespace declaration. but prefix:xmlns="..." isn't.
126
if (prefix == XMLSymbols.PREFIX_XMLNS ||
127                 prefix == XMLSymbols.EMPTY_STRING && localpart == XMLSymbols.PREFIX_XMLNS) {
128
129                 // get the internalized value of this attribute
130
String JavaDoc uri = fSymbolTable.addSymbol(attributes.getValue(i));
131
132                 // 1. "xmlns" can't be bound to any namespace
133
if (prefix == XMLSymbols.PREFIX_XMLNS && localpart == XMLSymbols.PREFIX_XMLNS) {
134                     fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN,
135                                                "CantBindXMLNS",
136                                                new Object JavaDoc[]{attributes.getQName(i)},
137                                                XMLErrorReporter.SEVERITY_FATAL_ERROR);
138                 }
139                 
140                 // 2. the namespace for "xmlns" can't be bound to any prefix
141
if (uri == NamespaceContext.XMLNS_URI) {
142                     fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN,
143                                                "CantBindXMLNS",
144                                                new Object JavaDoc[]{attributes.getQName(i)},
145                                                XMLErrorReporter.SEVERITY_FATAL_ERROR);
146                 }
147                 
148                 // 3. "xml" can't be bound to any other namespace than it's own
149
if (localpart == XMLSymbols.PREFIX_XML) {
150                     if (uri != NamespaceContext.XML_URI) {
151                         fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN,
152                                                    "CantBindXML",
153                                                    new Object JavaDoc[]{attributes.getQName(i)},
154                                                    XMLErrorReporter.SEVERITY_FATAL_ERROR);
155                     }
156                 }
157                 // 4. the namespace for "xml" can't be bound to any other prefix
158
else {
159                     if (uri ==NamespaceContext.XML_URI) {
160                         fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN,
161                                                    "CantBindXML",
162                                                    new Object JavaDoc[]{attributes.getQName(i)},
163                                                    XMLErrorReporter.SEVERITY_FATAL_ERROR);
164                     }
165                 }
166
167                 prefix = localpart != XMLSymbols.PREFIX_XMLNS ? localpart : XMLSymbols.EMPTY_STRING;
168
169                 // http://www.w3.org/TR/1999/REC-xml-names-19990114/#dt-prefix
170
// We should only report an error if there is a prefix,
171
// that is, the local part is not "xmlns". -SG
172
if (uri == XMLSymbols.EMPTY_STRING && localpart != XMLSymbols.PREFIX_XMLNS) {
173                     fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN,
174                                                "EmptyPrefixedAttName",
175                                                new Object JavaDoc[]{attributes.getQName(i)},
176                                                XMLErrorReporter.SEVERITY_FATAL_ERROR);
177                     continue;
178                 }
179
180                 // declare prefix in context
181
fNamespaceContext.declarePrefix(prefix, uri.length() != 0 ? uri : null);
182             }
183         }
184
185         // bind the element
186
String JavaDoc prefix = element.prefix != null
187                       ? element.prefix : XMLSymbols.EMPTY_STRING;
188         element.uri = fNamespaceContext.getURI(prefix);
189         if (element.prefix == null && element.uri != null) {
190             element.prefix = XMLSymbols.EMPTY_STRING;
191         }
192         if (element.prefix != null && element.uri == null) {
193             fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN,
194                                        "ElementPrefixUnbound",
195                                        new Object JavaDoc[]{element.prefix, element.rawname},
196                                        XMLErrorReporter.SEVERITY_FATAL_ERROR);
197         }
198
199         // bind the attributes
200
for (int i = 0; i < length; i++) {
201             attributes.getName(i, fAttributeQName);
202             String JavaDoc aprefix = fAttributeQName.prefix != null
203                            ? fAttributeQName.prefix : XMLSymbols.EMPTY_STRING;
204             String JavaDoc arawname = fAttributeQName.rawname;
205             if (arawname == XMLSymbols.PREFIX_XMLNS) {
206                 fAttributeQName.uri = fNamespaceContext.getURI(XMLSymbols.PREFIX_XMLNS);
207                 attributes.setName(i, fAttributeQName);
208             }
209             else if (aprefix != XMLSymbols.EMPTY_STRING) {
210                 fAttributeQName.uri = fNamespaceContext.getURI(aprefix);
211                 if (fAttributeQName.uri == null) {
212                     fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN,
213                                                "AttributePrefixUnbound",
214                                                new Object JavaDoc[]{element.rawname,arawname,aprefix},
215                                                XMLErrorReporter.SEVERITY_FATAL_ERROR);
216                 }
217                 attributes.setName(i, fAttributeQName);
218             }
219         }
220
221         // verify that duplicate attributes don't exist
222
// Example: <foo xmlns:a='NS' xmlns:b='NS' a:attr='v1' b:attr='v2'/>
223
int attrCount = attributes.getLength();
224         for (int i = 0; i < attrCount - 1; i++) {
225             String JavaDoc auri = attributes.getURI(i);
226             if (auri == null || auri == NamespaceContext.XMLNS_URI) {
227                 continue;
228             }
229             String JavaDoc alocalpart = attributes.getLocalName(i);
230             for (int j = i + 1; j < attrCount; j++) {
231                 String JavaDoc blocalpart = attributes.getLocalName(j);
232                 String JavaDoc buri = attributes.getURI(j);
233                 if (alocalpart == blocalpart && auri == buri) {
234                     fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN,
235                                                "AttributeNSNotUnique",
236                                                new Object JavaDoc[]{element.rawname,alocalpart, auri},
237                                                XMLErrorReporter.SEVERITY_FATAL_ERROR);
238                 }
239             }
240         }
241
242
243     } // startNamespaceScope(QName,XMLAttributes)
244

245
246     /** Handles end element. */
247     protected void endNamespaceScope(QName element, Augmentations augs, boolean isEmpty)
248         throws XNIException {
249
250         // bind element
251
String JavaDoc eprefix = element.prefix != null ? element.prefix : XMLSymbols.EMPTY_STRING;
252         element.uri = fNamespaceContext.getURI(eprefix);
253         if (element.uri != null) {
254             element.prefix = eprefix;
255         }
256
257         // call handlers
258
if (fDocumentHandler != null) {
259             if (!isEmpty) {
260                 fDocumentHandler.endElement(element, augs);
261             }
262         }
263
264         // pop context
265
fNamespaceContext.popContext();
266
267     } // endNamespaceScope(QName,boolean)
268

269 }
270
Popular Tags