| •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 |
|
| 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 |
} |