33542
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, 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 |
package javax.xml.catalog;
|
|
26 |
|
|
27 |
import java.net.URI;
|
|
28 |
import java.nio.file.Files;
|
|
29 |
import java.nio.file.Paths;
|
|
30 |
import java.util.ArrayList;
|
|
31 |
import java.util.HashMap;
|
|
32 |
import java.util.List;
|
|
33 |
import java.util.Map;
|
|
34 |
|
|
35 |
/**
|
|
36 |
* Represents a group entry.
|
|
37 |
*
|
|
38 |
* @since 9
|
|
39 |
*/
|
|
40 |
class GroupEntry extends BaseEntry {
|
|
41 |
static final int ATTRIBUTE_PREFER = 0;
|
|
42 |
static final int ATTRIBUTE_DEFFER = 1;
|
|
43 |
static final int ATTRIBUTE_RESOLUTION = 2;
|
|
44 |
|
|
45 |
//Unmodifiable features when the Catalog is created
|
|
46 |
CatalogFeatures features;
|
|
47 |
|
|
48 |
//Value of the prefer attribute
|
|
49 |
boolean isPreferPublic = true;
|
|
50 |
|
|
51 |
//The catalog instance this group belongs to
|
|
52 |
CatalogImpl catalog;
|
|
53 |
|
|
54 |
//A list of all entries in a catalog or group
|
|
55 |
List<BaseEntry> entries = new ArrayList<>();
|
|
56 |
|
|
57 |
//loaded delegated catalog by system id
|
|
58 |
Map<String, Catalog> delegateCatalogs = new HashMap<>();
|
|
59 |
|
|
60 |
//A list of all loaded Catalogs, including this, and next catalogs
|
|
61 |
Map<String, Catalog> loadedCatalogs = new HashMap<>();
|
|
62 |
|
|
63 |
/*
|
|
64 |
A list of Catalog Ids that have already been searched in a matching
|
|
65 |
operation. Check this list before constructing new Catalog to avoid circular
|
|
66 |
reference.
|
|
67 |
*/
|
|
68 |
List<String> catalogsSearched = new ArrayList<>();
|
|
69 |
|
|
70 |
//A flag to indicate whether the current match is a system or uri
|
|
71 |
boolean isInstantMatch = false;
|
|
72 |
|
|
73 |
//A match of a rewrite type
|
|
74 |
String rewriteMatch = null;
|
|
75 |
|
|
76 |
//The length of the longest match of a rewrite type
|
|
77 |
int longestRewriteMatch = 0;
|
|
78 |
|
|
79 |
//A match of a suffix type
|
|
80 |
String suffixMatch = null;
|
|
81 |
|
|
82 |
//The length of the longest match of a suffix type
|
|
83 |
int longestSuffixMatch = 0;
|
|
84 |
|
|
85 |
/**
|
|
86 |
* PreferType represents possible values of the prefer property
|
|
87 |
*/
|
|
88 |
public static enum PreferType {
|
|
89 |
PUBLIC("public"),
|
|
90 |
SYSTEM("system");
|
|
91 |
|
|
92 |
final String literal;
|
|
93 |
|
|
94 |
PreferType(String literal) {
|
|
95 |
this.literal = literal;
|
|
96 |
}
|
|
97 |
|
|
98 |
public boolean prefer(String prefer) {
|
|
99 |
return literal.equals(prefer);
|
|
100 |
}
|
|
101 |
}
|
|
102 |
|
|
103 |
/**
|
|
104 |
* PreferType represents possible values of the resolve property
|
|
105 |
*/
|
|
106 |
public static enum ResolveType {
|
|
107 |
STRICT(CatalogFeatures.RESOLVE_STRICT),
|
|
108 |
CONTINUE(CatalogFeatures.RESOLVE_CONTINUE),
|
|
109 |
IGNORE(CatalogFeatures.RESOLVE_IGNORE);
|
|
110 |
|
|
111 |
final String literal;
|
|
112 |
|
|
113 |
ResolveType(String literal) {
|
|
114 |
this.literal = literal;
|
|
115 |
}
|
|
116 |
|
|
117 |
static public ResolveType getType(String resolveType) {
|
|
118 |
for (ResolveType type : ResolveType.values()) {
|
|
119 |
if (type.isType(resolveType)) {
|
|
120 |
return type;
|
|
121 |
}
|
|
122 |
}
|
|
123 |
return null;
|
|
124 |
}
|
|
125 |
|
|
126 |
public boolean isType(String type) {
|
|
127 |
return literal.equals(type);
|
|
128 |
}
|
|
129 |
}
|
|
130 |
|
|
131 |
/**
|
|
132 |
* Constructs a GroupEntry
|
|
133 |
*
|
|
134 |
* @param type The type of the entry
|
|
135 |
*/
|
|
136 |
public GroupEntry(CatalogEntryType type) {
|
|
137 |
super(type);
|
|
138 |
}
|
|
139 |
|
|
140 |
/**
|
|
141 |
* Constructs a group entry.
|
|
142 |
*
|
|
143 |
* @param base The baseURI attribute
|
|
144 |
* @param attributes The attributes
|
|
145 |
*/
|
|
146 |
public GroupEntry(String base, String... attributes) {
|
|
147 |
this(null, base, attributes);
|
|
148 |
}
|
|
149 |
|
|
150 |
/**
|
|
151 |
* Resets the group entry to its initial state.
|
|
152 |
*/
|
|
153 |
public void reset() {
|
|
154 |
isInstantMatch = false;
|
|
155 |
rewriteMatch = null;
|
|
156 |
longestRewriteMatch = 0;
|
|
157 |
suffixMatch = null;
|
|
158 |
longestSuffixMatch = 0;
|
|
159 |
}
|
|
160 |
/**
|
|
161 |
* Constructs a group entry.
|
|
162 |
* @param catalog The parent catalog
|
|
163 |
* @param base The baseURI attribute
|
|
164 |
* @param attributes The attributes
|
|
165 |
*/
|
|
166 |
public GroupEntry(CatalogImpl catalog, String base, String... attributes) {
|
|
167 |
super(CatalogEntryType.GROUP, base);
|
|
168 |
setPrefer(attributes[ATTRIBUTE_PREFER]);
|
|
169 |
this.catalog = catalog;
|
|
170 |
}
|
|
171 |
|
|
172 |
/**
|
|
173 |
* Adds an entry.
|
|
174 |
*
|
|
175 |
* @param entry The entry to be added.
|
|
176 |
*/
|
|
177 |
public void addEntry(BaseEntry entry) {
|
|
178 |
entries.add(entry);
|
|
179 |
}
|
|
180 |
|
|
181 |
/**
|
|
182 |
* Sets the prefer property. If the value is null or empty, or any String
|
|
183 |
* other than the defined, it will be assumed as the default value.
|
|
184 |
*
|
|
185 |
* @param value The value of the prefer attribute
|
|
186 |
*/
|
|
187 |
public final void setPrefer(String value) {
|
|
188 |
isPreferPublic = PreferType.PUBLIC.prefer(value);
|
|
189 |
}
|
|
190 |
|
|
191 |
/**
|
|
192 |
* Queries the prefer attribute
|
|
193 |
*
|
|
194 |
* @return true if the prefer attribute is set to system, false if not.
|
|
195 |
*/
|
|
196 |
public boolean isPreferPublic() {
|
|
197 |
return isPreferPublic;
|
|
198 |
}
|
|
199 |
|
|
200 |
/**
|
|
201 |
* Attempt to find a matching entry in the catalog by systemId.
|
|
202 |
*
|
|
203 |
* <p>
|
|
204 |
* The method searches through the system-type entries, including system,
|
|
205 |
* rewriteSystem, systemSuffix, delegateSystem, and group entries in the
|
|
206 |
* current catalog in order to find a match.
|
|
207 |
*
|
|
208 |
*
|
|
209 |
* @param systemId The system identifier of the external entity being
|
|
210 |
* referenced.
|
|
211 |
*
|
|
212 |
* @return An URI string if a mapping is found, or null otherwise.
|
|
213 |
*/
|
|
214 |
public String matchSystem(String systemId) {
|
|
215 |
String match = null;
|
|
216 |
for (BaseEntry entry : entries) {
|
|
217 |
switch (entry.type) {
|
|
218 |
case SYSTEM:
|
|
219 |
match = ((SystemEntry) entry).match(systemId);
|
|
220 |
//if there's a matching system entry, use it
|
|
221 |
if (match != null) {
|
|
222 |
isInstantMatch = true;
|
|
223 |
return match;
|
|
224 |
}
|
|
225 |
break;
|
|
226 |
case REWRITESYSTEM:
|
|
227 |
match = ((RewriteSystem) entry).match(systemId, longestRewriteMatch);
|
|
228 |
if (match != null) {
|
|
229 |
rewriteMatch = match;
|
|
230 |
longestRewriteMatch = ((RewriteSystem) entry).getSystemIdStartString().length();
|
|
231 |
}
|
|
232 |
break;
|
|
233 |
case SYSTEMSUFFIX:
|
|
234 |
match = ((SystemSuffix) entry).match(systemId, longestSuffixMatch);
|
|
235 |
if (match != null) {
|
|
236 |
suffixMatch = match;
|
|
237 |
longestSuffixMatch = ((SystemSuffix) entry).getSystemIdSuffix().length();
|
|
238 |
}
|
|
239 |
break;
|
|
240 |
case GROUP:
|
|
241 |
GroupEntry grpEntry = (GroupEntry) entry;
|
|
242 |
match = grpEntry.matchSystem(systemId);
|
|
243 |
if (grpEntry.isInstantMatch) {
|
|
244 |
//use it if there is a match of the system type
|
|
245 |
return match;
|
|
246 |
} else if (grpEntry.longestRewriteMatch > longestRewriteMatch) {
|
|
247 |
rewriteMatch = match;
|
|
248 |
} else if (grpEntry.longestSuffixMatch > longestSuffixMatch) {
|
|
249 |
suffixMatch = match;
|
|
250 |
}
|
|
251 |
break;
|
|
252 |
}
|
|
253 |
}
|
|
254 |
|
|
255 |
if (longestRewriteMatch > 0) {
|
|
256 |
return rewriteMatch;
|
|
257 |
} else if (longestSuffixMatch > 0) {
|
|
258 |
return suffixMatch;
|
|
259 |
}
|
|
260 |
|
|
261 |
//if no single match is found, try delegates
|
|
262 |
return matchDelegate(CatalogEntryType.DELEGATESYSTEM, systemId);
|
|
263 |
}
|
|
264 |
|
|
265 |
/**
|
|
266 |
* Attempt to find a matching entry in the catalog by publicId.
|
|
267 |
*
|
|
268 |
* <p>
|
|
269 |
* The method searches through the public-type entries, including public,
|
|
270 |
* delegatePublic, and group entries in the current catalog in order to find
|
|
271 |
* a match.
|
|
272 |
*
|
|
273 |
*
|
|
274 |
* @param publicId The public identifier of the external entity being
|
|
275 |
* referenced.
|
|
276 |
*
|
|
277 |
* @return An URI string if a mapping is found, or null otherwise.
|
|
278 |
*/
|
|
279 |
public String matchPublic(String publicId) {
|
|
280 |
//as the specification required
|
|
281 |
if (!isPreferPublic) {
|
|
282 |
return null;
|
|
283 |
}
|
|
284 |
|
|
285 |
//match public entries
|
|
286 |
String match = null;
|
|
287 |
for (BaseEntry entry : entries) {
|
|
288 |
switch (entry.type) {
|
|
289 |
case PUBLIC:
|
|
290 |
match = ((PublicEntry) entry).match(publicId);
|
|
291 |
break;
|
|
292 |
case GROUP:
|
|
293 |
match = ((GroupEntry) entry).matchPublic(publicId);
|
|
294 |
break;
|
|
295 |
}
|
|
296 |
if (match != null) {
|
|
297 |
return match;
|
|
298 |
}
|
|
299 |
}
|
|
300 |
|
|
301 |
//if no single match is found, try delegates
|
|
302 |
return matchDelegate(CatalogEntryType.DELEGATEPUBLIC, publicId);
|
|
303 |
}
|
|
304 |
|
|
305 |
/**
|
|
306 |
* Attempt to find a matching entry in the catalog by the uri element.
|
|
307 |
*
|
|
308 |
* <p>
|
|
309 |
* The method searches through the uri-type entries, including uri,
|
|
310 |
* rewriteURI, uriSuffix, delegateURI and group entries in the current
|
|
311 |
* catalog in order to find a match.
|
|
312 |
*
|
|
313 |
*
|
|
314 |
* @param uri The URI reference of a resource.
|
|
315 |
*
|
|
316 |
* @return An URI string if a mapping is found, or null otherwise.
|
|
317 |
*/
|
|
318 |
public String matchURI(String uri) {
|
|
319 |
String match = null;
|
|
320 |
for (BaseEntry entry : entries) {
|
|
321 |
switch (entry.type) {
|
|
322 |
case URI:
|
|
323 |
match = ((UriEntry) entry).match(uri);
|
|
324 |
if (match != null) {
|
|
325 |
isInstantMatch = true;
|
|
326 |
return match;
|
|
327 |
}
|
|
328 |
break;
|
|
329 |
case REWRITEURI:
|
|
330 |
match = ((RewriteUri) entry).match(uri, longestRewriteMatch);
|
|
331 |
if (match != null) {
|
|
332 |
rewriteMatch = match;
|
|
333 |
longestRewriteMatch = ((RewriteUri) entry).getURIStartString().length();
|
|
334 |
}
|
|
335 |
break;
|
|
336 |
case URISUFFIX:
|
|
337 |
match = ((UriSuffix) entry).match(uri, longestSuffixMatch);
|
|
338 |
if (match != null) {
|
|
339 |
suffixMatch = match;
|
|
340 |
longestSuffixMatch = ((UriSuffix) entry).getURISuffix().length();
|
|
341 |
}
|
|
342 |
break;
|
|
343 |
case GROUP:
|
|
344 |
GroupEntry grpEntry = (GroupEntry) entry;
|
|
345 |
match = grpEntry.matchURI(uri);
|
|
346 |
if (grpEntry.isInstantMatch) {
|
|
347 |
//use it if there is a match of the uri type
|
|
348 |
return match;
|
|
349 |
} else if (grpEntry.longestRewriteMatch > longestRewriteMatch) {
|
|
350 |
rewriteMatch = match;
|
|
351 |
} else if (grpEntry.longestSuffixMatch > longestSuffixMatch) {
|
|
352 |
suffixMatch = match;
|
|
353 |
}
|
|
354 |
break;
|
|
355 |
}
|
|
356 |
}
|
|
357 |
|
|
358 |
if (longestRewriteMatch > 0) {
|
|
359 |
return rewriteMatch;
|
|
360 |
} else if (longestSuffixMatch > 0) {
|
|
361 |
return suffixMatch;
|
|
362 |
}
|
|
363 |
|
|
364 |
//if no single match is found, try delegates
|
|
365 |
return matchDelegate(CatalogEntryType.DELEGATEURI, uri);
|
|
366 |
}
|
|
367 |
|
|
368 |
/**
|
|
369 |
* Matches delegatePublic or delegateSystem against the specified id
|
|
370 |
*
|
|
371 |
* @param isSystem The flag to indicate whether the delegate is system or
|
|
372 |
* public
|
|
373 |
* @param id The system or public id to be matched
|
|
374 |
* @return The URI string if a mapping is found, or null otherwise.
|
|
375 |
*/
|
|
376 |
private String matchDelegate(CatalogEntryType type, String id) {
|
|
377 |
String match = null;
|
|
378 |
int longestMatch = 0;
|
|
379 |
URI catalogId = null;
|
|
380 |
URI temp;
|
|
381 |
|
|
382 |
//Check delegate types in the current catalog
|
|
383 |
for (BaseEntry entry : entries) {
|
|
384 |
if (entry.type == type) {
|
|
385 |
if (type == CatalogEntryType.DELEGATESYSTEM) {
|
|
386 |
temp = ((DelegateSystem)entry).matchURI(id, longestMatch);
|
|
387 |
} else if (type == CatalogEntryType.DELEGATEPUBLIC) {
|
|
388 |
temp = ((DelegatePublic)entry).matchURI(id, longestMatch);
|
|
389 |
} else {
|
|
390 |
temp = ((DelegateUri)entry).matchURI(id, longestMatch);
|
|
391 |
}
|
|
392 |
if (temp != null) {
|
|
393 |
longestMatch = entry.getMatchId().length();
|
|
394 |
catalogId = temp;
|
|
395 |
}
|
|
396 |
}
|
|
397 |
}
|
|
398 |
|
|
399 |
//Check delegate Catalogs
|
|
400 |
if (catalogId != null) {
|
|
401 |
Catalog delegateCatalog = loadCatalog(catalogId);
|
|
402 |
|
|
403 |
if (delegateCatalog != null) {
|
|
404 |
if (type == CatalogEntryType.DELEGATESYSTEM) {
|
|
405 |
match = delegateCatalog.matchSystem(id);
|
|
406 |
} else if (type == CatalogEntryType.DELEGATEPUBLIC) {
|
|
407 |
match = delegateCatalog.matchPublic(id);
|
|
408 |
} else {
|
|
409 |
match = delegateCatalog.matchURI(id);
|
|
410 |
}
|
|
411 |
}
|
|
412 |
}
|
|
413 |
|
|
414 |
return match;
|
|
415 |
}
|
|
416 |
|
|
417 |
/**
|
|
418 |
* Loads all delegate catalogs.
|
|
419 |
*/
|
|
420 |
void loadDelegateCatalogs() {
|
|
421 |
entries.stream()
|
|
422 |
.filter((entry) -> (entry.type == CatalogEntryType.DELEGATESYSTEM ||
|
|
423 |
entry.type == CatalogEntryType.DELEGATEPUBLIC ||
|
|
424 |
entry.type == CatalogEntryType.DELEGATEURI))
|
|
425 |
.map((entry) -> (AltCatalog)entry)
|
|
426 |
.forEach((altCatalog) -> {
|
|
427 |
loadCatalog(altCatalog.getCatalogURI());
|
|
428 |
});
|
|
429 |
}
|
|
430 |
|
|
431 |
/**
|
|
432 |
* Loads a delegate catalog by the catalogId specified.
|
|
433 |
* @param catalogId the catalog Id
|
|
434 |
*/
|
|
435 |
Catalog loadCatalog(URI catalogURI) {
|
|
436 |
Catalog delegateCatalog = null;
|
|
437 |
if (catalogURI != null) {
|
|
438 |
String catalogId = catalogURI.toASCIIString();
|
|
439 |
delegateCatalog = getLoadedCatalog(catalogId);
|
|
440 |
if (delegateCatalog == null) {
|
|
441 |
if (verifyCatalogFile(catalogURI)) {
|
|
442 |
delegateCatalog = new CatalogImpl(catalog, features, catalogId);
|
|
443 |
delegateCatalogs.put(catalogId, delegateCatalog);
|
|
444 |
}
|
|
445 |
}
|
|
446 |
}
|
|
447 |
|
|
448 |
return delegateCatalog;
|
|
449 |
}
|
|
450 |
|
|
451 |
/**
|
|
452 |
* Returns a previously loaded Catalog object if found.
|
|
453 |
*
|
|
454 |
* @param catalogId The systemId of a catalog
|
|
455 |
* @return a Catalog object previously loaded, or null if none in the saved
|
|
456 |
* list
|
|
457 |
*/
|
|
458 |
Catalog getLoadedCatalog(String catalogId) {
|
|
459 |
Catalog c = null;
|
|
460 |
|
|
461 |
//checl delegate Catalogs
|
|
462 |
c = delegateCatalogs.get(catalogId);
|
|
463 |
if (c == null) {
|
|
464 |
//check other loaded Catalogs
|
|
465 |
c = loadedCatalogs.get(catalogId);
|
|
466 |
}
|
|
467 |
|
|
468 |
return c;
|
|
469 |
}
|
|
470 |
|
|
471 |
|
|
472 |
/**
|
|
473 |
* Verifies that the catalog file represented by the catalogId exists. If it
|
|
474 |
* doesn't, returns false to ignore it as specified in the Catalog
|
|
475 |
* specification, section 8. Resource Failures.
|
|
476 |
* <p>
|
|
477 |
* Verifies that the catalog represented by the catalogId has not been
|
|
478 |
* searched or is not circularly referenced.
|
|
479 |
*
|
|
480 |
* @param catalogId The URI to a catalog
|
|
481 |
* @throws CatalogException if circular reference is found.
|
|
482 |
* @return true if the catalogId passed verification, false otherwise
|
|
483 |
*/
|
|
484 |
final boolean verifyCatalogFile(URI catalogURI) {
|
|
485 |
if (catalogURI == null) {
|
|
486 |
return false;
|
|
487 |
}
|
|
488 |
|
|
489 |
//Ignore it if it doesn't exist
|
|
490 |
if (!Files.exists(Paths.get(catalogURI))) {
|
|
491 |
return false;
|
|
492 |
}
|
|
493 |
|
|
494 |
String catalogId = catalogURI.toASCIIString();
|
|
495 |
if (catalogsSearched.contains(catalogId)) {
|
|
496 |
CatalogMessages.reportRunTimeError(CatalogMessages.ERR_CIRCULAR_REFERENCE,
|
|
497 |
new Object[]{CatalogMessages.sanitize(catalogId)});
|
|
498 |
}
|
|
499 |
|
|
500 |
return true;
|
|
501 |
}
|
|
502 |
|
|
503 |
}
|