summaryrefslogtreecommitdiff
path: root/src/main/java/org
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-02-14 11:05:26 +0000
committerMichael Williamson <mike@zwobble.org>2016-02-14 11:05:26 +0000
commitcd489318924935fb2ca6a3e1a41559da1cf6adb5 (patch)
treed63496dc7010b8a5a9ae896c58bfce6db18a4a79 /src/main/java/org
parent006ee096741fd267d9b37f92dfec33f6de5077a9 (diff)
Split out DocumentConverter
Diffstat (limited to 'src/main/java/org')
-rw-r--r--src/main/java/org/zwobble/mammoth/DocumentConverter.java95
-rw-r--r--src/main/java/org/zwobble/mammoth/Mammoth.java91
2 files changed, 96 insertions, 90 deletions
diff --git a/src/main/java/org/zwobble/mammoth/DocumentConverter.java b/src/main/java/org/zwobble/mammoth/DocumentConverter.java
new file mode 100644
index 0000000..7c0a47f
--- /dev/null
+++ b/src/main/java/org/zwobble/mammoth/DocumentConverter.java
@@ -0,0 +1,95 @@
+package org.zwobble.mammoth;
+
+import org.zwobble.mammoth.documents.*;
+import org.zwobble.mammoth.html.Html;
+import org.zwobble.mammoth.html.HtmlNode;
+
+import java.util.List;
+
+import static org.zwobble.mammoth.util.MammothLists.eagerFlatMap;
+import static org.zwobble.mammoth.util.MammothLists.list;
+
+public class DocumentConverter {
+ public static List<HtmlNode> convertToHtml(Document document) {
+ return convertChildrenToHtml(document);
+ }
+
+ private static List<HtmlNode> convertChildrenToHtml(HasChildren element) {
+ return eagerFlatMap(
+ element.getChildren(),
+ DocumentConverter::convertToHtml);
+ }
+
+ private static List<HtmlNode> convertToHtml(DocumentElement element) {
+ return element.accept(new DocumentElementVisitor<List<HtmlNode>>() {
+ @Override
+ public List<HtmlNode> visit(Paragraph paragraph) {
+ List<HtmlNode> content = convertChildrenToHtml(paragraph);
+ if (content.isEmpty()) {
+ return list();
+ } else {
+ return list(Html.element("p", content));
+ }
+ }
+
+ @Override
+ public List<HtmlNode> visit(Run run) {
+ return convertChildrenToHtml(run);
+ }
+
+ @Override
+ public List<HtmlNode> visit(Text text) {
+ if (text.getValue().isEmpty()) {
+ return list();
+ } else {
+ return list(Html.text(text.getValue()));
+ }
+ }
+
+ @Override
+ public List<HtmlNode> visit(Tab tab) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public List<HtmlNode> visit(LineBreak lineBreak) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public List<HtmlNode> visit(Table table) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public List<HtmlNode> visit(TableRow tableRow) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public List<HtmlNode> visit(TableCell tableCell) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public List<HtmlNode> visit(Hyperlink hyperlink) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public List<HtmlNode> visit(Bookmark bookmark) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public List<HtmlNode> visit(NoteReference noteReference) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public List<HtmlNode> visit(Image image) {
+ throw new UnsupportedOperationException();
+ }
+ });
+ }
+}
diff --git a/src/main/java/org/zwobble/mammoth/Mammoth.java b/src/main/java/org/zwobble/mammoth/Mammoth.java
index 40f2a6e..9da4c14 100644
--- a/src/main/java/org/zwobble/mammoth/Mammoth.java
+++ b/src/main/java/org/zwobble/mammoth/Mammoth.java
@@ -1,21 +1,15 @@
package org.zwobble.mammoth;
-import org.zwobble.mammoth.documents.*;
import org.zwobble.mammoth.docx.*;
import org.zwobble.mammoth.html.Html;
-import org.zwobble.mammoth.html.HtmlNode;
import org.zwobble.mammoth.results.Result;
import org.zwobble.mammoth.xml.XmlElement;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
-import java.util.List;
import java.util.zip.ZipFile;
-import static org.zwobble.mammoth.util.MammothLists.eagerFlatMap;
-import static org.zwobble.mammoth.util.MammothLists.list;
-
public class Mammoth {
public static Result<String> convertToHtml(File file) {
try (DocxFile zipFile = new ZippedDocxFile(new ZipFile(file))) {
@@ -39,93 +33,10 @@ public class Mammoth {
zipFile,
fileReader));
return reader.readElement(documentXml)
- .map(Mammoth::convertToHtml)
+ .map(DocumentConverter::convertToHtml)
.map(Html::write);
} catch (IOException e) {
throw new UnsupportedOperationException("Should return a result of failure");
}
}
-
- private static List<HtmlNode> convertToHtml(Document document) {
- return convertChildrenToHtml(document);
- }
-
- private static List<HtmlNode> convertChildrenToHtml(HasChildren element) {
- return eagerFlatMap(
- element.getChildren(),
- Mammoth::convertToHtml);
- }
-
- private static List<HtmlNode> convertToHtml(DocumentElement element) {
- return element.accept(new DocumentElementVisitor<List<HtmlNode>>() {
- @Override
- public List<HtmlNode> visit(Paragraph paragraph) {
- List<HtmlNode> content = convertChildrenToHtml(paragraph);
- if (content.isEmpty()) {
- return list();
- } else {
- return list(Html.element("p", content));
- }
- }
-
- @Override
- public List<HtmlNode> visit(Run run) {
- return convertChildrenToHtml(run);
- }
-
- @Override
- public List<HtmlNode> visit(Text text) {
- if (text.getValue().isEmpty()) {
- return list();
- } else {
- return list(Html.text(text.getValue()));
- }
- }
-
- @Override
- public List<HtmlNode> visit(Tab tab) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public List<HtmlNode> visit(LineBreak lineBreak) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public List<HtmlNode> visit(Table table) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public List<HtmlNode> visit(TableRow tableRow) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public List<HtmlNode> visit(TableCell tableCell) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public List<HtmlNode> visit(Hyperlink hyperlink) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public List<HtmlNode> visit(Bookmark bookmark) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public List<HtmlNode> visit(NoteReference noteReference) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public List<HtmlNode> visit(Image image) {
- throw new UnsupportedOperationException();
- }
- });
- }
}