Debian Codegraph @

src/com/github/weisj/jsvg/parser/impl/DefaultElementLoader.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
25 import org.jetbrains.annotations.NotNull;
26 import org.jetbrains.annotations.Nullable;
27
28 import com.github.weisj.jsvg.parser.DomDocument;
29 import com.github.weisj.jsvg.parser.ElementLoader;
30
31 class DefaultElementLoader implements ElementLoader {
32
33 private static final DocumentLoader DEFAULT_DOCUMENT_LOADER = new DefaultDocumentLoader();
34 private final DocumentLoader documentLoader;
35
36 enum AllowExternalResources {
37 DENY,
38 ALLOW
39 }
40
41 DefaultElementLoader(AllowExternalResources allowExternalResources) {
42 documentLoader = createDocumentLoader(allowExternalResources);
43 }
44
45 private static @NotNull DocumentLoader createDocumentLoader(AllowExternalResources allowExternalResources) {
46 if (allowExternalResources == AllowExternalResources.DENY) return DEFAULT_DOCUMENT_LOADER;
47 return new ExternalDocumentLoader();
48 }
49
50 @Override
51 public <T> @Nullable T loadElement(@NotNull Class<T> type, @Nullable String value, @NotNull DomDocument document) {
52 Url url = Url.parse(value, Url.RequireFragment.YES);
53 if (url == null) return null;
54 DomDocument resolutionDocument = document;
55 if (url.url() != null) {
56 resolutionDocument = documentLoader.resolveDocument(document, url.url());
57 if (resolutionDocument == null) return null;
58 }
59 return resolutionDocument.getElementById(type, url.fragment());
60 }
61
62 interface DocumentLoader {
63 @Nullable
64 DomDocument resolveDocument(@NotNull DomDocument document, @NotNull String name);
65 }
66
67 private static class DefaultDocumentLoader implements DocumentLoader {
68 @Override
69 public @Nullable DomDocument resolveDocument(@NotNull DomDocument document, @NotNull String name) {
70 if (name.isEmpty()) return document;
71 return null;
72 }
73 }
74 }