diff options
Diffstat (limited to 'src/main')
9 files changed, 119 insertions, 37 deletions
diff --git a/src/main/java/org/zwobble/mammoth/DocumentConverter.java b/src/main/java/org/zwobble/mammoth/DocumentConverter.java index 154b878..0cf100c 100644 --- a/src/main/java/org/zwobble/mammoth/DocumentConverter.java +++ b/src/main/java/org/zwobble/mammoth/DocumentConverter.java @@ -131,7 +131,14 @@ public class DocumentConverter { if (run.isBold()) { nodes = list(Html.collapsibleElement("strong", nodes)); } - return nodes; + HtmlPath mapping = styleMap.getRunHtmlPath(run) + .orElseGet(() -> { + if (run.getStyle().isPresent()) { + warnings.add(warning("Unrecognised run style: " + run.getStyle().get().describe())); + } + return HtmlPath.EMPTY; + }); + return mapping.wrap(nodes); } @Override diff --git a/src/main/java/org/zwobble/mammoth/Mammoth.java b/src/main/java/org/zwobble/mammoth/Mammoth.java index 4963c3a..e89e282 100644 --- a/src/main/java/org/zwobble/mammoth/Mammoth.java +++ b/src/main/java/org/zwobble/mammoth/Mammoth.java @@ -11,10 +11,7 @@ import org.zwobble.mammoth.docx.InMemoryDocxFile; import org.zwobble.mammoth.docx.ZippedDocxFile; import org.zwobble.mammoth.html.Html; import org.zwobble.mammoth.results.Result; -import org.zwobble.mammoth.styles.HtmlPath; -import org.zwobble.mammoth.styles.HtmlPathElement; -import org.zwobble.mammoth.styles.ParagraphMatcher; -import org.zwobble.mammoth.styles.StyleMap; +import org.zwobble.mammoth.styles.*; import org.zwobble.mammoth.util.Casts; import java.io.File; @@ -51,9 +48,14 @@ public class Mammoth { private static final StyleMap DEFAULT_STYLE_MAP = StyleMap.builder() .mapParagraph(ParagraphMatcher.styleName("footnote text"), HtmlPath.element("p")) + .mapRun(RunMatcher.styleName("footnote reference"), HtmlPath.EMPTY) .mapParagraph(ParagraphMatcher.styleName("endnote text"), HtmlPath.element("p")) + .mapRun(RunMatcher.styleName("endnote reference"), HtmlPath.EMPTY) + .mapParagraph(ParagraphMatcher.styleName("Footnote"), HtmlPath.element("p")) + .mapRun(RunMatcher.styleName("Footnote anchor"), HtmlPath.EMPTY) .mapParagraph(ParagraphMatcher.styleName("Endnote"), HtmlPath.element("p")) + .mapRun(RunMatcher.styleName("Endnote anchor"), HtmlPath.EMPTY) .mapParagraph( ParagraphMatcher.unorderedList("0"), @@ -61,6 +63,7 @@ public class Mammoth { HtmlPathElement.collapsible("ul"), HtmlPathElement.fresh("li")))) + .mapRun(RunMatcher.styleName("Hyperlink"), HtmlPath.EMPTY) .mapParagraph(ParagraphMatcher.styleName("Normal"), HtmlPath.element("p")) .build(); diff --git a/src/main/java/org/zwobble/mammoth/styles/DocumentElementMatcher.java b/src/main/java/org/zwobble/mammoth/styles/DocumentElementMatcher.java new file mode 100644 index 0000000..19d5be5 --- /dev/null +++ b/src/main/java/org/zwobble/mammoth/styles/DocumentElementMatcher.java @@ -0,0 +1,5 @@ +package org.zwobble.mammoth.styles; + +public interface DocumentElementMatcher<T> { + boolean matches(T element); +} diff --git a/src/main/java/org/zwobble/mammoth/styles/DocumentElementMatching.java b/src/main/java/org/zwobble/mammoth/styles/DocumentElementMatching.java new file mode 100644 index 0000000..0ed00fc --- /dev/null +++ b/src/main/java/org/zwobble/mammoth/styles/DocumentElementMatching.java @@ -0,0 +1,26 @@ +package org.zwobble.mammoth.styles; + +import org.zwobble.mammoth.documents.Style; + +import java.util.Optional; +import java.util.function.BiPredicate; + +class DocumentElementMatching { + static boolean matchesStyle(Optional<String> styleId, Optional<String> styleName, Optional<Style> style) { + return matchesStyleId(styleId, style) && matchesStyleName(styleName, style); + } + + private static boolean matchesStyleId(Optional<String> styleId, Optional<Style> style) { + return matches(styleId, style.map(Style::getStyleId), Object::equals); + } + + private static boolean matchesStyleName(Optional<String> styleName, Optional<Style> style) { + return matches(styleName, style.flatMap(Style::getName), String::equalsIgnoreCase); + } + + static <T> boolean matches(Optional<T> required, Optional<T> actual, BiPredicate<T, T> areEqual) { + return required + .map(requiredValue -> actual.map(actualValue -> areEqual.test(requiredValue, actualValue)).orElse(false)) + .orElse(true); + } +} diff --git a/src/main/java/org/zwobble/mammoth/styles/ParagraphMatcher.java b/src/main/java/org/zwobble/mammoth/styles/ParagraphMatcher.java index 5ce1512..395a20b 100644 --- a/src/main/java/org/zwobble/mammoth/styles/ParagraphMatcher.java +++ b/src/main/java/org/zwobble/mammoth/styles/ParagraphMatcher.java @@ -2,12 +2,10 @@ package org.zwobble.mammoth.styles; import org.zwobble.mammoth.documents.NumberingLevel; import org.zwobble.mammoth.documents.Paragraph; -import org.zwobble.mammoth.documents.Style; import java.util.Optional; -import java.util.function.BiPredicate; -public class ParagraphMatcher { +public class ParagraphMatcher implements DocumentElementMatcher<Paragraph> { public static final ParagraphMatcher ANY = new ParagraphMatcher(Optional.empty(), Optional.empty(), Optional.empty()); public static ParagraphMatcher styleId(String styleId) { @@ -36,26 +34,17 @@ public class ParagraphMatcher { this.numbering = numbering; } + @Override public boolean matches(Paragraph paragraph) { - return matchesStyleId(paragraph) && matchesStyleName(paragraph) && matchesNumbering(paragraph); + return matchesStyle(paragraph) && matchesNumbering(paragraph); } - private boolean matchesStyleId(Paragraph paragraph) { - return matches(styleId, paragraph.getStyle().map(Style::getStyleId), Object::equals); - } - - private boolean matchesStyleName(Paragraph paragraph) { - return matches(styleName, paragraph.getStyle().flatMap(Style::getName), String::equalsIgnoreCase); + private boolean matchesStyle(Paragraph paragraph) { + return DocumentElementMatching.matchesStyle(styleId, styleName, paragraph.getStyle()); } private boolean matchesNumbering(Paragraph paragraph) { - return matches(numbering, paragraph.getNumbering(), (first, second) -> + return DocumentElementMatching.matches(numbering, paragraph.getNumbering(), (first, second) -> first.isOrdered() == second.isOrdered() && first.getLevelIndex().equalsIgnoreCase(second.getLevelIndex())); } - - private <T> boolean matches(Optional<T> required, Optional<T> actual, BiPredicate<T, T> areEqual) { - return required - .map(requiredValue -> actual.map(actualValue -> areEqual.test(requiredValue, actualValue)).orElse(false)) - .orElse(true); - } } diff --git a/src/main/java/org/zwobble/mammoth/styles/RunMatcher.java b/src/main/java/org/zwobble/mammoth/styles/RunMatcher.java new file mode 100644 index 0000000..75e883c --- /dev/null +++ b/src/main/java/org/zwobble/mammoth/styles/RunMatcher.java @@ -0,0 +1,28 @@ +package org.zwobble.mammoth.styles; + +import org.zwobble.mammoth.documents.Run; + +import java.util.Optional; + +public class RunMatcher implements DocumentElementMatcher<Run> { + public static RunMatcher styleId(String styleId) { + return new RunMatcher(Optional.of(styleId), Optional.empty()); + } + + public static RunMatcher styleName(String styleName) { + return new RunMatcher(Optional.empty(), Optional.of(styleName)); + } + + private final Optional<String> styleId; + private final Optional<String> styleName; + + public RunMatcher(Optional<String> styleId, Optional<String> styleName) { + this.styleId = styleId; + this.styleName = styleName; + } + + @Override + public boolean matches(Run run) { + return DocumentElementMatching.matchesStyle(styleId, styleName, run.getStyle()); + } +} diff --git a/src/main/java/org/zwobble/mammoth/styles/StyleMap.java b/src/main/java/org/zwobble/mammoth/styles/StyleMap.java index 7aae7df..d214acb 100644 --- a/src/main/java/org/zwobble/mammoth/styles/StyleMap.java +++ b/src/main/java/org/zwobble/mammoth/styles/StyleMap.java @@ -2,6 +2,7 @@ package org.zwobble.mammoth.styles; import com.google.common.collect.Iterables; import org.zwobble.mammoth.documents.Paragraph; +import org.zwobble.mammoth.documents.Run; import java.util.List; import java.util.Optional; @@ -15,12 +16,19 @@ public class StyleMap { private final HtmlPath underline; private final HtmlPath strikethrough; - private final List<StyleMapping> paragraphStyles; - - public StyleMap(HtmlPath underline, HtmlPath strikethrough, List<StyleMapping> paragraphStyles) { + private final List<StyleMapping<Paragraph>> paragraphStyles; + private final List<StyleMapping<Run>> runStyles; + + public StyleMap( + HtmlPath underline, + HtmlPath strikethrough, + List<StyleMapping<Paragraph>> paragraphStyles, + List<StyleMapping<Run>> runStyles) + { this.underline = underline; this.strikethrough = strikethrough; this.paragraphStyles = paragraphStyles; + this.runStyles = runStyles; } public HtmlPath getUnderline() { @@ -32,7 +40,17 @@ public class StyleMap { } public Optional<HtmlPath> getParagraphHtmlPath(Paragraph paragraph) { - com.google.common.base.Optional<StyleMapping> mapping = Iterables.tryFind(paragraphStyles, styleMapping -> styleMapping.matches(paragraph)); + com.google.common.base.Optional<StyleMapping<Paragraph>> mapping = Iterables.tryFind( + paragraphStyles, + styleMapping -> styleMapping.matches(paragraph)); + return Optional.ofNullable(mapping.orNull()) + .map(StyleMapping::getHtmlPath); + } + + public Optional<HtmlPath> getRunHtmlPath(Run run) { + com.google.common.base.Optional<StyleMapping<Run>> mapping = Iterables.tryFind( + runStyles, + styleMapping -> styleMapping.matches(run)); return Optional.ofNullable(mapping.orNull()) .map(StyleMapping::getHtmlPath); } diff --git a/src/main/java/org/zwobble/mammoth/styles/StyleMapBuilder.java b/src/main/java/org/zwobble/mammoth/styles/StyleMapBuilder.java index b33e0db..cdc3f31 100644 --- a/src/main/java/org/zwobble/mammoth/styles/StyleMapBuilder.java +++ b/src/main/java/org/zwobble/mammoth/styles/StyleMapBuilder.java @@ -1,11 +1,14 @@ package org.zwobble.mammoth.styles; import com.google.common.collect.ImmutableList; +import org.zwobble.mammoth.documents.Paragraph; +import org.zwobble.mammoth.documents.Run; public class StyleMapBuilder { private HtmlPath underline; private HtmlPath strikethrough; - private final ImmutableList.Builder<StyleMapping> paragraphStyles = ImmutableList.builder(); + private final ImmutableList.Builder<StyleMapping<Paragraph>> paragraphStyles = ImmutableList.builder(); + private final ImmutableList.Builder<StyleMapping<Run>> runStyles = ImmutableList.builder(); public StyleMapBuilder() { this.underline = HtmlPath.EMPTY; @@ -22,12 +25,17 @@ public class StyleMapBuilder { return this; } - public StyleMapBuilder mapParagraph(ParagraphMatcher paragraphMatcher, HtmlPath path) { - paragraphStyles.add(new StyleMapping(paragraphMatcher, path)); + public StyleMapBuilder mapParagraph(ParagraphMatcher matcher, HtmlPath path) { + paragraphStyles.add(new StyleMapping<Paragraph>(matcher, path)); + return this; + } + + public StyleMapBuilder mapRun(RunMatcher matcher, HtmlPath path) { + runStyles.add(new StyleMapping<Run>(matcher, path)); return this; } public StyleMap build() { - return new StyleMap(underline, strikethrough, paragraphStyles.build()); + return new StyleMap(underline, strikethrough, paragraphStyles.build(), runStyles.build()); } } diff --git a/src/main/java/org/zwobble/mammoth/styles/StyleMapping.java b/src/main/java/org/zwobble/mammoth/styles/StyleMapping.java index 59abb84..c94d1aa 100644 --- a/src/main/java/org/zwobble/mammoth/styles/StyleMapping.java +++ b/src/main/java/org/zwobble/mammoth/styles/StyleMapping.java @@ -1,18 +1,16 @@ package org.zwobble.mammoth.styles; -import org.zwobble.mammoth.documents.Paragraph; - -public class StyleMapping { - private final ParagraphMatcher matcher; +public class StyleMapping<T> { + private final DocumentElementMatcher<T> matcher; private final HtmlPath htmlPath; - public StyleMapping(ParagraphMatcher matcher, HtmlPath htmlPath) { + public StyleMapping(DocumentElementMatcher<T> matcher, HtmlPath htmlPath) { this.matcher = matcher; this.htmlPath = htmlPath; } - public boolean matches(Paragraph paragraph) { - return matcher.matches(paragraph); + public boolean matches(T element) { + return matcher.matches(element); } public HtmlPath getHtmlPath() { |
