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