|
| 1 | +package org.asciidoctor.maven.site.parser; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import org.apache.maven.doxia.sink.Sink; |
| 7 | +import org.asciidoctor.ast.StructuralNode; |
| 8 | +import org.asciidoctor.maven.site.parser.processors.DescriptionListNodeProcessor; |
| 9 | +import org.asciidoctor.maven.site.parser.processors.DocumentNodeProcessor; |
| 10 | +import org.asciidoctor.maven.site.parser.processors.ImageNodeProcessor; |
| 11 | +import org.asciidoctor.maven.site.parser.processors.ListItemNodeProcessor; |
| 12 | +import org.asciidoctor.maven.site.parser.processors.ListingNodeProcessor; |
| 13 | +import org.asciidoctor.maven.site.parser.processors.LiteralNodeProcessor; |
| 14 | +import org.asciidoctor.maven.site.parser.processors.NoOpNodeProcessor; |
| 15 | +import org.asciidoctor.maven.site.parser.processors.OrderedListNodeProcessor; |
| 16 | +import org.asciidoctor.maven.site.parser.processors.ParagraphNodeProcessor; |
| 17 | +import org.asciidoctor.maven.site.parser.processors.PreambleNodeProcessor; |
| 18 | +import org.asciidoctor.maven.site.parser.processors.SectionNodeProcessor; |
| 19 | +import org.asciidoctor.maven.site.parser.processors.TableNodeProcessor; |
| 20 | +import org.asciidoctor.maven.site.parser.processors.UnorderedListNodeProcessor; |
| 21 | + |
| 22 | +/** |
| 23 | + * Factory and repository for NodeProcessors. |
| 24 | + * |
| 25 | + * @author abelsromero |
| 26 | + * @since 3.1.0 |
| 27 | + */ |
| 28 | +public class NodeSinker { |
| 29 | + |
| 30 | + private final List<NodeProcessor> nodeProcessors; |
| 31 | + |
| 32 | + private final NodeProcessor noOpProcessor; |
| 33 | + |
| 34 | + public NodeSinker(Sink sink) { |
| 35 | + nodeProcessors = Arrays.asList( |
| 36 | + new DescriptionListNodeProcessor(sink, this), |
| 37 | + new DocumentNodeProcessor(sink, this), |
| 38 | + new ImageNodeProcessor(sink, this), |
| 39 | + new ListItemNodeProcessor(sink, this), |
| 40 | + new ListingNodeProcessor(sink, this), |
| 41 | + new ListingNodeProcessor(sink, this), |
| 42 | + new LiteralNodeProcessor(sink, this), |
| 43 | + new OrderedListNodeProcessor(sink, this), |
| 44 | + new ParagraphNodeProcessor(sink, this), |
| 45 | + new PreambleNodeProcessor(sink, this), |
| 46 | + new SectionNodeProcessor(sink, this), |
| 47 | + new TableNodeProcessor(sink, this), |
| 48 | + new UnorderedListNodeProcessor(sink, this) |
| 49 | + ); |
| 50 | + noOpProcessor = new NoOpNodeProcessor(sink, this); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Returns first NodeProcessor that can treat the node. |
| 55 | + **/ |
| 56 | + private NodeProcessor get(StructuralNode node) { |
| 57 | + return nodeProcessors.stream() |
| 58 | + .filter(nodeProcessor -> nodeProcessor.applies(node)) |
| 59 | + .findFirst() |
| 60 | + .orElse(noOpProcessor); |
| 61 | + } |
| 62 | + |
| 63 | + public void sink(StructuralNode node) { |
| 64 | + get(node).process(node); |
| 65 | + } |
| 66 | +} |
0 commit comments