diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-02-24 08:16:16 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-02-24 08:16:16 +0000 |
| commit | 3e2786a004eddf968a5fa7d5a92dff5b1b5f7734 (patch) | |
| tree | 7fa1bcc1e484cd0ef782f64baf504ee83333d397 /src/main/java/org/zwobble/mammoth/html | |
| parent | 0dc3a12affc2e46c1a0df2899cf51a1ae3fdbd61 (diff) | |
Create internal package
Diffstat (limited to 'src/main/java/org/zwobble/mammoth/html')
7 files changed, 0 insertions, 382 deletions
diff --git a/src/main/java/org/zwobble/mammoth/html/Html.java b/src/main/java/org/zwobble/mammoth/html/Html.java deleted file mode 100644 index e1bcef7..0000000 --- a/src/main/java/org/zwobble/mammoth/html/Html.java +++ /dev/null @@ -1,179 +0,0 @@ -package org.zwobble.mammoth.html; - -import org.zwobble.mammoth.util.MammothLists; -import org.zwobble.mammoth.util.MammothOptionals; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -import static org.zwobble.mammoth.util.Casts.tryCast; -import static org.zwobble.mammoth.util.MammothIterables.tryGetLast; -import static org.zwobble.mammoth.util.MammothLists.list; -import static org.zwobble.mammoth.util.MammothMaps.map; - -public class Html { - public static final HtmlNode FORCE_WRITE = HtmlForceWrite.FORCE_WRITE; - - public static String write(List<HtmlNode> nodes) { - StringBuilder builder = new StringBuilder(); - nodes.forEach(node -> HtmlWriter.write(node, builder)); - return builder.toString(); - } - - public static HtmlNode text(String value) { - return new HtmlTextNode(value); - } - - public static HtmlNode element(String tagName) { - return element(tagName, list()); - } - - public static HtmlNode element(String tagName, Map<String, String> attributes) { - return element(tagName, attributes, list()); - } - - public static HtmlNode element(String tagName, List<HtmlNode> children) { - return element(tagName, map(), children); - } - - public static HtmlNode element(String tagName, Map<String, String> attributes, List<HtmlNode> children) { - return new HtmlElement(list(tagName), attributes, children, false); - } - - public static HtmlNode collapsibleElement(String tagName) { - return collapsibleElement(list(tagName)); - } - - public static HtmlNode collapsibleElement(List<String> tagNames) { - return collapsibleElement(tagNames, map(), list()); - } - - public static HtmlNode collapsibleElement(String tagName, List<HtmlNode> children) { - return collapsibleElement(tagName, map(), children); - } - - public static HtmlNode collapsibleElement(String tagName, Map<String, String> attributes, List<HtmlNode> children) { - return collapsibleElement(list(tagName), attributes, children); - } - - public static HtmlNode collapsibleElement(List<String> tagNames, Map<String, String> attributes, List<HtmlNode> children) { - return new HtmlElement(tagNames, attributes, children, true); - } - - public static HtmlNode selfClosingElement(String tagName) { - return selfClosingElement(tagName, map()); - } - - public static HtmlNode selfClosingElement(String tagName, Map<String, String> attributes) { - return new HtmlSelfClosingElement(tagName, attributes); - } - - public static List<HtmlNode> stripEmpty(List<HtmlNode> nodes) { - return MammothLists.eagerFlatMap(nodes, node -> stripEmpty(node)); - } - - private static List<HtmlNode> stripEmpty(HtmlNode node) { - return node.accept(new HtmlNode.Mapper<List<HtmlNode>>() { - @Override - public List<HtmlNode> visit(HtmlElement element) { - List<HtmlNode> children = stripEmpty(element.getChildren()); - if (children.isEmpty()) { - return list(); - } else { - return list(new HtmlElement( - element.getTagNames(), - element.getAttributes(), - children, - element.isCollapsible())); - } - } - - @Override - public List<HtmlNode> visit(HtmlSelfClosingElement element) { - return list(element); - } - - @Override - public List<HtmlNode> visit(HtmlTextNode node) { - if (node.getValue().isEmpty()) { - return list(); - } else { - return list(node); - } - } - - @Override - public List<HtmlNode> visit(HtmlForceWrite forceWrite) { - return list(forceWrite); - } - }); - } - - public static List<HtmlNode> collapse(List<HtmlNode> nodes) { - List<HtmlNode> collapsed = new ArrayList<>(); - - for (HtmlNode node : nodes) { - collapsingAdd(collapsed, node); - } - - return collapsed; - } - - private static void collapsingAdd(List<HtmlNode> collapsed, HtmlNode node) { - HtmlNode collapsedNode = collapse(node); - if (!tryCollapse(collapsed, collapsedNode)) { - collapsed.add(collapsedNode); - } - } - - private static HtmlNode collapse(HtmlNode node) { - return node.accept(new HtmlNode.Mapper<HtmlNode>() { - @Override - public HtmlNode visit(HtmlElement element) { - return new HtmlElement( - element.getTagNames(), - element.getAttributes(), - collapse(element.getChildren()), - element.isCollapsible()); - } - - @Override - public HtmlNode visit(HtmlSelfClosingElement element) { - return element; - } - - @Override - public HtmlNode visit(HtmlTextNode node) { - return node; - } - - @Override - public HtmlNode visit(HtmlForceWrite forceWrite) { - return forceWrite; - } - }); - } - - private static boolean tryCollapse(List<HtmlNode> collapsed, HtmlNode node) { - return MammothOptionals.map( - tryGetLast(collapsed).flatMap(last -> tryCast(HtmlElement.class, last)), - tryCast(HtmlElement.class, node), - (last, next) -> { - if (next.isCollapsible() && isMatch(last, next)) { - for (HtmlNode child : next.getChildren()) { - collapsingAdd(last.getChildren(), child); - } - return true; - } else { - return false; - } - } - ).orElse(false); - } - - private static boolean isMatch(HtmlElement first, HtmlElement second) { - return second.getTagNames().contains(first.getTagName()) && - first.getAttributes().equals(second.getAttributes()); - } -} diff --git a/src/main/java/org/zwobble/mammoth/html/HtmlElement.java b/src/main/java/org/zwobble/mammoth/html/HtmlElement.java deleted file mode 100644 index b6dab22..0000000 --- a/src/main/java/org/zwobble/mammoth/html/HtmlElement.java +++ /dev/null @@ -1,48 +0,0 @@ -package org.zwobble.mammoth.html; - -import java.util.List; -import java.util.Map; - -public class HtmlElement implements HtmlNode { - private final List<String> tagNames; - private final Map<String, String> attributes; - private final List<HtmlNode> children; - private final boolean collapsible; - - public HtmlElement(List<String> tagNames, Map<String, String> attributes, List<HtmlNode> children, boolean collapsible) { - this.tagNames = tagNames; - this.attributes = attributes; - this.children = children; - this.collapsible = collapsible; - } - - public List<String> getTagNames() { - return tagNames; - } - - public String getTagName() { - return tagNames.get(0); - } - - public Map<String, String> getAttributes() { - return attributes; - } - - public List<HtmlNode> getChildren() { - return children; - } - - public boolean isCollapsible() { - return collapsible; - } - - @Override - public void visit(Visitor visitor) { - visitor.visit(this); - } - - @Override - public <T> T accept(Mapper<T> visitor) { - return visitor.visit(this); - } -} diff --git a/src/main/java/org/zwobble/mammoth/html/HtmlForceWrite.java b/src/main/java/org/zwobble/mammoth/html/HtmlForceWrite.java deleted file mode 100644 index f704fd9..0000000 --- a/src/main/java/org/zwobble/mammoth/html/HtmlForceWrite.java +++ /dev/null @@ -1,18 +0,0 @@ -package org.zwobble.mammoth.html; - -public class HtmlForceWrite implements HtmlNode { - public static final HtmlForceWrite FORCE_WRITE = new HtmlForceWrite(); - - private HtmlForceWrite() { - } - - @Override - public void visit(Visitor visitor) { - visitor.visit(this); - } - - @Override - public <T> T accept(Mapper<T> visitor) { - return visitor.visit(this); - } -} diff --git a/src/main/java/org/zwobble/mammoth/html/HtmlNode.java b/src/main/java/org/zwobble/mammoth/html/HtmlNode.java deleted file mode 100644 index 6099519..0000000 --- a/src/main/java/org/zwobble/mammoth/html/HtmlNode.java +++ /dev/null @@ -1,21 +0,0 @@ -package org.zwobble.mammoth.html; - -public interface HtmlNode { - // TODO: rename visit to accept - void visit(Visitor visitor); - <T> T accept(Mapper<T> visitor); - - interface Visitor { - void visit(HtmlElement element); - void visit(HtmlSelfClosingElement element); - void visit(HtmlTextNode node); - void visit(HtmlForceWrite forceWrite); - } - - interface Mapper<T> { - T visit(HtmlElement element); - T visit(HtmlSelfClosingElement element); - T visit(HtmlTextNode node); - T visit(HtmlForceWrite forceWrite); - } -} diff --git a/src/main/java/org/zwobble/mammoth/html/HtmlSelfClosingElement.java b/src/main/java/org/zwobble/mammoth/html/HtmlSelfClosingElement.java deleted file mode 100644 index 62bfd73..0000000 --- a/src/main/java/org/zwobble/mammoth/html/HtmlSelfClosingElement.java +++ /dev/null @@ -1,31 +0,0 @@ -package org.zwobble.mammoth.html; - -import java.util.Map; - -public class HtmlSelfClosingElement implements HtmlNode { - private final String tagName; - private final Map<String, String> attributes; - - public HtmlSelfClosingElement(String tagName, Map<String, String> attributes) { - this.tagName = tagName; - this.attributes = attributes; - } - - public String getTagName() { - return tagName; - } - - public Map<String, String> getAttributes() { - return attributes; - } - - @Override - public void visit(Visitor visitor) { - visitor.visit(this); - } - - @Override - public <T> T accept(Mapper<T> visitor) { - return visitor.visit(this); - } -} diff --git a/src/main/java/org/zwobble/mammoth/html/HtmlTextNode.java b/src/main/java/org/zwobble/mammoth/html/HtmlTextNode.java deleted file mode 100644 index a9f9fd5..0000000 --- a/src/main/java/org/zwobble/mammoth/html/HtmlTextNode.java +++ /dev/null @@ -1,23 +0,0 @@ -package org.zwobble.mammoth.html; - -public class HtmlTextNode implements HtmlNode { - private final String value; - - public HtmlTextNode(String value) { - this.value = value; - } - - public String getValue() { - return value; - } - - @Override - public void visit(Visitor visitor) { - visitor.visit(this); - } - - @Override - public <T> T accept(Mapper<T> visitor) { - return visitor.visit(this); - } -} diff --git a/src/main/java/org/zwobble/mammoth/html/HtmlWriter.java b/src/main/java/org/zwobble/mammoth/html/HtmlWriter.java deleted file mode 100644 index 4174918..0000000 --- a/src/main/java/org/zwobble/mammoth/html/HtmlWriter.java +++ /dev/null @@ -1,62 +0,0 @@ -package org.zwobble.mammoth.html; - -import java.util.Map; - -import static org.zwobble.mammoth.util.MammothLists.orderedBy; - -public class HtmlWriter { - public static void write(HtmlNode node, StringBuilder builder) { - node.visit(new HtmlNode.Visitor() { - @Override - public void visit(HtmlElement element) { - builder.append("<").append(element.getTagName()); - - HtmlWriter.generateAttributes(element.getAttributes(), builder); - - builder.append(">"); - - element.getChildren().forEach(node -> write(node, builder)); - - builder - .append("</") - .append(element.getTagName()) - .append(">"); - } - - @Override - public void visit(HtmlSelfClosingElement element) { - builder.append("<").append(element.getTagName()); - HtmlWriter.generateAttributes(element.getAttributes(), builder); - builder.append(" />"); - } - - @Override - public void visit(HtmlTextNode node) { - builder.append(HtmlWriter.escapeText(node.getValue())); - } - - @Override - public void visit(HtmlForceWrite forceWrite) { - } - }); - } - - private static void generateAttributes(Map<String, String> attributes, StringBuilder builder) { - for (Map.Entry<String, String> attribute : orderedBy(attributes.entrySet(), Map.Entry::getKey)) { - builder - .append(" ") - .append(attribute.getKey()) - .append("=\"") - .append(HtmlWriter.escapeAttributeValue(attribute.getValue())) - .append("\""); - } - } - - private static String escapeText(String text) { - return text.replace("&", "&").replace("<", "<").replace(">", ">"); - } - - private static String escapeAttributeValue(String value) { - return escapeText(value).replace("\"", """); - } -} |
