6
|
1 |
/*
|
|
2 |
* reserved comment block
|
|
3 |
* DO NOT REMOVE OR ALTER!
|
|
4 |
*/
|
|
5 |
/*
|
|
6 |
* The Apache Software License, Version 1.1
|
|
7 |
*
|
|
8 |
*
|
|
9 |
* Copyright (c) 1999-2003 The Apache Software Foundation.
|
|
10 |
* All rights reserved.
|
|
11 |
*
|
|
12 |
* Redistribution and use in source and binary forms, with or without
|
|
13 |
* modification, are permitted provided that the following conditions
|
|
14 |
* are met:
|
|
15 |
*
|
|
16 |
* 1. Redistributions of source code must retain the above copyright
|
|
17 |
* notice, this list of conditions and the following disclaimer.
|
|
18 |
*
|
|
19 |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
20 |
* notice, this list of conditions and the following disclaimer in
|
|
21 |
* the documentation and/or other materials provided with the
|
|
22 |
* distribution.
|
|
23 |
*
|
|
24 |
* 3. The end-user documentation included with the redistribution,
|
|
25 |
* if any, must include the following acknowledgment:
|
|
26 |
* "This product includes software developed by the
|
|
27 |
* Apache Software Foundation (http://www.apache.org/)."
|
|
28 |
* Alternately, this acknowledgment may appear in the software itself,
|
|
29 |
* if and wherever such third-party acknowledgments normally appear.
|
|
30 |
*
|
|
31 |
* 4. The names "Xerces" and "Apache Software Foundation" must
|
|
32 |
* not be used to endorse or promote products derived from this
|
|
33 |
* software without prior written permission. For written
|
|
34 |
* permission, please contact apache@apache.org.
|
|
35 |
*
|
|
36 |
* 5. Products derived from this software may not be called "Apache",
|
|
37 |
* nor may "Apache" appear in their name, without prior written
|
|
38 |
* permission of the Apache Software Foundation.
|
|
39 |
*
|
|
40 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
|
41 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
42 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
43 |
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
|
44 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
45 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
46 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
|
47 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
48 |
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
49 |
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
50 |
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
51 |
* SUCH DAMAGE.
|
|
52 |
* ====================================================================
|
|
53 |
*
|
|
54 |
* This software consists of voluntary contributions made by many
|
|
55 |
* individuals on behalf of the Apache Software Foundation and was
|
|
56 |
* originally based on software copyright (c) 2002, International
|
|
57 |
* Business Machines, Inc., http://www.apache.org. For more
|
|
58 |
* information on the Apache Software Foundation, please see
|
|
59 |
* <http://www.apache.org/>.
|
|
60 |
*/
|
|
61 |
|
|
62 |
package com.sun.org.apache.xerces.internal.impl;
|
|
63 |
|
|
64 |
import java.io.IOException;
|
|
65 |
|
|
66 |
import com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidatorFilter;
|
|
67 |
import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter;
|
|
68 |
import com.sun.org.apache.xerces.internal.util.XMLAttributesImpl;
|
|
69 |
import com.sun.org.apache.xerces.internal.util.XMLSymbols;
|
|
70 |
import com.sun.org.apache.xerces.internal.xni.NamespaceContext;
|
|
71 |
import com.sun.org.apache.xerces.internal.xni.QName;
|
|
72 |
import com.sun.org.apache.xerces.internal.xni.XMLDocumentHandler;
|
|
73 |
import com.sun.org.apache.xerces.internal.xni.XNIException;
|
|
74 |
import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager;
|
|
75 |
import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;
|
|
76 |
import com.sun.org.apache.xerces.internal.xni.parser.XMLDocumentSource;
|
|
77 |
import javax.xml.stream.events.XMLEvent;
|
|
78 |
|
|
79 |
|
|
80 |
/**
|
|
81 |
* The scanner acts as the source for the document
|
|
82 |
* information which is communicated to the document handler.
|
|
83 |
*
|
|
84 |
* This class scans an XML document, checks if document has a DTD, and if
|
|
85 |
* DTD is not found the scanner will remove the DTD Validator from the pipeline and perform
|
|
86 |
* namespace binding.
|
|
87 |
*
|
|
88 |
* Note: This scanner should only be used when the namespace processing is on!
|
|
89 |
*
|
|
90 |
* <p>
|
|
91 |
* This component requires the following features and properties from the
|
|
92 |
* component manager that uses it:
|
|
93 |
* <ul>
|
|
94 |
* <li>http://xml.org/sax/features/namespaces {true} -- if the value of this
|
|
95 |
* feature is set to false this scanner must not be used.</li>
|
|
96 |
* <li>http://xml.org/sax/features/validation</li>
|
|
97 |
* <li>http://apache.org/xml/features/nonvalidating/load-external-dtd</li>
|
|
98 |
* <li>http://apache.org/xml/features/scanner/notify-char-refs</li>
|
|
99 |
* <li>http://apache.org/xml/features/scanner/notify-builtin-refs</li>
|
|
100 |
* <li>http://apache.org/xml/properties/internal/symbol-table</li>
|
|
101 |
* <li>http://apache.org/xml/properties/internal/error-reporter</li>
|
|
102 |
* <li>http://apache.org/xml/properties/internal/entity-manager</li>
|
|
103 |
* <li>http://apache.org/xml/properties/internal/dtd-scanner</li>
|
|
104 |
* </ul>
|
|
105 |
*
|
|
106 |
* @xerces.internal
|
|
107 |
*
|
|
108 |
* @author Elena Litani, IBM
|
|
109 |
* @author Michael Glavassevich, IBM
|
|
110 |
* @author Sunitha Reddy, Sun Microsystems
|
|
111 |
*/
|
|
112 |
public class XML11NSDocumentScannerImpl extends XML11DocumentScannerImpl {
|
|
113 |
|
|
114 |
/**
|
|
115 |
* If is true, the dtd validator is no longer in the pipeline
|
|
116 |
* and the scanner should bind namespaces
|
|
117 |
*/
|
|
118 |
protected boolean fBindNamespaces;
|
|
119 |
|
|
120 |
/**
|
|
121 |
* If validating parser, make sure we report an error in the
|
|
122 |
* scanner if DTD grammar is missing.
|
|
123 |
*/
|
|
124 |
protected boolean fPerformValidation;
|
|
125 |
|
|
126 |
// private data
|
|
127 |
//
|
|
128 |
|
|
129 |
/** DTD validator */
|
|
130 |
private XMLDTDValidatorFilter fDTDValidator;
|
|
131 |
|
|
132 |
/**
|
|
133 |
* Saw spaces after element name or between attributes.
|
|
134 |
*
|
|
135 |
* This is reserved for the case where scanning of a start element spans
|
|
136 |
* several methods, as is the case when scanning the start of a root element
|
|
137 |
* where a DTD external subset may be read after scanning the element name.
|
|
138 |
*/
|
|
139 |
private boolean fSawSpace;
|
|
140 |
|
|
141 |
|
|
142 |
/**
|
|
143 |
* The scanner is responsible for removing DTD validator
|
|
144 |
* from the pipeline if it is not needed.
|
|
145 |
*
|
|
146 |
* @param validator the DTD validator from the pipeline
|
|
147 |
*/
|
|
148 |
public void setDTDValidator(XMLDTDValidatorFilter validator) {
|
|
149 |
fDTDValidator = validator;
|
|
150 |
}
|
|
151 |
|
|
152 |
/**
|
|
153 |
* Scans a start element. This method will handle the binding of
|
|
154 |
* namespace information and notifying the handler of the start
|
|
155 |
* of the element.
|
|
156 |
* <p>
|
|
157 |
* <pre>
|
|
158 |
* [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
|
|
159 |
* [40] STag ::= '<' Name (S Attribute)* S? '>'
|
|
160 |
* </pre>
|
|
161 |
* <p>
|
|
162 |
* <strong>Note:</strong> This method assumes that the leading
|
|
163 |
* '<' character has been consumed.
|
|
164 |
* <p>
|
|
165 |
* <strong>Note:</strong> This method uses the fElementQName and
|
|
166 |
* fAttributes variables. The contents of these variables will be
|
|
167 |
* destroyed. The caller should copy important information out of
|
|
168 |
* these variables before calling this method.
|
|
169 |
*
|
|
170 |
* @return True if element is empty. (i.e. It matches
|
|
171 |
* production [44].
|
|
172 |
*/
|
|
173 |
protected boolean scanStartElement() throws IOException, XNIException {
|
|
174 |
|
|
175 |
if (DEBUG_START_END_ELEMENT)
|
|
176 |
System.out.println(">>> scanStartElementNS()");
|
|
177 |
// Note: namespace processing is on by default
|
|
178 |
fEntityScanner.scanQName(fElementQName);
|
|
179 |
// REVISIT - [Q] Why do we need this local variable? -- mrglavas
|
|
180 |
String rawname = fElementQName.rawname;
|
|
181 |
if (fBindNamespaces) {
|
|
182 |
fNamespaceContext.pushContext();
|
|
183 |
if (fScannerState == SCANNER_STATE_ROOT_ELEMENT) {
|
|
184 |
if (fPerformValidation) {
|
|
185 |
fErrorReporter.reportError(
|
|
186 |
XMLMessageFormatter.XML_DOMAIN,
|
|
187 |
"MSG_GRAMMAR_NOT_FOUND",
|
|
188 |
new Object[] { rawname },
|
|
189 |
XMLErrorReporter.SEVERITY_ERROR);
|
|
190 |
|
|
191 |
if (fDoctypeName == null
|
|
192 |
|| !fDoctypeName.equals(rawname)) {
|
|
193 |
fErrorReporter.reportError(
|
|
194 |
XMLMessageFormatter.XML_DOMAIN,
|
|
195 |
"RootElementTypeMustMatchDoctypedecl",
|
|
196 |
new Object[] { fDoctypeName, rawname },
|
|
197 |
XMLErrorReporter.SEVERITY_ERROR);
|
|
198 |
}
|
|
199 |
}
|
|
200 |
}
|
|
201 |
}
|
|
202 |
|
|
203 |
// push element stack
|
|
204 |
fCurrentElement = fElementStack.pushElement(fElementQName);
|
|
205 |
|
|
206 |
// attributes
|
|
207 |
boolean empty = false;
|
|
208 |
fAttributes.removeAllAttributes();
|
|
209 |
do {
|
|
210 |
// spaces
|
|
211 |
boolean sawSpace = fEntityScanner.skipSpaces();
|
|
212 |
|
|
213 |
// end tag?
|
|
214 |
int c = fEntityScanner.peekChar();
|
|
215 |
if (c == '>') {
|
|
216 |
fEntityScanner.scanChar();
|
|
217 |
break;
|
|
218 |
} else if (c == '/') {
|
|
219 |
fEntityScanner.scanChar();
|
|
220 |
if (!fEntityScanner.skipChar('>')) {
|
|
221 |
reportFatalError(
|
|
222 |
"ElementUnterminated",
|
|
223 |
new Object[] { rawname });
|
|
224 |
}
|
|
225 |
empty = true;
|
|
226 |
break;
|
|
227 |
} else if (!isValidNameStartChar(c) || !sawSpace) {
|
|
228 |
// Second chance. Check if this character is a high
|
|
229 |
// surrogate of a valid name start character.
|
|
230 |
if (!isValidNameStartHighSurrogate(c) || !sawSpace) {
|
|
231 |
reportFatalError(
|
|
232 |
"ElementUnterminated",
|
|
233 |
new Object[] { rawname });
|
|
234 |
}
|
|
235 |
}
|
|
236 |
|
|
237 |
// attributes
|
|
238 |
scanAttribute(fAttributes);
|
|
239 |
if (fSecurityManager != null && fAttributes.getLength() > fElementAttributeLimit){
|
|
240 |
fErrorReporter.reportError(XMLMessageFormatter.XML_DOMAIN,
|
|
241 |
"ElementAttributeLimit",
|
|
242 |
new Object[]{rawname, new Integer(fElementAttributeLimit) },
|
|
243 |
XMLErrorReporter.SEVERITY_FATAL_ERROR );
|
|
244 |
}
|
|
245 |
|
|
246 |
} while (true);
|
|
247 |
|
|
248 |
if (fBindNamespaces) {
|
|
249 |
// REVISIT: is it required? forbit xmlns prefix for element
|
|
250 |
if (fElementQName.prefix == XMLSymbols.PREFIX_XMLNS) {
|
|
251 |
fErrorReporter.reportError(
|
|
252 |
XMLMessageFormatter.XMLNS_DOMAIN,
|
|
253 |
"ElementXMLNSPrefix",
|
|
254 |
new Object[] { fElementQName.rawname },
|
|
255 |
XMLErrorReporter.SEVERITY_FATAL_ERROR);
|
|
256 |
}
|
|
257 |
|
|
258 |
// bind the element
|
|
259 |
String prefix =
|
|
260 |
fElementQName.prefix != null
|
|
261 |
? fElementQName.prefix
|
|
262 |
: XMLSymbols.EMPTY_STRING;
|
|
263 |
// assign uri to the element
|
|
264 |
fElementQName.uri = fNamespaceContext.getURI(prefix);
|
|
265 |
// make sure that object in the element stack is updated as well
|
|
266 |
fCurrentElement.uri = fElementQName.uri;
|
|
267 |
|
|
268 |
if (fElementQName.prefix == null && fElementQName.uri != null) {
|
|
269 |
fElementQName.prefix = XMLSymbols.EMPTY_STRING;
|
|
270 |
// making sure that the object in the element stack is updated too.
|
|
271 |
fCurrentElement.prefix = XMLSymbols.EMPTY_STRING;
|
|
272 |
}
|
|
273 |
if (fElementQName.prefix != null && fElementQName.uri == null) {
|
|
274 |
fErrorReporter.reportError(
|
|
275 |
XMLMessageFormatter.XMLNS_DOMAIN,
|
|
276 |
"ElementPrefixUnbound",
|
|
277 |
new Object[] {
|
|
278 |
fElementQName.prefix,
|
|
279 |
fElementQName.rawname },
|
|
280 |
XMLErrorReporter.SEVERITY_FATAL_ERROR);
|
|
281 |
}
|
|
282 |
|
|
283 |
// bind attributes (xmlns are already bound bellow)
|
|
284 |
int length = fAttributes.getLength();
|
|
285 |
for (int i = 0; i < length; i++) {
|
|
286 |
fAttributes.getName(i, fAttributeQName);
|
|
287 |
|
|
288 |
String aprefix =
|
|
289 |
fAttributeQName.prefix != null
|
|
290 |
? fAttributeQName.prefix
|
|
291 |
: XMLSymbols.EMPTY_STRING;
|
|
292 |
String uri = fNamespaceContext.getURI(aprefix);
|
|
293 |
// REVISIT: try removing the first "if" and see if it is faster.
|
|
294 |
//
|
|
295 |
if (fAttributeQName.uri != null
|
|
296 |
&& fAttributeQName.uri == uri) {
|
|
297 |
continue;
|
|
298 |
}
|
|
299 |
if (aprefix != XMLSymbols.EMPTY_STRING) {
|
|
300 |
fAttributeQName.uri = uri;
|
|
301 |
if (uri == null) {
|
|
302 |
fErrorReporter.reportError(
|
|
303 |
XMLMessageFormatter.XMLNS_DOMAIN,
|
|
304 |
"AttributePrefixUnbound",
|
|
305 |
new Object[] {
|
|
306 |
fElementQName.rawname,
|
|
307 |
fAttributeQName.rawname,
|
|
308 |
aprefix },
|
|
309 |
XMLErrorReporter.SEVERITY_FATAL_ERROR);
|
|
310 |
}
|
|
311 |
fAttributes.setURI(i, uri);
|
|
312 |
}
|
|
313 |
}
|
|
314 |
|
|
315 |
if (length > 1) {
|
|
316 |
QName name = fAttributes.checkDuplicatesNS();
|
|
317 |
if (name != null) {
|
|
318 |
if (name.uri != null) {
|
|
319 |
fErrorReporter.reportError(
|
|
320 |
XMLMessageFormatter.XMLNS_DOMAIN,
|
|
321 |
"AttributeNSNotUnique",
|
|
322 |
new Object[] {
|
|
323 |
fElementQName.rawname,
|
|
324 |
name.localpart,
|
|
325 |
name.uri },
|
|
326 |
XMLErrorReporter.SEVERITY_FATAL_ERROR);
|
|
327 |
} else {
|
|
328 |
fErrorReporter.reportError(
|
|
329 |
XMLMessageFormatter.XMLNS_DOMAIN,
|
|
330 |
"AttributeNotUnique",
|
|
331 |
new Object[] {
|
|
332 |
fElementQName.rawname,
|
|
333 |
name.rawname },
|
|
334 |
XMLErrorReporter.SEVERITY_FATAL_ERROR);
|
|
335 |
}
|
|
336 |
}
|
|
337 |
}
|
|
338 |
}
|
|
339 |
|
|
340 |
// call handler
|
|
341 |
|
|
342 |
if (empty) {
|
|
343 |
|
|
344 |
//decrease the markup depth..
|
|
345 |
fMarkupDepth--;
|
|
346 |
|
|
347 |
// check that this element was opened in the same entity
|
|
348 |
if (fMarkupDepth < fEntityStack[fEntityDepth - 1]) {
|
|
349 |
reportFatalError(
|
|
350 |
"ElementEntityMismatch",
|
|
351 |
new Object[] { fCurrentElement.rawname });
|
|
352 |
}
|
|
353 |
|
|
354 |
fDocumentHandler.emptyElement(fElementQName, fAttributes, null);
|
|
355 |
|
|
356 |
/*if (fBindNamespaces) {
|
|
357 |
fNamespaceContext.popContext();
|
|
358 |
}*/
|
|
359 |
fScanEndElement = true;
|
|
360 |
|
|
361 |
//pop the element off the stack..
|
|
362 |
fElementStack.popElement();
|
|
363 |
} else {
|
|
364 |
|
|
365 |
if(dtdGrammarUtil != null)
|
|
366 |
dtdGrammarUtil.startElement(fElementQName, fAttributes);
|
|
367 |
|
|
368 |
if (fDocumentHandler != null)
|
|
369 |
fDocumentHandler.startElement(fElementQName, fAttributes, null);
|
|
370 |
}
|
|
371 |
|
|
372 |
if (DEBUG_START_END_ELEMENT)
|
|
373 |
System.out.println("<<< scanStartElement(): " + empty);
|
|
374 |
return empty;
|
|
375 |
|
|
376 |
} // scanStartElement():boolean
|
|
377 |
|
|
378 |
/**
|
|
379 |
* Scans the name of an element in a start or empty tag.
|
|
380 |
*
|
|
381 |
* @see #scanStartElement()
|
|
382 |
*/
|
|
383 |
protected void scanStartElementName ()
|
|
384 |
throws IOException, XNIException {
|
|
385 |
// Note: namespace processing is on by default
|
|
386 |
fEntityScanner.scanQName(fElementQName);
|
|
387 |
// Must skip spaces here because the DTD scanner
|
|
388 |
// would consume them at the end of the external subset.
|
|
389 |
fSawSpace = fEntityScanner.skipSpaces();
|
|
390 |
} // scanStartElementName()
|
|
391 |
|
|
392 |
/**
|
|
393 |
* Scans the remainder of a start or empty tag after the element name.
|
|
394 |
*
|
|
395 |
* @see #scanStartElement
|
|
396 |
* @return True if element is empty.
|
|
397 |
*/
|
|
398 |
protected boolean scanStartElementAfterName()
|
|
399 |
throws IOException, XNIException {
|
|
400 |
|
|
401 |
// REVISIT - [Q] Why do we need this local variable? -- mrglavas
|
|
402 |
String rawname = fElementQName.rawname;
|
|
403 |
if (fBindNamespaces) {
|
|
404 |
fNamespaceContext.pushContext();
|
|
405 |
if (fScannerState == SCANNER_STATE_ROOT_ELEMENT) {
|
|
406 |
if (fPerformValidation) {
|
|
407 |
fErrorReporter.reportError(
|
|
408 |
XMLMessageFormatter.XML_DOMAIN,
|
|
409 |
"MSG_GRAMMAR_NOT_FOUND",
|
|
410 |
new Object[] { rawname },
|
|
411 |
XMLErrorReporter.SEVERITY_ERROR);
|
|
412 |
|
|
413 |
if (fDoctypeName == null
|
|
414 |
|| !fDoctypeName.equals(rawname)) {
|
|
415 |
fErrorReporter.reportError(
|
|
416 |
XMLMessageFormatter.XML_DOMAIN,
|
|
417 |
"RootElementTypeMustMatchDoctypedecl",
|
|
418 |
new Object[] { fDoctypeName, rawname },
|
|
419 |
XMLErrorReporter.SEVERITY_ERROR);
|
|
420 |
}
|
|
421 |
}
|
|
422 |
}
|
|
423 |
}
|
|
424 |
|
|
425 |
// push element stack
|
|
426 |
fCurrentElement = fElementStack.pushElement(fElementQName);
|
|
427 |
|
|
428 |
// attributes
|
|
429 |
boolean empty = false;
|
|
430 |
fAttributes.removeAllAttributes();
|
|
431 |
do {
|
|
432 |
|
|
433 |
// end tag?
|
|
434 |
int c = fEntityScanner.peekChar();
|
|
435 |
if (c == '>') {
|
|
436 |
fEntityScanner.scanChar();
|
|
437 |
break;
|
|
438 |
} else if (c == '/') {
|
|
439 |
fEntityScanner.scanChar();
|
|
440 |
if (!fEntityScanner.skipChar('>')) {
|
|
441 |
reportFatalError(
|
|
442 |
"ElementUnterminated",
|
|
443 |
new Object[] { rawname });
|
|
444 |
}
|
|
445 |
empty = true;
|
|
446 |
break;
|
|
447 |
} else if (!isValidNameStartChar(c) || !fSawSpace) {
|
|
448 |
// Second chance. Check if this character is a high
|
|
449 |
// surrogate of a valid name start character.
|
|
450 |
if (!isValidNameStartHighSurrogate(c) || !fSawSpace) {
|
|
451 |
reportFatalError(
|
|
452 |
"ElementUnterminated",
|
|
453 |
new Object[] { rawname });
|
|
454 |
}
|
|
455 |
}
|
|
456 |
|
|
457 |
// attributes
|
|
458 |
scanAttribute(fAttributes);
|
|
459 |
|
|
460 |
// spaces
|
|
461 |
fSawSpace = fEntityScanner.skipSpaces();
|
|
462 |
|
|
463 |
} while (true);
|
|
464 |
|
|
465 |
if (fBindNamespaces) {
|
|
466 |
// REVISIT: is it required? forbit xmlns prefix for element
|
|
467 |
if (fElementQName.prefix == XMLSymbols.PREFIX_XMLNS) {
|
|
468 |
fErrorReporter.reportError(
|
|
469 |
XMLMessageFormatter.XMLNS_DOMAIN,
|
|
470 |
"ElementXMLNSPrefix",
|
|
471 |
new Object[] { fElementQName.rawname },
|
|
472 |
XMLErrorReporter.SEVERITY_FATAL_ERROR);
|
|
473 |
}
|
|
474 |
|
|
475 |
// bind the element
|
|
476 |
String prefix =
|
|
477 |
fElementQName.prefix != null
|
|
478 |
? fElementQName.prefix
|
|
479 |
: XMLSymbols.EMPTY_STRING;
|
|
480 |
// assign uri to the element
|
|
481 |
fElementQName.uri = fNamespaceContext.getURI(prefix);
|
|
482 |
// make sure that object in the element stack is updated as well
|
|
483 |
fCurrentElement.uri = fElementQName.uri;
|
|
484 |
|
|
485 |
if (fElementQName.prefix == null && fElementQName.uri != null) {
|
|
486 |
fElementQName.prefix = XMLSymbols.EMPTY_STRING;
|
|
487 |
// making sure that the object in the element stack is updated too.
|
|
488 |
fCurrentElement.prefix = XMLSymbols.EMPTY_STRING;
|
|
489 |
}
|
|
490 |
if (fElementQName.prefix != null && fElementQName.uri == null) {
|
|
491 |
fErrorReporter.reportError(
|
|
492 |
XMLMessageFormatter.XMLNS_DOMAIN,
|
|
493 |
"ElementPrefixUnbound",
|
|
494 |
new Object[] {
|
|
495 |
fElementQName.prefix,
|
|
496 |
fElementQName.rawname },
|
|
497 |
XMLErrorReporter.SEVERITY_FATAL_ERROR);
|
|
498 |
}
|
|
499 |
|
|
500 |
// bind attributes (xmlns are already bound bellow)
|
|
501 |
int length = fAttributes.getLength();
|
|
502 |
for (int i = 0; i < length; i++) {
|
|
503 |
fAttributes.getName(i, fAttributeQName);
|
|
504 |
|
|
505 |
String aprefix =
|
|
506 |
fAttributeQName.prefix != null
|
|
507 |
? fAttributeQName.prefix
|
|
508 |
: XMLSymbols.EMPTY_STRING;
|
|
509 |
String uri = fNamespaceContext.getURI(aprefix);
|
|
510 |
// REVISIT: try removing the first "if" and see if it is faster.
|
|
511 |
//
|
|
512 |
if (fAttributeQName.uri != null
|
|
513 |
&& fAttributeQName.uri == uri) {
|
|
514 |
continue;
|
|
515 |
}
|
|
516 |
if (aprefix != XMLSymbols.EMPTY_STRING) {
|
|
517 |
fAttributeQName.uri = uri;
|
|
518 |
if (uri == null) {
|
|
519 |
fErrorReporter.reportError(
|
|
520 |
XMLMessageFormatter.XMLNS_DOMAIN,
|
|
521 |
"AttributePrefixUnbound",
|
|
522 |
new Object[] {
|
|
523 |
fElementQName.rawname,
|
|
524 |
fAttributeQName.rawname,
|
|
525 |
aprefix },
|
|
526 |
XMLErrorReporter.SEVERITY_FATAL_ERROR);
|
|
527 |
}
|
|
528 |
fAttributes.setURI(i, uri);
|
|
529 |
}
|
|
530 |
}
|
|
531 |
|
|
532 |
if (length > 1) {
|
|
533 |
QName name = fAttributes.checkDuplicatesNS();
|
|
534 |
if (name != null) {
|
|
535 |
if (name.uri != null) {
|
|
536 |
fErrorReporter.reportError(
|
|
537 |
XMLMessageFormatter.XMLNS_DOMAIN,
|
|
538 |
"AttributeNSNotUnique",
|
|
539 |
new Object[] {
|
|
540 |
fElementQName.rawname,
|
|
541 |
name.localpart,
|
|
542 |
name.uri },
|
|
543 |
XMLErrorReporter.SEVERITY_FATAL_ERROR);
|
|
544 |
} else {
|
|
545 |
fErrorReporter.reportError(
|
|
546 |
XMLMessageFormatter.XMLNS_DOMAIN,
|
|
547 |
"AttributeNotUnique",
|
|
548 |
new Object[] {
|
|
549 |
fElementQName.rawname,
|
|
550 |
name.rawname },
|
|
551 |
XMLErrorReporter.SEVERITY_FATAL_ERROR);
|
|
552 |
}
|
|
553 |
}
|
|
554 |
}
|
|
555 |
}
|
|
556 |
|
|
557 |
// call handler
|
|
558 |
if (fDocumentHandler != null) {
|
|
559 |
if (empty) {
|
|
560 |
|
|
561 |
//decrease the markup depth..
|
|
562 |
fMarkupDepth--;
|
|
563 |
|
|
564 |
// check that this element was opened in the same entity
|
|
565 |
if (fMarkupDepth < fEntityStack[fEntityDepth - 1]) {
|
|
566 |
reportFatalError(
|
|
567 |
"ElementEntityMismatch",
|
|
568 |
new Object[] { fCurrentElement.rawname });
|
|
569 |
}
|
|
570 |
|
|
571 |
fDocumentHandler.emptyElement(fElementQName, fAttributes, null);
|
|
572 |
|
|
573 |
if (fBindNamespaces) {
|
|
574 |
fNamespaceContext.popContext();
|
|
575 |
}
|
|
576 |
//pop the element off the stack..
|
|
577 |
fElementStack.popElement();
|
|
578 |
} else {
|
|
579 |
fDocumentHandler.startElement(fElementQName, fAttributes, null);
|
|
580 |
}
|
|
581 |
}
|
|
582 |
|
|
583 |
if (DEBUG_START_END_ELEMENT)
|
|
584 |
System.out.println("<<< scanStartElementAfterName(): " + empty);
|
|
585 |
return empty;
|
|
586 |
|
|
587 |
} // scanStartElementAfterName()
|
|
588 |
|
|
589 |
/**
|
|
590 |
* Scans an attribute.
|
|
591 |
* <p>
|
|
592 |
* <pre>
|
|
593 |
* [41] Attribute ::= Name Eq AttValue
|
|
594 |
* </pre>
|
|
595 |
* <p>
|
|
596 |
* <strong>Note:</strong> This method assumes that the next
|
|
597 |
* character on the stream is the first character of the attribute
|
|
598 |
* name.
|
|
599 |
* <p>
|
|
600 |
* <strong>Note:</strong> This method uses the fAttributeQName and
|
|
601 |
* fQName variables. The contents of these variables will be
|
|
602 |
* destroyed.
|
|
603 |
*
|
|
604 |
* @param attributes The attributes list for the scanned attribute.
|
|
605 |
*/
|
|
606 |
protected void scanAttribute(XMLAttributesImpl attributes)
|
|
607 |
throws IOException, XNIException {
|
|
608 |
if (DEBUG_START_END_ELEMENT)
|
|
609 |
System.out.println(">>> scanAttribute()");
|
|
610 |
|
|
611 |
// name
|
|
612 |
fEntityScanner.scanQName(fAttributeQName);
|
|
613 |
|
|
614 |
// equals
|
|
615 |
fEntityScanner.skipSpaces();
|
|
616 |
if (!fEntityScanner.skipChar('=')) {
|
|
617 |
reportFatalError(
|
|
618 |
"EqRequiredInAttribute",
|
|
619 |
new Object[] {
|
|
620 |
fCurrentElement.rawname,
|
|
621 |
fAttributeQName.rawname });
|
|
622 |
}
|
|
623 |
fEntityScanner.skipSpaces();
|
|
624 |
|
|
625 |
// content
|
|
626 |
int attrIndex;
|
|
627 |
|
|
628 |
if (fBindNamespaces) {
|
|
629 |
attrIndex = attributes.getLength();
|
|
630 |
attributes.addAttributeNS(
|
|
631 |
fAttributeQName,
|
|
632 |
XMLSymbols.fCDATASymbol,
|
|
633 |
null);
|
|
634 |
} else {
|
|
635 |
int oldLen = attributes.getLength();
|
|
636 |
attrIndex =
|
|
637 |
attributes.addAttribute(
|
|
638 |
fAttributeQName,
|
|
639 |
XMLSymbols.fCDATASymbol,
|
|
640 |
null);
|
|
641 |
|
|
642 |
// WFC: Unique Att Spec
|
|
643 |
if (oldLen == attributes.getLength()) {
|
|
644 |
reportFatalError(
|
|
645 |
"AttributeNotUnique",
|
|
646 |
new Object[] {
|
|
647 |
fCurrentElement.rawname,
|
|
648 |
fAttributeQName.rawname });
|
|
649 |
}
|
|
650 |
}
|
|
651 |
|
|
652 |
//REVISIT: one more case needs to be included: external PE and standalone is no
|
|
653 |
boolean isVC = fHasExternalDTD && !fStandalone;
|
|
654 |
|
|
655 |
// REVISIT: it seems that this function should not take attributes, and length
|
|
656 |
scanAttributeValue(
|
|
657 |
this.fTempString,
|
|
658 |
fTempString2,
|
|
659 |
fAttributeQName.rawname,
|
|
660 |
isVC,
|
|
661 |
fCurrentElement.rawname);
|
|
662 |
String value = fTempString.toString();
|
|
663 |
attributes.setValue(attrIndex, value);
|
|
664 |
attributes.setNonNormalizedValue(attrIndex, fTempString2.toString());
|
|
665 |
attributes.setSpecified(attrIndex, true);
|
|
666 |
|
|
667 |
// record namespace declarations if any.
|
|
668 |
if (fBindNamespaces) {
|
|
669 |
|
|
670 |
String localpart = fAttributeQName.localpart;
|
|
671 |
String prefix =
|
|
672 |
fAttributeQName.prefix != null
|
|
673 |
? fAttributeQName.prefix
|
|
674 |
: XMLSymbols.EMPTY_STRING;
|
|
675 |
// when it's of form xmlns="..." or xmlns:prefix="...",
|
|
676 |
// it's a namespace declaration. but prefix:xmlns="..." isn't.
|
|
677 |
if (prefix == XMLSymbols.PREFIX_XMLNS
|
|
678 |
|| prefix == XMLSymbols.EMPTY_STRING
|
|
679 |
&& localpart == XMLSymbols.PREFIX_XMLNS) {
|
|
680 |
|
|
681 |
// get the internalized value of this attribute
|
|
682 |
String uri = fSymbolTable.addSymbol(value);
|
|
683 |
|
|
684 |
// 1. "xmlns" can't be bound to any namespace
|
|
685 |
if (prefix == XMLSymbols.PREFIX_XMLNS
|
|
686 |
&& localpart == XMLSymbols.PREFIX_XMLNS) {
|
|
687 |
fErrorReporter.reportError(
|
|
688 |
XMLMessageFormatter.XMLNS_DOMAIN,
|
|
689 |
"CantBindXMLNS",
|
|
690 |
new Object[] { fAttributeQName },
|
|
691 |
XMLErrorReporter.SEVERITY_FATAL_ERROR);
|
|
692 |
}
|
|
693 |
|
|
694 |
// 2. the namespace for "xmlns" can't be bound to any prefix
|
|
695 |
if (uri == NamespaceContext.XMLNS_URI) {
|
|
696 |
fErrorReporter.reportError(
|
|
697 |
XMLMessageFormatter.XMLNS_DOMAIN,
|
|
698 |
"CantBindXMLNS",
|
|
699 |
new Object[] { fAttributeQName },
|
|
700 |
XMLErrorReporter.SEVERITY_FATAL_ERROR);
|
|
701 |
}
|
|
702 |
|
|
703 |
// 3. "xml" can't be bound to any other namespace than it's own
|
|
704 |
if (localpart == XMLSymbols.PREFIX_XML) {
|
|
705 |
if (uri != NamespaceContext.XML_URI) {
|
|
706 |
fErrorReporter.reportError(
|
|
707 |
XMLMessageFormatter.XMLNS_DOMAIN,
|
|
708 |
"CantBindXML",
|
|
709 |
new Object[] { fAttributeQName },
|
|
710 |
XMLErrorReporter.SEVERITY_FATAL_ERROR);
|
|
711 |
}
|
|
712 |
}
|
|
713 |
// 4. the namespace for "xml" can't be bound to any other prefix
|
|
714 |
else {
|
|
715 |
if (uri == NamespaceContext.XML_URI) {
|
|
716 |
fErrorReporter.reportError(
|
|
717 |
XMLMessageFormatter.XMLNS_DOMAIN,
|
|
718 |
"CantBindXML",
|
|
719 |
new Object[] { fAttributeQName },
|
|
720 |
XMLErrorReporter.SEVERITY_FATAL_ERROR);
|
|
721 |
}
|
|
722 |
}
|
|
723 |
|
|
724 |
prefix =
|
|
725 |
localpart != XMLSymbols.PREFIX_XMLNS
|
|
726 |
? localpart
|
|
727 |
: XMLSymbols.EMPTY_STRING;
|
|
728 |
|
|
729 |
// Declare prefix in context. Removing the association between a prefix and a
|
|
730 |
// namespace name is permitted in XML 1.1, so if the uri value is the empty string,
|
|
731 |
// the prefix is being unbound. -- mrglavas
|
|
732 |
fNamespaceContext.declarePrefix(
|
|
733 |
prefix,
|
|
734 |
uri.length() != 0 ? uri : null);
|
|
735 |
// bind namespace attribute to a namespace
|
|
736 |
attributes.setURI(
|
|
737 |
attrIndex,
|
|
738 |
fNamespaceContext.getURI(XMLSymbols.PREFIX_XMLNS));
|
|
739 |
|
|
740 |
} else {
|
|
741 |
// attempt to bind attribute
|
|
742 |
if (fAttributeQName.prefix != null) {
|
|
743 |
attributes.setURI(
|
|
744 |
attrIndex,
|
|
745 |
fNamespaceContext.getURI(fAttributeQName.prefix));
|
|
746 |
}
|
|
747 |
}
|
|
748 |
}
|
|
749 |
|
|
750 |
if (DEBUG_START_END_ELEMENT)
|
|
751 |
System.out.println("<<< scanAttribute()");
|
|
752 |
} // scanAttribute(XMLAttributes)
|
|
753 |
|
|
754 |
/**
|
|
755 |
* Scans an end element.
|
|
756 |
* <p>
|
|
757 |
* <pre>
|
|
758 |
* [42] ETag ::= '</' Name S? '>'
|
|
759 |
* </pre>
|
|
760 |
* <p>
|
|
761 |
* <strong>Note:</strong> This method uses the fElementQName variable.
|
|
762 |
* The contents of this variable will be destroyed. The caller should
|
|
763 |
* copy the needed information out of this variable before calling
|
|
764 |
* this method.
|
|
765 |
*
|
|
766 |
* @return The element depth.
|
|
767 |
*/
|
|
768 |
protected int scanEndElement() throws IOException, XNIException {
|
|
769 |
if (DEBUG_START_END_ELEMENT)
|
|
770 |
System.out.println(">>> scanEndElement()");
|
|
771 |
|
|
772 |
// pop context
|
|
773 |
QName endElementName = fElementStack.popElement();
|
|
774 |
|
|
775 |
// Take advantage of the fact that next string _should_ be "fElementQName.rawName",
|
|
776 |
//In scanners most of the time is consumed on checks done for XML characters, we can
|
|
777 |
// optimize on it and avoid the checks done for endElement,
|
|
778 |
//we will also avoid symbol table lookup - neeraj.bajaj@sun.com
|
|
779 |
|
|
780 |
// this should work both for namespace processing true or false...
|
|
781 |
|
|
782 |
//REVISIT: if the string is not the same as expected.. we need to do better error handling..
|
|
783 |
//We can skip this for now... In any case if the string doesn't match -- document is not well formed.
|
|
784 |
|
|
785 |
if (!fEntityScanner.skipString(endElementName.rawname)) {
|
|
786 |
reportFatalError(
|
|
787 |
"ETagRequired",
|
|
788 |
new Object[] { endElementName.rawname });
|
|
789 |
}
|
|
790 |
|
|
791 |
// end
|
|
792 |
fEntityScanner.skipSpaces();
|
|
793 |
if (!fEntityScanner.skipChar('>')) {
|
|
794 |
reportFatalError(
|
|
795 |
"ETagUnterminated",
|
|
796 |
new Object[] { endElementName.rawname });
|
|
797 |
}
|
|
798 |
fMarkupDepth--;
|
|
799 |
|
|
800 |
//we have increased the depth for two markup "<" characters
|
|
801 |
fMarkupDepth--;
|
|
802 |
|
|
803 |
// check that this element was opened in the same entity
|
|
804 |
if (fMarkupDepth < fEntityStack[fEntityDepth - 1]) {
|
|
805 |
reportFatalError(
|
|
806 |
"ElementEntityMismatch",
|
|
807 |
new Object[] { endElementName.rawname });
|
|
808 |
}
|
|
809 |
|
|
810 |
// call handler
|
|
811 |
if (fDocumentHandler != null) {
|
|
812 |
fDocumentHandler.endElement(endElementName, null);
|
|
813 |
|
|
814 |
/*if (fBindNamespaces) {
|
|
815 |
fNamespaceContext.popContext();
|
|
816 |
}*/
|
|
817 |
|
|
818 |
}
|
|
819 |
|
|
820 |
if(dtdGrammarUtil != null)
|
|
821 |
dtdGrammarUtil.endElement(endElementName);
|
|
822 |
|
|
823 |
return fMarkupDepth;
|
|
824 |
|
|
825 |
} // scanEndElement():int
|
|
826 |
|
|
827 |
public void reset(XMLComponentManager componentManager)
|
|
828 |
throws XMLConfigurationException {
|
|
829 |
|
|
830 |
super.reset(componentManager);
|
|
831 |
fPerformValidation = false;
|
|
832 |
fBindNamespaces = false;
|
|
833 |
}
|
|
834 |
|
|
835 |
/** Creates a content Driver. */
|
|
836 |
protected Driver createContentDriver() {
|
|
837 |
return new NS11ContentDriver();
|
|
838 |
} // createContentDriver():Driver
|
|
839 |
|
|
840 |
|
|
841 |
/** return the next state on the input
|
|
842 |
*
|
|
843 |
* @return int
|
|
844 |
*/
|
|
845 |
|
|
846 |
public int next() throws IOException, XNIException {
|
|
847 |
//since namespace context should still be valid when the parser is at the end element state therefore
|
|
848 |
//we pop the context only when next() has been called after the end element state was encountered. - nb.
|
|
849 |
|
|
850 |
if((fScannerLastState == XMLEvent.END_ELEMENT) && fBindNamespaces){
|
|
851 |
fScannerLastState = -1;
|
|
852 |
fNamespaceContext.popContext();
|
|
853 |
}
|
|
854 |
|
|
855 |
return fScannerLastState = super.next();
|
|
856 |
}
|
|
857 |
|
|
858 |
|
|
859 |
/**
|
|
860 |
* Driver to handle content scanning.
|
|
861 |
*/
|
|
862 |
protected final class NS11ContentDriver extends ContentDriver {
|
|
863 |
/**
|
|
864 |
* Scan for root element hook. This method is a hook for
|
|
865 |
* subclasses to add code that handles scanning for the root
|
|
866 |
* element. This method will also attempt to remove DTD validator
|
|
867 |
* from the pipeline, if there is no DTD grammar. If DTD validator
|
|
868 |
* is no longer in the pipeline bind namespaces in the scanner.
|
|
869 |
*
|
|
870 |
*
|
|
871 |
* @return True if the caller should stop and return true which
|
|
872 |
* allows the scanner to switch to a new scanning
|
|
873 |
* Driver. A return value of false indicates that
|
|
874 |
* the content Driver should continue as normal.
|
|
875 |
*/
|
|
876 |
protected boolean scanRootElementHook()
|
|
877 |
throws IOException, XNIException {
|
|
878 |
|
|
879 |
if (fExternalSubsetResolver != null && !fSeenDoctypeDecl
|
|
880 |
&& !fDisallowDoctype && (fValidation || fLoadExternalDTD)) {
|
|
881 |
scanStartElementName();
|
|
882 |
resolveExternalSubsetAndRead();
|
|
883 |
reconfigurePipeline();
|
|
884 |
if (scanStartElementAfterName()) {
|
|
885 |
setScannerState(SCANNER_STATE_TRAILING_MISC);
|
|
886 |
setDriver(fTrailingMiscDriver);
|
|
887 |
return true;
|
|
888 |
}
|
|
889 |
}
|
|
890 |
else {
|
|
891 |
reconfigurePipeline();
|
|
892 |
if (scanStartElement()) {
|
|
893 |
setScannerState(SCANNER_STATE_TRAILING_MISC);
|
|
894 |
setDriver(fTrailingMiscDriver);
|
|
895 |
return true;
|
|
896 |
}
|
|
897 |
}
|
|
898 |
return false;
|
|
899 |
|
|
900 |
} // scanRootElementHook():boolean
|
|
901 |
|
|
902 |
/**
|
|
903 |
* Re-configures pipeline by removing the DTD validator
|
|
904 |
* if no DTD grammar exists. If no validator exists in the
|
|
905 |
* pipeline or there is no DTD grammar, namespace binding
|
|
906 |
* is performed by the scanner in the enclosing class.
|
|
907 |
*/
|
|
908 |
private void reconfigurePipeline() {
|
|
909 |
if (fDTDValidator == null) {
|
|
910 |
fBindNamespaces = true;
|
|
911 |
}
|
|
912 |
else if (!fDTDValidator.hasGrammar()) {
|
|
913 |
fBindNamespaces = true;
|
|
914 |
fPerformValidation = fDTDValidator.validate();
|
|
915 |
// re-configure pipeline
|
|
916 |
XMLDocumentSource source = fDTDValidator.getDocumentSource();
|
|
917 |
XMLDocumentHandler handler = fDTDValidator.getDocumentHandler();
|
|
918 |
source.setDocumentHandler(handler);
|
|
919 |
if (handler != null)
|
|
920 |
handler.setDocumentSource(source);
|
|
921 |
fDTDValidator.setDocumentSource(null);
|
|
922 |
fDTDValidator.setDocumentHandler(null);
|
|
923 |
}
|
|
924 |
} // reconfigurePipeline()
|
|
925 |
}
|
|
926 |
}
|