| •1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
package com.github.weisj.jsvg.parser.impl; |
| 23 |
|
| 24 |
import java.io.InputStream; |
| 25 |
import java.net.URI; |
| 26 |
import java.net.URL; |
| 27 |
import java.util.*; |
| 28 |
|
| 29 |
import org.jetbrains.annotations.NotNull; |
| 30 |
import org.jetbrains.annotations.Nullable; |
| 31 |
|
| 32 |
import com.github.weisj.jsvg.logging.Logger; |
| 33 |
import com.github.weisj.jsvg.logging.Logger.Level; |
| 34 |
import com.github.weisj.jsvg.logging.impl.LogFactory; |
| 35 |
import com.github.weisj.jsvg.parser.DomDocument; |
| 36 |
|
| 37 |
class ExternalDocumentLoader implements DefaultElementLoader.DocumentLoader { |
| 38 |
private static final Logger LOGGER = LogFactory.createLogger(ExternalDocumentLoader.class); |
| 39 |
|
| 40 |
private final @NotNull Map<URI, CachedDocument> cache = new HashMap<>(); |
| 41 |
|
| 42 |
@Override |
| 43 |
public @Nullable DomDocument resolveDocument(@NotNull DomDocument document, @NotNull String name) { |
| 44 |
if (name.isEmpty()) return document; |
| 45 |
return locateDocument(document, name); |
| 46 |
} |
| 47 |
|
| 48 |
private @Nullable DomDocument locateDocument(@NotNull DomDocument document, @NotNull String name) { |
| 49 |
URI documentUri = document |
| 50 |
.loaderContext() |
| 51 |
.externalResourcePolicy() |
| 52 |
.resolveResourceURI(document.rootURI(), name); |
| 53 |
if (documentUri == null) return null; |
| 54 |
|
| 55 |
try { |
| 56 |
URL documentUrl = documentUri.toURL(); |
| 57 |
synchronized (cache) { |
| 58 |
CachedDocument cachedDocument = cache.get(documentUri); |
| 59 |
if (cachedDocument != null) { |
| 60 |
ParsedDocument cached = cachedDocument.document; |
| 61 |
if (cached == null) { |
| 62 |
throw new IllegalStateException("Reference cycle containing external document: " + documentUri); |
| 63 |
} |
| 64 |
return cached; |
| 65 |
} |
| 66 |
} |
| 67 |
CachedDocument cachedDocument = new CachedDocument(); |
| 68 |
synchronized (cache) { |
| 69 |
cache.put(documentUri, cachedDocument); |
| 70 |
} |
| 71 |
|
| 72 |
try (InputStream is = StreamUtil.createDocumentInputStream(documentUrl.openStream())) { |
| 73 |
SVGDocumentBuilder builder = new StaxSVGLoader().parse( |
| 74 |
is, documentUri, document.loaderContext()); |
| 75 |
|
| 76 |
if (builder == null) return null; |
| 77 |
builder.preProcess(); |
| 78 |
|
| 79 |
ParsedDocument parsedDocument = builder.parsedDocument(); |
| 80 |
synchronized (cache) { |
| 81 |
cachedDocument.document = parsedDocument; |
| 82 |
} |
| 83 |
return parsedDocument; |
| 84 |
} |
| 85 |
} catch (Exception e) { |
| 86 |
LOGGER.log(Level.WARNING, String.format("Failed to load external document: %s from %s - %s", |
| 87 |
name, documentUri, e.getMessage())); |
| 88 |
return null; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
private static final class CachedDocument { |
| 93 |
private @Nullable ParsedDocument document; |
| 94 |
} |
| 95 |
} |