Debian Codegraph @

src/com/github/weisj/jsvg/parser/impl/MutableLoaderContext.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 org.jetbrains.annotations.NotNull;
25 import org.jetbrains.annotations.Nullable;
26
27 import com.github.weisj.jsvg.paint.impl.DefaultPaintParser;
28 import com.github.weisj.jsvg.parser.*;
29 import com.github.weisj.jsvg.parser.css.CssParser;
30 import com.github.weisj.jsvg.parser.css.impl.SimpleCssParser;
31 import com.github.weisj.jsvg.parser.resources.ResourceLoader;
32 import com.github.weisj.jsvg.parser.resources.ResourcePolicy;
33 import com.github.weisj.jsvg.parser.resources.impl.DefaultResourcePolicy;
34 import com.github.weisj.jsvg.parser.resources.impl.SynchronousResourceLoader;
35
36
37 public final class MutableLoaderContext implements LoaderContext, LoaderContext.Builder {
38 private static final ResourceLoader DEFAULT_RESOURCE_LOADER = new SynchronousResourceLoader();
39 private static final ElementLoader DEFAULT_ELEMENT_LOADER =
40 new DefaultElementLoader(DefaultElementLoader.AllowExternalResources.DENY);
41 private static final CssParser DEFAULT_CSS_PARSER = new SimpleCssParser();
42 private static final PaintParser DEFAULT_PAINT_PARSER = new DefaultPaintParser();
43 private @Nullable DomProcessor preProcessor = null;
44 private @NotNull CssParser cssParser = DEFAULT_CSS_PARSER;
45 private @NotNull PaintParser paintParser = DEFAULT_PAINT_PARSER;
46 private @NotNull ResourceLoader resourceLoader = DEFAULT_RESOURCE_LOADER;
47 private @NotNull ElementLoader elementLoader = DEFAULT_ELEMENT_LOADER;
48 private @NotNull ResourcePolicy resourcePolicy = ResourcePolicy.DENY_EXTERNAL;
49 private @NotNull DocumentLimits documentLimits = DocumentLimits.DEFAULT;
50
51 public static @NotNull MutableLoaderContext createDefault() {
52 return new MutableLoaderContext();
53 }
54
55 @Override
56 public @Nullable DomProcessor preProcessor() {
57 return preProcessor;
58 }
59
60 @Override
61 public @NotNull CssParser cssParser() {
62 return cssParser;
63 }
64
65 @Override
66 public @NotNull PaintParser paintParser() {
67 return paintParser;
68 }
69
70 @Override
71 public @NotNull ResourceLoader resourceLoader() {
72 return resourceLoader;
73 }
74
75 @Override
76 public @NotNull ElementLoader elementLoader() {
77 return elementLoader;
78 }
79
80 @Override
81 public @NotNull ResourcePolicy externalResourcePolicy() {
82 return resourcePolicy;
83 }
84
85 @Override
86 public @NotNull DocumentLimits documentLimits() {
87 return documentLimits;
88 }
89
90 @Override
91 public @NotNull Builder preProcessor(@Nullable DomProcessor preProcessor) {
92 this.preProcessor = preProcessor;
93 return this;
94 }
95
96 @Override
97 public @NotNull Builder cssParser(@NotNull CssParser cssParser) {
98 this.cssParser = cssParser;
99 return this;
100 }
101
102 @Override
103 public @NotNull Builder paintParser(@NotNull PaintParser paintParser) {
104 this.paintParser = paintParser;
105 return this;
106 }
107
108 @Override
109 public @NotNull Builder resourceLoader(@NotNull ResourceLoader resourceLoader) {
110 this.resourceLoader = resourceLoader;
111 return this;
112 }
113
114 @Override
115 public @NotNull Builder elementLoader(@NotNull ElementLoader elementLoader) {
116 this.elementLoader = elementLoader;
117 return this;
118 }
119
120 @Override
121 public @NotNull Builder externalResourcePolicy(@NotNull ResourcePolicy policy) {
122 this.resourcePolicy = policy;
123 return this;
124 }
125
126 @Override
127 public @NotNull Builder documentLimits(@NotNull DocumentLimits documentLimits) {
128 this.documentLimits = documentLimits;
129 return this;
130 }
131
132 @Override
133 public @NotNull LoaderContext build() {
134 // Check if policy changed. This avoids instantiating the heavier external loader.
135 if (!(this.resourcePolicy instanceof DefaultResourcePolicy)
136 || ((DefaultResourcePolicy) this.resourcePolicy).allowsExternalResources()) {
137 this.elementLoader = new DefaultElementLoader(DefaultElementLoader.AllowExternalResources.ALLOW);
138 }
139 return this;
140 }
141 }