author | joehw |
Tue, 09 Jul 2013 16:34:52 -0700 | |
changeset 18890 | 25bdeca3173b |
parent 17645 | d2f0e76ff056 |
child 20967 | d3f1925927e5 |
permissions | -rw-r--r-- |
12005 | 1 |
/* |
17645 | 2 |
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. |
12005 | 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 |
||
17534 | 23 |
import com.sun.org.apache.xerces.internal.impl.Constants; |
24 |
import com.sun.org.apache.xerces.internal.impl.io.ASCIIReader; |
|
25 |
import com.sun.org.apache.xerces.internal.impl.io.UCSReader; |
|
26 |
import com.sun.org.apache.xerces.internal.impl.io.UTF8Reader; |
|
27 |
import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter; |
|
28 |
import com.sun.org.apache.xerces.internal.impl.XMLEntityHandler; |
|
29 |
import com.sun.org.apache.xerces.internal.impl.validation.ValidationManager; |
|
30 |
import com.sun.org.apache.xerces.internal.util.*; |
|
31 |
import com.sun.org.apache.xerces.internal.util.SecurityManager; |
|
32 |
import com.sun.org.apache.xerces.internal.util.URI; |
|
33 |
import com.sun.org.apache.xerces.internal.utils.SecuritySupport; |
|
18890
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17645
diff
changeset
|
34 |
import com.sun.org.apache.xerces.internal.utils.XMLSecurityPropertyManager; |
17534 | 35 |
import com.sun.org.apache.xerces.internal.xni.Augmentations; |
36 |
import com.sun.org.apache.xerces.internal.xni.XMLResourceIdentifier; |
|
37 |
import com.sun.org.apache.xerces.internal.xni.XNIException; |
|
38 |
import com.sun.org.apache.xerces.internal.xni.parser.*; |
|
39 |
import com.sun.xml.internal.stream.Entity; |
|
12005 | 40 |
import com.sun.xml.internal.stream.StaxEntityResolverWrapper; |
41 |
import com.sun.xml.internal.stream.StaxXMLInputSource; |
|
42 |
import com.sun.xml.internal.stream.XMLEntityStorage; |
|
43 |
import java.io.*; |
|
44 |
import java.lang.reflect.Method; |
|
45 |
import java.net.HttpURLConnection; |
|
17534 | 46 |
import java.net.URISyntaxException; |
12005 | 47 |
import java.net.URL; |
48 |
import java.net.URLConnection; |
|
49 |
import java.util.Hashtable; |
|
50 |
import java.util.Iterator; |
|
51 |
import java.util.Locale; |
|
52 |
import java.util.Map; |
|
53 |
import java.util.Stack; |
|
17534 | 54 |
import javax.xml.XMLConstants; |
12005 | 55 |
|
56 |
||
57 |
/** |
|
58 |
* Will keep track of current entity. |
|
59 |
* |
|
60 |
* The entity manager handles the registration of general and parameter |
|
61 |
* entities; resolves entities; and starts entities. The entity manager |
|
62 |
* is a central component in a standard parser configuration and this |
|
63 |
* class works directly with the entity scanner to manage the underlying |
|
64 |
* xni. |
|
65 |
* <p> |
|
66 |
* This component requires the following features and properties from the |
|
67 |
* component manager that uses it: |
|
68 |
* <ul> |
|
69 |
* <li>http://xml.org/sax/features/validation</li> |
|
70 |
* <li>http://xml.org/sax/features/external-general-entities</li> |
|
71 |
* <li>http://xml.org/sax/features/external-parameter-entities</li> |
|
72 |
* <li>http://apache.org/xml/features/allow-java-encodings</li> |
|
73 |
* <li>http://apache.org/xml/properties/internal/symbol-table</li> |
|
74 |
* <li>http://apache.org/xml/properties/internal/error-reporter</li> |
|
75 |
* <li>http://apache.org/xml/properties/internal/entity-resolver</li> |
|
76 |
* </ul> |
|
77 |
* |
|
78 |
* |
|
79 |
* @author Andy Clark, IBM |
|
80 |
* @author Arnaud Le Hors, IBM |
|
81 |
* @author K.Venugopal SUN Microsystems |
|
82 |
* @author Neeraj Bajaj SUN Microsystems |
|
83 |
* @author Sunitha Reddy SUN Microsystems |
|
84 |
* @version $Id: XMLEntityManager.java,v 1.17 2010-11-01 04:39:41 joehw Exp $ |
|
85 |
*/ |
|
86 |
public class XMLEntityManager implements XMLComponent, XMLEntityResolver { |
|
87 |
||
88 |
// |
|
89 |
// Constants |
|
90 |
// |
|
91 |
||
92 |
/** Default buffer size (2048). */ |
|
93 |
public static final int DEFAULT_BUFFER_SIZE = 8192; |
|
94 |
||
95 |
/** Default buffer size before we've finished with the XMLDecl: */ |
|
96 |
public static final int DEFAULT_XMLDECL_BUFFER_SIZE = 64; |
|
97 |
||
98 |
/** Default internal entity buffer size (1024). */ |
|
99 |
public static final int DEFAULT_INTERNAL_BUFFER_SIZE = 1024; |
|
100 |
||
101 |
// feature identifiers |
|
102 |
||
103 |
/** Feature identifier: validation. */ |
|
104 |
protected static final String VALIDATION = |
|
105 |
Constants.SAX_FEATURE_PREFIX + Constants.VALIDATION_FEATURE; |
|
106 |
||
107 |
/** |
|
108 |
* standard uri conformant (strict uri). |
|
109 |
* http://apache.org/xml/features/standard-uri-conformant |
|
110 |
*/ |
|
111 |
protected boolean fStrictURI; |
|
112 |
||
113 |
||
114 |
/** Feature identifier: external general entities. */ |
|
115 |
protected static final String EXTERNAL_GENERAL_ENTITIES = |
|
116 |
Constants.SAX_FEATURE_PREFIX + Constants.EXTERNAL_GENERAL_ENTITIES_FEATURE; |
|
117 |
||
118 |
/** Feature identifier: external parameter entities. */ |
|
119 |
protected static final String EXTERNAL_PARAMETER_ENTITIES = |
|
120 |
Constants.SAX_FEATURE_PREFIX + Constants.EXTERNAL_PARAMETER_ENTITIES_FEATURE; |
|
121 |
||
122 |
/** Feature identifier: allow Java encodings. */ |
|
123 |
protected static final String ALLOW_JAVA_ENCODINGS = |
|
124 |
Constants.XERCES_FEATURE_PREFIX + Constants.ALLOW_JAVA_ENCODINGS_FEATURE; |
|
125 |
||
126 |
/** Feature identifier: warn on duplicate EntityDef */ |
|
127 |
protected static final String WARN_ON_DUPLICATE_ENTITYDEF = |
|
128 |
Constants.XERCES_FEATURE_PREFIX +Constants.WARN_ON_DUPLICATE_ENTITYDEF_FEATURE; |
|
129 |
||
17534 | 130 |
/** Feature identifier: load external DTD. */ |
131 |
protected static final String LOAD_EXTERNAL_DTD = |
|
132 |
Constants.XERCES_FEATURE_PREFIX + Constants.LOAD_EXTERNAL_DTD_FEATURE; |
|
133 |
||
12005 | 134 |
// property identifiers |
135 |
||
136 |
/** Property identifier: symbol table. */ |
|
137 |
protected static final String SYMBOL_TABLE = |
|
138 |
Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY; |
|
139 |
||
140 |
/** Property identifier: error reporter. */ |
|
141 |
protected static final String ERROR_REPORTER = |
|
142 |
Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY; |
|
143 |
||
144 |
/** Feature identifier: standard uri conformant */ |
|
145 |
protected static final String STANDARD_URI_CONFORMANT = |
|
146 |
Constants.XERCES_FEATURE_PREFIX +Constants.STANDARD_URI_CONFORMANT_FEATURE; |
|
147 |
||
148 |
/** Property identifier: entity resolver. */ |
|
149 |
protected static final String ENTITY_RESOLVER = |
|
150 |
Constants.XERCES_PROPERTY_PREFIX + Constants.ENTITY_RESOLVER_PROPERTY; |
|
151 |
||
152 |
protected static final String STAX_ENTITY_RESOLVER = |
|
153 |
Constants.XERCES_PROPERTY_PREFIX + Constants.STAX_ENTITY_RESOLVER_PROPERTY; |
|
154 |
||
155 |
// property identifier: ValidationManager |
|
156 |
protected static final String VALIDATION_MANAGER = |
|
157 |
Constants.XERCES_PROPERTY_PREFIX + Constants.VALIDATION_MANAGER_PROPERTY; |
|
158 |
||
159 |
/** property identifier: buffer size. */ |
|
160 |
protected static final String BUFFER_SIZE = |
|
161 |
Constants.XERCES_PROPERTY_PREFIX + Constants.BUFFER_SIZE_PROPERTY; |
|
162 |
||
163 |
/** property identifier: security manager. */ |
|
164 |
protected static final String SECURITY_MANAGER = |
|
165 |
Constants.XERCES_PROPERTY_PREFIX + Constants.SECURITY_MANAGER_PROPERTY; |
|
166 |
||
17534 | 167 |
protected static final String PARSER_SETTINGS = |
12005 | 168 |
Constants.XERCES_FEATURE_PREFIX + Constants.PARSER_SETTINGS; |
17534 | 169 |
|
18890
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17645
diff
changeset
|
170 |
/** Property identifier: Security property manager. */ |
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17645
diff
changeset
|
171 |
private static final String XML_SECURITY_PROPERTY_MANAGER = |
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17645
diff
changeset
|
172 |
Constants.XML_SECURITY_PROPERTY_MANAGER; |
17534 | 173 |
|
174 |
/** access external dtd: file protocol */ |
|
175 |
static final String EXTERNAL_ACCESS_DEFAULT = Constants.EXTERNAL_ACCESS_DEFAULT; |
|
176 |
||
177 |
||
12005 | 178 |
// recognized features and properties |
179 |
||
180 |
/** Recognized features. */ |
|
181 |
private static final String[] RECOGNIZED_FEATURES = { |
|
182 |
VALIDATION, |
|
183 |
EXTERNAL_GENERAL_ENTITIES, |
|
184 |
EXTERNAL_PARAMETER_ENTITIES, |
|
185 |
ALLOW_JAVA_ENCODINGS, |
|
12903 | 186 |
WARN_ON_DUPLICATE_ENTITYDEF, |
187 |
STANDARD_URI_CONFORMANT |
|
12005 | 188 |
}; |
189 |
||
190 |
/** Feature defaults. */ |
|
191 |
private static final Boolean[] FEATURE_DEFAULTS = { |
|
192 |
null, |
|
193 |
Boolean.TRUE, |
|
194 |
Boolean.TRUE, |
|
195 |
Boolean.TRUE, |
|
196 |
Boolean.FALSE, |
|
12903 | 197 |
Boolean.FALSE |
12005 | 198 |
}; |
199 |
||
200 |
/** Recognized properties. */ |
|
201 |
private static final String[] RECOGNIZED_PROPERTIES = { |
|
202 |
SYMBOL_TABLE, |
|
203 |
ERROR_REPORTER, |
|
204 |
ENTITY_RESOLVER, |
|
205 |
VALIDATION_MANAGER, |
|
206 |
BUFFER_SIZE, |
|
207 |
SECURITY_MANAGER, |
|
18890
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17645
diff
changeset
|
208 |
XML_SECURITY_PROPERTY_MANAGER |
12005 | 209 |
}; |
210 |
||
211 |
/** Property defaults. */ |
|
212 |
private static final Object[] PROPERTY_DEFAULTS = { |
|
213 |
null, |
|
214 |
null, |
|
215 |
null, |
|
216 |
null, |
|
217 |
new Integer(DEFAULT_BUFFER_SIZE), |
|
17534 | 218 |
null, |
18890
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17645
diff
changeset
|
219 |
null |
12005 | 220 |
}; |
221 |
||
222 |
private static final String XMLEntity = "[xml]".intern(); |
|
223 |
private static final String DTDEntity = "[dtd]".intern(); |
|
224 |
||
225 |
// debugging |
|
226 |
||
227 |
/** |
|
228 |
* Debug printing of buffer. This debugging flag works best when you |
|
229 |
* resize the DEFAULT_BUFFER_SIZE down to something reasonable like |
|
230 |
* 64 characters. |
|
231 |
*/ |
|
232 |
private static final boolean DEBUG_BUFFER = false; |
|
233 |
||
234 |
/** warn on duplicate Entity declaration. |
|
235 |
* http://apache.org/xml/features/warn-on-duplicate-entitydef |
|
236 |
*/ |
|
237 |
protected boolean fWarnDuplicateEntityDef; |
|
238 |
||
239 |
/** Debug some basic entities. */ |
|
240 |
private static final boolean DEBUG_ENTITIES = false; |
|
241 |
||
242 |
/** Debug switching readers for encodings. */ |
|
243 |
private static final boolean DEBUG_ENCODINGS = false; |
|
244 |
||
245 |
// should be diplayed trace resolving messages |
|
246 |
private static final boolean DEBUG_RESOLVER = false ; |
|
247 |
||
248 |
// |
|
249 |
// Data |
|
250 |
// |
|
251 |
||
252 |
// features |
|
253 |
||
254 |
/** |
|
255 |
* Validation. This feature identifier is: |
|
256 |
* http://xml.org/sax/features/validation |
|
257 |
*/ |
|
258 |
protected boolean fValidation; |
|
259 |
||
260 |
/** |
|
261 |
* External general entities. This feature identifier is: |
|
262 |
* http://xml.org/sax/features/external-general-entities |
|
263 |
*/ |
|
264 |
protected boolean fExternalGeneralEntities; |
|
265 |
||
266 |
/** |
|
267 |
* External parameter entities. This feature identifier is: |
|
268 |
* http://xml.org/sax/features/external-parameter-entities |
|
269 |
*/ |
|
270 |
protected boolean fExternalParameterEntities; |
|
271 |
||
272 |
/** |
|
273 |
* Allow Java encoding names. This feature identifier is: |
|
274 |
* http://apache.org/xml/features/allow-java-encodings |
|
275 |
*/ |
|
276 |
protected boolean fAllowJavaEncodings = true ; |
|
277 |
||
17534 | 278 |
/** Load external DTD. */ |
279 |
protected boolean fLoadExternalDTD = true; |
|
12005 | 280 |
|
281 |
// properties |
|
282 |
||
283 |
/** |
|
284 |
* Symbol table. This property identifier is: |
|
285 |
* http://apache.org/xml/properties/internal/symbol-table |
|
286 |
*/ |
|
287 |
protected SymbolTable fSymbolTable; |
|
288 |
||
289 |
/** |
|
290 |
* Error reporter. This property identifier is: |
|
291 |
* http://apache.org/xml/properties/internal/error-reporter |
|
292 |
*/ |
|
293 |
protected XMLErrorReporter fErrorReporter; |
|
294 |
||
295 |
/** |
|
296 |
* Entity resolver. This property identifier is: |
|
297 |
* http://apache.org/xml/properties/internal/entity-resolver |
|
298 |
*/ |
|
299 |
protected XMLEntityResolver fEntityResolver; |
|
300 |
||
301 |
/** Stax Entity Resolver. This property identifier is XMLInputFactory.ENTITY_RESOLVER */ |
|
302 |
||
303 |
protected StaxEntityResolverWrapper fStaxEntityResolver; |
|
304 |
||
305 |
/** Property Manager. This is used from Stax */ |
|
306 |
protected PropertyManager fPropertyManager ; |
|
307 |
||
17534 | 308 |
/** used to restrict external access */ |
309 |
protected String fAccessExternalDTD = EXTERNAL_ACCESS_DEFAULT; |
|
12005 | 310 |
// settings |
311 |
||
312 |
/** |
|
313 |
* Validation manager. This property identifier is: |
|
314 |
* http://apache.org/xml/properties/internal/validation-manager |
|
315 |
*/ |
|
316 |
protected ValidationManager fValidationManager; |
|
317 |
||
318 |
// settings |
|
319 |
||
320 |
/** |
|
321 |
* Buffer size. We get this value from a property. The default size |
|
322 |
* is used if the input buffer size property is not specified. |
|
323 |
* REVISIT: do we need a property for internal entity buffer size? |
|
324 |
*/ |
|
325 |
protected int fBufferSize = DEFAULT_BUFFER_SIZE; |
|
326 |
||
327 |
// stores defaults for entity expansion limit if it has |
|
328 |
// been set on the configuration. |
|
329 |
protected SecurityManager fSecurityManager = null; |
|
330 |
||
331 |
/** |
|
332 |
* True if the document entity is standalone. This should really |
|
333 |
* only be set by the document source (e.g. XMLDocumentScanner). |
|
334 |
*/ |
|
335 |
protected boolean fStandalone; |
|
336 |
||
337 |
// are the entities being parsed in the external subset? |
|
338 |
// NOTE: this *is not* the same as whether they're external entities! |
|
339 |
protected boolean fInExternalSubset = false; |
|
340 |
||
341 |
||
342 |
// handlers |
|
343 |
/** Entity handler. */ |
|
344 |
protected XMLEntityHandler fEntityHandler; |
|
345 |
||
346 |
/** Current entity scanner */ |
|
347 |
protected XMLEntityScanner fEntityScanner ; |
|
348 |
||
349 |
/** XML 1.0 entity scanner. */ |
|
350 |
protected XMLEntityScanner fXML10EntityScanner; |
|
351 |
||
352 |
/** XML 1.1 entity scanner. */ |
|
353 |
protected XMLEntityScanner fXML11EntityScanner; |
|
354 |
||
355 |
/** entity expansion limit (contains useful data if and only if |
|
356 |
fSecurityManager is non-null) */ |
|
357 |
protected int fEntityExpansionLimit = 0; |
|
358 |
||
359 |
/** count of entities expanded: */ |
|
360 |
protected int fEntityExpansionCount = 0; |
|
361 |
||
362 |
// entities |
|
363 |
||
364 |
/** Entities. */ |
|
365 |
protected Hashtable fEntities = new Hashtable(); |
|
366 |
||
367 |
/** Entity stack. */ |
|
368 |
protected Stack fEntityStack = new Stack(); |
|
369 |
||
370 |
/** Current entity. */ |
|
371 |
protected Entity.ScannedEntity fCurrentEntity = null; |
|
372 |
||
17534 | 373 |
/** identify if the InputSource is created by a resolver */ |
374 |
boolean fISCreatedByResolver = false; |
|
375 |
||
12005 | 376 |
// shared context |
377 |
||
378 |
protected XMLEntityStorage fEntityStorage ; |
|
379 |
||
380 |
protected final Object [] defaultEncoding = new Object[]{"UTF-8", null}; |
|
381 |
||
382 |
||
383 |
// temp vars |
|
384 |
||
385 |
/** Resource identifer. */ |
|
386 |
private final XMLResourceIdentifierImpl fResourceIdentifier = new XMLResourceIdentifierImpl(); |
|
387 |
||
388 |
/** Augmentations for entities. */ |
|
389 |
private final Augmentations fEntityAugs = new AugmentationsImpl(); |
|
390 |
||
391 |
/** Pool of character buffers. */ |
|
392 |
private CharacterBufferPool fBufferPool = new CharacterBufferPool(fBufferSize, DEFAULT_INTERNAL_BUFFER_SIZE); |
|
393 |
||
394 |
// |
|
395 |
// Constructors |
|
396 |
// |
|
397 |
||
398 |
/** |
|
399 |
* If this constructor is used to create the object, reset() should be invoked on this object |
|
400 |
*/ |
|
401 |
public XMLEntityManager() { |
|
402 |
fEntityStorage = new XMLEntityStorage(this) ; |
|
403 |
setScannerVersion(Constants.XML_VERSION_1_0); |
|
404 |
} // <init>() |
|
405 |
||
406 |
/** Default constructor. */ |
|
407 |
public XMLEntityManager(PropertyManager propertyManager) { |
|
408 |
fPropertyManager = propertyManager ; |
|
409 |
//pass a reference to current entity being scanned |
|
410 |
//fEntityStorage = new XMLEntityStorage(fCurrentEntity) ; |
|
411 |
fEntityStorage = new XMLEntityStorage(this) ; |
|
412 |
fEntityScanner = new XMLEntityScanner(propertyManager, this) ; |
|
413 |
reset(propertyManager); |
|
414 |
} // <init>() |
|
415 |
||
416 |
/** |
|
417 |
* Adds an internal entity declaration. |
|
418 |
* <p> |
|
419 |
* <strong>Note:</strong> This method ignores subsequent entity |
|
420 |
* declarations. |
|
421 |
* <p> |
|
422 |
* <strong>Note:</strong> The name should be a unique symbol. The |
|
423 |
* SymbolTable can be used for this purpose. |
|
424 |
* |
|
425 |
* @param name The name of the entity. |
|
426 |
* @param text The text of the entity. |
|
427 |
* |
|
428 |
* @see SymbolTable |
|
429 |
*/ |
|
430 |
public void addInternalEntity(String name, String text) { |
|
431 |
if (!fEntities.containsKey(name)) { |
|
432 |
Entity entity = new Entity.InternalEntity(name, text, fInExternalSubset); |
|
433 |
fEntities.put(name, entity); |
|
434 |
} else{ |
|
435 |
if(fWarnDuplicateEntityDef){ |
|
436 |
fErrorReporter.reportError(XMLMessageFormatter.XML_DOMAIN, |
|
437 |
"MSG_DUPLICATE_ENTITY_DEFINITION", |
|
438 |
new Object[]{ name }, |
|
439 |
XMLErrorReporter.SEVERITY_WARNING ); |
|
440 |
} |
|
441 |
} |
|
442 |
||
443 |
} // addInternalEntity(String,String) |
|
444 |
||
445 |
/** |
|
446 |
* Adds an external entity declaration. |
|
447 |
* <p> |
|
448 |
* <strong>Note:</strong> This method ignores subsequent entity |
|
449 |
* declarations. |
|
450 |
* <p> |
|
451 |
* <strong>Note:</strong> The name should be a unique symbol. The |
|
452 |
* SymbolTable can be used for this purpose. |
|
453 |
* |
|
454 |
* @param name The name of the entity. |
|
455 |
* @param publicId The public identifier of the entity. |
|
456 |
* @param literalSystemId The system identifier of the entity. |
|
457 |
* @param baseSystemId The base system identifier of the entity. |
|
458 |
* This is the system identifier of the entity |
|
459 |
* where <em>the entity being added</em> and |
|
460 |
* is used to expand the system identifier when |
|
461 |
* the system identifier is a relative URI. |
|
462 |
* When null the system identifier of the first |
|
463 |
* external entity on the stack is used instead. |
|
464 |
* |
|
465 |
* @see SymbolTable |
|
466 |
*/ |
|
467 |
public void addExternalEntity(String name, |
|
468 |
String publicId, String literalSystemId, |
|
469 |
String baseSystemId) throws IOException { |
|
470 |
if (!fEntities.containsKey(name)) { |
|
471 |
if (baseSystemId == null) { |
|
472 |
// search for the first external entity on the stack |
|
473 |
int size = fEntityStack.size(); |
|
474 |
if (size == 0 && fCurrentEntity != null && fCurrentEntity.entityLocation != null) { |
|
475 |
baseSystemId = fCurrentEntity.entityLocation.getExpandedSystemId(); |
|
476 |
} |
|
477 |
for (int i = size - 1; i >= 0 ; i--) { |
|
478 |
Entity.ScannedEntity externalEntity = |
|
479 |
(Entity.ScannedEntity)fEntityStack.elementAt(i); |
|
480 |
if (externalEntity.entityLocation != null && externalEntity.entityLocation.getExpandedSystemId() != null) { |
|
481 |
baseSystemId = externalEntity.entityLocation.getExpandedSystemId(); |
|
482 |
break; |
|
483 |
} |
|
484 |
} |
|
485 |
} |
|
486 |
Entity entity = new Entity.ExternalEntity(name, |
|
487 |
new XMLEntityDescriptionImpl(name, publicId, literalSystemId, baseSystemId, |
|
488 |
expandSystemId(literalSystemId, baseSystemId, false)), null, fInExternalSubset); |
|
489 |
fEntities.put(name, entity); |
|
490 |
} else{ |
|
491 |
if(fWarnDuplicateEntityDef){ |
|
492 |
fErrorReporter.reportError(XMLMessageFormatter.XML_DOMAIN, |
|
493 |
"MSG_DUPLICATE_ENTITY_DEFINITION", |
|
494 |
new Object[]{ name }, |
|
495 |
XMLErrorReporter.SEVERITY_WARNING ); |
|
496 |
} |
|
497 |
} |
|
498 |
||
499 |
} // addExternalEntity(String,String,String,String) |
|
500 |
||
501 |
||
502 |
/** |
|
503 |
* Adds an unparsed entity declaration. |
|
504 |
* <p> |
|
505 |
* <strong>Note:</strong> This method ignores subsequent entity |
|
506 |
* declarations. |
|
507 |
* <p> |
|
508 |
* <strong>Note:</strong> The name should be a unique symbol. The |
|
509 |
* SymbolTable can be used for this purpose. |
|
510 |
* |
|
511 |
* @param name The name of the entity. |
|
512 |
* @param publicId The public identifier of the entity. |
|
513 |
* @param systemId The system identifier of the entity. |
|
514 |
* @param notation The name of the notation. |
|
515 |
* |
|
516 |
* @see SymbolTable |
|
517 |
*/ |
|
518 |
public void addUnparsedEntity(String name, |
|
519 |
String publicId, String systemId, |
|
520 |
String baseSystemId, String notation) { |
|
521 |
if (!fEntities.containsKey(name)) { |
|
522 |
Entity.ExternalEntity entity = new Entity.ExternalEntity(name, |
|
523 |
new XMLEntityDescriptionImpl(name, publicId, systemId, baseSystemId, null), |
|
524 |
notation, fInExternalSubset); |
|
525 |
fEntities.put(name, entity); |
|
526 |
} else{ |
|
527 |
if(fWarnDuplicateEntityDef){ |
|
528 |
fErrorReporter.reportError(XMLMessageFormatter.XML_DOMAIN, |
|
529 |
"MSG_DUPLICATE_ENTITY_DEFINITION", |
|
530 |
new Object[]{ name }, |
|
531 |
XMLErrorReporter.SEVERITY_WARNING ); |
|
532 |
} |
|
533 |
} |
|
534 |
} // addUnparsedEntity(String,String,String,String) |
|
535 |
||
536 |
||
537 |
/** get the entity storage object from entity manager */ |
|
538 |
public XMLEntityStorage getEntityStore(){ |
|
539 |
return fEntityStorage ; |
|
540 |
} |
|
541 |
||
542 |
/** return the entity responsible for reading the entity */ |
|
543 |
public XMLEntityScanner getEntityScanner(){ |
|
544 |
if(fEntityScanner == null) { |
|
545 |
// default to 1.0 |
|
546 |
if(fXML10EntityScanner == null) { |
|
547 |
fXML10EntityScanner = new XMLEntityScanner(); |
|
548 |
} |
|
549 |
fXML10EntityScanner.reset(fSymbolTable, this, fErrorReporter); |
|
550 |
fEntityScanner = fXML10EntityScanner; |
|
551 |
} |
|
552 |
return fEntityScanner; |
|
553 |
||
554 |
} |
|
555 |
||
556 |
public void setScannerVersion(short version) { |
|
557 |
||
558 |
if(version == Constants.XML_VERSION_1_0) { |
|
559 |
if(fXML10EntityScanner == null) { |
|
560 |
fXML10EntityScanner = new XMLEntityScanner(); |
|
561 |
} |
|
562 |
fXML10EntityScanner.reset(fSymbolTable, this, fErrorReporter); |
|
563 |
fEntityScanner = fXML10EntityScanner; |
|
564 |
fEntityScanner.setCurrentEntity(fCurrentEntity); |
|
565 |
} else { |
|
566 |
if(fXML11EntityScanner == null) { |
|
567 |
fXML11EntityScanner = new XML11EntityScanner(); |
|
568 |
} |
|
569 |
fXML11EntityScanner.reset(fSymbolTable, this, fErrorReporter); |
|
570 |
fEntityScanner = fXML11EntityScanner; |
|
571 |
fEntityScanner.setCurrentEntity(fCurrentEntity); |
|
572 |
} |
|
573 |
||
574 |
} |
|
575 |
||
576 |
/** |
|
577 |
* This method uses the passed-in XMLInputSource to make |
|
578 |
* fCurrentEntity usable for reading. |
|
579 |
* @param name name of the entity (XML is it's the document entity) |
|
580 |
* @param xmlInputSource the input source, with sufficient information |
|
581 |
* to begin scanning characters. |
|
582 |
* @param literal True if this entity is started within a |
|
583 |
* literal value. |
|
584 |
* @param isExternal whether this entity should be treated as an internal or external entity. |
|
585 |
* @throws IOException if anything can't be read |
|
586 |
* XNIException If any parser-specific goes wrong. |
|
587 |
* @return the encoding of the new entity or null if a character stream was employed |
|
588 |
*/ |
|
589 |
public String setupCurrentEntity(String name, XMLInputSource xmlInputSource, |
|
590 |
boolean literal, boolean isExternal) |
|
591 |
throws IOException, XNIException { |
|
592 |
// get information |
|
593 |
||
594 |
final String publicId = xmlInputSource.getPublicId(); |
|
595 |
String literalSystemId = xmlInputSource.getSystemId(); |
|
596 |
String baseSystemId = xmlInputSource.getBaseSystemId(); |
|
597 |
String encoding = xmlInputSource.getEncoding(); |
|
598 |
final boolean encodingExternallySpecified = (encoding != null); |
|
599 |
Boolean isBigEndian = null; |
|
600 |
||
601 |
// create reader |
|
602 |
InputStream stream = null; |
|
603 |
Reader reader = xmlInputSource.getCharacterStream(); |
|
604 |
||
605 |
// First chance checking strict URI |
|
606 |
String expandedSystemId = expandSystemId(literalSystemId, baseSystemId, fStrictURI); |
|
607 |
if (baseSystemId == null) { |
|
608 |
baseSystemId = expandedSystemId; |
|
609 |
} |
|
610 |
if (reader == null) { |
|
611 |
stream = xmlInputSource.getByteStream(); |
|
612 |
if (stream == null) { |
|
13266
8be9cee3d6dd
7183760: DocumentBuilder.parse(String uri) is not IPv6 enabled
joehw
parents:
13176
diff
changeset
|
613 |
URL location = new URL(expandedSystemId); |
12005 | 614 |
URLConnection connect = location.openConnection(); |
615 |
if (!(connect instanceof HttpURLConnection)) { |
|
616 |
stream = connect.getInputStream(); |
|
617 |
} |
|
618 |
else { |
|
619 |
boolean followRedirects = true; |
|
620 |
||
621 |
// setup URLConnection if we have an HTTPInputSource |
|
622 |
if (xmlInputSource instanceof HTTPInputSource) { |
|
623 |
final HttpURLConnection urlConnection = (HttpURLConnection) connect; |
|
624 |
final HTTPInputSource httpInputSource = (HTTPInputSource) xmlInputSource; |
|
625 |
||
626 |
// set request properties |
|
627 |
Iterator propIter = httpInputSource.getHTTPRequestProperties(); |
|
628 |
while (propIter.hasNext()) { |
|
629 |
Map.Entry entry = (Map.Entry) propIter.next(); |
|
630 |
urlConnection.setRequestProperty((String) entry.getKey(), (String) entry.getValue()); |
|
631 |
} |
|
632 |
||
633 |
// set preference for redirection |
|
634 |
followRedirects = httpInputSource.getFollowHTTPRedirects(); |
|
635 |
if (!followRedirects) { |
|
636 |
setInstanceFollowRedirects(urlConnection, followRedirects); |
|
637 |
} |
|
638 |
} |
|
639 |
||
640 |
stream = connect.getInputStream(); |
|
641 |
||
642 |
// REVISIT: If the URLConnection has external encoding |
|
643 |
// information, we should be reading it here. It's located |
|
644 |
// in the charset parameter of Content-Type. -- mrglavas |
|
645 |
||
646 |
if (followRedirects) { |
|
647 |
String redirect = connect.getURL().toString(); |
|
648 |
// E43: Check if the URL was redirected, and then |
|
649 |
// update literal and expanded system IDs if needed. |
|
650 |
if (!redirect.equals(expandedSystemId)) { |
|
651 |
literalSystemId = redirect; |
|
652 |
expandedSystemId = redirect; |
|
653 |
} |
|
654 |
} |
|
655 |
} |
|
656 |
} |
|
657 |
||
658 |
// wrap this stream in RewindableInputStream |
|
659 |
stream = new RewindableInputStream(stream); |
|
660 |
||
661 |
// perform auto-detect of encoding if necessary |
|
662 |
if (encoding == null) { |
|
663 |
// read first four bytes and determine encoding |
|
664 |
final byte[] b4 = new byte[4]; |
|
665 |
int count = 0; |
|
666 |
for (; count<4; count++ ) { |
|
667 |
b4[count] = (byte)stream.read(); |
|
668 |
} |
|
669 |
if (count == 4) { |
|
670 |
Object [] encodingDesc = getEncodingName(b4, count); |
|
671 |
encoding = (String)(encodingDesc[0]); |
|
672 |
isBigEndian = (Boolean)(encodingDesc[1]); |
|
673 |
||
674 |
stream.reset(); |
|
675 |
// Special case UTF-8 files with BOM created by Microsoft |
|
676 |
// tools. It's more efficient to consume the BOM than make |
|
677 |
// the reader perform extra checks. -Ac |
|
678 |
if (count > 2 && encoding.equals("UTF-8")) { |
|
679 |
int b0 = b4[0] & 0xFF; |
|
680 |
int b1 = b4[1] & 0xFF; |
|
681 |
int b2 = b4[2] & 0xFF; |
|
682 |
if (b0 == 0xEF && b1 == 0xBB && b2 == 0xBF) { |
|
683 |
// ignore first three bytes... |
|
684 |
stream.skip(3); |
|
685 |
} |
|
686 |
} |
|
687 |
reader = createReader(stream, encoding, isBigEndian); |
|
688 |
} else { |
|
689 |
reader = createReader(stream, encoding, isBigEndian); |
|
690 |
} |
|
691 |
} |
|
692 |
||
693 |
// use specified encoding |
|
694 |
else { |
|
695 |
encoding = encoding.toUpperCase(Locale.ENGLISH); |
|
696 |
||
697 |
// If encoding is UTF-8, consume BOM if one is present. |
|
698 |
if (encoding.equals("UTF-8")) { |
|
699 |
final int[] b3 = new int[3]; |
|
700 |
int count = 0; |
|
701 |
for (; count < 3; ++count) { |
|
702 |
b3[count] = stream.read(); |
|
703 |
if (b3[count] == -1) |
|
704 |
break; |
|
705 |
} |
|
706 |
if (count == 3) { |
|
707 |
if (b3[0] != 0xEF || b3[1] != 0xBB || b3[2] != 0xBF) { |
|
708 |
// First three bytes are not BOM, so reset. |
|
709 |
stream.reset(); |
|
710 |
} |
|
711 |
} else { |
|
712 |
stream.reset(); |
|
713 |
} |
|
714 |
} |
|
715 |
// If encoding is UTF-16, we still need to read the first four bytes |
|
716 |
// in order to discover the byte order. |
|
717 |
else if (encoding.equals("UTF-16")) { |
|
718 |
final int[] b4 = new int[4]; |
|
719 |
int count = 0; |
|
720 |
for (; count < 4; ++count) { |
|
721 |
b4[count] = stream.read(); |
|
722 |
if (b4[count] == -1) |
|
723 |
break; |
|
724 |
} |
|
725 |
stream.reset(); |
|
726 |
||
727 |
String utf16Encoding = "UTF-16"; |
|
728 |
if (count >= 2) { |
|
729 |
final int b0 = b4[0]; |
|
730 |
final int b1 = b4[1]; |
|
731 |
if (b0 == 0xFE && b1 == 0xFF) { |
|
732 |
// UTF-16, big-endian |
|
733 |
utf16Encoding = "UTF-16BE"; |
|
734 |
isBigEndian = Boolean.TRUE; |
|
735 |
} |
|
736 |
else if (b0 == 0xFF && b1 == 0xFE) { |
|
737 |
// UTF-16, little-endian |
|
738 |
utf16Encoding = "UTF-16LE"; |
|
739 |
isBigEndian = Boolean.FALSE; |
|
740 |
} |
|
741 |
else if (count == 4) { |
|
742 |
final int b2 = b4[2]; |
|
743 |
final int b3 = b4[3]; |
|
744 |
if (b0 == 0x00 && b1 == 0x3C && b2 == 0x00 && b3 == 0x3F) { |
|
745 |
// UTF-16, big-endian, no BOM |
|
746 |
utf16Encoding = "UTF-16BE"; |
|
747 |
isBigEndian = Boolean.TRUE; |
|
748 |
} |
|
749 |
if (b0 == 0x3C && b1 == 0x00 && b2 == 0x3F && b3 == 0x00) { |
|
750 |
// UTF-16, little-endian, no BOM |
|
751 |
utf16Encoding = "UTF-16LE"; |
|
752 |
isBigEndian = Boolean.FALSE; |
|
753 |
} |
|
754 |
} |
|
755 |
} |
|
756 |
reader = createReader(stream, utf16Encoding, isBigEndian); |
|
757 |
} |
|
758 |
// If encoding is UCS-4, we still need to read the first four bytes |
|
759 |
// in order to discover the byte order. |
|
760 |
else if (encoding.equals("ISO-10646-UCS-4")) { |
|
761 |
final int[] b4 = new int[4]; |
|
762 |
int count = 0; |
|
763 |
for (; count < 4; ++count) { |
|
764 |
b4[count] = stream.read(); |
|
765 |
if (b4[count] == -1) |
|
766 |
break; |
|
767 |
} |
|
768 |
stream.reset(); |
|
769 |
||
770 |
// Ignore unusual octet order for now. |
|
771 |
if (count == 4) { |
|
772 |
// UCS-4, big endian (1234) |
|
773 |
if (b4[0] == 0x00 && b4[1] == 0x00 && b4[2] == 0x00 && b4[3] == 0x3C) { |
|
774 |
isBigEndian = Boolean.TRUE; |
|
775 |
} |
|
776 |
// UCS-4, little endian (1234) |
|
777 |
else if (b4[0] == 0x3C && b4[1] == 0x00 && b4[2] == 0x00 && b4[3] == 0x00) { |
|
778 |
isBigEndian = Boolean.FALSE; |
|
779 |
} |
|
780 |
} |
|
781 |
} |
|
782 |
// If encoding is UCS-2, we still need to read the first four bytes |
|
783 |
// in order to discover the byte order. |
|
784 |
else if (encoding.equals("ISO-10646-UCS-2")) { |
|
785 |
final int[] b4 = new int[4]; |
|
786 |
int count = 0; |
|
787 |
for (; count < 4; ++count) { |
|
788 |
b4[count] = stream.read(); |
|
789 |
if (b4[count] == -1) |
|
790 |
break; |
|
791 |
} |
|
792 |
stream.reset(); |
|
793 |
||
794 |
if (count == 4) { |
|
795 |
// UCS-2, big endian |
|
796 |
if (b4[0] == 0x00 && b4[1] == 0x3C && b4[2] == 0x00 && b4[3] == 0x3F) { |
|
797 |
isBigEndian = Boolean.TRUE; |
|
798 |
} |
|
799 |
// UCS-2, little endian |
|
800 |
else if (b4[0] == 0x3C && b4[1] == 0x00 && b4[2] == 0x3F && b4[3] == 0x00) { |
|
801 |
isBigEndian = Boolean.FALSE; |
|
802 |
} |
|
803 |
} |
|
804 |
} |
|
805 |
||
806 |
reader = createReader(stream, encoding, isBigEndian); |
|
807 |
} |
|
808 |
||
809 |
// read one character at a time so we don't jump too far |
|
810 |
// ahead, converting characters from the byte stream in |
|
811 |
// the wrong encoding |
|
812 |
if (DEBUG_ENCODINGS) { |
|
813 |
System.out.println("$$$ no longer wrapping reader in OneCharReader"); |
|
814 |
} |
|
815 |
//reader = new OneCharReader(reader); |
|
816 |
} |
|
817 |
||
818 |
// We've seen a new Reader. |
|
819 |
// Push it on the stack so we can close it later. |
|
820 |
//fOwnReaders.add(reader); |
|
821 |
||
822 |
// push entity on stack |
|
823 |
if (fCurrentEntity != null) { |
|
824 |
fEntityStack.push(fCurrentEntity); |
|
825 |
} |
|
826 |
||
827 |
// create entity |
|
828 |
/* if encoding is specified externally, 'encoding' information present |
|
829 |
* in the prolog of the XML document is not considered. Hence, prolog can |
|
830 |
* be read in Chunks of data instead of byte by byte. |
|
831 |
*/ |
|
832 |
fCurrentEntity = new com.sun.xml.internal.stream.Entity.ScannedEntity(name,new XMLResourceIdentifierImpl(publicId, literalSystemId, baseSystemId, expandedSystemId),stream, reader, encoding, literal, encodingExternallySpecified, isExternal); |
|
833 |
fCurrentEntity.setEncodingExternallySpecified(encodingExternallySpecified); |
|
834 |
fEntityScanner.setCurrentEntity(fCurrentEntity); |
|
835 |
fResourceIdentifier.setValues(publicId, literalSystemId, baseSystemId, expandedSystemId); |
|
836 |
return encoding; |
|
837 |
} //setupCurrentEntity(String, XMLInputSource, boolean, boolean): String |
|
838 |
||
839 |
||
840 |
/** |
|
841 |
* Checks whether an entity given by name is external. |
|
842 |
* |
|
843 |
* @param entityName The name of the entity to check. |
|
844 |
* @return True if the entity is external, false otherwise |
|
845 |
* (including when the entity is not declared). |
|
846 |
*/ |
|
847 |
public boolean isExternalEntity(String entityName) { |
|
848 |
||
849 |
Entity entity = (Entity)fEntities.get(entityName); |
|
850 |
if (entity == null) { |
|
851 |
return false; |
|
852 |
} |
|
853 |
return entity.isExternal(); |
|
854 |
} |
|
855 |
||
856 |
/** |
|
857 |
* Checks whether the declaration of an entity given by name is |
|
858 |
* // in the external subset. |
|
859 |
* |
|
860 |
* @param entityName The name of the entity to check. |
|
861 |
* @return True if the entity was declared in the external subset, false otherwise |
|
862 |
* (including when the entity is not declared). |
|
863 |
*/ |
|
864 |
public boolean isEntityDeclInExternalSubset(String entityName) { |
|
865 |
||
866 |
Entity entity = (Entity)fEntities.get(entityName); |
|
867 |
if (entity == null) { |
|
868 |
return false; |
|
869 |
} |
|
870 |
return entity.isEntityDeclInExternalSubset(); |
|
871 |
} |
|
872 |
||
873 |
||
874 |
||
875 |
// |
|
876 |
// Public methods |
|
877 |
// |
|
878 |
||
879 |
/** |
|
880 |
* Sets whether the document entity is standalone. |
|
881 |
* |
|
882 |
* @param standalone True if document entity is standalone. |
|
883 |
*/ |
|
884 |
public void setStandalone(boolean standalone) { |
|
885 |
fStandalone = standalone; |
|
886 |
} |
|
887 |
// setStandalone(boolean) |
|
888 |
||
889 |
/** Returns true if the document entity is standalone. */ |
|
890 |
public boolean isStandalone() { |
|
891 |
return fStandalone; |
|
892 |
} //isStandalone():boolean |
|
893 |
||
894 |
public boolean isDeclaredEntity(String entityName) { |
|
895 |
||
896 |
Entity entity = (Entity)fEntities.get(entityName); |
|
897 |
return entity != null; |
|
898 |
} |
|
899 |
||
900 |
public boolean isUnparsedEntity(String entityName) { |
|
901 |
||
902 |
Entity entity = (Entity)fEntities.get(entityName); |
|
903 |
if (entity == null) { |
|
904 |
return false; |
|
905 |
} |
|
906 |
return entity.isUnparsed(); |
|
907 |
} |
|
908 |
||
909 |
||
910 |
||
911 |
// this simply returns the fResourceIdentifier object; |
|
912 |
// this should only be used with caution by callers that |
|
913 |
// carefully manage the entity manager's behaviour, so that |
|
914 |
// this doesn't returning meaningless or misleading data. |
|
915 |
// @return a reference to the current fResourceIdentifier object |
|
916 |
public XMLResourceIdentifier getCurrentResourceIdentifier() { |
|
917 |
return fResourceIdentifier; |
|
918 |
} |
|
919 |
||
920 |
/** |
|
921 |
* Sets the entity handler. When an entity starts and ends, the |
|
922 |
* entity handler is notified of the change. |
|
923 |
* |
|
924 |
* @param entityHandler The new entity handler. |
|
925 |
*/ |
|
926 |
||
927 |
public void setEntityHandler(com.sun.org.apache.xerces.internal.impl.XMLEntityHandler entityHandler) { |
|
928 |
fEntityHandler = (XMLEntityHandler) entityHandler; |
|
929 |
} // setEntityHandler(XMLEntityHandler) |
|
930 |
||
931 |
//this function returns StaxXMLInputSource |
|
932 |
public StaxXMLInputSource resolveEntityAsPerStax(XMLResourceIdentifier resourceIdentifier) throws java.io.IOException{ |
|
933 |
||
934 |
if(resourceIdentifier == null ) return null; |
|
935 |
||
936 |
String publicId = resourceIdentifier.getPublicId(); |
|
937 |
String literalSystemId = resourceIdentifier.getLiteralSystemId(); |
|
938 |
String baseSystemId = resourceIdentifier.getBaseSystemId(); |
|
939 |
String expandedSystemId = resourceIdentifier.getExpandedSystemId(); |
|
940 |
// if no base systemId given, assume that it's relative |
|
941 |
// to the systemId of the current scanned entity |
|
942 |
// Sometimes the system id is not (properly) expanded. |
|
943 |
// We need to expand the system id if: |
|
944 |
// a. the expanded one was null; or |
|
945 |
// b. the base system id was null, but becomes non-null from the current entity. |
|
946 |
boolean needExpand = (expandedSystemId == null); |
|
947 |
// REVISIT: why would the baseSystemId ever be null? if we |
|
948 |
// didn't have to make this check we wouldn't have to reuse the |
|
949 |
// fXMLResourceIdentifier object... |
|
950 |
if (baseSystemId == null && fCurrentEntity != null && fCurrentEntity.entityLocation != null) { |
|
951 |
baseSystemId = fCurrentEntity.entityLocation.getExpandedSystemId(); |
|
952 |
if (baseSystemId != null) |
|
953 |
needExpand = true; |
|
954 |
} |
|
955 |
if (needExpand) |
|
956 |
expandedSystemId = expandSystemId(literalSystemId, baseSystemId,false); |
|
957 |
||
958 |
// give the entity resolver a chance |
|
959 |
StaxXMLInputSource staxInputSource = null; |
|
960 |
XMLInputSource xmlInputSource = null; |
|
961 |
||
962 |
XMLResourceIdentifierImpl ri = null; |
|
963 |
||
964 |
if (resourceIdentifier instanceof XMLResourceIdentifierImpl) { |
|
965 |
ri = (XMLResourceIdentifierImpl)resourceIdentifier; |
|
966 |
} else { |
|
967 |
fResourceIdentifier.clear(); |
|
968 |
ri = fResourceIdentifier; |
|
969 |
} |
|
970 |
ri.setValues(publicId, literalSystemId, baseSystemId, expandedSystemId); |
|
971 |
if(DEBUG_RESOLVER){ |
|
972 |
System.out.println("BEFORE Calling resolveEntity") ; |
|
973 |
} |
|
974 |
||
17534 | 975 |
fISCreatedByResolver = false; |
12005 | 976 |
//either of Stax or Xerces would be null |
977 |
if(fStaxEntityResolver != null){ |
|
978 |
staxInputSource = fStaxEntityResolver.resolveEntity(ri); |
|
17534 | 979 |
if(staxInputSource != null) { |
980 |
fISCreatedByResolver = true; |
|
981 |
} |
|
12005 | 982 |
} |
983 |
||
984 |
if(fEntityResolver != null){ |
|
985 |
xmlInputSource = fEntityResolver.resolveEntity(ri); |
|
17534 | 986 |
if(xmlInputSource != null) { |
987 |
fISCreatedByResolver = true; |
|
988 |
} |
|
12005 | 989 |
} |
990 |
||
991 |
if(xmlInputSource != null){ |
|
992 |
//wrap this XMLInputSource to StaxInputSource |
|
17534 | 993 |
staxInputSource = new StaxXMLInputSource(xmlInputSource, fISCreatedByResolver); |
12005 | 994 |
} |
995 |
||
996 |
// do default resolution |
|
997 |
//this works for both stax & Xerces, if staxInputSource is null, it means parser need to revert to default resolution |
|
998 |
if (staxInputSource == null) { |
|
999 |
// REVISIT: when systemId is null, I think we should return null. |
|
1000 |
// is this the right solution? -SG |
|
1001 |
//if (systemId != null) |
|
1002 |
staxInputSource = new StaxXMLInputSource(new XMLInputSource(publicId, literalSystemId, baseSystemId)); |
|
1003 |
}else if(staxInputSource.hasXMLStreamOrXMLEventReader()){ |
|
1004 |
//Waiting for the clarification from EG. - nb |
|
1005 |
} |
|
1006 |
||
1007 |
if (DEBUG_RESOLVER) { |
|
1008 |
System.err.println("XMLEntityManager.resolveEntity(" + publicId + ")"); |
|
1009 |
System.err.println(" = " + xmlInputSource); |
|
1010 |
} |
|
1011 |
||
1012 |
return staxInputSource; |
|
1013 |
||
1014 |
} |
|
1015 |
||
1016 |
/** |
|
1017 |
* Resolves the specified public and system identifiers. This |
|
1018 |
* method first attempts to resolve the entity based on the |
|
1019 |
* EntityResolver registered by the application. If no entity |
|
1020 |
* resolver is registered or if the registered entity handler |
|
1021 |
* is unable to resolve the entity, then default entity |
|
1022 |
* resolution will occur. |
|
1023 |
* |
|
1024 |
* @param publicId The public identifier of the entity. |
|
1025 |
* @param systemId The system identifier of the entity. |
|
1026 |
* @param baseSystemId The base system identifier of the entity. |
|
1027 |
* This is the system identifier of the current |
|
1028 |
* entity and is used to expand the system |
|
1029 |
* identifier when the system identifier is a |
|
1030 |
* relative URI. |
|
1031 |
* |
|
1032 |
* @return Returns an input source that wraps the resolved entity. |
|
1033 |
* This method will never return null. |
|
1034 |
* |
|
1035 |
* @throws IOException Thrown on i/o error. |
|
1036 |
* @throws XNIException Thrown by entity resolver to signal an error. |
|
1037 |
*/ |
|
1038 |
public XMLInputSource resolveEntity(XMLResourceIdentifier resourceIdentifier) throws IOException, XNIException { |
|
1039 |
if(resourceIdentifier == null ) return null; |
|
1040 |
String publicId = resourceIdentifier.getPublicId(); |
|
1041 |
String literalSystemId = resourceIdentifier.getLiteralSystemId(); |
|
1042 |
String baseSystemId = resourceIdentifier.getBaseSystemId(); |
|
1043 |
String expandedSystemId = resourceIdentifier.getExpandedSystemId(); |
|
1044 |
String namespace = resourceIdentifier.getNamespace(); |
|
1045 |
||
1046 |
// if no base systemId given, assume that it's relative |
|
1047 |
// to the systemId of the current scanned entity |
|
1048 |
// Sometimes the system id is not (properly) expanded. |
|
1049 |
// We need to expand the system id if: |
|
1050 |
// a. the expanded one was null; or |
|
1051 |
// b. the base system id was null, but becomes non-null from the current entity. |
|
1052 |
boolean needExpand = (expandedSystemId == null); |
|
1053 |
// REVISIT: why would the baseSystemId ever be null? if we |
|
1054 |
// didn't have to make this check we wouldn't have to reuse the |
|
1055 |
// fXMLResourceIdentifier object... |
|
1056 |
if (baseSystemId == null && fCurrentEntity != null && fCurrentEntity.entityLocation != null) { |
|
1057 |
baseSystemId = fCurrentEntity.entityLocation.getExpandedSystemId(); |
|
1058 |
if (baseSystemId != null) |
|
1059 |
needExpand = true; |
|
1060 |
} |
|
1061 |
if (needExpand) |
|
1062 |
expandedSystemId = expandSystemId(literalSystemId, baseSystemId,false); |
|
1063 |
||
1064 |
// give the entity resolver a chance |
|
1065 |
XMLInputSource xmlInputSource = null; |
|
1066 |
||
1067 |
if (fEntityResolver != null) { |
|
1068 |
resourceIdentifier.setBaseSystemId(baseSystemId); |
|
1069 |
resourceIdentifier.setExpandedSystemId(expandedSystemId); |
|
1070 |
xmlInputSource = fEntityResolver.resolveEntity(resourceIdentifier); |
|
1071 |
} |
|
1072 |
||
1073 |
// do default resolution |
|
1074 |
// REVISIT: what's the correct behavior if the user provided an entity |
|
1075 |
// resolver (fEntityResolver != null), but resolveEntity doesn't return |
|
1076 |
// an input source (xmlInputSource == null)? |
|
1077 |
// do we do default resolution, or do we just return null? -SG |
|
1078 |
if (xmlInputSource == null) { |
|
1079 |
// REVISIT: when systemId is null, I think we should return null. |
|
1080 |
// is this the right solution? -SG |
|
1081 |
//if (systemId != null) |
|
1082 |
xmlInputSource = new XMLInputSource(publicId, literalSystemId, baseSystemId); |
|
1083 |
} |
|
1084 |
||
1085 |
if (DEBUG_RESOLVER) { |
|
1086 |
System.err.println("XMLEntityManager.resolveEntity(" + publicId + ")"); |
|
1087 |
System.err.println(" = " + xmlInputSource); |
|
1088 |
} |
|
1089 |
||
1090 |
return xmlInputSource; |
|
1091 |
||
1092 |
} // resolveEntity(XMLResourceIdentifier):XMLInputSource |
|
1093 |
||
1094 |
/** |
|
1095 |
* Starts a named entity. |
|
1096 |
* |
|
1097 |
* @param entityName The name of the entity to start. |
|
1098 |
* @param literal True if this entity is started within a literal |
|
1099 |
* value. |
|
1100 |
* |
|
1101 |
* @throws IOException Thrown on i/o error. |
|
1102 |
* @throws XNIException Thrown by entity handler to signal an error. |
|
1103 |
*/ |
|
1104 |
public void startEntity(String entityName, boolean literal) |
|
1105 |
throws IOException, XNIException { |
|
1106 |
||
1107 |
// was entity declared? |
|
12458 | 1108 |
Entity entity = (Entity)fEntityStorage.getEntity(entityName); |
12005 | 1109 |
if (entity == null) { |
1110 |
if (fEntityHandler != null) { |
|
1111 |
String encoding = null; |
|
1112 |
fResourceIdentifier.clear(); |
|
1113 |
fEntityAugs.removeAllItems(); |
|
1114 |
fEntityAugs.putItem(Constants.ENTITY_SKIPPED, Boolean.TRUE); |
|
1115 |
fEntityHandler.startEntity(entityName, fResourceIdentifier, encoding, fEntityAugs); |
|
1116 |
fEntityAugs.removeAllItems(); |
|
1117 |
fEntityAugs.putItem(Constants.ENTITY_SKIPPED, Boolean.TRUE); |
|
1118 |
fEntityHandler.endEntity(entityName, fEntityAugs); |
|
1119 |
} |
|
1120 |
return; |
|
1121 |
} |
|
1122 |
||
1123 |
// should we skip external entities? |
|
1124 |
boolean external = entity.isExternal(); |
|
17534 | 1125 |
Entity.ExternalEntity externalEntity = null; |
1126 |
String extLitSysId = null, extBaseSysId = null, expandedSystemId = null; |
|
12005 | 1127 |
if (external) { |
17534 | 1128 |
externalEntity = (Entity.ExternalEntity)entity; |
1129 |
extLitSysId = (externalEntity.entityLocation != null ? externalEntity.entityLocation.getLiteralSystemId() : null); |
|
1130 |
extBaseSysId = (externalEntity.entityLocation != null ? externalEntity.entityLocation.getBaseSystemId() : null); |
|
1131 |
expandedSystemId = expandSystemId(extLitSysId, extBaseSysId); |
|
12005 | 1132 |
boolean unparsed = entity.isUnparsed(); |
1133 |
boolean parameter = entityName.startsWith("%"); |
|
1134 |
boolean general = !parameter; |
|
1135 |
if (unparsed || (general && !fExternalGeneralEntities) || |
|
1136 |
(parameter && !fExternalParameterEntities)) { |
|
1137 |
||
1138 |
if (fEntityHandler != null) { |
|
1139 |
fResourceIdentifier.clear(); |
|
1140 |
final String encoding = null; |
|
1141 |
fResourceIdentifier.setValues( |
|
1142 |
(externalEntity.entityLocation != null ? externalEntity.entityLocation.getPublicId() : null), |
|
1143 |
extLitSysId, extBaseSysId, expandedSystemId); |
|
1144 |
fEntityAugs.removeAllItems(); |
|
1145 |
fEntityAugs.putItem(Constants.ENTITY_SKIPPED, Boolean.TRUE); |
|
1146 |
fEntityHandler.startEntity(entityName, fResourceIdentifier, encoding, fEntityAugs); |
|
1147 |
fEntityAugs.removeAllItems(); |
|
1148 |
fEntityAugs.putItem(Constants.ENTITY_SKIPPED, Boolean.TRUE); |
|
1149 |
fEntityHandler.endEntity(entityName, fEntityAugs); |
|
1150 |
} |
|
1151 |
return; |
|
1152 |
} |
|
1153 |
} |
|
1154 |
||
1155 |
// is entity recursive? |
|
1156 |
int size = fEntityStack.size(); |
|
1157 |
for (int i = size; i >= 0; i--) { |
|
1158 |
Entity activeEntity = i == size |
|
1159 |
? fCurrentEntity |
|
1160 |
: (Entity)fEntityStack.elementAt(i); |
|
1161 |
if (activeEntity.name == entityName) { |
|
1162 |
String path = entityName; |
|
1163 |
for (int j = i + 1; j < size; j++) { |
|
1164 |
activeEntity = (Entity)fEntityStack.elementAt(j); |
|
1165 |
path = path + " -> " + activeEntity.name; |
|
1166 |
} |
|
1167 |
path = path + " -> " + fCurrentEntity.name; |
|
1168 |
path = path + " -> " + entityName; |
|
1169 |
fErrorReporter.reportError(this.getEntityScanner(),XMLMessageFormatter.XML_DOMAIN, |
|
1170 |
"RecursiveReference", |
|
1171 |
new Object[] { entityName, path }, |
|
1172 |
XMLErrorReporter.SEVERITY_FATAL_ERROR); |
|
1173 |
||
1174 |
if (fEntityHandler != null) { |
|
1175 |
fResourceIdentifier.clear(); |
|
1176 |
final String encoding = null; |
|
1177 |
if (external) { |
|
1178 |
fResourceIdentifier.setValues( |
|
1179 |
(externalEntity.entityLocation != null ? externalEntity.entityLocation.getPublicId() : null), |
|
1180 |
extLitSysId, extBaseSysId, expandedSystemId); |
|
1181 |
} |
|
1182 |
fEntityAugs.removeAllItems(); |
|
1183 |
fEntityAugs.putItem(Constants.ENTITY_SKIPPED, Boolean.TRUE); |
|
1184 |
fEntityHandler.startEntity(entityName, fResourceIdentifier, encoding, fEntityAugs); |
|
1185 |
fEntityAugs.removeAllItems(); |
|
1186 |
fEntityAugs.putItem(Constants.ENTITY_SKIPPED, Boolean.TRUE); |
|
1187 |
fEntityHandler.endEntity(entityName, fEntityAugs); |
|
1188 |
} |
|
1189 |
||
1190 |
return; |
|
1191 |
} |
|
1192 |
} |
|
1193 |
||
1194 |
// resolve external entity |
|
1195 |
StaxXMLInputSource staxInputSource = null; |
|
1196 |
XMLInputSource xmlInputSource = null ; |
|
1197 |
||
1198 |
if (external) { |
|
1199 |
staxInputSource = resolveEntityAsPerStax(externalEntity.entityLocation); |
|
1200 |
/** xxx: Waiting from the EG |
|
1201 |
* //simply return if there was entity resolver registered and application |
|
1202 |
* //returns either XMLStreamReader or XMLEventReader. |
|
1203 |
* if(staxInputSource.hasXMLStreamOrXMLEventReader()) return ; |
|
1204 |
*/ |
|
1205 |
xmlInputSource = staxInputSource.getXMLInputSource() ; |
|
17534 | 1206 |
if (!fISCreatedByResolver) { |
1207 |
//let the not-LoadExternalDTD or not-SupportDTD process to handle the situation |
|
1208 |
if (fLoadExternalDTD) { |
|
1209 |
String accessError = SecuritySupport.checkAccess(expandedSystemId, fAccessExternalDTD, Constants.ACCESS_EXTERNAL_ALL); |
|
1210 |
if (accessError != null) { |
|
1211 |
fErrorReporter.reportError(this.getEntityScanner(),XMLMessageFormatter.XML_DOMAIN, |
|
1212 |
"AccessExternalEntity", |
|
1213 |
new Object[] { SecuritySupport.sanitizePath(expandedSystemId), accessError }, |
|
1214 |
XMLErrorReporter.SEVERITY_FATAL_ERROR); |
|
1215 |
} |
|
1216 |
} |
|
1217 |
} |
|
12005 | 1218 |
} |
1219 |
// wrap internal entity |
|
1220 |
else { |
|
1221 |
Entity.InternalEntity internalEntity = (Entity.InternalEntity)entity; |
|
1222 |
Reader reader = new StringReader(internalEntity.text); |
|
1223 |
xmlInputSource = new XMLInputSource(null, null, null, reader, null); |
|
1224 |
} |
|
1225 |
||
1226 |
// start the entity |
|
1227 |
startEntity(entityName, xmlInputSource, literal, external); |
|
1228 |
||
1229 |
} // startEntity(String,boolean) |
|
1230 |
||
1231 |
/** |
|
1232 |
* Starts the document entity. The document entity has the "[xml]" |
|
1233 |
* pseudo-name. |
|
1234 |
* |
|
1235 |
* @param xmlInputSource The input source of the document entity. |
|
1236 |
* |
|
1237 |
* @throws IOException Thrown on i/o error. |
|
1238 |
* @throws XNIException Thrown by entity handler to signal an error. |
|
1239 |
*/ |
|
1240 |
public void startDocumentEntity(XMLInputSource xmlInputSource) |
|
1241 |
throws IOException, XNIException { |
|
1242 |
startEntity(XMLEntity, xmlInputSource, false, true); |
|
1243 |
} // startDocumentEntity(XMLInputSource) |
|
1244 |
||
1245 |
//xxx these methods are not required. |
|
1246 |
/** |
|
1247 |
* Starts the DTD entity. The DTD entity has the "[dtd]" |
|
1248 |
* pseudo-name. |
|
1249 |
* |
|
1250 |
* @param xmlInputSource The input source of the DTD entity. |
|
1251 |
* |
|
1252 |
* @throws IOException Thrown on i/o error. |
|
1253 |
* @throws XNIException Thrown by entity handler to signal an error. |
|
1254 |
*/ |
|
1255 |
public void startDTDEntity(XMLInputSource xmlInputSource) |
|
1256 |
throws IOException, XNIException { |
|
1257 |
startEntity(DTDEntity, xmlInputSource, false, true); |
|
1258 |
} // startDTDEntity(XMLInputSource) |
|
1259 |
||
1260 |
// indicate start of external subset so that |
|
1261 |
// location of entity decls can be tracked |
|
1262 |
public void startExternalSubset() { |
|
1263 |
fInExternalSubset = true; |
|
1264 |
} |
|
1265 |
||
1266 |
public void endExternalSubset() { |
|
1267 |
fInExternalSubset = false; |
|
1268 |
} |
|
1269 |
||
1270 |
/** |
|
1271 |
* Starts an entity. |
|
1272 |
* <p> |
|
1273 |
* This method can be used to insert an application defined XML |
|
1274 |
* entity stream into the parsing stream. |
|
1275 |
* |
|
1276 |
* @param name The name of the entity. |
|
1277 |
* @param xmlInputSource The input source of the entity. |
|
1278 |
* @param literal True if this entity is started within a |
|
1279 |
* literal value. |
|
1280 |
* @param isExternal whether this entity should be treated as an internal or external entity. |
|
1281 |
* |
|
1282 |
* @throws IOException Thrown on i/o error. |
|
1283 |
* @throws XNIException Thrown by entity handler to signal an error. |
|
1284 |
*/ |
|
1285 |
public void startEntity(String name, |
|
1286 |
XMLInputSource xmlInputSource, |
|
1287 |
boolean literal, boolean isExternal) |
|
1288 |
throws IOException, XNIException { |
|
1289 |
||
1290 |
String encoding = setupCurrentEntity(name, xmlInputSource, literal, isExternal); |
|
1291 |
||
1292 |
//when entity expansion limit is set by the Application, we need to |
|
1293 |
//check for the entity expansion limit set by the parser, if number of entity |
|
1294 |
//expansions exceeds the entity expansion limit, parser will throw fatal error. |
|
1295 |
// Note that this represents the nesting level of open entities. |
|
1296 |
fEntityExpansionCount++; |
|
1297 |
if( fSecurityManager != null && fEntityExpansionCount > fEntityExpansionLimit ){ |
|
1298 |
fErrorReporter.reportError(XMLMessageFormatter.XML_DOMAIN, |
|
1299 |
"EntityExpansionLimitExceeded", |
|
1300 |
new Object[]{new Integer(fEntityExpansionLimit) }, |
|
1301 |
XMLErrorReporter.SEVERITY_FATAL_ERROR ); |
|
1302 |
// is there anything better to do than reset the counter? |
|
1303 |
// at least one can envision debugging applications where this might |
|
1304 |
// be useful... |
|
1305 |
fEntityExpansionCount = 0; |
|
1306 |
} |
|
1307 |
||
1308 |
// call handler |
|
1309 |
if (fEntityHandler != null) { |
|
1310 |
fEntityHandler.startEntity(name, fResourceIdentifier, encoding, null); |
|
1311 |
} |
|
1312 |
||
1313 |
} // startEntity(String,XMLInputSource) |
|
1314 |
||
1315 |
/** |
|
1316 |
* Return the current entity being scanned. Current entity is SET using startEntity function. |
|
1317 |
* @return Entity.ScannedEntity |
|
1318 |
*/ |
|
1319 |
||
1320 |
public Entity.ScannedEntity getCurrentEntity(){ |
|
1321 |
return fCurrentEntity ; |
|
1322 |
} |
|
1323 |
||
1324 |
/** |
|
1325 |
* Return the top level entity handled by this manager, or null |
|
1326 |
* if no entity was added. |
|
1327 |
*/ |
|
1328 |
public Entity.ScannedEntity getTopLevelEntity() { |
|
1329 |
return (Entity.ScannedEntity) |
|
1330 |
(fEntityStack.empty() ? null : fEntityStack.elementAt(0)); |
|
1331 |
} |
|
1332 |
||
1333 |
||
1334 |
/** |
|
1335 |
* Close all opened InputStreams and Readers opened by this parser. |
|
1336 |
*/ |
|
1337 |
public void closeReaders() { |
|
12458 | 1338 |
/** this call actually does nothing, readers are closed in the endEntity method |
1339 |
* through the current entity. |
|
1340 |
* The change seems to have happened during the jdk6 development with the |
|
1341 |
* addition of StAX |
|
1342 |
**/ |
|
12005 | 1343 |
} |
1344 |
||
1345 |
public void endEntity() throws IOException, XNIException { |
|
1346 |
||
1347 |
// call handler |
|
1348 |
if (DEBUG_BUFFER) { |
|
1349 |
System.out.print("(endEntity: "); |
|
1350 |
print(); |
|
1351 |
System.out.println(); |
|
1352 |
} |
|
1353 |
//pop the entity from the stack |
|
1354 |
Entity.ScannedEntity entity = fEntityStack.size() > 0 ? (Entity.ScannedEntity)fEntityStack.pop() : null ; |
|
1355 |
||
12458 | 1356 |
/** need to close the reader first since the program can end |
1357 |
* prematurely (e.g. fEntityHandler.endEntity may throw exception) |
|
1358 |
* leaving the reader open |
|
1359 |
*/ |
|
1360 |
//close the reader |
|
1361 |
if(fCurrentEntity != null){ |
|
1362 |
//close the reader |
|
1363 |
try{ |
|
1364 |
fCurrentEntity.close(); |
|
1365 |
}catch(IOException ex){ |
|
1366 |
throw new XNIException(ex); |
|
1367 |
} |
|
1368 |
} |
|
1369 |
||
12005 | 1370 |
if (fEntityHandler != null) { |
1371 |
//so this is the last opened entity, signal it to current fEntityHandler using Augmentation |
|
1372 |
if(entity == null){ |
|
1373 |
fEntityAugs.removeAllItems(); |
|
1374 |
fEntityAugs.putItem(Constants.LAST_ENTITY, Boolean.TRUE); |
|
1375 |
fEntityHandler.endEntity(fCurrentEntity.name, fEntityAugs); |
|
1376 |
fEntityAugs.removeAllItems(); |
|
1377 |
}else{ |
|
1378 |
fEntityHandler.endEntity(fCurrentEntity.name, null); |
|
1379 |
} |
|
1380 |
} |
|
1381 |
//check if it is a document entity |
|
1382 |
boolean documentEntity = fCurrentEntity.name == XMLEntity; |
|
1383 |
||
1384 |
//set popped entity as current entity |
|
1385 |
fCurrentEntity = entity; |
|
1386 |
fEntityScanner.setCurrentEntity(fCurrentEntity); |
|
1387 |
||
1388 |
//check if there are any entity left in the stack -- if there are |
|
1389 |
//no entries EOF has been reached. |
|
1390 |
// throw exception when it is the last entity but it is not a document entity |
|
1391 |
||
1392 |
if(fCurrentEntity == null & !documentEntity){ |
|
1393 |
throw new EOFException() ; |
|
1394 |
} |
|
1395 |
||
1396 |
if (DEBUG_BUFFER) { |
|
1397 |
System.out.print(")endEntity: "); |
|
1398 |
print(); |
|
1399 |
System.out.println(); |
|
1400 |
} |
|
1401 |
||
1402 |
} // endEntity() |
|
1403 |
||
1404 |
||
1405 |
// |
|
1406 |
// XMLComponent methods |
|
1407 |
// |
|
1408 |
public void reset(PropertyManager propertyManager){ |
|
1409 |
//reset fEntityStorage |
|
1410 |
fEntityStorage.reset(propertyManager); |
|
1411 |
//reset XMLEntityReaderImpl |
|
1412 |
fEntityScanner.reset(propertyManager); |
|
1413 |
// xerces properties |
|
1414 |
fSymbolTable = (SymbolTable)propertyManager.getProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.SYMBOL_TABLE_PROPERTY); |
|
1415 |
fErrorReporter = (XMLErrorReporter)propertyManager.getProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY); |
|
1416 |
try { |
|
1417 |
fStaxEntityResolver = (StaxEntityResolverWrapper)propertyManager.getProperty(STAX_ENTITY_RESOLVER); |
|
1418 |
} catch (XMLConfigurationException e) { |
|
1419 |
fStaxEntityResolver = null; |
|
1420 |
} |
|
1421 |
||
17534 | 1422 |
// Zephyr feature ignore-external-dtd is the opposite of Xerces' load-external-dtd |
1423 |
fLoadExternalDTD = !((Boolean)propertyManager.getProperty(Constants.ZEPHYR_PROPERTY_PREFIX + Constants.IGNORE_EXTERNAL_DTD)).booleanValue(); |
|
1424 |
||
1425 |
// JAXP 1.5 feature |
|
18890
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17645
diff
changeset
|
1426 |
XMLSecurityPropertyManager spm = (XMLSecurityPropertyManager) propertyManager.getProperty(XML_SECURITY_PROPERTY_MANAGER); |
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17645
diff
changeset
|
1427 |
fAccessExternalDTD = spm.getValue(XMLSecurityPropertyManager.Property.ACCESS_EXTERNAL_DTD); |
17534 | 1428 |
|
12005 | 1429 |
// initialize state |
1430 |
//fStandalone = false; |
|
1431 |
fEntities.clear(); |
|
1432 |
fEntityStack.removeAllElements(); |
|
1433 |
fCurrentEntity = null; |
|
1434 |
fValidation = false; |
|
1435 |
fExternalGeneralEntities = true; |
|
1436 |
fExternalParameterEntities = true; |
|
1437 |
fAllowJavaEncodings = true ; |
|
1438 |
} |
|
1439 |
||
1440 |
/** |
|
1441 |
* Resets the component. The component can query the component manager |
|
1442 |
* about any features and properties that affect the operation of the |
|
1443 |
* component. |
|
1444 |
* |
|
1445 |
* @param componentManager The component manager. |
|
1446 |
* |
|
1447 |
* @throws SAXException Thrown by component on initialization error. |
|
1448 |
* For example, if a feature or property is |
|
1449 |
* required for the operation of the component, the |
|
1450 |
* component manager may throw a |
|
1451 |
* SAXNotRecognizedException or a |
|
1452 |
* SAXNotSupportedException. |
|
1453 |
*/ |
|
1454 |
public void reset(XMLComponentManager componentManager) |
|
1455 |
throws XMLConfigurationException { |
|
1456 |
||
1457 |
boolean parser_settings = componentManager.getFeature(PARSER_SETTINGS, true); |
|
1458 |
||
1459 |
if (!parser_settings) { |
|
1460 |
// parser settings have not been changed |
|
1461 |
reset(); |
|
1462 |
if(fEntityScanner != null){ |
|
1463 |
fEntityScanner.reset(componentManager); |
|
1464 |
} |
|
1465 |
if(fEntityStorage != null){ |
|
1466 |
fEntityStorage.reset(componentManager); |
|
1467 |
} |
|
1468 |
return; |
|
1469 |
} |
|
1470 |
||
1471 |
// sax features |
|
1472 |
fValidation = componentManager.getFeature(VALIDATION, false); |
|
1473 |
fExternalGeneralEntities = componentManager.getFeature(EXTERNAL_GENERAL_ENTITIES, true); |
|
1474 |
fExternalParameterEntities = componentManager.getFeature(EXTERNAL_PARAMETER_ENTITIES, true); |
|
1475 |
||
1476 |
// xerces features |
|
1477 |
fAllowJavaEncodings = componentManager.getFeature(ALLOW_JAVA_ENCODINGS, false); |
|
1478 |
fWarnDuplicateEntityDef = componentManager.getFeature(WARN_ON_DUPLICATE_ENTITYDEF, false); |
|
1479 |
fStrictURI = componentManager.getFeature(STANDARD_URI_CONFORMANT, false); |
|
17534 | 1480 |
fLoadExternalDTD = componentManager.getFeature(LOAD_EXTERNAL_DTD, true); |
12005 | 1481 |
|
1482 |
// xerces properties |
|
1483 |
fSymbolTable = (SymbolTable)componentManager.getProperty(SYMBOL_TABLE); |
|
1484 |
fErrorReporter = (XMLErrorReporter)componentManager.getProperty(ERROR_REPORTER); |
|
1485 |
fEntityResolver = (XMLEntityResolver)componentManager.getProperty(ENTITY_RESOLVER, null); |
|
1486 |
fStaxEntityResolver = (StaxEntityResolverWrapper)componentManager.getProperty(STAX_ENTITY_RESOLVER, null); |
|
1487 |
fValidationManager = (ValidationManager)componentManager.getProperty(VALIDATION_MANAGER, null); |
|
1488 |
fSecurityManager = (SecurityManager)componentManager.getProperty(SECURITY_MANAGER, null); |
|
1489 |
||
17534 | 1490 |
// JAXP 1.5 feature |
18890
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17645
diff
changeset
|
1491 |
XMLSecurityPropertyManager spm = (XMLSecurityPropertyManager) componentManager.getProperty(XML_SECURITY_PROPERTY_MANAGER, null); |
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17645
diff
changeset
|
1492 |
if (spm == null) { |
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17645
diff
changeset
|
1493 |
spm = new XMLSecurityPropertyManager(); |
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17645
diff
changeset
|
1494 |
} |
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17645
diff
changeset
|
1495 |
fAccessExternalDTD = spm.getValue(XMLSecurityPropertyManager.Property.ACCESS_EXTERNAL_DTD); |
17534 | 1496 |
|
12005 | 1497 |
//reset general state |
1498 |
reset(); |
|
1499 |
||
1500 |
fEntityScanner.reset(componentManager); |
|
1501 |
fEntityStorage.reset(componentManager); |
|
1502 |
||
1503 |
} // reset(XMLComponentManager) |
|
1504 |
||
1505 |
// reset general state. Should not be called other than by |
|
1506 |
// a class acting as a component manager but not |
|
1507 |
// implementing that interface for whatever reason. |
|
1508 |
public void reset() { |
|
1509 |
fEntityExpansionLimit = (fSecurityManager != null)?fSecurityManager.getEntityExpansionLimit():0; |
|
1510 |
||
1511 |
// initialize state |
|
1512 |
fStandalone = false; |
|
1513 |
fEntities.clear(); |
|
1514 |
fEntityStack.removeAllElements(); |
|
1515 |
fEntityExpansionCount = 0; |
|
1516 |
||
1517 |
fCurrentEntity = null; |
|
1518 |
// reset scanner |
|
1519 |
if(fXML10EntityScanner != null){ |
|
1520 |
fXML10EntityScanner.reset(fSymbolTable, this, fErrorReporter); |
|
1521 |
} |
|
1522 |
if(fXML11EntityScanner != null) { |
|
1523 |
fXML11EntityScanner.reset(fSymbolTable, this, fErrorReporter); |
|
1524 |
} |
|
1525 |
||
1526 |
// DEBUG |
|
1527 |
if (DEBUG_ENTITIES) { |
|
1528 |
addInternalEntity("text", "Hello, World."); |
|
1529 |
addInternalEntity("empty-element", "<foo/>"); |
|
1530 |
addInternalEntity("balanced-element", "<foo></foo>"); |
|
1531 |
addInternalEntity("balanced-element-with-text", "<foo>Hello, World</foo>"); |
|
1532 |
addInternalEntity("balanced-element-with-entity", "<foo>&text;</foo>"); |
|
1533 |
addInternalEntity("unbalanced-entity", "<foo>"); |
|
1534 |
addInternalEntity("recursive-entity", "<foo>&recursive-entity2;</foo>"); |
|
1535 |
addInternalEntity("recursive-entity2", "<bar>&recursive-entity3;</bar>"); |
|
1536 |
addInternalEntity("recursive-entity3", "<baz>&recursive-entity;</baz>"); |
|
1537 |
try { |
|
1538 |
addExternalEntity("external-text", null, "external-text.ent", "test/external-text.xml"); |
|
1539 |
addExternalEntity("external-balanced-element", null, "external-balanced-element.ent", "test/external-balanced-element.xml"); |
|
1540 |
addExternalEntity("one", null, "ent/one.ent", "test/external-entity.xml"); |
|
1541 |
addExternalEntity("two", null, "ent/two.ent", "test/ent/one.xml"); |
|
1542 |
} |
|
1543 |
catch (IOException ex) { |
|
1544 |
// should never happen |
|
1545 |
} |
|
1546 |
} |
|
1547 |
||
1548 |
fEntityHandler = null; |
|
1549 |
||
1550 |
// reset scanner |
|
1551 |
//if(fEntityScanner!=null) |
|
1552 |
// fEntityScanner.reset(fSymbolTable, this,fErrorReporter); |
|
1553 |
||
1554 |
} |
|
1555 |
/** |
|
1556 |
* Returns a list of feature identifiers that are recognized by |
|
1557 |
* this component. This method may return null if no features |
|
1558 |
* are recognized by this component. |
|
1559 |
*/ |
|
1560 |
public String[] getRecognizedFeatures() { |
|
1561 |
return (String[])(RECOGNIZED_FEATURES.clone()); |
|
1562 |
} // getRecognizedFeatures():String[] |
|
1563 |
||
1564 |
/** |
|
1565 |
* Sets the state of a feature. This method is called by the component |
|
1566 |
* manager any time after reset when a feature changes state. |
|
1567 |
* <p> |
|
1568 |
* <strong>Note:</strong> Components should silently ignore features |
|
1569 |
* that do not affect the operation of the component. |
|
1570 |
* |
|
1571 |
* @param featureId The feature identifier. |
|
1572 |
* @param state The state of the feature. |
|
1573 |
* |
|
1574 |
* @throws SAXNotRecognizedException The component should not throw |
|
1575 |
* this exception. |
|
1576 |
* @throws SAXNotSupportedException The component should not throw |
|
1577 |
* this exception. |
|
1578 |
*/ |
|
1579 |
public void setFeature(String featureId, boolean state) |
|
1580 |
throws XMLConfigurationException { |
|
1581 |
||
1582 |
// xerces features |
|
1583 |
if (featureId.startsWith(Constants.XERCES_FEATURE_PREFIX)) { |
|
1584 |
final int suffixLength = featureId.length() - Constants.XERCES_FEATURE_PREFIX.length(); |
|
1585 |
if (suffixLength == Constants.ALLOW_JAVA_ENCODINGS_FEATURE.length() && |
|
1586 |
featureId.endsWith(Constants.ALLOW_JAVA_ENCODINGS_FEATURE)) { |
|
1587 |
fAllowJavaEncodings = state; |
|
1588 |
} |
|
17534 | 1589 |
if (suffixLength == Constants.LOAD_EXTERNAL_DTD_FEATURE.length() && |
1590 |
featureId.endsWith(Constants.LOAD_EXTERNAL_DTD_FEATURE)) { |
|
1591 |
fLoadExternalDTD = state; |
|
1592 |
return; |
|
1593 |
} |
|
12005 | 1594 |
} |
1595 |
||
1596 |
} // setFeature(String,boolean) |
|
1597 |
||
1598 |
/** |
|
1599 |
* Sets the value of a property. This method is called by the component |
|
1600 |
* manager any time after reset when a property changes value. |
|
1601 |
* <p> |
|
1602 |
* <strong>Note:</strong> Components should silently ignore properties |
|
1603 |
* that do not affect the operation of the component. |
|
1604 |
* |
|
1605 |
* @param propertyId The property identifier. |
|
1606 |
* @param value The value of the property. |
|
1607 |
* |
|
1608 |
* @throws SAXNotRecognizedException The component should not throw |
|
1609 |
* this exception. |
|
1610 |
* @throws SAXNotSupportedException The component should not throw |
|
1611 |
* this exception. |
|
1612 |
*/ |
|
1613 |
public void setProperty(String propertyId, Object value){ |
|
1614 |
// Xerces properties |
|
1615 |
if (propertyId.startsWith(Constants.XERCES_PROPERTY_PREFIX)) { |
|
1616 |
final int suffixLength = propertyId.length() - Constants.XERCES_PROPERTY_PREFIX.length(); |
|
1617 |
||
1618 |
if (suffixLength == Constants.SYMBOL_TABLE_PROPERTY.length() && |
|
1619 |
propertyId.endsWith(Constants.SYMBOL_TABLE_PROPERTY)) { |
|
1620 |
fSymbolTable = (SymbolTable)value; |
|
1621 |
return; |
|
1622 |
} |
|
1623 |
if (suffixLength == Constants.ERROR_REPORTER_PROPERTY.length() && |
|
1624 |
propertyId.endsWith(Constants.ERROR_REPORTER_PROPERTY)) { |
|
1625 |
fErrorReporter = (XMLErrorReporter)value; |
|
1626 |
return; |
|
1627 |
} |
|
1628 |
if (suffixLength == Constants.ENTITY_RESOLVER_PROPERTY.length() && |
|
1629 |
propertyId.endsWith(Constants.ENTITY_RESOLVER_PROPERTY)) { |
|
1630 |
fEntityResolver = (XMLEntityResolver)value; |
|
1631 |
return; |
|
1632 |
} |
|
1633 |
if (suffixLength == Constants.BUFFER_SIZE_PROPERTY.length() && |
|
1634 |
propertyId.endsWith(Constants.BUFFER_SIZE_PROPERTY)) { |
|
1635 |
Integer bufferSize = (Integer)value; |
|
1636 |
if (bufferSize != null && |
|
1637 |
bufferSize.intValue() > DEFAULT_XMLDECL_BUFFER_SIZE) { |
|
1638 |
fBufferSize = bufferSize.intValue(); |
|
1639 |
fEntityScanner.setBufferSize(fBufferSize); |
|
1640 |
fBufferPool.setExternalBufferSize(fBufferSize); |
|
1641 |
} |
|
1642 |
} |
|
1643 |
if (suffixLength == Constants.SECURITY_MANAGER_PROPERTY.length() && |
|
1644 |
propertyId.endsWith(Constants.SECURITY_MANAGER_PROPERTY)) { |
|
1645 |
fSecurityManager = (SecurityManager)value; |
|
1646 |
fEntityExpansionLimit = (fSecurityManager != null)?fSecurityManager.getEntityExpansionLimit():0; |
|
1647 |
} |
|
1648 |
} |
|
1649 |
||
17534 | 1650 |
//JAXP 1.5 properties |
18890
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17645
diff
changeset
|
1651 |
if (propertyId.equals(XML_SECURITY_PROPERTY_MANAGER)) |
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17645
diff
changeset
|
1652 |
{ |
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17645
diff
changeset
|
1653 |
XMLSecurityPropertyManager spm = (XMLSecurityPropertyManager)value; |
25bdeca3173b
8016648: FEATURE_SECURE_PROCESSING set to true or false causes SAXParseException to be thrown
joehw
parents:
17645
diff
changeset
|
1654 |
fAccessExternalDTD = spm.getValue(XMLSecurityPropertyManager.Property.ACCESS_EXTERNAL_DTD); |
17534 | 1655 |
} |
12005 | 1656 |
} |
17534 | 1657 |
|
12005 | 1658 |
/** |
1659 |
* Returns a list of property identifiers that are recognized by |
|
1660 |
* this component. This method may return null if no properties |
|
1661 |
* are recognized by this component. |
|
1662 |
*/ |
|
1663 |
public String[] getRecognizedProperties() { |
|
1664 |
return (String[])(RECOGNIZED_PROPERTIES.clone()); |
|
1665 |
} // getRecognizedProperties():String[] |
|
1666 |
/** |
|
1667 |
* Returns the default state for a feature, or null if this |
|
1668 |
* component does not want to report a default value for this |
|
1669 |
* feature. |
|
1670 |
* |
|
1671 |
* @param featureId The feature identifier. |
|
1672 |
* |
|
1673 |
* @since Xerces 2.2.0 |
|
1674 |
*/ |
|
1675 |
public Boolean getFeatureDefault(String featureId) { |
|
1676 |
for (int i = 0; i < RECOGNIZED_FEATURES.length; i++) { |
|
1677 |
if (RECOGNIZED_FEATURES[i].equals(featureId)) { |
|
1678 |
return FEATURE_DEFAULTS[i]; |
|
1679 |
} |
|
1680 |
} |
|
1681 |
return null; |
|
1682 |
} // getFeatureDefault(String):Boolean |
|
1683 |
||
1684 |
/** |
|
1685 |
* Returns the default state for a property, or null if this |
|
1686 |
* component does not want to report a default value for this |
|
1687 |
* property. |
|
1688 |
* |
|
1689 |
* @param propertyId The property identifier. |
|
1690 |
* |
|
1691 |
* @since Xerces 2.2.0 |
|
1692 |
*/ |
|
1693 |
public Object getPropertyDefault(String propertyId) { |
|
1694 |
for (int i = 0; i < RECOGNIZED_PROPERTIES.length; i++) { |
|
1695 |
if (RECOGNIZED_PROPERTIES[i].equals(propertyId)) { |
|
1696 |
return PROPERTY_DEFAULTS[i]; |
|
1697 |
} |
|
1698 |
} |
|
1699 |
return null; |
|
1700 |
} // getPropertyDefault(String):Object |
|
1701 |
||
1702 |
// |
|
1703 |
// Public static methods |
|
1704 |
// |
|
1705 |
||
1706 |
/** |
|
1707 |
* Expands a system id and returns the system id as a URI, if |
|
1708 |
* it can be expanded. A return value of null means that the |
|
1709 |
* identifier is already expanded. An exception thrown |
|
1710 |
* indicates a failure to expand the id. |
|
1711 |
* |
|
1712 |
* @param systemId The systemId to be expanded. |
|
1713 |
* |
|
1714 |
* @return Returns the URI string representing the expanded system |
|
1715 |
* identifier. A null value indicates that the given |
|
1716 |
* system identifier is already expanded. |
|
1717 |
* |
|
1718 |
*/ |
|
1719 |
public static String expandSystemId(String systemId) { |
|
1720 |
return expandSystemId(systemId, null); |
|
1721 |
} // expandSystemId(String):String |
|
1722 |
||
1723 |
// |
|
1724 |
// Public static methods |
|
1725 |
// |
|
1726 |
||
1727 |
// current value of the "user.dir" property |
|
1728 |
private static String gUserDir; |
|
1729 |
// cached URI object for the current value of the escaped "user.dir" property stored as a URI |
|
1730 |
private static URI gUserDirURI; |
|
1731 |
// which ASCII characters need to be escaped |
|
1732 |
private static boolean gNeedEscaping[] = new boolean[128]; |
|
1733 |
// the first hex character if a character needs to be escaped |
|
1734 |
private static char gAfterEscaping1[] = new char[128]; |
|
1735 |
// the second hex character if a character needs to be escaped |
|
1736 |
private static char gAfterEscaping2[] = new char[128]; |
|
1737 |
private static char[] gHexChs = {'0', '1', '2', '3', '4', '5', '6', '7', |
|
1738 |
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; |
|
1739 |
// initialize the above 3 arrays |
|
1740 |
static { |
|
1741 |
for (int i = 0; i <= 0x1f; i++) { |
|
1742 |
gNeedEscaping[i] = true; |
|
1743 |
gAfterEscaping1[i] = gHexChs[i >> 4]; |
|
1744 |
gAfterEscaping2[i] = gHexChs[i & 0xf]; |
|
1745 |
} |
|
1746 |
gNeedEscaping[0x7f] = true; |
|
1747 |
gAfterEscaping1[0x7f] = '7'; |
|
1748 |
gAfterEscaping2[0x7f] = 'F'; |
|
1749 |
char[] escChs = {' ', '<', '>', '#', '%', '"', '{', '}', |
|
1750 |
'|', '\\', '^', '~', '[', ']', '`'}; |
|
1751 |
int len = escChs.length; |
|
1752 |
char ch; |
|
1753 |
for (int i = 0; i < len; i++) { |
|
1754 |
ch = escChs[i]; |
|
1755 |
gNeedEscaping[ch] = true; |
|
1756 |
gAfterEscaping1[ch] = gHexChs[ch >> 4]; |
|
1757 |
gAfterEscaping2[ch] = gHexChs[ch & 0xf]; |
|
1758 |
} |
|
1759 |
} |
|
1760 |
||
1761 |
// To escape the "user.dir" system property, by using %HH to represent |
|
1762 |
// special ASCII characters: 0x00~0x1F, 0x7F, ' ', '<', '>', '#', '%' |
|
1763 |
// and '"'. It's a static method, so needs to be synchronized. |
|
1764 |
// this method looks heavy, but since the system property isn't expected |
|
1765 |
// to change often, so in most cases, we only need to return the URI |
|
1766 |
// that was escaped before. |
|
1767 |
// According to the URI spec, non-ASCII characters (whose value >= 128) |
|
1768 |
// need to be escaped too. |
|
1769 |
// REVISIT: don't know how to escape non-ASCII characters, especially |
|
1770 |
// which encoding to use. Leave them for now. |
|
1771 |
private static synchronized URI getUserDir() throws URI.MalformedURIException { |
|
1772 |
// get the user.dir property |
|
1773 |
String userDir = ""; |
|
1774 |
try { |
|
16953 | 1775 |
userDir = SecuritySupport.getSystemProperty("user.dir"); |
12005 | 1776 |
} |
1777 |
catch (SecurityException se) { |
|
1778 |
} |
|
1779 |
||
1780 |
// return empty string if property value is empty string. |
|
1781 |
if (userDir.length() == 0) |
|
1782 |
return new URI("file", "", "", null, null); |
|
1783 |
// compute the new escaped value if the new property value doesn't |
|
1784 |
// match the previous one |
|
1785 |
if (gUserDirURI != null && userDir.equals(gUserDir)) { |
|
1786 |
return gUserDirURI; |
|
1787 |
} |
|
1788 |
||
1789 |
// record the new value as the global property value |
|
1790 |
gUserDir = userDir; |
|
1791 |
||
1792 |
char separator = java.io.File.separatorChar; |
|
1793 |
userDir = userDir.replace(separator, '/'); |
|
1794 |
||
1795 |
int len = userDir.length(), ch; |
|
1796 |
StringBuffer buffer = new StringBuffer(len*3); |
|
1797 |
// change C:/blah to /C:/blah |
|
1798 |
if (len >= 2 && userDir.charAt(1) == ':') { |
|
1799 |
ch = Character.toUpperCase(userDir.charAt(0)); |
|
1800 |
if (ch >= 'A' && ch <= 'Z') { |
|
1801 |
buffer.append('/'); |
|
1802 |
} |
|
1803 |
} |
|
1804 |
||
1805 |
// for each character in the path |
|
1806 |
int i = 0; |
|
1807 |
for (; i < len; i++) { |
|
1808 |
ch = userDir.charAt(i); |
|
1809 |
// if it's not an ASCII character, break here, and use UTF-8 encoding |
|
1810 |
if (ch >= 128) |
|
1811 |
break; |
|
1812 |
if (gNeedEscaping[ch]) { |
|
1813 |
buffer.append('%'); |
|
1814 |
buffer.append(gAfterEscaping1[ch]); |
|
1815 |
buffer.append(gAfterEscaping2[ch]); |
|
1816 |
// record the fact that it's escaped |
|
1817 |
} |
|
1818 |
else { |
|
1819 |
buffer.append((char)ch); |
|
1820 |
} |
|
1821 |
} |
|
1822 |
||
1823 |
// we saw some non-ascii character |
|
1824 |
if (i < len) { |
|
1825 |
// get UTF-8 bytes for the remaining sub-string |
|
1826 |
byte[] bytes = null; |
|
1827 |
byte b; |
|
1828 |
try { |
|
1829 |
bytes = userDir.substring(i).getBytes("UTF-8"); |
|
1830 |
} catch (java.io.UnsupportedEncodingException e) { |
|
1831 |
// should never happen |
|
1832 |
return new URI("file", "", userDir, null, null); |
|
1833 |
} |
|
1834 |
len = bytes.length; |
|
1835 |
||
1836 |
// for each byte |
|
1837 |
for (i = 0; i < len; i++) { |
|
1838 |
b = bytes[i]; |
|
1839 |
// for non-ascii character: make it positive, then escape |
|
1840 |
if (b < 0) { |
|
1841 |
ch = b + 256; |
|
1842 |
buffer.append('%'); |
|
1843 |
buffer.append(gHexChs[ch >> 4]); |
|
1844 |
buffer.append(gHexChs[ch & 0xf]); |
|
1845 |
} |
|
1846 |
else if (gNeedEscaping[b]) { |
|
1847 |
buffer.append('%'); |
|
1848 |
buffer.append(gAfterEscaping1[b]); |
|
1849 |
buffer.append(gAfterEscaping2[b]); |
|
1850 |
} |
|
1851 |
else { |
|
1852 |
buffer.append((char)b); |
|
1853 |
} |
|
1854 |
} |
|
1855 |
} |
|
1856 |
||
1857 |
// change blah/blah to blah/blah/ |
|
1858 |
if (!userDir.endsWith("/")) |
|
1859 |
buffer.append('/'); |
|
1860 |
||
1861 |
gUserDirURI = new URI("file", "", buffer.toString(), null, null); |
|
1862 |
||
1863 |
return gUserDirURI; |
|
1864 |
} |
|
1865 |
||
1866 |
/** |
|
1867 |
* Absolutizes a URI using the current value |
|
1868 |
* of the "user.dir" property as the base URI. If |
|
1869 |
* the URI is already absolute, this is a no-op. |
|
1870 |
* |
|
1871 |
* @param uri the URI to absolutize |
|
1872 |
*/ |
|
1873 |
public static void absolutizeAgainstUserDir(URI uri) |
|
1874 |
throws URI.MalformedURIException { |
|
1875 |
uri.absolutize(getUserDir()); |
|
1876 |
} |
|
1877 |
||
1878 |
/** |
|
1879 |
* Expands a system id and returns the system id as a URI, if |
|
1880 |
* it can be expanded. A return value of null means that the |
|
1881 |
* identifier is already expanded. An exception thrown |
|
1882 |
* indicates a failure to expand the id. |
|
1883 |
* |
|
1884 |
* @param systemId The systemId to be expanded. |
|
1885 |
* |
|
1886 |
* @return Returns the URI string representing the expanded system |
|
1887 |
* identifier. A null value indicates that the given |
|
1888 |
* system identifier is already expanded. |
|
1889 |
* |
|
1890 |
*/ |
|
1891 |
public static String expandSystemId(String systemId, String baseSystemId) { |
|
1892 |
||
1893 |
// check for bad parameters id |
|
1894 |
if (systemId == null || systemId.length() == 0) { |
|
1895 |
return systemId; |
|
1896 |
} |
|
1897 |
// if id already expanded, return |
|
1898 |
try { |
|
1899 |
URI uri = new URI(systemId); |
|
1900 |
if (uri != null) { |
|
1901 |
return systemId; |
|
1902 |
} |
|
1903 |
} catch (URI.MalformedURIException e) { |
|
1904 |
// continue on... |
|
1905 |
} |
|
1906 |
// normalize id |
|
1907 |
String id = fixURI(systemId); |
|
1908 |
||
1909 |
// normalize base |
|
1910 |
URI base = null; |
|
1911 |
URI uri = null; |
|
1912 |
try { |
|
1913 |
if (baseSystemId == null || baseSystemId.length() == 0 || |
|
1914 |
baseSystemId.equals(systemId)) { |
|
1915 |
String dir = getUserDir().toString(); |
|
1916 |
base = new URI("file", "", dir, null, null); |
|
1917 |
} else { |
|
1918 |
try { |
|
1919 |
base = new URI(fixURI(baseSystemId)); |
|
1920 |
} catch (URI.MalformedURIException e) { |
|
1921 |
if (baseSystemId.indexOf(':') != -1) { |
|
1922 |
// for xml schemas we might have baseURI with |
|
1923 |
// a specified drive |
|
1924 |
base = new URI("file", "", fixURI(baseSystemId), null, null); |
|
1925 |
} else { |
|
1926 |
String dir = getUserDir().toString(); |
|
1927 |
dir = dir + fixURI(baseSystemId); |
|
1928 |
base = new URI("file", "", dir, null, null); |
|
1929 |
} |
|
1930 |
} |
|
1931 |
} |
|
1932 |
// expand id |
|
1933 |
uri = new URI(base, id); |
|
1934 |
} catch (Exception e) { |
|
1935 |
// let it go through |
|
1936 |
||
1937 |
} |
|
1938 |
||
1939 |
if (uri == null) { |
|
1940 |
return systemId; |
|
1941 |
} |
|
1942 |
return uri.toString(); |
|
1943 |
||
1944 |
} // expandSystemId(String,String):String |
|
1945 |
||
1946 |
/** |
|
1947 |
* Expands a system id and returns the system id as a URI, if |
|
1948 |
* it can be expanded. A return value of null means that the |
|
1949 |
* identifier is already expanded. An exception thrown |
|
1950 |
* indicates a failure to expand the id. |
|
1951 |
* |
|
1952 |
* @param systemId The systemId to be expanded. |
|
1953 |
* |
|
1954 |
* @return Returns the URI string representing the expanded system |
|
1955 |
* identifier. A null value indicates that the given |
|
1956 |
* system identifier is already expanded. |
|
1957 |
* |
|
1958 |
*/ |
|
1959 |
public static String expandSystemId(String systemId, String baseSystemId, |
|
1960 |
boolean strict) |
|
1961 |
throws URI.MalformedURIException { |
|
1962 |
||
1963 |
// check if there is a system id before |
|
1964 |
// trying to expand it. |
|
1965 |
if (systemId == null) { |
|
1966 |
return null; |
|
1967 |
} |
|
1968 |
||
1969 |
// system id has to be a valid URI |
|
1970 |
if (strict) { |
|
1971 |
||
1972 |
||
1973 |
// check if there is a system id before |
|
1974 |
// trying to expand it. |
|
1975 |
if (systemId == null) { |
|
1976 |
return null; |
|
1977 |
} |
|
1978 |
||
1979 |
try { |
|
1980 |
// if it's already an absolute one, return it |
|
1981 |
new URI(systemId); |
|
1982 |
return systemId; |
|
1983 |
} |
|
1984 |
catch (URI.MalformedURIException ex) { |
|
1985 |
} |
|
1986 |
URI base = null; |
|
1987 |
// if there isn't a base uri, use the working directory |
|
1988 |
if (baseSystemId == null || baseSystemId.length() == 0) { |
|
1989 |
base = new URI("file", "", getUserDir().toString(), null, null); |
|
1990 |
} |
|
1991 |
// otherwise, use the base uri |
|
1992 |
else { |
|
1993 |
try { |
|
1994 |
base = new URI(baseSystemId); |
|
1995 |
} |
|
1996 |
catch (URI.MalformedURIException e) { |
|
1997 |
// assume "base" is also a relative uri |
|
1998 |
String dir = getUserDir().toString(); |
|
1999 |
dir = dir + baseSystemId; |
|
2000 |
base = new URI("file", "", dir, null, null); |
|
2001 |
} |
|
2002 |
} |
|
2003 |
// absolutize the system id using the base |
|
2004 |
URI uri = new URI(base, systemId); |
|
2005 |
// return the string rep of the new uri (an absolute one) |
|
2006 |
return uri.toString(); |
|
2007 |
||
2008 |
// if any exception is thrown, it'll get thrown to the caller. |
|
2009 |
} |
|
2010 |
||
2011 |
// Assume the URIs are well-formed. If it turns out they're not, try fixing them up. |
|
2012 |
try { |
|
2013 |
return expandSystemIdStrictOff(systemId, baseSystemId); |
|
2014 |
} |
|
2015 |
catch (URI.MalformedURIException e) { |
|
2016 |
/** Xerces URI rejects unicode, try java.net.URI |
|
2017 |
* this is not ideal solution, but it covers known cases which either |
|
2018 |
* Xerces URI or java.net.URI can handle alone |
|
2019 |
* will file bug against java.net.URI |
|
2020 |
*/ |
|
2021 |
try { |
|
2022 |
return expandSystemIdStrictOff1(systemId, baseSystemId); |
|
2023 |
} catch (URISyntaxException ex) { |
|
2024 |
// continue on... |
|
2025 |
} |
|
2026 |
} |
|
2027 |
// check for bad parameters id |
|
2028 |
if (systemId.length() == 0) { |
|
2029 |
return systemId; |
|
2030 |
} |
|
2031 |
||
2032 |
// normalize id |
|
2033 |
String id = fixURI(systemId); |
|
2034 |
||
2035 |
// normalize base |
|
2036 |
URI base = null; |
|
2037 |
URI uri = null; |
|
2038 |
try { |
|
2039 |
if (baseSystemId == null || baseSystemId.length() == 0 || |
|
2040 |
baseSystemId.equals(systemId)) { |
|
2041 |
base = getUserDir(); |
|
2042 |
} |
|
2043 |
else { |
|
2044 |
try { |
|
2045 |
base = new URI(fixURI(baseSystemId).trim()); |
|
2046 |
} |
|
2047 |
catch (URI.MalformedURIException e) { |
|
2048 |
if (baseSystemId.indexOf(':') != -1) { |
|
2049 |
// for xml schemas we might have baseURI with |
|
2050 |
// a specified drive |
|
2051 |
base = new URI("file", "", fixURI(baseSystemId).trim(), null, null); |
|
2052 |
} |
|
2053 |
else { |
|
2054 |
base = new URI(getUserDir(), fixURI(baseSystemId)); |
|
2055 |
} |
|
2056 |
} |
|
2057 |
} |
|
2058 |
// expand id |
|
2059 |
uri = new URI(base, id.trim()); |
|
2060 |
} |
|
2061 |
catch (Exception e) { |
|
2062 |
// let it go through |
|
2063 |
||
2064 |
} |
|
2065 |
||
2066 |
if (uri == null) { |
|
2067 |
return systemId; |
|
2068 |
} |
|
2069 |
return uri.toString(); |
|
2070 |
||
2071 |
} // expandSystemId(String,String,boolean):String |
|
2072 |
||
2073 |
/** |
|
2074 |
* Helper method for expandSystemId(String,String,boolean):String |
|
2075 |
*/ |
|
2076 |
private static String expandSystemIdStrictOn(String systemId, String baseSystemId) |
|
2077 |
throws URI.MalformedURIException { |
|
2078 |
||
2079 |
URI systemURI = new URI(systemId, true); |
|
2080 |
// If it's already an absolute one, return it |
|
2081 |
if (systemURI.isAbsoluteURI()) { |
|
2082 |
return systemId; |
|
2083 |
} |
|
2084 |
||
2085 |
// If there isn't a base URI, use the working directory |
|
2086 |
URI baseURI = null; |
|
2087 |
if (baseSystemId == null || baseSystemId.length() == 0) { |
|
2088 |
baseURI = getUserDir(); |
|
2089 |
} |
|
2090 |
else { |
|
2091 |
baseURI = new URI(baseSystemId, true); |
|
2092 |
if (!baseURI.isAbsoluteURI()) { |
|
2093 |
// assume "base" is also a relative uri |
|
2094 |
baseURI.absolutize(getUserDir()); |
|
2095 |
} |
|
2096 |
} |
|
2097 |
||
2098 |
// absolutize the system identifier using the base URI |
|
2099 |
systemURI.absolutize(baseURI); |
|
2100 |
||
2101 |
// return the string rep of the new uri (an absolute one) |
|
2102 |
return systemURI.toString(); |
|
2103 |
||
2104 |
// if any exception is thrown, it'll get thrown to the caller. |
|
2105 |
||
2106 |
} // expandSystemIdStrictOn(String,String):String |
|
2107 |
||
2108 |
/** |
|
2109 |
* Attempt to set whether redirects will be followed for an <code>HttpURLConnection</code>. |
|
2110 |
* This may fail on earlier JDKs which do not support setting this preference. |
|
2111 |
*/ |
|
2112 |
public static void setInstanceFollowRedirects(HttpURLConnection urlCon, boolean followRedirects) { |
|
2113 |
try { |
|
2114 |
Method method = HttpURLConnection.class.getMethod("setInstanceFollowRedirects", new Class[] {Boolean.TYPE}); |
|
2115 |
method.invoke(urlCon, new Object[] {followRedirects ? Boolean.TRUE : Boolean.FALSE}); |
|
2116 |
} |
|
2117 |
// setInstanceFollowRedirects doesn't exist. |
|
2118 |
catch (Exception exc) {} |
|
2119 |
} |
|
2120 |
||
2121 |
||
2122 |
/** |
|
2123 |
* Helper method for expandSystemId(String,String,boolean):String |
|
2124 |
*/ |
|
2125 |
private static String expandSystemIdStrictOff(String systemId, String baseSystemId) |
|
2126 |
throws URI.MalformedURIException { |
|
2127 |
||
2128 |
URI systemURI = new URI(systemId, true); |
|
2129 |
// If it's already an absolute one, return it |
|
2130 |
if (systemURI.isAbsoluteURI()) { |
|
2131 |
if (systemURI.getScheme().length() > 1) { |
|
2132 |
return systemId; |
|
2133 |
} |
|
2134 |
/** |
|
2135 |
* If the scheme's length is only one character, |
|
2136 |
* it's likely that this was intended as a file |
|
2137 |
* path. Fixing this up in expandSystemId to |
|
2138 |
* maintain backwards compatibility. |
|
2139 |
*/ |
|
2140 |
throw new URI.MalformedURIException(); |
|
2141 |
} |
|
2142 |
||
2143 |
// If there isn't a base URI, use the working directory |
|
2144 |
URI baseURI = null; |
|
2145 |
if (baseSystemId == null || baseSystemId.length() == 0) { |
|
2146 |
baseURI = getUserDir(); |
|
2147 |
} |
|
2148 |
else { |
|
2149 |
baseURI = new URI(baseSystemId, true); |
|
2150 |
if (!baseURI.isAbsoluteURI()) { |
|
2151 |
// assume "base" is also a relative uri |
|
2152 |
baseURI.absolutize(getUserDir()); |
|
2153 |
} |
|
2154 |
} |
|
2155 |
||
2156 |
// absolutize the system identifier using the base URI |
|
2157 |
systemURI.absolutize(baseURI); |
|
2158 |
||
2159 |
// return the string rep of the new uri (an absolute one) |
|
2160 |
return systemURI.toString(); |
|
2161 |
||
2162 |
// if any exception is thrown, it'll get thrown to the caller. |
|
2163 |
||
2164 |
} // expandSystemIdStrictOff(String,String):String |
|
2165 |
||
2166 |
private static String expandSystemIdStrictOff1(String systemId, String baseSystemId) |
|
2167 |
throws URISyntaxException, URI.MalformedURIException { |
|
2168 |
||
2169 |
java.net.URI systemURI = new java.net.URI(systemId); |
|
2170 |
// If it's already an absolute one, return it |
|
2171 |
if (systemURI.isAbsolute()) { |
|
2172 |
if (systemURI.getScheme().length() > 1) { |
|
2173 |
return systemId; |
|
2174 |
} |
|
2175 |
/** |
|
2176 |
* If the scheme's length is only one character, |
|
2177 |
* it's likely that this was intended as a file |
|
2178 |
* path. Fixing this up in expandSystemId to |
|
2179 |
* maintain backwards compatibility. |
|
2180 |
*/ |
|
2181 |
throw new URISyntaxException(systemId, "the scheme's length is only one character"); |
|
2182 |
} |
|
2183 |
||
2184 |
// If there isn't a base URI, use the working directory |
|
2185 |
URI baseURI = null; |
|
2186 |
if (baseSystemId == null || baseSystemId.length() == 0) { |
|
2187 |
baseURI = getUserDir(); |
|
2188 |
} |
|
2189 |
else { |
|
2190 |
baseURI = new URI(baseSystemId, true); |
|
2191 |
if (!baseURI.isAbsoluteURI()) { |
|
2192 |
// assume "base" is also a relative uri |
|
2193 |
baseURI.absolutize(getUserDir()); |
|
2194 |
} |
|
2195 |
} |
|
2196 |
||
2197 |
// absolutize the system identifier using the base URI |
|
2198 |
// systemURI.absolutize(baseURI); |
|
2199 |
systemURI = (new java.net.URI(baseURI.toString())).resolve(systemURI); |
|
2200 |
||
2201 |
// return the string rep of the new uri (an absolute one) |
|
2202 |
return systemURI.toString(); |
|
2203 |
||
2204 |
// if any exception is thrown, it'll get thrown to the caller. |
|
2205 |
||
2206 |
} // expandSystemIdStrictOff(String,String):String |
|
2207 |
||
2208 |
// |
|
2209 |
// Protected methods |
|
2210 |
// |
|
2211 |
||
2212 |
||
2213 |
/** |
|
2214 |
* Returns the IANA encoding name that is auto-detected from |
|
2215 |
* the bytes specified, with the endian-ness of that encoding where appropriate. |
|
2216 |
* |
|
2217 |
* @param b4 The first four bytes of the input. |
|
2218 |
* @param count The number of bytes actually read. |
|
2219 |
* @return a 2-element array: the first element, an IANA-encoding string, |
|
2220 |
* the second element a Boolean which is true iff the document is big endian, false |
|
2221 |
* if it's little-endian, and null if the distinction isn't relevant. |
|
2222 |
*/ |
|
2223 |
protected Object[] getEncodingName(byte[] b4, int count) { |
|
2224 |
||
2225 |
if (count < 2) { |
|
2226 |
return defaultEncoding; |
|
2227 |
} |
|
2228 |
||
2229 |
// UTF-16, with BOM |
|
2230 |
int b0 = b4[0] & 0xFF; |
|
2231 |
int b1 = b4[1] & 0xFF; |
|
2232 |
if (b0 == 0xFE && b1 == 0xFF) { |
|
2233 |
// UTF-16, big-endian |
|
2234 |
return new Object [] {"UTF-16BE", new Boolean(true)}; |
|
2235 |
} |
|
2236 |
if (b0 == 0xFF && b1 == 0xFE) { |
|
2237 |
// UTF-16, little-endian |
|
2238 |
return new Object [] {"UTF-16LE", new Boolean(false)}; |
|
2239 |
} |
|
2240 |
||
2241 |
// default to UTF-8 if we don't have enough bytes to make a |
|
2242 |
// good determination of the encoding |
|
2243 |
if (count < 3) { |
|
2244 |
return defaultEncoding; |
|
2245 |
} |
|
2246 |
||
2247 |
// UTF-8 with a BOM |
|
2248 |
int b2 = b4[2] & 0xFF; |
|
2249 |
if (b0 == 0xEF && b1 == 0xBB && b2 == 0xBF) { |
|
2250 |
return defaultEncoding; |
|
2251 |
} |
|
2252 |
||
2253 |
// default to UTF-8 if we don't have enough bytes to make a |
|
2254 |
// good determination of the encoding |
|
2255 |
if (count < 4) { |
|
2256 |
return defaultEncoding; |
|
2257 |
} |
|
2258 |
||
2259 |
// other encodings |
|
2260 |
int b3 = b4[3] & 0xFF; |
|
2261 |
if (b0 == 0x00 && b1 == 0x00 && b2 == 0x00 && b3 == 0x3C) { |
|
2262 |
// UCS-4, big endian (1234) |
|
2263 |
return new Object [] {"ISO-10646-UCS-4", new Boolean(true)}; |
|
2264 |
} |
|
2265 |
if (b0 == 0x3C && b1 == 0x00 && b2 == 0x00 && b3 == 0x00) { |
|
2266 |
// UCS-4, little endian (4321) |
|
2267 |
return new Object [] {"ISO-10646-UCS-4", new Boolean(false)}; |
|
2268 |
} |
|
2269 |
if (b0 == 0x00 && b1 == 0x00 && b2 == 0x3C && b3 == 0x00) { |
|
2270 |
// UCS-4, unusual octet order (2143) |
|
2271 |
// REVISIT: What should this be? |
|
2272 |
return new Object [] {"ISO-10646-UCS-4", null}; |
|
2273 |
} |
|
2274 |
if (b0 == 0x00 && b1 == 0x3C && b2 == 0x00 && b3 == 0x00) { |
|
2275 |
// UCS-4, unusual octect order (3412) |
|
2276 |
// REVISIT: What should this be? |
|
2277 |
return new Object [] {"ISO-10646-UCS-4", null}; |
|
2278 |
} |
|
2279 |
if (b0 == 0x00 && b1 == 0x3C && b2 == 0x00 && b3 == 0x3F) { |
|
2280 |
// UTF-16, big-endian, no BOM |
|
2281 |
// (or could turn out to be UCS-2... |
|
2282 |
// REVISIT: What should this be? |
|
2283 |
return new Object [] {"UTF-16BE", new Boolean(true)}; |
|
2284 |
} |
|
2285 |
if (b0 == 0x3C && b1 == 0x00 && b2 == 0x3F && b3 == 0x00) { |
|
2286 |
// UTF-16, little-endian, no BOM |
|
2287 |
// (or could turn out to be UCS-2... |
|
2288 |
return new Object [] {"UTF-16LE", new Boolean(false)}; |
|
2289 |
} |
|
2290 |
if (b0 == 0x4C && b1 == 0x6F && b2 == 0xA7 && b3 == 0x94) { |
|
2291 |
// EBCDIC |
|
2292 |
// a la xerces1, return CP037 instead of EBCDIC here |
|
2293 |
return new Object [] {"CP037", null}; |
|
2294 |
} |
|
2295 |
||
2296 |
return defaultEncoding; |
|
2297 |
||
2298 |
} // getEncodingName(byte[],int):Object[] |
|
2299 |
||
2300 |
/** |
|
2301 |
* Creates a reader capable of reading the given input stream in |
|
2302 |
* the specified encoding. |
|
2303 |
* |
|
2304 |
* @param inputStream The input stream. |
|
2305 |
* @param encoding The encoding name that the input stream is |
|
2306 |
* encoded using. If the user has specified that |
|
2307 |
* Java encoding names are allowed, then the |
|
2308 |
* encoding name may be a Java encoding name; |
|
2309 |
* otherwise, it is an ianaEncoding name. |
|
2310 |
* @param isBigEndian For encodings (like uCS-4), whose names cannot |
|
2311 |
* specify a byte order, this tells whether the order is bigEndian. null menas |
|
2312 |
* unknown or not relevant. |
|
2313 |
* |
|
2314 |
* @return Returns a reader. |
|
2315 |
*/ |
|
2316 |
protected Reader createReader(InputStream inputStream, String encoding, Boolean isBigEndian) |
|
2317 |
throws IOException { |
|
2318 |
||
2319 |
// normalize encoding name |
|
2320 |
if (encoding == null) { |
|
2321 |
encoding = "UTF-8"; |
|
2322 |
} |
|
2323 |
||
2324 |
// try to use an optimized reader |
|
2325 |
String ENCODING = encoding.toUpperCase(Locale.ENGLISH); |
|
2326 |
if (ENCODING.equals("UTF-8")) { |
|
2327 |
if (DEBUG_ENCODINGS) { |
|
2328 |
System.out.println("$$$ creating UTF8Reader"); |
|
2329 |
} |
|
2330 |
return new UTF8Reader(inputStream, fBufferSize, fErrorReporter.getMessageFormatter(XMLMessageFormatter.XML_DOMAIN), fErrorReporter.getLocale() ); |
|
2331 |
} |
|
2332 |
if (ENCODING.equals("US-ASCII")) { |
|
2333 |
if (DEBUG_ENCODINGS) { |
|
2334 |
System.out.println("$$$ creating ASCIIReader"); |
|
2335 |
} |
|
2336 |
return new ASCIIReader(inputStream, fBufferSize, fErrorReporter.getMessageFormatter(XMLMessageFormatter.XML_DOMAIN), fErrorReporter.getLocale()); |
|
2337 |
} |
|
2338 |
if(ENCODING.equals("ISO-10646-UCS-4")) { |
|
2339 |
if(isBigEndian != null) { |
|
2340 |
boolean isBE = isBigEndian.booleanValue(); |
|
2341 |
if(isBE) { |
|
2342 |
return new UCSReader(inputStream, UCSReader.UCS4BE); |
|
2343 |
} else { |
|
2344 |
return new UCSReader(inputStream, UCSReader.UCS4LE); |
|
2345 |
} |
|
2346 |
} else { |
|
2347 |
fErrorReporter.reportError(this.getEntityScanner(),XMLMessageFormatter.XML_DOMAIN, |
|
2348 |
"EncodingByteOrderUnsupported", |
|
2349 |
new Object[] { encoding }, |
|
2350 |
XMLErrorReporter.SEVERITY_FATAL_ERROR); |
|
2351 |
} |
|
2352 |
} |
|
2353 |
if(ENCODING.equals("ISO-10646-UCS-2")) { |
|
2354 |
if(isBigEndian != null) { // sould never happen with this encoding... |
|
2355 |
boolean isBE = isBigEndian.booleanValue(); |
|
2356 |
if(isBE) { |
|
2357 |
return new UCSReader(inputStream, UCSReader.UCS2BE); |
|
2358 |
} else { |
|
2359 |
return new UCSReader(inputStream, UCSReader.UCS2LE); |
|
2360 |
} |
|
2361 |
} else { |
|
2362 |
fErrorReporter.reportError(this.getEntityScanner(),XMLMessageFormatter.XML_DOMAIN, |
|
2363 |
"EncodingByteOrderUnsupported", |
|
2364 |
new Object[] { encoding }, |
|
2365 |
XMLErrorReporter.SEVERITY_FATAL_ERROR); |
|
2366 |
} |
|
2367 |
} |
|
2368 |
||
2369 |
// check for valid name |
|
2370 |
boolean validIANA = XMLChar.isValidIANAEncoding(encoding); |
|
2371 |
boolean validJava = XMLChar.isValidJavaEncoding(encoding); |
|
2372 |
if (!validIANA || (fAllowJavaEncodings && !validJava)) { |
|
2373 |
fErrorReporter.reportError(this.getEntityScanner(),XMLMessageFormatter.XML_DOMAIN, |
|
2374 |
"EncodingDeclInvalid", |
|
2375 |
new Object[] { encoding }, |
|
2376 |
XMLErrorReporter.SEVERITY_FATAL_ERROR); |
|
2377 |
// NOTE: AndyH suggested that, on failure, we use ISO Latin 1 |
|
2378 |
// because every byte is a valid ISO Latin 1 character. |
|
2379 |
// It may not translate correctly but if we failed on |
|
2380 |
// the encoding anyway, then we're expecting the content |
|
2381 |
// of the document to be bad. This will just prevent an |
|
2382 |
// invalid UTF-8 sequence to be detected. This is only |
|
2383 |
// important when continue-after-fatal-error is turned |
|
2384 |
// on. -Ac |
|
2385 |
encoding = "ISO-8859-1"; |
|
2386 |
} |
|
2387 |
||
2388 |
// try to use a Java reader |
|
2389 |
String javaEncoding = EncodingMap.getIANA2JavaMapping(ENCODING); |
|
2390 |
if (javaEncoding == null) { |
|
2391 |
if(fAllowJavaEncodings) { |
|
2392 |
javaEncoding = encoding; |
|
2393 |
} else { |
|
2394 |
fErrorReporter.reportError(this.getEntityScanner(),XMLMessageFormatter.XML_DOMAIN, |
|
2395 |
"EncodingDeclInvalid", |
|
2396 |
new Object[] { encoding }, |
|
2397 |
XMLErrorReporter.SEVERITY_FATAL_ERROR); |
|
2398 |
// see comment above. |
|
2399 |
javaEncoding = "ISO8859_1"; |
|
2400 |
} |
|
2401 |
} |
|
2402 |
if (DEBUG_ENCODINGS) { |
|
2403 |
System.out.print("$$$ creating Java InputStreamReader: encoding="+javaEncoding); |
|
2404 |
if (javaEncoding == encoding) { |
|
2405 |
System.out.print(" (IANA encoding)"); |
|
2406 |
} |
|
2407 |
System.out.println(); |
|
2408 |
} |
|
2409 |
return new BufferedReader( new InputStreamReader(inputStream, javaEncoding)); |
|
2410 |
||
2411 |
} // createReader(InputStream,String, Boolean): Reader |
|
2412 |
||
2413 |
||
2414 |
/** |
|
2415 |
* Return the public identifier for the current document event. |
|
2416 |
* <p> |
|
2417 |
* The return value is the public identifier of the document |
|
2418 |
* entity or of the external parsed entity in which the markup |
|
2419 |
* triggering the event appears. |
|
2420 |
* |
|
2421 |
* @return A string containing the public identifier, or |
|
2422 |
* null if none is available. |
|
2423 |
*/ |
|
2424 |
public String getPublicId() { |
|
2425 |
return (fCurrentEntity != null && fCurrentEntity.entityLocation != null) ? fCurrentEntity.entityLocation.getPublicId() : null; |
|
2426 |
} // getPublicId():String |
|
2427 |
||
2428 |
/** |
|
2429 |
* Return the expanded system identifier for the current document event. |
|
2430 |
* <p> |
|
2431 |
* The return value is the expanded system identifier of the document |
|
2432 |
* entity or of the external parsed entity in which the markup |
|
2433 |
* triggering the event appears. |
|
2434 |
* <p> |
|
2435 |
* If the system identifier is a URL, the parser must resolve it |
|
2436 |
* fully before passing it to the application. |
|
2437 |
* |
|
2438 |
* @return A string containing the expanded system identifier, or null |
|
2439 |
* if none is available. |
|
2440 |
*/ |
|
2441 |
public String getExpandedSystemId() { |
|
2442 |
if (fCurrentEntity != null) { |
|
2443 |
if (fCurrentEntity.entityLocation != null && |
|
2444 |
fCurrentEntity.entityLocation.getExpandedSystemId() != null ) { |
|
2445 |
return fCurrentEntity.entityLocation.getExpandedSystemId(); |
|
2446 |
} else { |
|
2447 |
// search for the first external entity on the stack |
|
2448 |
int size = fEntityStack.size(); |
|
2449 |
for (int i = size - 1; i >= 0 ; i--) { |
|
2450 |
Entity.ScannedEntity externalEntity = |
|
2451 |
(Entity.ScannedEntity)fEntityStack.elementAt(i); |
|
2452 |
||
2453 |
if (externalEntity.entityLocation != null && |
|
2454 |
externalEntity.entityLocation.getExpandedSystemId() != null) { |
|
2455 |
return externalEntity.entityLocation.getExpandedSystemId(); |
|
2456 |
} |
|
2457 |
} |
|
2458 |
} |
|
2459 |
} |
|
2460 |
return null; |
|
2461 |
} // getExpandedSystemId():String |
|
2462 |
||
2463 |
/** |
|
2464 |
* Return the literal system identifier for the current document event. |
|
2465 |
* <p> |
|
2466 |
* The return value is the literal system identifier of the document |
|
2467 |
* entity or of the external parsed entity in which the markup |
|
2468 |
* triggering the event appears. |
|
2469 |
* <p> |
|
2470 |
* @return A string containing the literal system identifier, or null |
|
2471 |
* if none is available. |
|
2472 |
*/ |
|
2473 |
public String getLiteralSystemId() { |
|
2474 |
if (fCurrentEntity != null) { |
|
2475 |
if (fCurrentEntity.entityLocation != null && |
|
2476 |
fCurrentEntity.entityLocation.getLiteralSystemId() != null ) { |
|
2477 |
return fCurrentEntity.entityLocation.getLiteralSystemId(); |
|
2478 |
} else { |
|
2479 |
// search for the first external entity on the stack |
|
2480 |
int size = fEntityStack.size(); |
|
2481 |
for (int i = size - 1; i >= 0 ; i--) { |
|
2482 |
Entity.ScannedEntity externalEntity = |
|
2483 |
(Entity.ScannedEntity)fEntityStack.elementAt(i); |
|
2484 |
||
2485 |
if (externalEntity.entityLocation != null && |
|
2486 |
externalEntity.entityLocation.getLiteralSystemId() != null) { |
|
2487 |
return externalEntity.entityLocation.getLiteralSystemId(); |
|
2488 |
} |
|
2489 |
} |
|
2490 |
} |
|
2491 |
} |
|
2492 |
return null; |
|
2493 |
} // getLiteralSystemId():String |
|
2494 |
||
2495 |
/** |
|
2496 |
* Return the line number where the current document event ends. |
|
2497 |
* <p> |
|
2498 |
* <strong>Warning:</strong> The return value from the method |
|
2499 |
* is intended only as an approximation for the sake of error |
|
2500 |
* reporting; it is not intended to provide sufficient information |
|
2501 |
* to edit the character content of the original XML document. |
|
2502 |
* <p> |
|
2503 |
* The return value is an approximation of the line number |
|
2504 |
* in the document entity or external parsed entity where the |
|
2505 |
* markup triggering the event appears. |
|
2506 |
* <p> |
|
2507 |
* If possible, the SAX driver should provide the line position |
|
2508 |
* of the first character after the text associated with the document |
|
2509 |
* event. The first line in the document is line 1. |
|
2510 |
* |
|
2511 |
* @return The line number, or -1 if none is available. |
|
2512 |
*/ |
|
2513 |
public int getLineNumber() { |
|
2514 |
if (fCurrentEntity != null) { |
|
2515 |
if (fCurrentEntity.isExternal()) { |
|
2516 |
return fCurrentEntity.lineNumber; |
|
2517 |
} else { |
|
2518 |
// search for the first external entity on the stack |
|
2519 |
int size = fEntityStack.size(); |
|
2520 |
for (int i=size-1; i>0 ; i--) { |
|
2521 |
Entity.ScannedEntity firstExternalEntity = (Entity.ScannedEntity)fEntityStack.elementAt(i); |
|
2522 |
if (firstExternalEntity.isExternal()) { |
|
2523 |
return firstExternalEntity.lineNumber; |
|
2524 |
} |
|
2525 |
} |
|
2526 |
} |
|
2527 |
} |
|
2528 |
||
2529 |
return -1; |
|
2530 |
||
2531 |
} // getLineNumber():int |
|
2532 |
||
2533 |
/** |
|
2534 |
* Return the column number where the current document event ends. |
|
2535 |
* <p> |
|
2536 |
* <strong>Warning:</strong> The return value from the method |
|
2537 |
* is intended only as an approximation for the sake of error |
|
2538 |
* reporting; it is not intended to provide sufficient information |
|
2539 |
* to edit the character content of the original XML document. |
|
2540 |
* <p> |
|
2541 |
* The return value is an approximation of the column number |
|
2542 |
* in the document entity or external parsed entity where the |
|
2543 |
* markup triggering the event appears. |
|
2544 |
* <p> |
|
2545 |
* If possible, the SAX driver should provide the line position |
|
2546 |
* of the first character after the text associated with the document |
|
2547 |
* event. |
|
2548 |
* <p> |
|
2549 |
* If possible, the SAX driver should provide the line position |
|
2550 |
* of the first character after the text associated with the document |
|
2551 |
* event. The first column in each line is column 1. |
|
2552 |
* |
|
2553 |
* @return The column number, or -1 if none is available. |
|
2554 |
*/ |
|
2555 |
public int getColumnNumber() { |
|
2556 |
if (fCurrentEntity != null) { |
|
2557 |
if (fCurrentEntity.isExternal()) { |
|
2558 |
return fCurrentEntity.columnNumber; |
|
2559 |
} else { |
|
2560 |
// search for the first external entity on the stack |
|
2561 |
int size = fEntityStack.size(); |
|
2562 |
for (int i=size-1; i>0 ; i--) { |
|
2563 |
Entity.ScannedEntity firstExternalEntity = (Entity.ScannedEntity)fEntityStack.elementAt(i); |
|
2564 |
if (firstExternalEntity.isExternal()) { |
|
2565 |
return firstExternalEntity.columnNumber; |
|
2566 |
} |
|
2567 |
} |
|
2568 |
} |
|
2569 |
} |
|
2570 |
||
2571 |
return -1; |
|
2572 |
} // getColumnNumber():int |
|
2573 |
||
2574 |
||
2575 |
// |
|
2576 |
// Protected static methods |
|
2577 |
// |
|
2578 |
||
2579 |
/** |
|
2580 |
* Fixes a platform dependent filename to standard URI form. |
|
2581 |
* |
|
2582 |
* @param str The string to fix. |
|
2583 |
* |
|
2584 |
* @return Returns the fixed URI string. |
|
2585 |
*/ |
|
2586 |
protected static String fixURI(String str) { |
|
2587 |
||
2588 |
// handle platform dependent strings |
|
2589 |
str = str.replace(java.io.File.separatorChar, '/'); |
|
2590 |
||
2591 |
// Windows fix |
|
2592 |
if (str.length() >= 2) { |
|
2593 |
char ch1 = str.charAt(1); |
|
2594 |
// change "C:blah" to "/C:blah" |
|
2595 |
if (ch1 == ':') { |
|
2596 |
char ch0 = Character.toUpperCase(str.charAt(0)); |
|
2597 |
if (ch0 >= 'A' && ch0 <= 'Z') { |
|
2598 |
str = "/" + str; |
|
2599 |
} |
|
2600 |
} |
|
2601 |
// change "//blah" to "file://blah" |
|
2602 |
else if (ch1 == '/' && str.charAt(0) == '/') { |
|
2603 |
str = "file:" + str; |
|
2604 |
} |
|
2605 |
} |
|
2606 |
||
2607 |
// replace spaces in file names with %20. |
|
2608 |
// Original comment from JDK5: the following algorithm might not be |
|
2609 |
// very performant, but people who want to use invalid URI's have to |
|
2610 |
// pay the price. |
|
2611 |
int pos = str.indexOf(' '); |
|
2612 |
if (pos >= 0) { |
|
2613 |
StringBuilder sb = new StringBuilder(str.length()); |
|
2614 |
// put characters before ' ' into the string builder |
|
2615 |
for (int i = 0; i < pos; i++) |
|
2616 |
sb.append(str.charAt(i)); |
|
2617 |
// and %20 for the space |
|
2618 |
sb.append("%20"); |
|
2619 |
// for the remamining part, also convert ' ' to "%20". |
|
2620 |
for (int i = pos+1; i < str.length(); i++) { |
|
2621 |
if (str.charAt(i) == ' ') |
|
2622 |
sb.append("%20"); |
|
2623 |
else |
|
2624 |
sb.append(str.charAt(i)); |
|
2625 |
} |
|
2626 |
str = sb.toString(); |
|
2627 |
} |
|
2628 |
||
2629 |
// done |
|
2630 |
return str; |
|
2631 |
||
2632 |
} // fixURI(String):String |
|
2633 |
||
2634 |
||
2635 |
// |
|
2636 |
// Package visible methods |
|
2637 |
// |
|
2638 |
/** Prints the contents of the buffer. */ |
|
2639 |
final void print() { |
|
2640 |
if (DEBUG_BUFFER) { |
|
2641 |
if (fCurrentEntity != null) { |
|
2642 |
System.out.print('['); |
|
2643 |
System.out.print(fCurrentEntity.count); |
|
2644 |
System.out.print(' '); |
|
2645 |
System.out.print(fCurrentEntity.position); |
|
2646 |
if (fCurrentEntity.count > 0) { |
|
2647 |
System.out.print(" \""); |
|
2648 |
for (int i = 0; i < fCurrentEntity.count; i++) { |
|
2649 |
if (i == fCurrentEntity.position) { |
|
2650 |
System.out.print('^'); |
|
2651 |
} |
|
2652 |
char c = fCurrentEntity.ch[i]; |
|
2653 |
switch (c) { |
|
2654 |
case '\n': { |
|
2655 |
System.out.print("\\n"); |
|
2656 |
break; |
|
2657 |
} |
|
2658 |
case '\r': { |
|
2659 |
System.out.print("\\r"); |
|
2660 |
break; |
|
2661 |
} |
|
2662 |
case '\t': { |
|
2663 |
System.out.print("\\t"); |
|
2664 |
break; |
|
2665 |
} |
|
2666 |
case '\\': { |
|
2667 |
System.out.print("\\\\"); |
|
2668 |
break; |
|
2669 |
} |
|
2670 |
default: { |
|
2671 |
System.out.print(c); |
|
2672 |
} |
|
2673 |
} |
|
2674 |
} |
|
2675 |
if (fCurrentEntity.position == fCurrentEntity.count) { |
|
2676 |
System.out.print('^'); |
|
2677 |
} |
|
2678 |
System.out.print('"'); |
|
2679 |
} |
|
2680 |
System.out.print(']'); |
|
2681 |
System.out.print(" @ "); |
|
2682 |
System.out.print(fCurrentEntity.lineNumber); |
|
2683 |
System.out.print(','); |
|
2684 |
System.out.print(fCurrentEntity.columnNumber); |
|
2685 |
} else { |
|
2686 |
System.out.print("*NO CURRENT ENTITY*"); |
|
2687 |
} |
|
2688 |
} |
|
2689 |
} // print() |
|
2690 |
||
2691 |
/** |
|
2692 |
* Buffer used in entity manager to reuse character arrays instead |
|
2693 |
* of creating new ones every time. |
|
2694 |
* |
|
2695 |
* @xerces.internal |
|
2696 |
* |
|
2697 |
* @author Ankit Pasricha, IBM |
|
2698 |
*/ |
|
2699 |
private static class CharacterBuffer { |
|
2700 |
||
2701 |
/** character buffer */ |
|
2702 |
private char[] ch; |
|
2703 |
||
2704 |
/** whether the buffer is for an external or internal scanned entity */ |
|
2705 |
private boolean isExternal; |
|
2706 |
||
2707 |
public CharacterBuffer(boolean isExternal, int size) { |
|
2708 |
this.isExternal = isExternal; |
|
2709 |
ch = new char[size]; |
|
2710 |
} |
|
2711 |
} |
|
2712 |
||
2713 |
||
2714 |
/** |
|
2715 |
* Stores a number of character buffers and provides it to the entity |
|
2716 |
* manager to use when an entity is seen. |
|
2717 |
* |
|
2718 |
* @xerces.internal |
|
2719 |
* |
|
2720 |
* @author Ankit Pasricha, IBM |
|
2721 |
*/ |
|
2722 |
private static class CharacterBufferPool { |
|
2723 |
||
2724 |
private static final int DEFAULT_POOL_SIZE = 3; |
|
2725 |
||
2726 |
private CharacterBuffer[] fInternalBufferPool; |
|
2727 |
private CharacterBuffer[] fExternalBufferPool; |
|
2728 |
||
2729 |
private int fExternalBufferSize; |
|
2730 |
private int fInternalBufferSize; |
|
2731 |
private int poolSize; |
|
2732 |
||
2733 |
private int fInternalTop; |
|
2734 |
private int fExternalTop; |
|
2735 |
||
2736 |
public CharacterBufferPool(int externalBufferSize, int internalBufferSize) { |
|
2737 |
this(DEFAULT_POOL_SIZE, externalBufferSize, internalBufferSize); |
|
2738 |
} |
|
2739 |
||
2740 |
public CharacterBufferPool(int poolSize, int externalBufferSize, int internalBufferSize) { |
|
2741 |
fExternalBufferSize = externalBufferSize; |
|
2742 |
fInternalBufferSize = internalBufferSize; |
|
2743 |
this.poolSize = poolSize; |
|
2744 |
init(); |
|
2745 |
} |
|
2746 |
||
2747 |
/** Initializes buffer pool. **/ |
|
2748 |
private void init() { |
|
2749 |
fInternalBufferPool = new CharacterBuffer[poolSize]; |
|
2750 |
fExternalBufferPool = new CharacterBuffer[poolSize]; |
|
2751 |
fInternalTop = -1; |
|
2752 |
fExternalTop = -1; |
|
2753 |
} |
|
2754 |
||
2755 |
/** Retrieves buffer from pool. **/ |
|
2756 |
public CharacterBuffer getBuffer(boolean external) { |
|
2757 |
if (external) { |
|
2758 |
if (fExternalTop > -1) { |
|
2759 |
return (CharacterBuffer)fExternalBufferPool[fExternalTop--]; |
|
2760 |
} |
|
2761 |
else { |
|
2762 |
return new CharacterBuffer(true, fExternalBufferSize); |
|
2763 |
} |
|
2764 |
} |
|
2765 |
else { |
|
2766 |
if (fInternalTop > -1) { |
|
2767 |
return (CharacterBuffer)fInternalBufferPool[fInternalTop--]; |
|
2768 |
} |
|
2769 |
else { |
|
2770 |
return new CharacterBuffer(false, fInternalBufferSize); |
|
2771 |
} |
|
2772 |
} |
|
2773 |
} |
|
2774 |
||
2775 |
/** Returns buffer to pool. **/ |
|
2776 |
public void returnToPool(CharacterBuffer buffer) { |
|
2777 |
if (buffer.isExternal) { |
|
2778 |
if (fExternalTop < fExternalBufferPool.length - 1) { |
|
2779 |
fExternalBufferPool[++fExternalTop] = buffer; |
|
2780 |
} |
|
2781 |
} |
|
2782 |
else if (fInternalTop < fInternalBufferPool.length - 1) { |
|
2783 |
fInternalBufferPool[++fInternalTop] = buffer; |
|
2784 |
} |
|
2785 |
} |
|
2786 |
||
2787 |
/** Sets the size of external buffers and dumps the old pool. **/ |
|
2788 |
public void setExternalBufferSize(int bufferSize) { |
|
2789 |
fExternalBufferSize = bufferSize; |
|
2790 |
fExternalBufferPool = new CharacterBuffer[poolSize]; |
|
2791 |
fExternalTop = -1; |
|
2792 |
} |
|
2793 |
} |
|
2794 |
||
2795 |
/** |
|
2796 |
* This class wraps the byte inputstreams we're presented with. |
|
2797 |
* We need it because java.io.InputStreams don't provide |
|
2798 |
* functionality to reread processed bytes, and they have a habit |
|
2799 |
* of reading more than one character when you call their read() |
|
2800 |
* methods. This means that, once we discover the true (declared) |
|
2801 |
* encoding of a document, we can neither backtrack to read the |
|
2802 |
* whole doc again nor start reading where we are with a new |
|
2803 |
* reader. |
|
2804 |
* |
|
2805 |
* This class allows rewinding an inputStream by allowing a mark |
|
2806 |
* to be set, and the stream reset to that position. <strong>The |
|
2807 |
* class assumes that it needs to read one character per |
|
2808 |
* invocation when it's read() method is inovked, but uses the |
|
2809 |
* underlying InputStream's read(char[], offset length) method--it |
|
2810 |
* won't buffer data read this way!</strong> |
|
2811 |
* |
|
2812 |
* @xerces.internal |
|
2813 |
* |
|
2814 |
* @author Neil Graham, IBM |
|
2815 |
* @author Glenn Marcy, IBM |
|
2816 |
*/ |
|
2817 |
||
2818 |
protected final class RewindableInputStream extends InputStream { |
|
2819 |
||
2820 |
private InputStream fInputStream; |
|
2821 |
private byte[] fData; |
|
2822 |
private int fStartOffset; |
|
2823 |
private int fEndOffset; |
|
2824 |
private int fOffset; |
|
2825 |
private int fLength; |
|
2826 |
private int fMark; |
|
2827 |
||
2828 |
public RewindableInputStream(InputStream is) { |
|
2829 |
fData = new byte[DEFAULT_XMLDECL_BUFFER_SIZE]; |
|
2830 |
fInputStream = is; |
|
2831 |
fStartOffset = 0; |
|
2832 |
fEndOffset = -1; |
|
2833 |
fOffset = 0; |
|
2834 |
fLength = 0; |
|
2835 |
fMark = 0; |
|
2836 |
} |
|
2837 |
||
2838 |
public void setStartOffset(int offset) { |
|
2839 |
fStartOffset = offset; |
|
2840 |
} |
|
2841 |
||
2842 |
public void rewind() { |
|
2843 |
fOffset = fStartOffset; |
|
2844 |
} |
|
2845 |
||
2846 |
public int read() throws IOException { |
|
2847 |
int b = 0; |
|
2848 |
if (fOffset < fLength) { |
|
2849 |
return fData[fOffset++] & 0xff; |
|
2850 |
} |
|
2851 |
if (fOffset == fEndOffset) { |
|
2852 |
return -1; |
|
2853 |
} |
|
2854 |
if (fOffset == fData.length) { |
|
2855 |
byte[] newData = new byte[fOffset << 1]; |
|
2856 |
System.arraycopy(fData, 0, newData, 0, fOffset); |
|
2857 |
fData = newData; |
|
2858 |
} |
|
2859 |
b = fInputStream.read(); |
|
2860 |
if (b == -1) { |
|
2861 |
fEndOffset = fOffset; |
|
2862 |
return -1; |
|
2863 |
} |
|
2864 |
fData[fLength++] = (byte)b; |
|
2865 |
fOffset++; |
|
2866 |
return b & 0xff; |
|
2867 |
} |
|
2868 |
||
2869 |
public int read(byte[] b, int off, int len) throws IOException { |
|
2870 |
int bytesLeft = fLength - fOffset; |
|
2871 |
if (bytesLeft == 0) { |
|
2872 |
if (fOffset == fEndOffset) { |
|
2873 |
return -1; |
|
2874 |
} |
|
2875 |
||
2876 |
/** |
|
2877 |
* //System.out.println("fCurrentEntitty = " + fCurrentEntity ); |
|
2878 |
* //System.out.println("fInputStream = " + fInputStream ); |
|
2879 |
* // better get some more for the voracious reader... */ |
|
2880 |
||
2881 |
if(fCurrentEntity.mayReadChunks || !fCurrentEntity.xmlDeclChunkRead) { |
|
2882 |
||
2883 |
if (!fCurrentEntity.xmlDeclChunkRead) |
|
2884 |
{ |
|
2885 |
fCurrentEntity.xmlDeclChunkRead = true; |
|
2886 |
len = fCurrentEntity.DEFAULT_XMLDECL_BUFFER_SIZE; |
|
2887 |
} |
|
2888 |
return fInputStream.read(b, off, len); |
|
2889 |
} |
|
2890 |
||
2891 |
int returnedVal = read(); |
|
2892 |
if(returnedVal == -1) { |
|
2893 |
fEndOffset = fOffset; |
|
2894 |
return -1; |
|
2895 |
} |
|
2896 |
b[off] = (byte)returnedVal; |
|
2897 |
return 1; |
|
2898 |
||
2899 |
} |
|
2900 |
if (len < bytesLeft) { |
|
2901 |
if (len <= 0) { |
|
2902 |
return 0; |
|
2903 |
} |
|
2904 |
} else { |
|
2905 |
len = bytesLeft; |
|
2906 |
} |
|
2907 |
if (b != null) { |
|
2908 |
System.arraycopy(fData, fOffset, b, off, len); |
|
2909 |
} |
|
2910 |
fOffset += len; |
|
2911 |
return len; |
|
2912 |
} |
|
2913 |
||
2914 |
public long skip(long n) |
|
2915 |
throws IOException { |
|
2916 |
int bytesLeft; |
|
2917 |
if (n <= 0) { |
|
2918 |
return 0; |
|
2919 |
} |
|
2920 |
bytesLeft = fLength - fOffset; |
|
2921 |
if (bytesLeft == 0) { |
|
2922 |
if (fOffset == fEndOffset) { |
|
2923 |
return 0; |
|
2924 |
} |
|
2925 |
return fInputStream.skip(n); |
|
2926 |
} |
|
2927 |
if (n <= bytesLeft) { |
|
2928 |
fOffset += n; |
|
2929 |
return n; |
|
2930 |
} |
|
2931 |
fOffset += bytesLeft; |
|
2932 |
if (fOffset == fEndOffset) { |
|
2933 |
return bytesLeft; |
|
2934 |
} |
|
2935 |
n -= bytesLeft; |
|
2936 |
/* |
|
2937 |
* In a manner of speaking, when this class isn't permitting more |
|
2938 |
* than one byte at a time to be read, it is "blocking". The |
|
2939 |
* available() method should indicate how much can be read without |
|
2940 |
* blocking, so while we're in this mode, it should only indicate |
|
2941 |
* that bytes in its buffer are available; otherwise, the result of |
|
2942 |
* available() on the underlying InputStream is appropriate. |
|
2943 |
*/ |
|
2944 |
return fInputStream.skip(n) + bytesLeft; |
|
2945 |
} |
|
2946 |
||
2947 |
public int available() throws IOException { |
|
2948 |
int bytesLeft = fLength - fOffset; |
|
2949 |
if (bytesLeft == 0) { |
|
2950 |
if (fOffset == fEndOffset) { |
|
2951 |
return -1; |
|
2952 |
} |
|
2953 |
return fCurrentEntity.mayReadChunks ? fInputStream.available() |
|
2954 |
: 0; |
|
2955 |
} |
|
2956 |
return bytesLeft; |
|
2957 |
} |
|
2958 |
||
2959 |
public void mark(int howMuch) { |
|
2960 |
fMark = fOffset; |
|
2961 |
} |
|
2962 |
||
2963 |
public void reset() { |
|
2964 |
fOffset = fMark; |
|
2965 |
//test(); |
|
2966 |
} |
|
2967 |
||
2968 |
public boolean markSupported() { |
|
2969 |
return true; |
|
2970 |
} |
|
2971 |
||
2972 |
public void close() throws IOException { |
|
2973 |
if (fInputStream != null) { |
|
2974 |
fInputStream.close(); |
|
2975 |
fInputStream = null; |
|
2976 |
} |
|
2977 |
} |
|
2978 |
} // end of RewindableInputStream class |
|
2979 |
||
2980 |
public void test(){ |
|
2981 |
//System.out.println("TESTING: Added familytree to entityManager"); |
|
2982 |
//Usecase1 |
|
2983 |
fEntityStorage.addExternalEntity("entityUsecase1",null, |
|
2984 |
"/space/home/stax/sun/6thJan2004/zephyr/data/test.txt", |
|
2985 |
"/space/home/stax/sun/6thJan2004/zephyr/data/entity.xml"); |
|
2986 |
||
2987 |
//Usecase2 |
|
2988 |
fEntityStorage.addInternalEntity("entityUsecase2","<Test>value</Test>"); |
|
2989 |
fEntityStorage.addInternalEntity("entityUsecase3","value3"); |
|
2990 |
fEntityStorage.addInternalEntity("text", "Hello World."); |
|
2991 |
fEntityStorage.addInternalEntity("empty-element", "<foo/>"); |
|
2992 |
fEntityStorage.addInternalEntity("balanced-element", "<foo></foo>"); |
|
2993 |
fEntityStorage.addInternalEntity("balanced-element-with-text", "<foo>Hello, World</foo>"); |
|
2994 |
fEntityStorage.addInternalEntity("balanced-element-with-entity", "<foo>&text;</foo>"); |
|
2995 |
fEntityStorage.addInternalEntity("unbalanced-entity", "<foo>"); |
|
2996 |
fEntityStorage.addInternalEntity("recursive-entity", "<foo>&recursive-entity2;</foo>"); |
|
2997 |
fEntityStorage.addInternalEntity("recursive-entity2", "<bar>&recursive-entity3;</bar>"); |
|
2998 |
fEntityStorage.addInternalEntity("recursive-entity3", "<baz>&recursive-entity;</baz>"); |
|
2999 |
fEntityStorage.addInternalEntity("ch","©"); |
|
3000 |
fEntityStorage.addInternalEntity("ch1","T"); |
|
3001 |
fEntityStorage.addInternalEntity("% ch2","param"); |
|
3002 |
} |
|
3003 |
||
3004 |
} // class XMLEntityManager |