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