Debian Codegraph @

src/com/github/weisj/jsvg/nodes/container/BaseContainerNode.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2021-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.nodes.container;
23
24 import org.jetbrains.annotations.NotNull;
25 import org.jetbrains.annotations.Nullable;
26
27 import com.github.weisj.jsvg.logging.Logger;
28 import com.github.weisj.jsvg.logging.Logger.Level;
29 import com.github.weisj.jsvg.logging.impl.LogFactory;
30 import com.github.weisj.jsvg.nodes.AbstractSVGNode;
31 import com.github.weisj.jsvg.nodes.SVGNode;
32 import com.github.weisj.jsvg.nodes.prototype.Container;
33 import com.github.weisj.jsvg.nodes.prototype.spec.Category;
34 import com.github.weisj.jsvg.nodes.prototype.spec.PermittedContent;
35
36 public abstract class BaseContainerNode<E> extends AbstractSVGNode implements Container<E> {
37 private static final boolean EXHAUSTIVE_CHECK = true;
38 private static final Logger LOGGER = LogFactory.createLogger(BaseContainerNode.class);
39
40 @Override
41 public final void addChild(@Nullable String id, @NotNull SVGNode node) {
42 if (isAcceptableType(node) && acceptChild(id, node)) {
43 doAdd(node);
44 }
45 }
46
47 protected abstract void doAdd(@NotNull SVGNode node);
48
49 /**
50 * Determine whether the container accepts this {@link SVGNode} as a child.
51 * By default, this will always report true but subclasses may choose to reject certain
52 * types of nodes.
53 *
54 * @param id the id of the node
55 * @param node the node itself
56 * @return whether the node can be inserted as a child.
57 */
58 protected boolean acceptChild(@Nullable String id, @NotNull SVGNode node) {
59 return true;
60 }
61
62 protected boolean isAcceptableType(@NotNull SVGNode node) {
63 PermittedContent allowedNodes = getClass().getAnnotation(PermittedContent.class);
64 if (allowedNodes == null) {
65 throw new IllegalStateException(
66 String.format("Element <%s> doesn't specify permitted content information", tagName()));
67 }
68 if (allowedNodes.any()) return true;
69
70 Category[] categories = Category.categoriesOf(node);
71 Class<? extends SVGNode> nodeType = node.getClass();
72 CategoryCheckResult result = doIntersect(allowedNodes.categories(), categories);
73 if (result == CategoryCheckResult.Allowed) return true;
74 for (Class<? extends SVGNode> type : allowedNodes.anyOf()) {
75 if (type.isAssignableFrom(nodeType)) return true;
76 }
77 if (EXHAUSTIVE_CHECK && result != CategoryCheckResult.Excluded) {
78 LOGGER.log(Level.WARNING, () -> String.format("Element <%s> not allowed in <%s> (or not implemented)",
79 node.tagName(), tagName()));
80 }
81 return false;
82 }
83
84 private CategoryCheckResult doIntersect(Category[] requested, Category[] provided) {
85 // Expected sizes for these arrays is pretty small hence we don't need to
86 // be smart about the intersection check.
87 CategoryCheckResult result = CategoryCheckResult.Denied;
88 for (Category request : requested) {
89 boolean effectivelyAllowed = request.isEffectivelyAllowed();
90 if (!effectivelyAllowed && !EXHAUSTIVE_CHECK) continue;
91 for (Category category : provided) {
92 if (request == category) {
93 if (effectivelyAllowed) return CategoryCheckResult.Allowed;
94 // Keep searching. Element may be allowed by another category.
95 result = CategoryCheckResult.Excluded;
96 }
97 }
98 }
99 return result;
100 }
101
102 private enum CategoryCheckResult {
103 /**
104 * The element is allowed as a child node.
105 */
106 Allowed,
107 /**
108 * The element isn't allowed as a child node.
109 */
110 Denied,
111 /**
112 * The element is allowed but excluded due to some other reason.
113 */
114 Excluded
115 }
116 }