| •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.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 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 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 |
|
| 86 |
|
| 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 |
|
| 95 |
result = CategoryCheckResult.Excluded; |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
return result; |
| 100 |
} |
| 101 |
|
| 102 |
private enum CategoryCheckResult { |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
Allowed, |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
Denied, |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
Excluded |
| 115 |
} |
| 116 |
} |