12005
|
1 |
/*
|
|
2 |
* Copyright (c) 2003, 2006, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
*/
|
|
4 |
|
|
5 |
/*
|
|
6 |
* Copyright 2005 The Apache Software Foundation.
|
|
7 |
*
|
|
8 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
9 |
* you may not use this file except in compliance with the License.
|
|
10 |
* You may obtain a copy of the License at
|
|
11 |
*
|
|
12 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
13 |
*
|
|
14 |
* Unless required by applicable law or agreed to in writing, software
|
|
15 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
16 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
17 |
* See the License for the specific language governing permissions and
|
|
18 |
* limitations under the License.
|
|
19 |
*/
|
|
20 |
|
|
21 |
package com.sun.org.apache.xerces.internal.impl;
|
|
22 |
|
|
23 |
|
|
24 |
import com.sun.org.apache.xerces.internal.util.Status;
|
|
25 |
import com.sun.xml.internal.stream.XMLEntityStorage;
|
|
26 |
import java.io.IOException;
|
|
27 |
import java.util.ArrayList;
|
|
28 |
import java.util.HashMap;
|
|
29 |
import javax.xml.stream.events.XMLEvent;
|
|
30 |
import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter;
|
|
31 |
import com.sun.org.apache.xerces.internal.util.SymbolTable;
|
|
32 |
import com.sun.org.apache.xerces.internal.util.XMLChar;
|
|
33 |
import com.sun.org.apache.xerces.internal.util.XMLResourceIdentifierImpl;
|
|
34 |
import com.sun.org.apache.xerces.internal.util.XMLStringBuffer;
|
|
35 |
import com.sun.org.apache.xerces.internal.xni.Augmentations;
|
|
36 |
import com.sun.org.apache.xerces.internal.xni.XMLAttributes;
|
|
37 |
import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier;
|
|
38 |
import com.sun.org.apache.xerces.internal.xni.XMLString;
|
|
39 |
import com.sun.org.apache.xerces.internal.xni.XNIException;
|
|
40 |
import com.sun.org.apache.xerces.internal.xni.parser.XMLComponent;
|
|
41 |
import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager;
|
|
42 |
import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;
|
|
43 |
import com.sun.xml.internal.stream.Entity;
|
|
44 |
|
|
45 |
//import com.sun.xml.stream.XMLEntityManager;
|
|
46 |
//import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter;
|
|
47 |
|
|
48 |
/**
|
|
49 |
* This class is responsible for holding scanning methods common to
|
|
50 |
* scanning the XML document structure and content as well as the DTD
|
|
51 |
* structure and content. Both XMLDocumentScanner and XMLDTDScanner inherit
|
|
52 |
* from this base class.
|
|
53 |
*
|
|
54 |
* <p>
|
|
55 |
* This component requires the following features and properties from the
|
|
56 |
* component manager that uses it:
|
|
57 |
* <ul>
|
|
58 |
* <li>http://xml.org/sax/features/validation</li>
|
|
59 |
* <li>http://apache.org/xml/features/scanner/notify-char-refs</li>
|
|
60 |
* <li>http://apache.org/xml/properties/internal/symbol-table</li>
|
|
61 |
* <li>http://apache.org/xml/properties/internal/error-reporter</li>
|
|
62 |
* <li>http://apache.org/xml/properties/internal/entity-manager</li>
|
|
63 |
* </ul>
|
|
64 |
*
|
|
65 |
* @author Andy Clark, IBM
|
|
66 |
* @author Arnaud Le Hors, IBM
|
|
67 |
* @author Eric Ye, IBM
|
|
68 |
* @author K.Venugopal SUN Microsystems
|
|
69 |
* @author Sunitha Reddy, SUN Microsystems
|
|
70 |
* @version $Id: XMLScanner.java,v 1.12 2010-11-01 04:39:41 joehw Exp $
|
|
71 |
*/
|
|
72 |
public abstract class XMLScanner
|
|
73 |
implements XMLComponent {
|
|
74 |
|
|
75 |
//
|
|
76 |
// Constants
|
|
77 |
//
|
|
78 |
|
|
79 |
// feature identifiers
|
|
80 |
|
|
81 |
/** Feature identifier: namespaces. */
|
|
82 |
protected static final String NAMESPACES =
|
|
83 |
Constants.SAX_FEATURE_PREFIX + Constants.NAMESPACES_FEATURE;
|
|
84 |
|
|
85 |
/** Feature identifier: validation. */
|
|
86 |
protected static final String VALIDATION =
|
|
87 |
Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE;
|
|
88 |
|
|
89 |
/** Feature identifier: notify character references. */
|
|
90 |
protected static final String NOTIFY_CHAR_REFS =
|
|
91 |
Constants.XERCES_FEATURE_PREFIX + Constants.NOTIFY_CHAR_REFS_FEATURE;
|
|
92 |
|
|
93 |
// property identifiers
|
|
94 |
|
|
95 |
protected static final String PARSER_SETTINGS =
|
|
96 |
Constants.XERCES_FEATURE_PREFIX + Constants.PARSER_SETTINGS;
|
|
97 |
/** Property identifier: symbol table. */
|
|
98 |
protected static final String SYMBOL_TABLE =
|
|
99 |
Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY;
|
|
100 |
|
|
101 |
/** Property identifier: error reporter. */
|
|
102 |
protected static final String ERROR_REPORTER =
|
|
103 |
Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY;
|
|
104 |
|
|
105 |
/** Property identifier: entity manager. */
|
|
106 |
protected static final String ENTITY_MANAGER =
|
|
107 |
Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_MANAGER_PROPERTY;
|
|
108 |
|
|
109 |
// debugging
|
|
110 |
|
|
111 |
/** Debug attribute normalization. */
|
|
112 |
protected static final boolean DEBUG_ATTR_NORMALIZATION = false;
|
|
113 |
|
|
114 |
|
|
115 |
//xxx: setting the default value as false, as we dont need to calculate this value
|
|
116 |
//we should have a feature when set to true computes this value
|
|
117 |
private boolean fNeedNonNormalizedValue = false;
|
|
118 |
|
|
119 |
protected ArrayList attributeValueCache = new ArrayList();
|
|
120 |
protected ArrayList stringBufferCache = new ArrayList();
|
|
121 |
protected int fStringBufferIndex = 0;
|
|
122 |
protected boolean fAttributeCacheInitDone = false;
|
|
123 |
protected int fAttributeCacheUsedCount = 0;
|
|
124 |
|
|
125 |
//
|
|
126 |
// Data
|
|
127 |
//
|
|
128 |
|
|
129 |
// features
|
|
130 |
|
|
131 |
/**
|
|
132 |
* Validation. This feature identifier is:
|
|
133 |
* http://xml.org/sax/features/validation
|
|
134 |
*/
|
|
135 |
protected boolean fValidation = false;
|
|
136 |
|
|
137 |
/** Namespaces. */
|
|
138 |
protected boolean fNamespaces;
|
|
139 |
|
|
140 |
/** Character references notification. */
|
|
141 |
protected boolean fNotifyCharRefs = false;
|
|
142 |
|
|
143 |
/** Internal parser-settings feature */
|
|
144 |
protected boolean fParserSettings = true;
|
|
145 |
|
|
146 |
// properties
|
|
147 |
|
|
148 |
protected PropertyManager fPropertyManager = null ;
|
|
149 |
/** Symbol table. */
|
|
150 |
protected SymbolTable fSymbolTable;
|
|
151 |
|
|
152 |
/** Error reporter. */
|
|
153 |
protected XMLErrorReporter fErrorReporter;
|
|
154 |
|
|
155 |
/** Entity manager. */
|
|
156 |
//protected XMLEntityManager fEntityManager = PropertyManager.getEntityManager();
|
|
157 |
protected XMLEntityManager fEntityManager = null ;
|
|
158 |
|
|
159 |
/** xxx this should be available from EntityManager Entity storage */
|
|
160 |
protected XMLEntityStorage fEntityStore = null ;
|
|
161 |
|
|
162 |
// protected data
|
|
163 |
|
|
164 |
/** event type */
|
|
165 |
protected XMLEvent fEvent ;
|
|
166 |
|
|
167 |
/** Entity scanner, this alwasy works on last entity that was opened. */
|
|
168 |
protected XMLEntityScanner fEntityScanner = null;
|
|
169 |
|
|
170 |
/** Entity depth. */
|
|
171 |
protected int fEntityDepth;
|
|
172 |
|
|
173 |
/** Literal value of the last character refence scanned. */
|
|
174 |
protected String fCharRefLiteral = null;
|
|
175 |
|
|
176 |
/** Scanning attribute. */
|
|
177 |
protected boolean fScanningAttribute;
|
|
178 |
|
|
179 |
/** Report entity boundary. */
|
|
180 |
protected boolean fReportEntity;
|
|
181 |
|
|
182 |
// symbols
|
|
183 |
|
|
184 |
/** Symbol: "version". */
|
|
185 |
protected final static String fVersionSymbol = "version".intern();
|
|
186 |
|
|
187 |
/** Symbol: "encoding". */
|
|
188 |
protected final static String fEncodingSymbol = "encoding".intern();
|
|
189 |
|
|
190 |
/** Symbol: "standalone". */
|
|
191 |
protected final static String fStandaloneSymbol = "standalone".intern();
|
|
192 |
|
|
193 |
/** Symbol: "amp". */
|
|
194 |
protected final static String fAmpSymbol = "amp".intern();
|
|
195 |
|
|
196 |
/** Symbol: "lt". */
|
|
197 |
protected final static String fLtSymbol = "lt".intern();
|
|
198 |
|
|
199 |
/** Symbol: "gt". */
|
|
200 |
protected final static String fGtSymbol = "gt".intern();
|
|
201 |
|
|
202 |
/** Symbol: "quot". */
|
|
203 |
protected final static String fQuotSymbol = "quot".intern();
|
|
204 |
|
|
205 |
/** Symbol: "apos". */
|
|
206 |
protected final static String fAposSymbol = "apos".intern();
|
|
207 |
|
|
208 |
// temporary variables
|
|
209 |
|
|
210 |
// NOTE: These objects are private to help prevent accidental modification
|
|
211 |
// of values by a subclass. If there were protected *and* the sub-
|
|
212 |
// modified the values, it would be difficult to track down the real
|
|
213 |
// cause of the bug. By making these private, we avoid this
|
|
214 |
// possibility.
|
|
215 |
|
|
216 |
/** String. */
|
|
217 |
private XMLString fString = new XMLString();
|
|
218 |
|
|
219 |
/** String buffer. */
|
|
220 |
private XMLStringBuffer fStringBuffer = new XMLStringBuffer();
|
|
221 |
|
|
222 |
/** String buffer. */
|
|
223 |
private XMLStringBuffer fStringBuffer2 = new XMLStringBuffer();
|
|
224 |
|
|
225 |
/** String buffer. */
|
|
226 |
private XMLStringBuffer fStringBuffer3 = new XMLStringBuffer();
|
|
227 |
|
|
228 |
// temporary location for Resource identification information.
|
|
229 |
protected XMLResourceIdentifierImpl fResourceIdentifier = new XMLResourceIdentifierImpl();
|
|
230 |
int initialCacheCount = 6;
|
|
231 |
//
|
|
232 |
// XMLComponent methods
|
|
233 |
//
|
|
234 |
|
|
235 |
/**
|
|
236 |
*
|
|
237 |
*
|
|
238 |
* @param componentManager The component manager.
|
|
239 |
*
|
|
240 |
* @throws SAXException Throws exception if required features and
|
|
241 |
* properties cannot be found.
|
|
242 |
*/
|
|
243 |
public void reset(XMLComponentManager componentManager)
|
|
244 |
throws XMLConfigurationException {
|
|
245 |
|
|
246 |
fParserSettings = componentManager.getFeature(PARSER_SETTINGS, true);
|
|
247 |
|
|
248 |
if (!fParserSettings) {
|
|
249 |
// parser settings have not been changed
|
|
250 |
init();
|
|
251 |
return;
|
|
252 |
}
|
|
253 |
|
|
254 |
|
|
255 |
// Xerces properties
|
|
256 |
fSymbolTable = (SymbolTable)componentManager.getProperty(SYMBOL_TABLE);
|
|
257 |
fErrorReporter = (XMLErrorReporter)componentManager.getProperty(ERROR_REPORTER);
|
|
258 |
fEntityManager = (XMLEntityManager)componentManager.getProperty(ENTITY_MANAGER);
|
|
259 |
|
|
260 |
//this step is extra because we have separated the storage of entity
|
|
261 |
fEntityStore = fEntityManager.getEntityStore() ;
|
|
262 |
|
|
263 |
// sax features
|
|
264 |
fValidation = componentManager.getFeature(VALIDATION, false);
|
|
265 |
fNamespaces = componentManager.getFeature(NAMESPACES, true);
|
|
266 |
fNotifyCharRefs = componentManager.getFeature(NOTIFY_CHAR_REFS, false);
|
|
267 |
|
|
268 |
init();
|
|
269 |
} // reset(XMLComponentManager)
|
|
270 |
|
|
271 |
protected void setPropertyManager(PropertyManager propertyManager){
|
|
272 |
fPropertyManager = propertyManager ;
|
|
273 |
}
|
|
274 |
|
|
275 |
/**
|
|
276 |
* Sets the value of a property during parsing.
|
|
277 |
*
|
|
278 |
* @param propertyId
|
|
279 |
* @param value
|
|
280 |
*/
|
|
281 |
public void setProperty(String propertyId, Object value)
|
|
282 |
throws XMLConfigurationException {
|
|
283 |
|
|
284 |
// Xerces properties
|
|
285 |
if (propertyId.startsWith(Constants.XERCES_PROPERTY_PREFIX)) {
|
|
286 |
String property =
|
|
287 |
propertyId.substring(Constants.XERCES_PROPERTY_PREFIX.length());
|
|
288 |
if (property.equals(Constants.SYMBOL_TABLE_PROPERTY)) {
|
|
289 |
fSymbolTable = (SymbolTable)value;
|
|
290 |
} else if (property.equals(Constants.ERROR_REPORTER_PROPERTY)) {
|
|
291 |
fErrorReporter = (XMLErrorReporter)value;
|
|
292 |
} else if (property.equals(Constants.ENTITY_MANAGER_PROPERTY)) {
|
|
293 |
fEntityManager = (XMLEntityManager)value;
|
|
294 |
}
|
|
295 |
}
|
|
296 |
/*else if(propertyId.equals(Constants.STAX_PROPERTIES)){
|
|
297 |
fStaxProperties = (HashMap)value;
|
|
298 |
//TODO::discuss with neeraj what are his thoughts on passing properties.
|
|
299 |
//For now use this
|
|
300 |
}*/
|
|
301 |
|
|
302 |
} // setProperty(String,Object)
|
|
303 |
|
|
304 |
/*
|
|
305 |
* Sets the feature of the scanner.
|
|
306 |
*/
|
|
307 |
public void setFeature(String featureId, boolean value)
|
|
308 |
throws XMLConfigurationException {
|
|
309 |
|
|
310 |
if (VALIDATION.equals(featureId)) {
|
|
311 |
fValidation = value;
|
|
312 |
} else if (NOTIFY_CHAR_REFS.equals(featureId)) {
|
|
313 |
fNotifyCharRefs = value;
|
|
314 |
}
|
|
315 |
}
|
|
316 |
|
|
317 |
/*
|
|
318 |
* Gets the state of the feature of the scanner.
|
|
319 |
*/
|
|
320 |
public boolean getFeature(String featureId)
|
|
321 |
throws XMLConfigurationException {
|
|
322 |
|
|
323 |
if (VALIDATION.equals(featureId)) {
|
|
324 |
return fValidation;
|
|
325 |
} else if (NOTIFY_CHAR_REFS.equals(featureId)) {
|
|
326 |
return fNotifyCharRefs;
|
|
327 |
}
|
|
328 |
throw new XMLConfigurationException(Status.NOT_RECOGNIZED, featureId);
|
|
329 |
}
|
|
330 |
|
|
331 |
//
|
|
332 |
// Protected methods
|
|
333 |
//
|
|
334 |
|
|
335 |
// anybody calling this had better have set Symtoltable!
|
|
336 |
protected void reset() {
|
|
337 |
init();
|
|
338 |
|
|
339 |
// DTD preparsing defaults:
|
|
340 |
fValidation = true;
|
|
341 |
fNotifyCharRefs = false;
|
|
342 |
|
|
343 |
}
|
|
344 |
|
|
345 |
public void reset(PropertyManager propertyManager) {
|
|
346 |
init();
|
|
347 |
// Xerces properties
|
|
348 |
fSymbolTable = (SymbolTable)propertyManager.getProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY);
|
|
349 |
|
|
350 |
fErrorReporter = (XMLErrorReporter)propertyManager.getProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY);
|
|
351 |
|
|
352 |
fEntityManager = (XMLEntityManager)propertyManager.getProperty(ENTITY_MANAGER);
|
|
353 |
fEntityStore = fEntityManager.getEntityStore() ;
|
|
354 |
fEntityScanner = (XMLEntityScanner)fEntityManager.getEntityScanner() ;
|
|
355 |
//fEntityManager.reset();
|
|
356 |
// DTD preparsing defaults:
|
|
357 |
fValidation = false;
|
|
358 |
fNotifyCharRefs = false;
|
|
359 |
|
|
360 |
}
|
|
361 |
// common scanning methods
|
|
362 |
|
|
363 |
/**
|
|
364 |
* Scans an XML or text declaration.
|
|
365 |
* <p>
|
|
366 |
* <pre>
|
|
367 |
* [23] XMLDecl ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>'
|
|
368 |
* [24] VersionInfo ::= S 'version' Eq (' VersionNum ' | " VersionNum ")
|
|
369 |
* [80] EncodingDecl ::= S 'encoding' Eq ('"' EncName '"' | "'" EncName "'" )
|
|
370 |
* [81] EncName ::= [A-Za-z] ([A-Za-z0-9._] | '-')*
|
|
371 |
* [32] SDDecl ::= S 'standalone' Eq (("'" ('yes' | 'no') "'")
|
|
372 |
* | ('"' ('yes' | 'no') '"'))
|
|
373 |
*
|
|
374 |
* [77] TextDecl ::= '<?xml' VersionInfo? EncodingDecl S? '?>'
|
|
375 |
* </pre>
|
|
376 |
*
|
|
377 |
* @param scanningTextDecl True if a text declaration is to
|
|
378 |
* be scanned instead of an XML
|
|
379 |
* declaration.
|
|
380 |
* @param pseudoAttributeValues An array of size 3 to return the version,
|
|
381 |
* encoding and standalone pseudo attribute values
|
|
382 |
* (in that order).
|
|
383 |
*
|
|
384 |
* <strong>Note:</strong> This method uses fString, anything in it
|
|
385 |
* at the time of calling is lost.
|
|
386 |
*/
|
|
387 |
protected void scanXMLDeclOrTextDecl(boolean scanningTextDecl,
|
|
388 |
String[] pseudoAttributeValues)
|
|
389 |
throws IOException, XNIException {
|
|
390 |
|
|
391 |
// pseudo-attribute values
|
|
392 |
String version = null;
|
|
393 |
String encoding = null;
|
|
394 |
String standalone = null;
|
|
395 |
|
|
396 |
// scan pseudo-attributes
|
|
397 |
final int STATE_VERSION = 0;
|
|
398 |
final int STATE_ENCODING = 1;
|
|
399 |
final int STATE_STANDALONE = 2;
|
|
400 |
final int STATE_DONE = 3;
|
|
401 |
int state = STATE_VERSION;
|
|
402 |
|
|
403 |
boolean dataFoundForTarget = false;
|
|
404 |
boolean sawSpace = fEntityScanner.skipSpaces();
|
|
405 |
while (fEntityScanner.peekChar() != '?') {
|
|
406 |
dataFoundForTarget = true;
|
|
407 |
String name = scanPseudoAttribute(scanningTextDecl, fString);
|
|
408 |
switch (state) {
|
|
409 |
case STATE_VERSION: {
|
|
410 |
if (name.equals(fVersionSymbol)) {
|
|
411 |
if (!sawSpace) {
|
|
412 |
reportFatalError(scanningTextDecl
|
|
413 |
? "SpaceRequiredBeforeVersionInTextDecl"
|
|
414 |
: "SpaceRequiredBeforeVersionInXMLDecl",
|
|
415 |
null);
|
|
416 |
}
|
|
417 |
version = fString.toString();
|
|
418 |
state = STATE_ENCODING;
|
|
419 |
if (!versionSupported(version)) {
|
|
420 |
reportFatalError("VersionNotSupported",
|
|
421 |
new Object[]{version});
|
|
422 |
}
|
|
423 |
|
|
424 |
if (version.equals("1.1")) {
|
|
425 |
Entity.ScannedEntity top = fEntityManager.getTopLevelEntity();
|
|
426 |
if (top != null && (top.version == null || top.version.equals("1.0"))) {
|
|
427 |
reportFatalError("VersionMismatch", null);
|
|
428 |
}
|
|
429 |
fEntityManager.setScannerVersion(Constants.XML_VERSION_1_1);
|
|
430 |
}
|
|
431 |
|
|
432 |
} else if (name.equals(fEncodingSymbol)) {
|
|
433 |
if (!scanningTextDecl) {
|
|
434 |
reportFatalError("VersionInfoRequired", null);
|
|
435 |
}
|
|
436 |
if (!sawSpace) {
|
|
437 |
reportFatalError(scanningTextDecl
|
|
438 |
? "SpaceRequiredBeforeEncodingInTextDecl"
|
|
439 |
: "SpaceRequiredBeforeEncodingInXMLDecl",
|
|
440 |
null);
|
|
441 |
}
|
|
442 |
encoding = fString.toString();
|
|
443 |
state = scanningTextDecl ? STATE_DONE : STATE_STANDALONE;
|
|
444 |
} else {
|
|
445 |
if (scanningTextDecl) {
|
|
446 |
reportFatalError("EncodingDeclRequired", null);
|
|
447 |
} else {
|
|
448 |
reportFatalError("VersionInfoRequired", null);
|
|
449 |
}
|
|
450 |
}
|
|
451 |
break;
|
|
452 |
}
|
|
453 |
case STATE_ENCODING: {
|
|
454 |
if (name.equals(fEncodingSymbol)) {
|
|
455 |
if (!sawSpace) {
|
|
456 |
reportFatalError(scanningTextDecl
|
|
457 |
? "SpaceRequiredBeforeEncodingInTextDecl"
|
|
458 |
: "SpaceRequiredBeforeEncodingInXMLDecl",
|
|
459 |
null);
|
|
460 |
}
|
|
461 |
encoding = fString.toString();
|
|
462 |
state = scanningTextDecl ? STATE_DONE : STATE_STANDALONE;
|
|
463 |
// TODO: check encoding name; set encoding on
|
|
464 |
// entity scanner
|
|
465 |
} else if (!scanningTextDecl && name.equals(fStandaloneSymbol)) {
|
|
466 |
if (!sawSpace) {
|
|
467 |
reportFatalError("SpaceRequiredBeforeStandalone",
|
|
468 |
null);
|
|
469 |
}
|
|
470 |
standalone = fString.toString();
|
|
471 |
state = STATE_DONE;
|
|
472 |
if (!standalone.equals("yes") && !standalone.equals("no")) {
|
|
473 |
reportFatalError("SDDeclInvalid", new Object[] {standalone});
|
|
474 |
}
|
|
475 |
} else {
|
|
476 |
reportFatalError("EncodingDeclRequired", null);
|
|
477 |
}
|
|
478 |
break;
|
|
479 |
}
|
|
480 |
case STATE_STANDALONE: {
|
|
481 |
if (name.equals(fStandaloneSymbol)) {
|
|
482 |
if (!sawSpace) {
|
|
483 |
reportFatalError("SpaceRequiredBeforeStandalone",
|
|
484 |
null);
|
|
485 |
}
|
|
486 |
standalone = fString.toString();
|
|
487 |
state = STATE_DONE;
|
|
488 |
if (!standalone.equals("yes") && !standalone.equals("no")) {
|
|
489 |
reportFatalError("SDDeclInvalid", new Object[] {standalone});
|
|
490 |
}
|
|
491 |
} else {
|
|
492 |
reportFatalError("EncodingDeclRequired", null);
|
|
493 |
}
|
|
494 |
break;
|
|
495 |
}
|
|
496 |
default: {
|
|
497 |
reportFatalError("NoMorePseudoAttributes", null);
|
|
498 |
}
|
|
499 |
}
|
|
500 |
sawSpace = fEntityScanner.skipSpaces();
|
|
501 |
}
|
|
502 |
// REVISIT: should we remove this error reporting?
|
|
503 |
if (scanningTextDecl && state != STATE_DONE) {
|
|
504 |
reportFatalError("MorePseudoAttributes", null);
|
|
505 |
}
|
|
506 |
|
|
507 |
// If there is no data in the xml or text decl then we fail to report error
|
|
508 |
// for version or encoding info above.
|
|
509 |
if (scanningTextDecl) {
|
|
510 |
if (!dataFoundForTarget && encoding == null) {
|
|
511 |
reportFatalError("EncodingDeclRequired", null);
|
|
512 |
}
|
|
513 |
} else {
|
|
514 |
if (!dataFoundForTarget && version == null) {
|
|
515 |
reportFatalError("VersionInfoRequired", null);
|
|
516 |
}
|
|
517 |
}
|
|
518 |
|
|
519 |
// end
|
|
520 |
if (!fEntityScanner.skipChar('?')) {
|
|
521 |
reportFatalError("XMLDeclUnterminated", null);
|
|
522 |
}
|
|
523 |
if (!fEntityScanner.skipChar('>')) {
|
|
524 |
reportFatalError("XMLDeclUnterminated", null);
|
|
525 |
|
|
526 |
}
|
|
527 |
|
|
528 |
// fill in return array
|
|
529 |
pseudoAttributeValues[0] = version;
|
|
530 |
pseudoAttributeValues[1] = encoding;
|
|
531 |
pseudoAttributeValues[2] = standalone;
|
|
532 |
|
|
533 |
} // scanXMLDeclOrTextDecl(boolean)
|
|
534 |
|
|
535 |
/**
|
|
536 |
* Scans a pseudo attribute.
|
|
537 |
*
|
|
538 |
* @param scanningTextDecl True if scanning this pseudo-attribute for a
|
|
539 |
* TextDecl; false if scanning XMLDecl. This
|
|
540 |
* flag is needed to report the correct type of
|
|
541 |
* error.
|
|
542 |
* @param value The string to fill in with the attribute
|
|
543 |
* value.
|
|
544 |
*
|
|
545 |
* @return The name of the attribute
|
|
546 |
*
|
|
547 |
* <strong>Note:</strong> This method uses fStringBuffer2, anything in it
|
|
548 |
* at the time of calling is lost.
|
|
549 |
*/
|
|
550 |
public String scanPseudoAttribute(boolean scanningTextDecl,
|
|
551 |
XMLString value)
|
|
552 |
throws IOException, XNIException {
|
|
553 |
|
|
554 |
String name = fEntityScanner.scanName();
|
|
555 |
// XMLEntityManager.print(fEntityManager.getCurrentEntity());
|
|
556 |
|
|
557 |
if (name == null) {
|
|
558 |
reportFatalError("PseudoAttrNameExpected", null);
|
|
559 |
}
|
|
560 |
fEntityScanner.skipSpaces();
|
|
561 |
if (!fEntityScanner.skipChar('=')) {
|
|
562 |
reportFatalError(scanningTextDecl ? "EqRequiredInTextDecl"
|
|
563 |
: "EqRequiredInXMLDecl", new Object[]{name});
|
|
564 |
}
|
|
565 |
fEntityScanner.skipSpaces();
|
|
566 |
int quote = fEntityScanner.peekChar();
|
|
567 |
if (quote != '\'' && quote != '"') {
|
|
568 |
reportFatalError(scanningTextDecl ? "QuoteRequiredInTextDecl"
|
|
569 |
: "QuoteRequiredInXMLDecl" , new Object[]{name});
|
|
570 |
}
|
|
571 |
fEntityScanner.scanChar();
|
|
572 |
int c = fEntityScanner.scanLiteral(quote, value);
|
|
573 |
if (c != quote) {
|
|
574 |
fStringBuffer2.clear();
|
|
575 |
do {
|
|
576 |
fStringBuffer2.append(value);
|
|
577 |
if (c != -1) {
|
|
578 |
if (c == '&' || c == '%' || c == '<' || c == ']') {
|
|
579 |
fStringBuffer2.append((char)fEntityScanner.scanChar());
|
|
580 |
} else if (XMLChar.isHighSurrogate(c)) {
|
|
581 |
scanSurrogates(fStringBuffer2);
|
|
582 |
} else if (isInvalidLiteral(c)) {
|
|
583 |
String key = scanningTextDecl
|
|
584 |
? "InvalidCharInTextDecl" : "InvalidCharInXMLDecl";
|
|
585 |
reportFatalError(key,
|
|
586 |
new Object[] {Integer.toString(c, 16)});
|
|
587 |
fEntityScanner.scanChar();
|
|
588 |
}
|
|
589 |
}
|
|
590 |
c = fEntityScanner.scanLiteral(quote, value);
|
|
591 |
} while (c != quote);
|
|
592 |
fStringBuffer2.append(value);
|
|
593 |
value.setValues(fStringBuffer2);
|
|
594 |
}
|
|
595 |
if (!fEntityScanner.skipChar(quote)) {
|
|
596 |
reportFatalError(scanningTextDecl ? "CloseQuoteMissingInTextDecl"
|
|
597 |
: "CloseQuoteMissingInXMLDecl",
|
|
598 |
new Object[]{name});
|
|
599 |
}
|
|
600 |
|
|
601 |
// return
|
|
602 |
return name;
|
|
603 |
|
|
604 |
} // scanPseudoAttribute(XMLString):String
|
|
605 |
|
|
606 |
/**
|
|
607 |
* Scans a processing instruction.
|
|
608 |
* <p>
|
|
609 |
* <pre>
|
|
610 |
* [16] PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>'
|
|
611 |
* [17] PITarget ::= Name - (('X' | 'x') ('M' | 'm') ('L' | 'l'))
|
|
612 |
* </pre>
|
|
613 |
*/
|
|
614 |
//CHANGED:
|
|
615 |
//EARLIER: scanPI()
|
|
616 |
//NOW: scanPI(XMLStringBuffer)
|
|
617 |
//it makes things more easy if XMLStringBUffer is passed. Motivation for this change is same
|
|
618 |
// as that for scanContent()
|
|
619 |
|
|
620 |
protected void scanPI(XMLStringBuffer data) throws IOException, XNIException {
|
|
621 |
|
|
622 |
// target
|
|
623 |
fReportEntity = false;
|
|
624 |
String target = fEntityScanner.scanName();
|
|
625 |
if (target == null) {
|
|
626 |
reportFatalError("PITargetRequired", null);
|
|
627 |
}
|
|
628 |
|
|
629 |
// scan data
|
|
630 |
scanPIData(target, data);
|
|
631 |
fReportEntity = true;
|
|
632 |
|
|
633 |
} // scanPI(XMLStringBuffer)
|
|
634 |
|
|
635 |
/**
|
|
636 |
* Scans a processing data. This is needed to handle the situation
|
|
637 |
* where a document starts with a processing instruction whose
|
|
638 |
* target name <em>starts with</em> "xml". (e.g. xmlfoo)
|
|
639 |
*
|
|
640 |
* This method would always read the whole data. We have while loop and data is buffered
|
|
641 |
* until delimeter is encountered.
|
|
642 |
*
|
|
643 |
* @param target The PI target
|
|
644 |
* @param data The string to fill in with the data
|
|
645 |
*/
|
|
646 |
|
|
647 |
//CHANGED:
|
|
648 |
//Earlier:This method uses the fStringBuffer and later buffer values are set to
|
|
649 |
//the supplied XMLString....
|
|
650 |
//Now: Changed the signature of this function to pass XMLStringBuffer.. and data would
|
|
651 |
//be appended to that buffer
|
|
652 |
|
|
653 |
protected void scanPIData(String target, XMLStringBuffer data)
|
|
654 |
throws IOException, XNIException {
|
|
655 |
|
|
656 |
// check target
|
|
657 |
if (target.length() == 3) {
|
|
658 |
char c0 = Character.toLowerCase(target.charAt(0));
|
|
659 |
char c1 = Character.toLowerCase(target.charAt(1));
|
|
660 |
char c2 = Character.toLowerCase(target.charAt(2));
|
|
661 |
if (c0 == 'x' && c1 == 'm' && c2 == 'l') {
|
|
662 |
reportFatalError("ReservedPITarget", null);
|
|
663 |
}
|
|
664 |
}
|
|
665 |
|
|
666 |
// spaces
|
|
667 |
if (!fEntityScanner.skipSpaces()) {
|
|
668 |
if (fEntityScanner.skipString("?>")) {
|
|
669 |
// we found the end, there is no data just return
|
|
670 |
return;
|
|
671 |
} else {
|
|
672 |
// if there is data there should be some space
|
|
673 |
reportFatalError("SpaceRequiredInPI", null);
|
|
674 |
}
|
|
675 |
}
|
|
676 |
|
|
677 |
// since scanData appends the parsed data to the buffer passed
|
|
678 |
// a while loop would append the whole of parsed data to the buffer(data:XMLStringBuffer)
|
|
679 |
//until all of the data is buffered.
|
|
680 |
if (fEntityScanner.scanData("?>", data)) {
|
|
681 |
do {
|
|
682 |
int c = fEntityScanner.peekChar();
|
|
683 |
if (c != -1) {
|
|
684 |
if (XMLChar.isHighSurrogate(c)) {
|
|
685 |
scanSurrogates(data);
|
|
686 |
} else if (isInvalidLiteral(c)) {
|
|
687 |
reportFatalError("InvalidCharInPI",
|
|
688 |
new Object[]{Integer.toHexString(c)});
|
|
689 |
fEntityScanner.scanChar();
|
|
690 |
}
|
|
691 |
}
|
|
692 |
} while (fEntityScanner.scanData("?>", data));
|
|
693 |
}
|
|
694 |
|
|
695 |
} // scanPIData(String,XMLString)
|
|
696 |
|
|
697 |
/**
|
|
698 |
* Scans a comment.
|
|
699 |
* <p>
|
|
700 |
* <pre>
|
|
701 |
* [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
|
|
702 |
* </pre>
|
|
703 |
* <p>
|
|
704 |
* <strong>Note:</strong> Called after scanning past '<!--'
|
|
705 |
* <strong>Note:</strong> This method uses fString, anything in it
|
|
706 |
* at the time of calling is lost.
|
|
707 |
*
|
|
708 |
* @param text The buffer to fill in with the text.
|
|
709 |
*/
|
|
710 |
protected void scanComment(XMLStringBuffer text)
|
|
711 |
throws IOException, XNIException {
|
|
712 |
|
|
713 |
//System.out.println( "XMLScanner#scanComment# In Scan Comment" );
|
|
714 |
// text
|
|
715 |
// REVISIT: handle invalid character, eof
|
|
716 |
text.clear();
|
|
717 |
while (fEntityScanner.scanData("--", text)) {
|
|
718 |
int c = fEntityScanner.peekChar();
|
|
719 |
|
|
720 |
//System.out.println( "XMLScanner#scanComment#text.toString() == " + text.toString() );
|
|
721 |
//System.out.println( "XMLScanner#scanComment#c == " + c );
|
|
722 |
|
|
723 |
if (c != -1) {
|
|
724 |
if (XMLChar.isHighSurrogate(c)) {
|
|
725 |
scanSurrogates(text);
|
|
726 |
}
|
|
727 |
if (isInvalidLiteral(c)) {
|
|
728 |
reportFatalError("InvalidCharInComment",
|
|
729 |
new Object[] { Integer.toHexString(c) });
|
|
730 |
fEntityScanner.scanChar();
|
|
731 |
}
|
|
732 |
}
|
|
733 |
}
|
|
734 |
if (!fEntityScanner.skipChar('>')) {
|
|
735 |
reportFatalError("DashDashInComment", null);
|
|
736 |
}
|
|
737 |
|
|
738 |
} // scanComment()
|
|
739 |
|
|
740 |
/**
|
|
741 |
* Scans an attribute value and normalizes whitespace converting all
|
|
742 |
* whitespace characters to space characters.
|
|
743 |
*
|
|
744 |
* [10] AttValue ::= '"' ([^<&"] | Reference)* '"' | "'" ([^<&'] | Reference)* "'"
|
|
745 |
*
|
|
746 |
* @param value The XMLString to fill in with the value.
|
|
747 |
* @param nonNormalizedValue The XMLString to fill in with the
|
|
748 |
* non-normalized value.
|
|
749 |
* @param atName The name of the attribute being parsed (for error msgs).
|
|
750 |
* @param attributes The attributes list for the scanned attribute.
|
|
751 |
* @param attrIndex The index of the attribute to use from the list.
|
|
752 |
* @param checkEntities true if undeclared entities should be reported as VC violation,
|
|
753 |
* false if undeclared entities should be reported as WFC violation.
|
|
754 |
*
|
|
755 |
* <strong>Note:</strong> This method uses fStringBuffer2, anything in it
|
|
756 |
* at the time of calling is lost.
|
|
757 |
**/
|
|
758 |
protected void scanAttributeValue(XMLString value,
|
|
759 |
XMLString nonNormalizedValue,
|
|
760 |
String atName,
|
|
761 |
XMLAttributes attributes, int attrIndex,
|
|
762 |
boolean checkEntities)
|
|
763 |
throws IOException, XNIException {
|
|
764 |
XMLStringBuffer stringBuffer = null;
|
|
765 |
// quote
|
|
766 |
int quote = fEntityScanner.peekChar();
|
|
767 |
if (quote != '\'' && quote != '"') {
|
|
768 |
reportFatalError("OpenQuoteExpected", new Object[]{atName});
|
|
769 |
}
|
|
770 |
|
|
771 |
fEntityScanner.scanChar();
|
|
772 |
int entityDepth = fEntityDepth;
|
|
773 |
|
|
774 |
int c = fEntityScanner.scanLiteral(quote, value);
|
|
775 |
if (DEBUG_ATTR_NORMALIZATION) {
|
|
776 |
System.out.println("** scanLiteral -> \""
|
|
777 |
+ value.toString() + "\"");
|
|
778 |
}
|
|
779 |
if(fNeedNonNormalizedValue){
|
|
780 |
fStringBuffer2.clear();
|
|
781 |
fStringBuffer2.append(value);
|
|
782 |
}
|
|
783 |
if(fEntityScanner.whiteSpaceLen > 0)
|
|
784 |
normalizeWhitespace(value);
|
|
785 |
if (DEBUG_ATTR_NORMALIZATION) {
|
|
786 |
System.out.println("** normalizeWhitespace -> \""
|
|
787 |
+ value.toString() + "\"");
|
|
788 |
}
|
|
789 |
if (c != quote) {
|
|
790 |
fScanningAttribute = true;
|
|
791 |
stringBuffer = getStringBuffer();
|
|
792 |
stringBuffer.clear();
|
|
793 |
do {
|
|
794 |
stringBuffer.append(value);
|
|
795 |
if (DEBUG_ATTR_NORMALIZATION) {
|
|
796 |
System.out.println("** value2: \""
|
|
797 |
+ stringBuffer.toString() + "\"");
|
|
798 |
}
|
|
799 |
if (c == '&') {
|
|
800 |
fEntityScanner.skipChar('&');
|
|
801 |
if (entityDepth == fEntityDepth && fNeedNonNormalizedValue ) {
|
|
802 |
fStringBuffer2.append('&');
|
|
803 |
}
|
|
804 |
if (fEntityScanner.skipChar('#')) {
|
|
805 |
if (entityDepth == fEntityDepth && fNeedNonNormalizedValue ) {
|
|
806 |
fStringBuffer2.append('#');
|
|
807 |
}
|
|
808 |
int ch ;
|
|
809 |
if (fNeedNonNormalizedValue)
|
|
810 |
ch = scanCharReferenceValue(stringBuffer, fStringBuffer2);
|
|
811 |
else
|
|
812 |
ch = scanCharReferenceValue(stringBuffer, null);
|
|
813 |
|
|
814 |
if (ch != -1) {
|
|
815 |
if (DEBUG_ATTR_NORMALIZATION) {
|
|
816 |
System.out.println("** value3: \""
|
|
817 |
+ stringBuffer.toString()
|
|
818 |
+ "\"");
|
|
819 |
}
|
|
820 |
}
|
|
821 |
} else {
|
|
822 |
String entityName = fEntityScanner.scanName();
|
|
823 |
if (entityName == null) {
|
|
824 |
reportFatalError("NameRequiredInReference", null);
|
|
825 |
} else if (entityDepth == fEntityDepth && fNeedNonNormalizedValue) {
|
|
826 |
fStringBuffer2.append(entityName);
|
|
827 |
}
|
|
828 |
if (!fEntityScanner.skipChar(';')) {
|
|
829 |
reportFatalError("SemicolonRequiredInReference",
|
|
830 |
new Object []{entityName});
|
|
831 |
} else if (entityDepth == fEntityDepth && fNeedNonNormalizedValue) {
|
|
832 |
fStringBuffer2.append(';');
|
|
833 |
}
|
|
834 |
if (entityName == fAmpSymbol) {
|
|
835 |
stringBuffer.append('&');
|
|
836 |
if (DEBUG_ATTR_NORMALIZATION) {
|
|
837 |
System.out.println("** value5: \""
|
|
838 |
+ stringBuffer.toString()
|
|
839 |
+ "\"");
|
|
840 |
}
|
|
841 |
} else if (entityName == fAposSymbol) {
|
|
842 |
stringBuffer.append('\'');
|
|
843 |
if (DEBUG_ATTR_NORMALIZATION) {
|
|
844 |
System.out.println("** value7: \""
|
|
845 |
+ stringBuffer.toString()
|
|
846 |
+ "\"");
|
|
847 |
}
|
|
848 |
} else if (entityName == fLtSymbol) {
|
|
849 |
stringBuffer.append('<');
|
|
850 |
if (DEBUG_ATTR_NORMALIZATION) {
|
|
851 |
System.out.println("** value9: \""
|
|
852 |
+ stringBuffer.toString()
|
|
853 |
+ "\"");
|
|
854 |
}
|
|
855 |
} else if (entityName == fGtSymbol) {
|
|
856 |
stringBuffer.append('>');
|
|
857 |
if (DEBUG_ATTR_NORMALIZATION) {
|
|
858 |
System.out.println("** valueB: \""
|
|
859 |
+ stringBuffer.toString()
|
|
860 |
+ "\"");
|
|
861 |
}
|
|
862 |
} else if (entityName == fQuotSymbol) {
|
|
863 |
stringBuffer.append('"');
|
|
864 |
if (DEBUG_ATTR_NORMALIZATION) {
|
|
865 |
System.out.println("** valueD: \""
|
|
866 |
+ stringBuffer.toString()
|
|
867 |
+ "\"");
|
|
868 |
}
|
|
869 |
} else {
|
|
870 |
if (fEntityStore.isExternalEntity(entityName)) {
|
|
871 |
reportFatalError("ReferenceToExternalEntity",
|
|
872 |
new Object[] { entityName });
|
|
873 |
} else {
|
|
874 |
if (!fEntityStore.isDeclaredEntity(entityName)) {
|
|
875 |
//WFC & VC: Entity Declared
|
|
876 |
if (checkEntities) {
|
|
877 |
if (fValidation) {
|
|
878 |
fErrorReporter.reportError(fEntityScanner,XMLMessageFormatter.XML_DOMAIN,
|
|
879 |
"EntityNotDeclared",
|
|
880 |
new Object[]{entityName},
|
|
881 |
XMLErrorReporter.SEVERITY_ERROR);
|
|
882 |
}
|
|
883 |
} else {
|
|
884 |
reportFatalError("EntityNotDeclared",
|
|
885 |
new Object[]{entityName});
|
|
886 |
}
|
|
887 |
}
|
|
888 |
fEntityManager.startEntity(entityName, true);
|
|
889 |
}
|
|
890 |
}
|
|
891 |
}
|
|
892 |
} else if (c == '<') {
|
|
893 |
reportFatalError("LessthanInAttValue",
|
|
894 |
new Object[] { null, atName });
|
|
895 |
fEntityScanner.scanChar();
|
|
896 |
if (entityDepth == fEntityDepth && fNeedNonNormalizedValue) {
|
|
897 |
fStringBuffer2.append((char)c);
|
|
898 |
}
|
|
899 |
} else if (c == '%' || c == ']') {
|
|
900 |
fEntityScanner.scanChar();
|
|
901 |
stringBuffer.append((char)c);
|
|
902 |
if (entityDepth == fEntityDepth && fNeedNonNormalizedValue) {
|
|
903 |
fStringBuffer2.append((char)c);
|
|
904 |
}
|
|
905 |
if (DEBUG_ATTR_NORMALIZATION) {
|
|
906 |
System.out.println("** valueF: \""
|
|
907 |
+ stringBuffer.toString() + "\"");
|
|
908 |
}
|
|
909 |
} else if (c == '\n' || c == '\r') {
|
|
910 |
fEntityScanner.scanChar();
|
|
911 |
stringBuffer.append(' ');
|
|
912 |
if (entityDepth == fEntityDepth && fNeedNonNormalizedValue) {
|
|
913 |
fStringBuffer2.append('\n');
|
|
914 |
}
|
|
915 |
} else if (c != -1 && XMLChar.isHighSurrogate(c)) {
|
|
916 |
if (scanSurrogates(fStringBuffer3)) {
|
|
917 |
stringBuffer.append(fStringBuffer3);
|
|
918 |
if (entityDepth == fEntityDepth && fNeedNonNormalizedValue) {
|
|
919 |
fStringBuffer2.append(fStringBuffer3);
|
|
920 |
}
|
|
921 |
if (DEBUG_ATTR_NORMALIZATION) {
|
|
922 |
System.out.println("** valueI: \""
|
|
923 |
+ stringBuffer.toString()
|
|
924 |
+ "\"");
|
|
925 |
}
|
|
926 |
}
|
|
927 |
} else if (c != -1 && isInvalidLiteral(c)) {
|
|
928 |
reportFatalError("InvalidCharInAttValue",
|
|
929 |
new Object[] {Integer.toString(c, 16)});
|
|
930 |
fEntityScanner.scanChar();
|
|
931 |
if (entityDepth == fEntityDepth && fNeedNonNormalizedValue) {
|
|
932 |
fStringBuffer2.append((char)c);
|
|
933 |
}
|
|
934 |
}
|
|
935 |
c = fEntityScanner.scanLiteral(quote, value);
|
|
936 |
if (entityDepth == fEntityDepth && fNeedNonNormalizedValue) {
|
|
937 |
fStringBuffer2.append(value);
|
|
938 |
}
|
|
939 |
if(fEntityScanner.whiteSpaceLen > 0)
|
|
940 |
normalizeWhitespace(value);
|
|
941 |
//Todo ::Move this check to Attributes , do conversion
|
|
942 |
//only if attribute is being accessed. -Venu
|
|
943 |
} while (c != quote || entityDepth != fEntityDepth);
|
|
944 |
stringBuffer.append(value);
|
|
945 |
if (DEBUG_ATTR_NORMALIZATION) {
|
|
946 |
System.out.println("** valueN: \""
|
|
947 |
+ stringBuffer.toString() + "\"");
|
|
948 |
}
|
|
949 |
value.setValues(stringBuffer);
|
|
950 |
fScanningAttribute = false;
|
|
951 |
}
|
|
952 |
if(fNeedNonNormalizedValue)
|
|
953 |
nonNormalizedValue.setValues(fStringBuffer2);
|
|
954 |
|
|
955 |
// quote
|
|
956 |
int cquote = fEntityScanner.scanChar();
|
|
957 |
if (cquote != quote) {
|
|
958 |
reportFatalError("CloseQuoteExpected", new Object[]{atName});
|
|
959 |
}
|
|
960 |
} // scanAttributeValue()
|
|
961 |
|
|
962 |
|
|
963 |
/**
|
|
964 |
* Scans External ID and return the public and system IDs.
|
|
965 |
*
|
|
966 |
* @param identifiers An array of size 2 to return the system id,
|
|
967 |
* and public id (in that order).
|
|
968 |
* @param optionalSystemId Specifies whether the system id is optional.
|
|
969 |
*
|
|
970 |
* <strong>Note:</strong> This method uses fString and fStringBuffer,
|
|
971 |
* anything in them at the time of calling is lost.
|
|
972 |
*/
|
|
973 |
protected void scanExternalID(String[] identifiers,
|
|
974 |
boolean optionalSystemId)
|
|
975 |
throws IOException, XNIException {
|
|
976 |
|
|
977 |
String systemId = null;
|
|
978 |
String publicId = null;
|
|
979 |
if (fEntityScanner.skipString("PUBLIC")) {
|
|
980 |
if (!fEntityScanner.skipSpaces()) {
|
|
981 |
reportFatalError("SpaceRequiredAfterPUBLIC", null);
|
|
982 |
}
|
|
983 |
scanPubidLiteral(fString);
|
|
984 |
publicId = fString.toString();
|
|
985 |
|
|
986 |
if (!fEntityScanner.skipSpaces() && !optionalSystemId) {
|
|
987 |
reportFatalError("SpaceRequiredBetweenPublicAndSystem", null);
|
|
988 |
}
|
|
989 |
}
|
|
990 |
|
|
991 |
if (publicId != null || fEntityScanner.skipString("SYSTEM")) {
|
|
992 |
if (publicId == null && !fEntityScanner.skipSpaces()) {
|
|
993 |
reportFatalError("SpaceRequiredAfterSYSTEM", null);
|
|
994 |
}
|
|
995 |
int quote = fEntityScanner.peekChar();
|
|
996 |
if (quote != '\'' && quote != '"') {
|
|
997 |
if (publicId != null && optionalSystemId) {
|
|
998 |
// looks like we don't have any system id
|
|
999 |
// simply return the public id
|
|
1000 |
identifiers[0] = null;
|
|
1001 |
identifiers[1] = publicId;
|
|
1002 |
return;
|
|
1003 |
}
|
|
1004 |
reportFatalError("QuoteRequiredInSystemID", null);
|
|
1005 |
}
|
|
1006 |
fEntityScanner.scanChar();
|
|
1007 |
XMLString ident = fString;
|
|
1008 |
if (fEntityScanner.scanLiteral(quote, ident) != quote) {
|
|
1009 |
fStringBuffer.clear();
|
|
1010 |
do {
|
|
1011 |
fStringBuffer.append(ident);
|
|
1012 |
int c = fEntityScanner.peekChar();
|
|
1013 |
if (XMLChar.isMarkup(c) || c == ']') {
|
|
1014 |
fStringBuffer.append((char)fEntityScanner.scanChar());
|
|
1015 |
} else if (c != -1 && isInvalidLiteral(c)) {
|
|
1016 |
reportFatalError("InvalidCharInSystemID",
|
|
1017 |
new Object[] {Integer.toString(c, 16)});
|
|
1018 |
}
|
|
1019 |
} while (fEntityScanner.scanLiteral(quote, ident) != quote);
|
|
1020 |
fStringBuffer.append(ident);
|
|
1021 |
ident = fStringBuffer;
|
|
1022 |
}
|
|
1023 |
systemId = ident.toString();
|
|
1024 |
if (!fEntityScanner.skipChar(quote)) {
|
|
1025 |
reportFatalError("SystemIDUnterminated", null);
|
|
1026 |
}
|
|
1027 |
}
|
|
1028 |
|
|
1029 |
// store result in array
|
|
1030 |
identifiers[0] = systemId;
|
|
1031 |
identifiers[1] = publicId;
|
|
1032 |
}
|
|
1033 |
|
|
1034 |
|
|
1035 |
/**
|
|
1036 |
* Scans public ID literal.
|
|
1037 |
*
|
|
1038 |
* [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'"
|
|
1039 |
* [13] PubidChar::= #x20 | #xD | #xA | [a-zA-Z0-9] | [-'()+,./:=?;!*#@$_%]
|
|
1040 |
*
|
|
1041 |
* The returned string is normalized according to the following rule,
|
|
1042 |
* from http://www.w3.org/TR/REC-xml#dt-pubid:
|
|
1043 |
*
|
|
1044 |
* Before a match is attempted, all strings of white space in the public
|
|
1045 |
* identifier must be normalized to single space characters (#x20), and
|
|
1046 |
* leading and trailing white space must be removed.
|
|
1047 |
*
|
|
1048 |
* @param literal The string to fill in with the public ID literal.
|
|
1049 |
* @return True on success.
|
|
1050 |
*
|
|
1051 |
* <strong>Note:</strong> This method uses fStringBuffer, anything in it at
|
|
1052 |
* the time of calling is lost.
|
|
1053 |
*/
|
|
1054 |
protected boolean scanPubidLiteral(XMLString literal)
|
|
1055 |
throws IOException, XNIException {
|
|
1056 |
int quote = fEntityScanner.scanChar();
|
|
1057 |
if (quote != '\'' && quote != '"') {
|
|
1058 |
reportFatalError("QuoteRequiredInPublicID", null);
|
|
1059 |
return false;
|
|
1060 |
}
|
|
1061 |
|
|
1062 |
fStringBuffer.clear();
|
|
1063 |
// skip leading whitespace
|
|
1064 |
boolean skipSpace = true;
|
|
1065 |
boolean dataok = true;
|
|
1066 |
while (true) {
|
|
1067 |
int c = fEntityScanner.scanChar();
|
|
1068 |
if (c == ' ' || c == '\n' || c == '\r') {
|
|
1069 |
if (!skipSpace) {
|
|
1070 |
// take the first whitespace as a space and skip the others
|
|
1071 |
fStringBuffer.append(' ');
|
|
1072 |
skipSpace = true;
|
|
1073 |
}
|
|
1074 |
} else if (c == quote) {
|
|
1075 |
if (skipSpace) {
|
|
1076 |
// if we finished on a space let's trim it
|
|
1077 |
fStringBuffer.length--;
|
|
1078 |
}
|
|
1079 |
literal.setValues(fStringBuffer);
|
|
1080 |
break;
|
|
1081 |
} else if (XMLChar.isPubid(c)) {
|
|
1082 |
fStringBuffer.append((char)c);
|
|
1083 |
skipSpace = false;
|
|
1084 |
} else if (c == -1) {
|
|
1085 |
reportFatalError("PublicIDUnterminated", null);
|
|
1086 |
return false;
|
|
1087 |
} else {
|
|
1088 |
dataok = false;
|
|
1089 |
reportFatalError("InvalidCharInPublicID",
|
|
1090 |
new Object[]{Integer.toHexString(c)});
|
|
1091 |
}
|
|
1092 |
}
|
|
1093 |
return dataok;
|
|
1094 |
}
|
|
1095 |
|
|
1096 |
|
|
1097 |
/**
|
|
1098 |
* Normalize whitespace in an XMLString converting all whitespace
|
|
1099 |
* characters to space characters.
|
|
1100 |
*/
|
|
1101 |
protected void normalizeWhitespace(XMLString value) {
|
|
1102 |
int i=0;
|
|
1103 |
int j=0;
|
|
1104 |
int [] buff = fEntityScanner.whiteSpaceLookup;
|
|
1105 |
int buffLen = fEntityScanner.whiteSpaceLen;
|
|
1106 |
int end = value.offset + value.length;
|
|
1107 |
while(i < buffLen){
|
|
1108 |
j = buff[i];
|
|
1109 |
if(j < end ){
|
|
1110 |
value.ch[j] = ' ';
|
|
1111 |
}
|
|
1112 |
i++;
|
|
1113 |
}
|
|
1114 |
}
|
|
1115 |
|
|
1116 |
//
|
|
1117 |
// XMLEntityHandler methods
|
|
1118 |
//
|
|
1119 |
|
|
1120 |
/**
|
|
1121 |
* This method notifies of the start of an entity. The document entity
|
|
1122 |
* has the pseudo-name of "[xml]" the DTD has the pseudo-name of "[dtd]"
|
|
1123 |
* parameter entity names start with '%'; and general entities are just
|
|
1124 |
* specified by their name.
|
|
1125 |
*
|
|
1126 |
* @param name The name of the entity.
|
|
1127 |
* @param identifier The resource identifier.
|
|
1128 |
* @param encoding The auto-detected IANA encoding name of the entity
|
|
1129 |
* stream. This value will be null in those situations
|
|
1130 |
* where the entity encoding is not auto-detected (e.g.
|
|
1131 |
* internal entities or a document entity that is
|
|
1132 |
* parsed from a java.io.Reader).
|
|
1133 |
*
|
|
1134 |
* @throws XNIException Thrown by handler to signal an error.
|
|
1135 |
*/
|
|
1136 |
public void startEntity(String name,
|
|
1137 |
XMLResourceIdentifier identifier,
|
|
1138 |
String encoding, Augmentations augs) throws XNIException {
|
|
1139 |
|
|
1140 |
// keep track of the entity depth
|
|
1141 |
fEntityDepth++;
|
|
1142 |
// must reset entity scanner
|
|
1143 |
fEntityScanner = fEntityManager.getEntityScanner();
|
|
1144 |
fEntityStore = fEntityManager.getEntityStore() ;
|
|
1145 |
} // startEntity(String,XMLResourceIdentifier,String)
|
|
1146 |
|
|
1147 |
/**
|
|
1148 |
* This method notifies the end of an entity. The document entity has
|
|
1149 |
* the pseudo-name of "[xml]" the DTD has the pseudo-name of "[dtd]"
|
|
1150 |
* parameter entity names start with '%'; and general entities are just
|
|
1151 |
* specified by their name.
|
|
1152 |
*
|
|
1153 |
* @param name The name of the entity.
|
|
1154 |
*
|
|
1155 |
* @throws XNIException Thrown by handler to signal an error.
|
|
1156 |
*/
|
|
1157 |
public void endEntity(String name, Augmentations augs) throws IOException, XNIException {
|
|
1158 |
|
|
1159 |
// keep track of the entity depth
|
|
1160 |
fEntityDepth--;
|
|
1161 |
|
|
1162 |
} // endEntity(String)
|
|
1163 |
|
|
1164 |
/**
|
|
1165 |
* Scans a character reference and append the corresponding chars to the
|
|
1166 |
* specified buffer.
|
|
1167 |
*
|
|
1168 |
* <p>
|
|
1169 |
* <pre>
|
|
1170 |
* [66] CharRef ::= '&#' [0-9]+ ';' | '&#x' [0-9a-fA-F]+ ';'
|
|
1171 |
* </pre>
|
|
1172 |
*
|
|
1173 |
* <strong>Note:</strong> This method uses fStringBuffer, anything in it
|
|
1174 |
* at the time of calling is lost.
|
|
1175 |
*
|
|
1176 |
* @param buf the character buffer to append chars to
|
|
1177 |
* @param buf2 the character buffer to append non-normalized chars to
|
|
1178 |
*
|
|
1179 |
* @return the character value or (-1) on conversion failure
|
|
1180 |
*/
|
|
1181 |
protected int scanCharReferenceValue(XMLStringBuffer buf, XMLStringBuffer buf2)
|
|
1182 |
throws IOException, XNIException {
|
|
1183 |
// scan hexadecimal value
|
|
1184 |
boolean hex = false;
|
|
1185 |
if (fEntityScanner.skipChar('x')) {
|
|
1186 |
if (buf2 != null) { buf2.append('x'); }
|
|
1187 |
hex = true;
|
|
1188 |
fStringBuffer3.clear();
|
|
1189 |
boolean digit = true;
|
|
1190 |
|
|
1191 |
int c = fEntityScanner.peekChar();
|
|
1192 |
digit = (c >= '0' && c <= '9') ||
|
|
1193 |
(c >= 'a' && c <= 'f') ||
|
|
1194 |
(c >= 'A' && c <= 'F');
|
|
1195 |
if (digit) {
|
|
1196 |
if (buf2 != null) { buf2.append((char)c); }
|
|
1197 |
fEntityScanner.scanChar();
|
|
1198 |
fStringBuffer3.append((char)c);
|
|
1199 |
|
|
1200 |
do {
|
|
1201 |
c = fEntityScanner.peekChar();
|
|
1202 |
digit = (c >= '0' && c <= '9') ||
|
|
1203 |
(c >= 'a' && c <= 'f') ||
|
|
1204 |
(c >= 'A' && c <= 'F');
|
|
1205 |
if (digit) {
|
|
1206 |
if (buf2 != null) { buf2.append((char)c); }
|
|
1207 |
fEntityScanner.scanChar();
|
|
1208 |
fStringBuffer3.append((char)c);
|
|
1209 |
}
|
|
1210 |
} while (digit);
|
|
1211 |
} else {
|
|
1212 |
reportFatalError("HexdigitRequiredInCharRef", null);
|
|
1213 |
}
|
|
1214 |
}
|
|
1215 |
|
|
1216 |
// scan decimal value
|
|
1217 |
else {
|
|
1218 |
fStringBuffer3.clear();
|
|
1219 |
boolean digit = true;
|
|
1220 |
|
|
1221 |
int c = fEntityScanner.peekChar();
|
|
1222 |
digit = c >= '0' && c <= '9';
|
|
1223 |
if (digit) {
|
|
1224 |
if (buf2 != null) { buf2.append((char)c); }
|
|
1225 |
fEntityScanner.scanChar();
|
|
1226 |
fStringBuffer3.append((char)c);
|
|
1227 |
|
|
1228 |
do {
|
|
1229 |
c = fEntityScanner.peekChar();
|
|
1230 |
digit = c >= '0' && c <= '9';
|
|
1231 |
if (digit) {
|
|
1232 |
if (buf2 != null) { buf2.append((char)c); }
|
|
1233 |
fEntityScanner.scanChar();
|
|
1234 |
fStringBuffer3.append((char)c);
|
|
1235 |
}
|
|
1236 |
} while (digit);
|
|
1237 |
} else {
|
|
1238 |
reportFatalError("DigitRequiredInCharRef", null);
|
|
1239 |
}
|
|
1240 |
}
|
|
1241 |
|
|
1242 |
// end
|
|
1243 |
if (!fEntityScanner.skipChar(';')) {
|
|
1244 |
reportFatalError("SemicolonRequiredInCharRef", null);
|
|
1245 |
}
|
|
1246 |
if (buf2 != null) { buf2.append(';'); }
|
|
1247 |
|
|
1248 |
// convert string to number
|
|
1249 |
int value = -1;
|
|
1250 |
try {
|
|
1251 |
value = Integer.parseInt(fStringBuffer3.toString(),
|
|
1252 |
hex ? 16 : 10);
|
|
1253 |
|
|
1254 |
// character reference must be a valid XML character
|
|
1255 |
if (isInvalid(value)) {
|
|
1256 |
StringBuffer errorBuf = new StringBuffer(fStringBuffer3.length + 1);
|
|
1257 |
if (hex) errorBuf.append('x');
|
|
1258 |
errorBuf.append(fStringBuffer3.ch, fStringBuffer3.offset, fStringBuffer3.length);
|
|
1259 |
reportFatalError("InvalidCharRef",
|
|
1260 |
new Object[]{errorBuf.toString()});
|
|
1261 |
}
|
|
1262 |
} catch (NumberFormatException e) {
|
|
1263 |
// Conversion failed, let -1 value drop through.
|
|
1264 |
// If we end up here, the character reference was invalid.
|
|
1265 |
StringBuffer errorBuf = new StringBuffer(fStringBuffer3.length + 1);
|
|
1266 |
if (hex) errorBuf.append('x');
|
|
1267 |
errorBuf.append(fStringBuffer3.ch, fStringBuffer3.offset, fStringBuffer3.length);
|
|
1268 |
reportFatalError("InvalidCharRef",
|
|
1269 |
new Object[]{errorBuf.toString()});
|
|
1270 |
}
|
|
1271 |
|
|
1272 |
// append corresponding chars to the given buffer
|
|
1273 |
if (!XMLChar.isSupplemental(value)) {
|
|
1274 |
buf.append((char) value);
|
|
1275 |
} else {
|
|
1276 |
// character is supplemental, split it into surrogate chars
|
|
1277 |
buf.append(XMLChar.highSurrogate(value));
|
|
1278 |
buf.append(XMLChar.lowSurrogate(value));
|
|
1279 |
}
|
|
1280 |
|
|
1281 |
// char refs notification code
|
|
1282 |
if (fNotifyCharRefs && value != -1) {
|
|
1283 |
String literal = "#" + (hex ? "x" : "") + fStringBuffer3.toString();
|
|
1284 |
if (!fScanningAttribute) {
|
|
1285 |
fCharRefLiteral = literal;
|
|
1286 |
}
|
|
1287 |
}
|
|
1288 |
|
|
1289 |
return value;
|
|
1290 |
}
|
|
1291 |
// returns true if the given character is not
|
|
1292 |
// valid with respect to the version of
|
|
1293 |
// XML understood by this scanner.
|
|
1294 |
protected boolean isInvalid(int value) {
|
|
1295 |
return (XMLChar.isInvalid(value));
|
|
1296 |
} // isInvalid(int): boolean
|
|
1297 |
|
|
1298 |
// returns true if the given character is not
|
|
1299 |
// valid or may not be used outside a character reference
|
|
1300 |
// with respect to the version of XML understood by this scanner.
|
|
1301 |
protected boolean isInvalidLiteral(int value) {
|
|
1302 |
return (XMLChar.isInvalid(value));
|
|
1303 |
} // isInvalidLiteral(int): boolean
|
|
1304 |
|
|
1305 |
// returns true if the given character is
|
|
1306 |
// a valid nameChar with respect to the version of
|
|
1307 |
// XML understood by this scanner.
|
|
1308 |
protected boolean isValidNameChar(int value) {
|
|
1309 |
return (XMLChar.isName(value));
|
|
1310 |
} // isValidNameChar(int): boolean
|
|
1311 |
|
|
1312 |
// returns true if the given character is
|
|
1313 |
// a valid NCName character with respect to the version of
|
|
1314 |
// XML understood by this scanner.
|
|
1315 |
protected boolean isValidNCName(int value) {
|
|
1316 |
return (XMLChar.isNCName(value));
|
|
1317 |
} // isValidNCName(int): boolean
|
|
1318 |
|
|
1319 |
// returns true if the given character is
|
|
1320 |
// a valid nameStartChar with respect to the version of
|
|
1321 |
// XML understood by this scanner.
|
|
1322 |
protected boolean isValidNameStartChar(int value) {
|
|
1323 |
return (XMLChar.isNameStart(value));
|
|
1324 |
} // isValidNameStartChar(int): boolean
|
|
1325 |
|
|
1326 |
protected boolean versionSupported(String version ) {
|
|
1327 |
return version.equals("1.0") || version.equals("1.1");
|
|
1328 |
} // version Supported
|
|
1329 |
|
|
1330 |
/**
|
|
1331 |
* Scans surrogates and append them to the specified buffer.
|
|
1332 |
* <p>
|
|
1333 |
* <strong>Note:</strong> This assumes the current char has already been
|
|
1334 |
* identified as a high surrogate.
|
|
1335 |
*
|
|
1336 |
* @param buf The StringBuffer to append the read surrogates to.
|
|
1337 |
* @return True if it succeeded.
|
|
1338 |
*/
|
|
1339 |
protected boolean scanSurrogates(XMLStringBuffer buf)
|
|
1340 |
throws IOException, XNIException {
|
|
1341 |
|
|
1342 |
int high = fEntityScanner.scanChar();
|
|
1343 |
int low = fEntityScanner.peekChar();
|
|
1344 |
if (!XMLChar.isLowSurrogate(low)) {
|
|
1345 |
reportFatalError("InvalidCharInContent",
|
|
1346 |
new Object[] {Integer.toString(high, 16)});
|
|
1347 |
return false;
|
|
1348 |
}
|
|
1349 |
fEntityScanner.scanChar();
|
|
1350 |
|
|
1351 |
// convert surrogates to supplemental character
|
|
1352 |
int c = XMLChar.supplemental((char)high, (char)low);
|
|
1353 |
|
|
1354 |
// supplemental character must be a valid XML character
|
|
1355 |
if (isInvalid(c)) {
|
|
1356 |
reportFatalError("InvalidCharInContent",
|
|
1357 |
new Object[]{Integer.toString(c, 16)});
|
|
1358 |
return false;
|
|
1359 |
}
|
|
1360 |
|
|
1361 |
// fill in the buffer
|
|
1362 |
buf.append((char)high);
|
|
1363 |
buf.append((char)low);
|
|
1364 |
|
|
1365 |
return true;
|
|
1366 |
|
|
1367 |
} // scanSurrogates():boolean
|
|
1368 |
|
|
1369 |
|
|
1370 |
/**
|
|
1371 |
* Convenience function used in all XML scanners.
|
|
1372 |
*/
|
|
1373 |
protected void reportFatalError(String msgId, Object[] args)
|
|
1374 |
throws XNIException {
|
|
1375 |
fErrorReporter.reportError(fEntityScanner, XMLMessageFormatter.XML_DOMAIN,
|
|
1376 |
msgId, args,
|
|
1377 |
XMLErrorReporter.SEVERITY_FATAL_ERROR);
|
|
1378 |
}
|
|
1379 |
|
|
1380 |
// private methods
|
|
1381 |
private void init() {
|
|
1382 |
// initialize scanner
|
|
1383 |
fEntityScanner = null;
|
|
1384 |
// initialize vars
|
|
1385 |
fEntityDepth = 0;
|
|
1386 |
fReportEntity = true;
|
|
1387 |
fResourceIdentifier.clear();
|
|
1388 |
|
|
1389 |
if(!fAttributeCacheInitDone){
|
|
1390 |
for(int i = 0; i < initialCacheCount; i++){
|
|
1391 |
attributeValueCache.add(new XMLString());
|
|
1392 |
stringBufferCache.add(new XMLStringBuffer());
|
|
1393 |
}
|
|
1394 |
fAttributeCacheInitDone = true;
|
|
1395 |
}
|
|
1396 |
fStringBufferIndex = 0;
|
|
1397 |
fAttributeCacheUsedCount = 0;
|
|
1398 |
|
|
1399 |
}
|
|
1400 |
|
|
1401 |
XMLStringBuffer getStringBuffer(){
|
|
1402 |
if((fStringBufferIndex < initialCacheCount )|| (fStringBufferIndex < stringBufferCache.size())){
|
|
1403 |
return (XMLStringBuffer)stringBufferCache.get(fStringBufferIndex++);
|
|
1404 |
}else{
|
|
1405 |
XMLStringBuffer tmpObj = new XMLStringBuffer();
|
|
1406 |
fStringBufferIndex++;
|
|
1407 |
stringBufferCache.add(tmpObj);
|
|
1408 |
return tmpObj;
|
|
1409 |
}
|
|
1410 |
}
|
|
1411 |
|
|
1412 |
|
|
1413 |
} // class XMLScanner
|