Debian Codegraph @

src/com/github/weisj/jsvg/parser/impl/ExternalDocumentLoader.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2024-2025 Jannis Weis
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7 * associated documentation files (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
9 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all copies or
13 * substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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 }