Debian Codegraph @

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

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2021-2026 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.util.*;
25
26 import org.jetbrains.annotations.NotNull;
27 import org.jetbrains.annotations.Nullable;
28
29 import com.github.weisj.jsvg.nodes.SVGNode;
30 import com.github.weisj.jsvg.nodes.animation.BaseAnimationNode;
31 import com.github.weisj.jsvg.nodes.prototype.Container;
32 import com.github.weisj.jsvg.nodes.prototype.Renderable;
33 import com.github.weisj.jsvg.nodes.prototype.spec.Category;
34 import com.github.weisj.jsvg.nodes.prototype.spec.PermittedContent;
35 import com.github.weisj.jsvg.parser.DomElement;
36 import com.github.weisj.jsvg.parser.TextContent;
37
38 public final class ParsedElement implements DomElement {
39
40 private enum BuildStatus {
41 NOT_BUILT,
42 IN_PROGRESS,
43 FINISHED
44 }
45
46 private final @Nullable String id;
47 private final @NotNull ParsedDocument document;
48 private final @Nullable ParsedElement parent;
49 private final @NotNull AttributeNode attributeNode;
50 private final @NotNull SVGNode node;
51
52 private final @NotNull List<@NotNull ParsedElement> children = new ArrayList<>();
53 private final @NotNull List<@NotNull ParsedElement> indirectChildren = new ArrayList<>();
54 private final @NotNull Map<String, @NotNull List<@NotNull ParsedElement>> animationElements = new HashMap<>();
55 private TextContentImpl textContent = null;
56
57 final CharacterDataParser characterDataParser;
58 private @NotNull BuildStatus buildStatus = BuildStatus.NOT_BUILT;
59 private int outgoingPaths = -1;
60
61 ParsedElement(@Nullable String id, @NotNull ParsedDocument document,
62 @Nullable ParsedElement parent, @NotNull AttributeNode element,
63 @NotNull SVGNode node) {
64 this.document = document;
65 this.parent = parent;
66 this.attributeNode = element;
67 this.node = node;
68 this.id = id;
69 PermittedContent permittedContent = node.getClass().getAnnotation(PermittedContent.class);
70 if (permittedContent == null) {
71 throw new IllegalStateException("Element <" + node.tagName() + "> doesn't specify permitted content");
72 }
73 if (permittedContent.charData()) {
74 characterDataParser = new CharacterDataParser();
75 } else {
76 characterDataParser = null;
77 }
78 }
79
80 @Override
81 public @Nullable String id() {
82 return id;
83 }
84
85 @Override
86 public @NotNull String tagName() {
87 return attributeNode.tagName();
88 }
89
90 @Override
91 public @NotNull List<@NotNull String> classNames() {
92 return attributeNode.classNames();
93 }
94
95 @Override
96 public @NotNull ParsedDocument document() {
97 return document;
98 }
99
100 @Override
101 public @NotNull List<ParsedElement> children() {
102 return children;
103 }
104
105 @Override
106 public @Nullable String attribute(@NotNull String name) {
107 return attributeNode.getValue(name);
108 }
109
110 @Override
111 public void setAttribute(@NotNull String name, @Nullable String value) {
112 if (value == null) {
113 attributeNode.attributes().remove(name);
114 } else {
115 attributeNode.attributes().put(name, value);
116 }
117 }
118
119 @Override
120 public @NotNull TextContentImpl textContent() {
121 if (textContent == null) {
122 textContent = new TextContentImpl(this);
123 }
124 return textContent;
125 }
126
127 public @NotNull Map<String, List<ParsedElement>> animationElements() {
128 return animationElements;
129 }
130
131 @Override
132 public @Nullable ParsedElement parent() {
133 return parent;
134 }
135
136 public @NotNull SVGNode node() {
137 return node;
138 }
139
140 public @NotNull SVGNode nodeEnsuringBuildStatus(int depth) {
141 if (buildStatus == BuildStatus.IN_PROGRESS) {
142 cyclicDependencyDetected();
143 } else if (buildStatus == BuildStatus.NOT_BUILT) {
144 build(depth);
145 }
146 return node;
147 }
148
149 public @NotNull AttributeNode attributeNode() {
150 return attributeNode;
151 }
152
153 void addChild(@NotNull ParsedElement parsedElement) {
154 if (Category.hasCategory(parsedElement.node, Category.Animation)) {
155 String attributeName = BaseAnimationNode.attributeName(parsedElement.attributeNode());
156 animationElements.computeIfAbsent(attributeName, k -> new ArrayList<>()).add(parsedElement);
157 }
158 children.add(parsedElement);
159 }
160
161 void addIndirectChild(@NotNull ParsedElement parsedElement) {
162 indirectChildren.add(parsedElement);
163 }
164
165 private void addChildrenAndContent() {
166 if (node instanceof Container) {
167 int contentListsSize = textContent == null ? 0 : textContent.contentLists().size();
168 for (int i = 0; i < children.size(); i++) {
169 if (i < contentListsSize) {
170 assert textContent != null;
171 addContentList(textContent.contentLists().get(i));
172 }
173 ParsedElement child = children.get(i);
174 ((Container<?>) node).addChild(child.id, child.node);
175 }
176 for (int i = children.size(); i < contentListsSize; i++) {
177 assert textContent != null;
178 addContentList(textContent.contentLists().get(i));
179 }
180 } else if (textContent != null) {
181 for (List<TextContent.Segment> contentList : textContent.contentLists()) {
182 addContentList(contentList);
183 }
184 }
185 }
186
187 private void addContentList(@NotNull List<TextContent.@NotNull Segment> contentList) {
188 for (TextContent.Segment text : contentList) {
189 node.addContent(text);
190 }
191 }
192
193 void build(int depth) {
194 if (buildStatus == BuildStatus.FINISHED) return;
195 if (buildStatus == BuildStatus.IN_PROGRESS) {
196 cyclicDependencyDetected();
197 return;
198 }
199 buildStatus = BuildStatus.IN_PROGRESS;
200
201 int maxNestingDepth = attributeNode.document().loaderContext().documentLimits().maxNestingDepth();
202 if (depth > maxNestingDepth) {
203 throw new IllegalStateException(
204 String.format("Maximum nesting depth reached %d > %d.%n", depth, maxNestingDepth)
205 + "Note: You can configure this using LoaderContext#documentLimits()");
206 }
207
208 addChildrenAndContent();
209
210 attributeNode.prepareForNodeBuilding();
211
212 // Build depth first to ensure child nodes are processed first.
213 // e.g. LinearGradient depends on its stops to be build first.
214 for (ParsedElement child : children) {
215 child.build(depth + 1);
216 }
217
218 document().setCurrentNestingDepth(depth);
219 node.build(attributeNode);
220 buildStatus = BuildStatus.FINISHED;
221 }
222
223 /*
224 * Returns the number of outgoing paths from this node terminating in a leaf node.
225 */
226 int outgoingPaths() {
227 if (outgoingPaths == -1) {
228 outgoingPaths = 0;
229 for (ParsedElement child : children) {
230 if (child.node instanceof Renderable) {
231 outgoingPaths += child.outgoingPaths();
232 }
233 }
234
235 for (ParsedElement child : indirectChildren) {
236 outgoingPaths += child.outgoingPaths();
237 }
238
239 outgoingPaths = Math.max(outgoingPaths, 1);
240 }
241 return outgoingPaths;
242 }
243
244 @Override
245 public String toString() {
246 return "ParsedElement{" + "node=" + node + '}';
247 }
248
249 private void cyclicDependencyDetected() {
250 throw new IllegalStateException("Cyclic dependency involving node '" + id + "' detected.");
251 }
252 }