| •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 java.net.URI; |
| 25 |
import java.util.*; |
| 26 |
|
| 27 |
import org.jetbrains.annotations.ApiStatus; |
| 28 |
import org.jetbrains.annotations.NotNull; |
| 29 |
import org.jetbrains.annotations.Nullable; |
| 30 |
|
| 31 |
import com.github.weisj.jsvg.SVGDocument; |
| 32 |
import com.github.weisj.jsvg.nodes.*; |
| 33 |
import com.github.weisj.jsvg.nodes.container.CommonRenderableContainerNode; |
| 34 |
import com.github.weisj.jsvg.parser.DomProcessor; |
| 35 |
import com.github.weisj.jsvg.parser.LoaderContext; |
| 36 |
import com.github.weisj.jsvg.parser.css.CssParser; |
| 37 |
import com.github.weisj.jsvg.parser.css.StyleSheet; |
| 38 |
|
| 39 |
public final class SVGDocumentBuilder { |
| 40 |
private final @NotNull ParsedDocument parsedDocument; |
| 41 |
private final @NotNull List<@NotNull Use> useElements = new ArrayList<>(); |
| 42 |
private final @NotNull List<@NotNull ParsedElement> styleElements = new ArrayList<>(); |
| 43 |
private final @NotNull List<@NotNull StyleSheet> styleSheets = new ArrayList<>(); |
| 44 |
private final @NotNull Deque<@NotNull ParsedElement> currentNodeStack = new ArrayDeque<>(); |
| 45 |
|
| 46 |
private final @NotNull LoaderContext loaderContext; |
| 47 |
private final @NotNull NodeSupplier nodeSupplier; |
| 48 |
|
| 49 |
private ParsedElement rootNode; |
| 50 |
|
| 51 |
public SVGDocumentBuilder( |
| 52 |
@Nullable URI rootURI, |
| 53 |
@NotNull LoaderContext loaderContext, |
| 54 |
@NotNull NodeSupplier nodeSupplier) { |
| 55 |
LoadHelper loadHelper = new LoadHelper( |
| 56 |
new AttributeParser(loaderContext.paintParser()), |
| 57 |
loaderContext); |
| 58 |
this.loaderContext = loaderContext; |
| 59 |
this.nodeSupplier = nodeSupplier; |
| 60 |
this.parsedDocument = new ParsedDocument(rootURI, loaderContext, loadHelper); |
| 61 |
} |
| 62 |
|
| 63 |
@ApiStatus.Internal |
| 64 |
@NotNull |
| 65 |
ParsedDocument parsedDocument() { |
| 66 |
return parsedDocument; |
| 67 |
} |
| 68 |
|
| 69 |
public void startDocument() { |
| 70 |
if (rootNode != null) throw new IllegalStateException("Document already started"); |
| 71 |
} |
| 72 |
|
| 73 |
public void endDocument() { |
| 74 |
if (rootNode == null) throw new IllegalStateException("Document is empty"); |
| 75 |
} |
| 76 |
|
| 77 |
public boolean startElement(@NotNull String tagName, @NotNull Map<String, String> attributes) { |
| 78 |
ParsedElement parentElement = !currentNodeStack.isEmpty() |
| 79 |
? currentNodeStack.peek() |
| 80 |
: null; |
| 81 |
|
| 82 |
if (parentElement != null) flushText(parentElement, true); |
| 83 |
|
| 84 |
@Nullable SVGNode newNode = nodeSupplier.create(tagName); |
| 85 |
if (newNode == null) return false; |
| 86 |
|
| 87 |
AttributeNode attributeNode = new AttributeNode(tagName, attributes, styleSheets); |
| 88 |
String id = attributes.get("id"); |
| 89 |
ParsedElement parsedElement = new ParsedElement(id, parsedDocument, parentElement, attributeNode, newNode); |
| 90 |
attributeNode.setElement(parsedElement); |
| 91 |
|
| 92 |
if (id != null && !parsedDocument.hasElementWithId(id)) { |
| 93 |
parsedDocument.registerNamedElement(id, parsedElement); |
| 94 |
} |
| 95 |
|
| 96 |
if (parentElement != null) { |
| 97 |
parentElement.addChild(parsedElement); |
| 98 |
} |
| 99 |
if (rootNode == null) rootNode = parsedElement; |
| 100 |
|
| 101 |
if (parsedElement.node() instanceof Style) { |
| 102 |
styleElements.add(parsedElement); |
| 103 |
} |
| 104 |
|
| 105 |
if (parsedElement.node() instanceof Use) { |
| 106 |
useElements.add((Use) parsedElement.node()); |
| 107 |
} |
| 108 |
|
| 109 |
currentNodeStack.push(parsedElement); |
| 110 |
return true; |
| 111 |
} |
| 112 |
|
| 113 |
public void addTextContent(char @NotNull [] characterData, int startOffset, int endOffset) { |
| 114 |
if (currentNodeStack.isEmpty()) { |
| 115 |
throw new IllegalStateException("Adding text content without a current node"); |
| 116 |
} |
| 117 |
ParsedElement currentElement = currentNodeStack.peek(); |
| 118 |
if (currentElement.characterDataParser == null) return; |
| 119 |
currentElement.characterDataParser.append(characterData, startOffset, endOffset); |
| 120 |
} |
| 121 |
|
| 122 |
public void endElement(@NotNull String tagName) { |
| 123 |
if (currentNodeStack.isEmpty()) { |
| 124 |
throw new IllegalStateException("No current node to end"); |
| 125 |
} |
| 126 |
ParsedElement currentElement = currentNodeStack.pop(); |
| 127 |
String currentNodeTagName = currentElement.attributeNode().tagName(); |
| 128 |
if (!currentNodeTagName.equals(tagName)) { |
| 129 |
throw new IllegalStateException( |
| 130 |
String.format("Closing tag %s doesn't match current node %s)", tagName, currentNodeTagName)); |
| 131 |
} |
| 132 |
flushText(currentElement, false); |
| 133 |
} |
| 134 |
|
| 135 |
private void flushText(@NotNull ParsedElement element, boolean segmentBreak) { |
| 136 |
if (element.characterDataParser != null && element.characterDataParser.canFlush(segmentBreak)) { |
| 137 |
element.textContent().currentContentList() |
| 138 |
.add(new StringSegment(element.characterDataParser.flush(segmentBreak))); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
void preProcess() { |
| 143 |
if (rootNode == null) throw new IllegalStateException("No root node"); |
| 144 |
|
| 145 |
DomProcessor preProcessor = loaderContext.preProcessor(); |
| 146 |
if (preProcessor != null) preProcessor.process(rootNode); |
| 147 |
} |
| 148 |
|
| 149 |
public @NotNull SVGDocument build() { |
| 150 |
preProcess(); |
| 151 |
processStyleSheets(); |
| 152 |
rootNode.build(0); |
| 153 |
validatePathCount(); |
| 154 |
validateUseElementsDepth(); |
| 155 |
return DocumentConstructorAccessor.constructor().create((SVG) rootNode.node()); |
| 156 |
} |
| 157 |
|
| 158 |
private void processStyleSheets() { |
| 159 |
if (styleElements.isEmpty()) return; |
| 160 |
CssParser cssParser = loaderContext.cssParser(); |
| 161 |
for (ParsedElement styleElement : styleElements) { |
| 162 |
styleElement.build(0); |
| 163 |
Style styleNode = (Style) styleElement.node(); |
| 164 |
styleNode.parseStyleSheet(cssParser); |
| 165 |
styleSheets.add(styleNode.styleSheet()); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
private void validatePathCount() { |
| 170 |
int pathCount = rootNode.outgoingPaths(); |
| 171 |
int maxPathCount = parsedDocument.loaderContext().documentLimits().maxPathCount(); |
| 172 |
if (pathCount > maxPathCount) { |
| 173 |
throw new IllegalStateException( |
| 174 |
String.format("Maximum count of rendered element instances exceeded %d > %d.%n", |
| 175 |
pathCount, maxPathCount) |
| 176 |
+ "Note: You can configure this using LoaderContext#documentLimits()"); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
private void validateUseElementsDepth() { |
| 181 |
if (useElements.isEmpty()) return; |
| 182 |
Map<SVGNode, Integer> checkedNodes = new HashMap<>(); |
| 183 |
int useNestingLimit = parsedDocument.loaderContext().documentLimits().maxUseNestingDepth(); |
| 184 |
for (Use useElement : useElements) { |
| 185 |
int depth = nestingDepthOf(useElement, checkedNodes); |
| 186 |
if (depth > useNestingLimit) { |
| 187 |
throw new IllegalStateException(String.format( |
| 188 |
"Maximum nesting depth for <use> exceeded %d > %d starting from node with id '%s'%n", |
| 189 |
depth, useNestingLimit, useElement.id()) |
| 190 |
+ "Note: You can configure this using LoaderContext#documentLimits()"); |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
private int nestingDepthOf(@NotNull SVGNode node, @NotNull Map<SVGNode, Integer> checkedNodes) { |
| 196 |
int cached = checkedNodes.getOrDefault(node, -1); |
| 197 |
if (cached >= 0) return cached; |
| 198 |
|
| 199 |
int depth = 0; |
| 200 |
if (node instanceof Use) { |
| 201 |
SVGNode referenced = ((Use) node).referencedNode(); |
| 202 |
if (referenced != null) { |
| 203 |
depth = nestingDepthOf(referenced, checkedNodes) + 1; |
| 204 |
} |
| 205 |
} else if (node instanceof CommonRenderableContainerNode) { |
| 206 |
for (SVGNode child : ((CommonRenderableContainerNode) node).children()) { |
| 207 |
depth = Math.max(depth, nestingDepthOf(child, checkedNodes)); |
| 208 |
} |
| 209 |
} |
| 210 |
checkedNodes.put(node, depth); |
| 211 |
|
| 212 |
return depth; |
| 213 |
} |
| 214 |
} |