12005
|
1 |
/*
|
|
2 |
* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package com.sun.xml.internal.stream;
|
|
27 |
|
|
28 |
import java.util.Hashtable;
|
|
29 |
|
|
30 |
import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter;
|
|
31 |
import com.sun.org.apache.xerces.internal.util.URI;
|
|
32 |
import com.sun.org.apache.xerces.internal.util.XMLResourceIdentifierImpl;
|
|
33 |
import com.sun.org.apache.xerces.internal.xni.parser.XMLComponentManager;
|
|
34 |
import com.sun.org.apache.xerces.internal.xni.parser.XMLConfigurationException;
|
|
35 |
import com.sun.org.apache.xerces.internal.impl.XMLEntityManager;
|
|
36 |
import com.sun.org.apache.xerces.internal.impl.PropertyManager;
|
|
37 |
import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter;
|
|
38 |
import com.sun.org.apache.xerces.internal.impl.Constants;
|
12458
|
39 |
import java.util.Enumeration;
|
12005
|
40 |
|
|
41 |
/**
|
|
42 |
*
|
|
43 |
* @author K.Venugopal SUN Microsystems
|
|
44 |
* @author Neeraj Bajaj SUN Microsystems
|
|
45 |
* @author Andy Clark, IBM
|
|
46 |
*
|
|
47 |
*/
|
|
48 |
public class XMLEntityStorage {
|
|
49 |
|
|
50 |
/** Property identifier: error reporter. */
|
|
51 |
protected static final String ERROR_REPORTER =
|
|
52 |
Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY;
|
|
53 |
|
|
54 |
/** Feature identifier: warn on duplicate EntityDef */
|
|
55 |
protected static final String WARN_ON_DUPLICATE_ENTITYDEF =
|
|
56 |
Constants.XERCES_FEATURE_PREFIX +Constants.WARN_ON_DUPLICATE_ENTITYDEF_FEATURE;
|
|
57 |
|
|
58 |
/** warn on duplicate Entity declaration.
|
|
59 |
* http://apache.org/xml/features/warn-on-duplicate-entitydef
|
|
60 |
*/
|
|
61 |
protected boolean fWarnDuplicateEntityDef;
|
|
62 |
|
|
63 |
/** Entities. */
|
|
64 |
protected Hashtable fEntities = new Hashtable();
|
|
65 |
|
|
66 |
protected Entity.ScannedEntity fCurrentEntity ;
|
|
67 |
|
|
68 |
private XMLEntityManager fEntityManager;
|
|
69 |
/**
|
|
70 |
* Error reporter. This property identifier is:
|
|
71 |
* http://apache.org/xml/properties/internal/error-reporter
|
|
72 |
*/
|
|
73 |
protected XMLErrorReporter fErrorReporter;
|
|
74 |
protected PropertyManager fPropertyManager ;
|
|
75 |
|
|
76 |
/* To keep track whether an entity is declared in external or internal subset*/
|
|
77 |
protected boolean fInExternalSubset = false;
|
|
78 |
|
|
79 |
/** Creates a new instance of XMLEntityStorage */
|
|
80 |
public XMLEntityStorage(PropertyManager propertyManager) {
|
|
81 |
fPropertyManager = propertyManager ;
|
|
82 |
}
|
|
83 |
|
|
84 |
/** Creates a new instance of XMLEntityStorage */
|
|
85 |
/*public XMLEntityStorage(Entity.ScannedEntity currentEntity) {
|
|
86 |
fCurrentEntity = currentEntity ;*/
|
|
87 |
public XMLEntityStorage(XMLEntityManager entityManager) {
|
|
88 |
fEntityManager = entityManager;
|
|
89 |
}
|
|
90 |
|
|
91 |
public void reset(PropertyManager propertyManager){
|
|
92 |
|
|
93 |
fErrorReporter = (XMLErrorReporter)propertyManager.getProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY);
|
|
94 |
fEntities.clear();
|
|
95 |
fCurrentEntity = null;
|
|
96 |
|
|
97 |
}
|
|
98 |
|
|
99 |
public void reset(){
|
|
100 |
fEntities.clear();
|
|
101 |
fCurrentEntity = null;
|
|
102 |
}
|
|
103 |
/**
|
|
104 |
* Resets the component. The component can query the component manager
|
|
105 |
* about any features and properties that affect the operation of the
|
|
106 |
* component.
|
|
107 |
*
|
|
108 |
* @param componentManager The component manager.
|
|
109 |
*
|
|
110 |
* @throws SAXException Thrown by component on initialization error.
|
|
111 |
* For example, if a feature or property is
|
|
112 |
* required for the operation of the component, the
|
|
113 |
* component manager may throw a
|
|
114 |
* SAXNotRecognizedException or a
|
|
115 |
* SAXNotSupportedException.
|
|
116 |
*/
|
|
117 |
public void reset(XMLComponentManager componentManager)
|
|
118 |
throws XMLConfigurationException {
|
|
119 |
|
|
120 |
|
|
121 |
// xerces features
|
|
122 |
|
|
123 |
fWarnDuplicateEntityDef = componentManager.getFeature(WARN_ON_DUPLICATE_ENTITYDEF, false);
|
|
124 |
|
|
125 |
fErrorReporter = (XMLErrorReporter)componentManager.getProperty(ERROR_REPORTER);
|
|
126 |
|
|
127 |
fEntities.clear();
|
|
128 |
fCurrentEntity = null;
|
|
129 |
|
|
130 |
} // reset(XMLComponentManager)
|
|
131 |
|
|
132 |
/**
|
12458
|
133 |
* Returns entity declaration.
|
|
134 |
*
|
|
135 |
* @param name The name of the entity.
|
|
136 |
*
|
|
137 |
* @see SymbolTable
|
12005
|
138 |
*/
|
12458
|
139 |
public Entity getEntity(String name) {
|
|
140 |
return (Entity)fEntities.get(name);
|
|
141 |
} // getEntity(String)
|
|
142 |
|
|
143 |
public boolean hasEntities() {
|
|
144 |
return (fEntities!=null);
|
|
145 |
} // getEntity(String)
|
12005
|
146 |
|
12458
|
147 |
public int getEntitySize() {
|
|
148 |
return fEntities.size();
|
|
149 |
} // getEntity(String)
|
|
150 |
|
|
151 |
public Enumeration getEntityKeys() {
|
|
152 |
return fEntities.keys();
|
|
153 |
}
|
12005
|
154 |
/**
|
|
155 |
* Adds an internal entity declaration.
|
|
156 |
* <p>
|
|
157 |
* <strong>Note:</strong> This method ignores subsequent entity
|
|
158 |
* declarations.
|
|
159 |
* <p>
|
|
160 |
* <strong>Note:</strong> The name should be a unique symbol. The
|
|
161 |
* SymbolTable can be used for this purpose.
|
|
162 |
*
|
|
163 |
* @param name The name of the entity.
|
|
164 |
* @param text The text of the entity.
|
|
165 |
*
|
|
166 |
* @see SymbolTable
|
|
167 |
*/
|
|
168 |
public void addInternalEntity(String name, String text) {
|
|
169 |
if (!fEntities.containsKey(name)) {
|
|
170 |
Entity entity = new Entity.InternalEntity(name, text, fInExternalSubset);
|
|
171 |
fEntities.put(name, entity);
|
|
172 |
}
|
|
173 |
else{
|
|
174 |
if(fWarnDuplicateEntityDef){
|
|
175 |
fErrorReporter.reportError(XMLMessageFormatter.XML_DOMAIN,
|
|
176 |
"MSG_DUPLICATE_ENTITY_DEFINITION",
|
|
177 |
new Object[]{ name },
|
|
178 |
XMLErrorReporter.SEVERITY_WARNING );
|
|
179 |
}
|
|
180 |
}
|
|
181 |
} // addInternalEntity(String,String)
|
|
182 |
|
|
183 |
/**
|
|
184 |
* Adds an external entity declaration.
|
|
185 |
* <p>
|
|
186 |
* <strong>Note:</strong> This method ignores subsequent entity
|
|
187 |
* declarations.
|
|
188 |
* <p>
|
|
189 |
* <strong>Note:</strong> The name should be a unique symbol. The
|
|
190 |
* SymbolTable can be used for this purpose.
|
|
191 |
*
|
|
192 |
* @param name The name of the entity.
|
|
193 |
* @param publicId The public identifier of the entity.
|
|
194 |
* @param literalSystemId The system identifier of the entity.
|
|
195 |
* @param baseSystemId The base system identifier of the entity.
|
|
196 |
* This is the system identifier of the entity
|
|
197 |
* where <em>the entity being added</em> and
|
|
198 |
* is used to expand the system identifier when
|
|
199 |
* the system identifier is a relative URI.
|
|
200 |
* When null the system identifier of the first
|
|
201 |
* external entity on the stack is used instead.
|
|
202 |
*
|
|
203 |
* @see SymbolTable
|
|
204 |
*/
|
|
205 |
public void addExternalEntity(String name,
|
|
206 |
String publicId, String literalSystemId,
|
|
207 |
String baseSystemId) {
|
|
208 |
if (!fEntities.containsKey(name)) {
|
|
209 |
if (baseSystemId == null) {
|
|
210 |
// search for the first external entity on the stack
|
|
211 |
//xxx commenting the 'size' variable..
|
|
212 |
/**
|
|
213 |
* int size = fEntityStack.size();
|
|
214 |
* if (size == 0 && fCurrentEntity != null && fCurrentEntity.entityLocation != null) {
|
|
215 |
* baseSystemId = fCurrentEntity.entityLocation.getExpandedSystemId();
|
|
216 |
* }
|
|
217 |
*/
|
|
218 |
|
|
219 |
//xxx we need to have information about the current entity.
|
|
220 |
if (fCurrentEntity != null && fCurrentEntity.entityLocation != null) {
|
|
221 |
baseSystemId = fCurrentEntity.entityLocation.getExpandedSystemId();
|
|
222 |
}
|
|
223 |
/**
|
|
224 |
* for (int i = size - 1; i >= 0 ; i--) {
|
|
225 |
* ScannedEntity externalEntity =
|
|
226 |
* (ScannedEntity)fEntityStack.elementAt(i);
|
|
227 |
* if (externalEntity.entityLocation != null && externalEntity.entityLocation.getExpandedSystemId() != null) {
|
|
228 |
* baseSystemId = externalEntity.entityLocation.getExpandedSystemId();
|
|
229 |
* break;
|
|
230 |
* }
|
|
231 |
* }
|
|
232 |
*/
|
|
233 |
}
|
|
234 |
|
|
235 |
fCurrentEntity = fEntityManager.getCurrentEntity();
|
|
236 |
Entity entity = new Entity.ExternalEntity(name,
|
|
237 |
new XMLResourceIdentifierImpl(publicId, literalSystemId,
|
|
238 |
baseSystemId, expandSystemId(literalSystemId, baseSystemId)),
|
|
239 |
null, fInExternalSubset);
|
|
240 |
//TODO :: Forced to pass true above remove it.
|
|
241 |
//(fCurrentEntity == null) ? fasle : fCurrentEntity.isEntityDeclInExternalSubset());
|
|
242 |
// null, fCurrentEntity.isEntityDeclInExternalSubset());
|
|
243 |
fEntities.put(name, entity);
|
|
244 |
}
|
|
245 |
else{
|
|
246 |
if(fWarnDuplicateEntityDef){
|
|
247 |
fErrorReporter.reportError(XMLMessageFormatter.XML_DOMAIN,
|
|
248 |
"MSG_DUPLICATE_ENTITY_DEFINITION",
|
|
249 |
new Object[]{ name },
|
|
250 |
XMLErrorReporter.SEVERITY_WARNING );
|
|
251 |
}
|
|
252 |
}
|
|
253 |
|
|
254 |
} // addExternalEntity(String,String,String,String)
|
|
255 |
|
|
256 |
/**
|
|
257 |
* Checks whether an entity given by name is external.
|
|
258 |
*
|
|
259 |
* @param entityName The name of the entity to check.
|
|
260 |
* @returns True if the entity is external, false otherwise
|
|
261 |
* (including when the entity is not declared).
|
|
262 |
*/
|
|
263 |
public boolean isExternalEntity(String entityName) {
|
|
264 |
|
|
265 |
Entity entity = (Entity)fEntities.get(entityName);
|
|
266 |
if (entity == null) {
|
|
267 |
return false;
|
|
268 |
}
|
|
269 |
return entity.isExternal();
|
|
270 |
}
|
|
271 |
|
|
272 |
/**
|
|
273 |
* Checks whether the declaration of an entity given by name is
|
|
274 |
* // in the external subset.
|
|
275 |
*
|
|
276 |
* @param entityName The name of the entity to check.
|
|
277 |
* @returns True if the entity was declared in the external subset, false otherwise
|
|
278 |
* (including when the entity is not declared).
|
|
279 |
*/
|
|
280 |
public boolean isEntityDeclInExternalSubset(String entityName) {
|
|
281 |
|
|
282 |
Entity entity = (Entity)fEntities.get(entityName);
|
|
283 |
if (entity == null) {
|
|
284 |
return false;
|
|
285 |
}
|
|
286 |
return entity.isEntityDeclInExternalSubset();
|
|
287 |
}
|
|
288 |
|
|
289 |
/**
|
|
290 |
* Adds an unparsed entity declaration.
|
|
291 |
* <p>
|
|
292 |
* <strong>Note:</strong> This method ignores subsequent entity
|
|
293 |
* declarations.
|
|
294 |
* <p>
|
|
295 |
* <strong>Note:</strong> The name should be a unique symbol. The
|
|
296 |
* SymbolTable can be used for this purpose.
|
|
297 |
*
|
|
298 |
* @param name The name of the entity.
|
|
299 |
* @param publicId The public identifier of the entity.
|
|
300 |
* @param systemId The system identifier of the entity.
|
|
301 |
* @param notation The name of the notation.
|
|
302 |
*
|
|
303 |
* @see SymbolTable
|
|
304 |
*/
|
|
305 |
public void addUnparsedEntity(String name,
|
|
306 |
String publicId, String systemId,
|
|
307 |
String baseSystemId, String notation) {
|
|
308 |
|
|
309 |
fCurrentEntity = fEntityManager.getCurrentEntity();
|
|
310 |
if (!fEntities.containsKey(name)) {
|
|
311 |
Entity entity = new Entity.ExternalEntity(name, new XMLResourceIdentifierImpl(publicId, systemId, baseSystemId, null), notation, fInExternalSubset);
|
|
312 |
// (fCurrentEntity == null) ? fasle : fCurrentEntity.isEntityDeclInExternalSubset());
|
|
313 |
// fCurrentEntity.isEntityDeclInExternalSubset());
|
|
314 |
fEntities.put(name, entity);
|
|
315 |
}
|
|
316 |
else{
|
|
317 |
if(fWarnDuplicateEntityDef){
|
|
318 |
fErrorReporter.reportError(XMLMessageFormatter.XML_DOMAIN,
|
|
319 |
"MSG_DUPLICATE_ENTITY_DEFINITION",
|
|
320 |
new Object[]{ name },
|
|
321 |
XMLErrorReporter.SEVERITY_WARNING );
|
|
322 |
}
|
|
323 |
}
|
|
324 |
} // addUnparsedEntity(String,String,String,String)
|
|
325 |
|
|
326 |
/**
|
|
327 |
* Checks whether an entity given by name is unparsed.
|
|
328 |
*
|
|
329 |
* @param entityName The name of the entity to check.
|
|
330 |
* @returns True if the entity is unparsed, false otherwise
|
|
331 |
* (including when the entity is not declared).
|
|
332 |
*/
|
|
333 |
public boolean isUnparsedEntity(String entityName) {
|
|
334 |
|
|
335 |
Entity entity = (Entity)fEntities.get(entityName);
|
|
336 |
if (entity == null) {
|
|
337 |
return false;
|
|
338 |
}
|
|
339 |
return entity.isUnparsed();
|
|
340 |
}
|
|
341 |
|
|
342 |
/**
|
|
343 |
* Checks whether an entity given by name is declared.
|
|
344 |
*
|
|
345 |
* @param entityName The name of the entity to check.
|
|
346 |
* @returns True if the entity is declared, false otherwise.
|
|
347 |
*/
|
|
348 |
public boolean isDeclaredEntity(String entityName) {
|
|
349 |
|
|
350 |
Entity entity = (Entity)fEntities.get(entityName);
|
|
351 |
return entity != null;
|
|
352 |
}
|
|
353 |
/**
|
|
354 |
* Expands a system id and returns the system id as a URI, if
|
|
355 |
* it can be expanded. A return value of null means that the
|
|
356 |
* identifier is already expanded. An exception thrown
|
|
357 |
* indicates a failure to expand the id.
|
|
358 |
*
|
|
359 |
* @param systemId The systemId to be expanded.
|
|
360 |
*
|
|
361 |
* @return Returns the URI string representing the expanded system
|
|
362 |
* identifier. A null value indicates that the given
|
|
363 |
* system identifier is already expanded.
|
|
364 |
*
|
|
365 |
*/
|
|
366 |
public static String expandSystemId(String systemId) {
|
|
367 |
return expandSystemId(systemId, null);
|
|
368 |
} // expandSystemId(String):String
|
|
369 |
|
|
370 |
// current value of the "user.dir" property
|
|
371 |
private static String gUserDir;
|
|
372 |
// escaped value of the current "user.dir" property
|
|
373 |
private static String gEscapedUserDir;
|
|
374 |
// which ASCII characters need to be escaped
|
|
375 |
private static boolean gNeedEscaping[] = new boolean[128];
|
|
376 |
// the first hex character if a character needs to be escaped
|
|
377 |
private static char gAfterEscaping1[] = new char[128];
|
|
378 |
// the second hex character if a character needs to be escaped
|
|
379 |
private static char gAfterEscaping2[] = new char[128];
|
|
380 |
private static char[] gHexChs = {'0', '1', '2', '3', '4', '5', '6', '7',
|
|
381 |
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
|
|
382 |
// initialize the above 3 arrays
|
|
383 |
static {
|
|
384 |
for (int i = 0; i <= 0x1f; i++) {
|
|
385 |
gNeedEscaping[i] = true;
|
|
386 |
gAfterEscaping1[i] = gHexChs[i >> 4];
|
|
387 |
gAfterEscaping2[i] = gHexChs[i & 0xf];
|
|
388 |
}
|
|
389 |
gNeedEscaping[0x7f] = true;
|
|
390 |
gAfterEscaping1[0x7f] = '7';
|
|
391 |
gAfterEscaping2[0x7f] = 'F';
|
|
392 |
char[] escChs = {' ', '<', '>', '#', '%', '"', '{', '}',
|
|
393 |
'|', '\\', '^', '~', '[', ']', '`'};
|
|
394 |
int len = escChs.length;
|
|
395 |
char ch;
|
|
396 |
for (int i = 0; i < len; i++) {
|
|
397 |
ch = escChs[i];
|
|
398 |
gNeedEscaping[ch] = true;
|
|
399 |
gAfterEscaping1[ch] = gHexChs[ch >> 4];
|
|
400 |
gAfterEscaping2[ch] = gHexChs[ch & 0xf];
|
|
401 |
}
|
|
402 |
}
|
|
403 |
// To escape the "user.dir" system property, by using %HH to represent
|
|
404 |
// special ASCII characters: 0x00~0x1F, 0x7F, ' ', '<', '>', '#', '%'
|
|
405 |
// and '"'. It's a static method, so needs to be synchronized.
|
|
406 |
// this method looks heavy, but since the system property isn't expected
|
|
407 |
// to change often, so in most cases, we only need to return the string
|
|
408 |
// that was escaped before.
|
|
409 |
// According to the URI spec, non-ASCII characters (whose value >= 128)
|
|
410 |
// need to be escaped too.
|
|
411 |
// REVISIT: don't know how to escape non-ASCII characters, especially
|
|
412 |
// which encoding to use. Leave them for now.
|
|
413 |
private static synchronized String getUserDir() {
|
|
414 |
// get the user.dir property
|
|
415 |
String userDir = "";
|
|
416 |
try {
|
|
417 |
userDir = System.getProperty("user.dir");
|
|
418 |
}
|
|
419 |
catch (SecurityException se) {
|
|
420 |
}
|
|
421 |
|
|
422 |
// return empty string if property value is empty string.
|
|
423 |
if (userDir.length() == 0)
|
|
424 |
return "";
|
|
425 |
|
|
426 |
// compute the new escaped value if the new property value doesn't
|
|
427 |
// match the previous one
|
|
428 |
if (userDir.equals(gUserDir)) {
|
|
429 |
return gEscapedUserDir;
|
|
430 |
}
|
|
431 |
|
|
432 |
// record the new value as the global property value
|
|
433 |
gUserDir = userDir;
|
|
434 |
|
|
435 |
char separator = java.io.File.separatorChar;
|
|
436 |
userDir = userDir.replace(separator, '/');
|
|
437 |
|
|
438 |
int len = userDir.length(), ch;
|
|
439 |
StringBuffer buffer = new StringBuffer(len*3);
|
|
440 |
// change C:/blah to /C:/blah
|
|
441 |
if (len >= 2 && userDir.charAt(1) == ':') {
|
|
442 |
ch = Character.toUpperCase(userDir.charAt(0));
|
|
443 |
if (ch >= 'A' && ch <= 'Z') {
|
|
444 |
buffer.append('/');
|
|
445 |
}
|
|
446 |
}
|
|
447 |
|
|
448 |
// for each character in the path
|
|
449 |
int i = 0;
|
|
450 |
for (; i < len; i++) {
|
|
451 |
ch = userDir.charAt(i);
|
|
452 |
// if it's not an ASCII character, break here, and use UTF-8 encoding
|
|
453 |
if (ch >= 128)
|
|
454 |
break;
|
|
455 |
if (gNeedEscaping[ch]) {
|
|
456 |
buffer.append('%');
|
|
457 |
buffer.append(gAfterEscaping1[ch]);
|
|
458 |
buffer.append(gAfterEscaping2[ch]);
|
|
459 |
// record the fact that it's escaped
|
|
460 |
}
|
|
461 |
else {
|
|
462 |
buffer.append((char)ch);
|
|
463 |
}
|
|
464 |
}
|
|
465 |
|
|
466 |
// we saw some non-ascii character
|
|
467 |
if (i < len) {
|
|
468 |
// get UTF-8 bytes for the remaining sub-string
|
|
469 |
byte[] bytes = null;
|
|
470 |
byte b;
|
|
471 |
try {
|
|
472 |
bytes = userDir.substring(i).getBytes("UTF-8");
|
|
473 |
} catch (java.io.UnsupportedEncodingException e) {
|
|
474 |
// should never happen
|
|
475 |
return userDir;
|
|
476 |
}
|
|
477 |
len = bytes.length;
|
|
478 |
|
|
479 |
// for each byte
|
|
480 |
for (i = 0; i < len; i++) {
|
|
481 |
b = bytes[i];
|
|
482 |
// for non-ascii character: make it positive, then escape
|
|
483 |
if (b < 0) {
|
|
484 |
ch = b + 256;
|
|
485 |
buffer.append('%');
|
|
486 |
buffer.append(gHexChs[ch >> 4]);
|
|
487 |
buffer.append(gHexChs[ch & 0xf]);
|
|
488 |
}
|
|
489 |
else if (gNeedEscaping[b]) {
|
|
490 |
buffer.append('%');
|
|
491 |
buffer.append(gAfterEscaping1[b]);
|
|
492 |
buffer.append(gAfterEscaping2[b]);
|
|
493 |
}
|
|
494 |
else {
|
|
495 |
buffer.append((char)b);
|
|
496 |
}
|
|
497 |
}
|
|
498 |
}
|
|
499 |
|
|
500 |
// change blah/blah to blah/blah/
|
|
501 |
if (!userDir.endsWith("/"))
|
|
502 |
buffer.append('/');
|
|
503 |
|
|
504 |
gEscapedUserDir = buffer.toString();
|
|
505 |
|
|
506 |
return gEscapedUserDir;
|
|
507 |
}
|
|
508 |
|
|
509 |
/**
|
|
510 |
* Expands a system id and returns the system id as a URI, if
|
|
511 |
* it can be expanded. A return value of null means that the
|
|
512 |
* identifier is already expanded. An exception thrown
|
|
513 |
* indicates a failure to expand the id.
|
|
514 |
*
|
|
515 |
* @param systemId The systemId to be expanded.
|
|
516 |
*
|
|
517 |
* @return Returns the URI string representing the expanded system
|
|
518 |
* identifier. A null value indicates that the given
|
|
519 |
* system identifier is already expanded.
|
|
520 |
*
|
|
521 |
*/
|
|
522 |
public static String expandSystemId(String systemId, String baseSystemId) {
|
|
523 |
|
|
524 |
// check for bad parameters id
|
|
525 |
if (systemId == null || systemId.length() == 0) {
|
|
526 |
return systemId;
|
|
527 |
}
|
|
528 |
// if id already expanded, return
|
|
529 |
try {
|
|
530 |
new URI(systemId);
|
|
531 |
return systemId;
|
|
532 |
} catch (URI.MalformedURIException e) {
|
|
533 |
// continue on...
|
|
534 |
}
|
|
535 |
// normalize id
|
|
536 |
String id = fixURI(systemId);
|
|
537 |
|
|
538 |
// normalize base
|
|
539 |
URI base = null;
|
|
540 |
URI uri = null;
|
|
541 |
try {
|
|
542 |
if (baseSystemId == null || baseSystemId.length() == 0 ||
|
|
543 |
baseSystemId.equals(systemId)) {
|
|
544 |
String dir = getUserDir();
|
|
545 |
base = new URI("file", "", dir, null, null);
|
|
546 |
}
|
|
547 |
else {
|
|
548 |
try {
|
|
549 |
base = new URI(fixURI(baseSystemId));
|
|
550 |
}
|
|
551 |
catch (URI.MalformedURIException e) {
|
|
552 |
if (baseSystemId.indexOf(':') != -1) {
|
|
553 |
// for xml schemas we might have baseURI with
|
|
554 |
// a specified drive
|
|
555 |
base = new URI("file", "", fixURI(baseSystemId), null, null);
|
|
556 |
}
|
|
557 |
else {
|
|
558 |
String dir = getUserDir();
|
|
559 |
dir = dir + fixURI(baseSystemId);
|
|
560 |
base = new URI("file", "", dir, null, null);
|
|
561 |
}
|
|
562 |
}
|
|
563 |
}
|
|
564 |
// expand id
|
|
565 |
uri = new URI(base, id);
|
|
566 |
}
|
|
567 |
catch (Exception e) {
|
|
568 |
// let it go through
|
|
569 |
|
|
570 |
}
|
|
571 |
|
|
572 |
if (uri == null) {
|
|
573 |
return systemId;
|
|
574 |
}
|
|
575 |
return uri.toString();
|
|
576 |
|
|
577 |
} // expandSystemId(String,String):String
|
|
578 |
//
|
|
579 |
// Protected static methods
|
|
580 |
//
|
|
581 |
|
|
582 |
/**
|
|
583 |
* Fixes a platform dependent filename to standard URI form.
|
|
584 |
*
|
|
585 |
* @param str The string to fix.
|
|
586 |
*
|
|
587 |
* @return Returns the fixed URI string.
|
|
588 |
*/
|
|
589 |
protected static String fixURI(String str) {
|
|
590 |
|
|
591 |
// handle platform dependent strings
|
|
592 |
str = str.replace(java.io.File.separatorChar, '/');
|
|
593 |
|
|
594 |
// Windows fix
|
|
595 |
if (str.length() >= 2) {
|
|
596 |
char ch1 = str.charAt(1);
|
|
597 |
// change "C:blah" to "/C:blah"
|
|
598 |
if (ch1 == ':') {
|
|
599 |
char ch0 = Character.toUpperCase(str.charAt(0));
|
|
600 |
if (ch0 >= 'A' && ch0 <= 'Z') {
|
|
601 |
str = "/" + str;
|
|
602 |
}
|
|
603 |
}
|
|
604 |
// change "//blah" to "file://blah"
|
|
605 |
else if (ch1 == '/' && str.charAt(0) == '/') {
|
|
606 |
str = "file:" + str;
|
|
607 |
}
|
|
608 |
}
|
|
609 |
|
|
610 |
// done
|
|
611 |
return str;
|
|
612 |
|
|
613 |
} // fixURI(String):String
|
|
614 |
|
|
615 |
// indicate start of external subset
|
|
616 |
public void startExternalSubset() {
|
|
617 |
fInExternalSubset = true;
|
|
618 |
}
|
|
619 |
|
|
620 |
public void endExternalSubset() {
|
|
621 |
fInExternalSubset = false;
|
|
622 |
}
|
|
623 |
}
|