aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2015-12-31 16:32:17 +0000
committerMichael Williamson <mike@zwobble.org>2015-12-31 16:32:17 +0000
commitb4bb718158ed274e664ea2796198597b1c1dfa9c (patch)
tree8f5915fcac93e1897d5a7d264a585048dae81c09 /src
parent36deaa6002a0e85d7da6efd7e569d91f976d9949 (diff)
Read defaults from content types XML
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/zwobble/mammoth/docx/ContentTypes.java19
-rw-r--r--src/main/java/org/zwobble/mammoth/docx/ContentTypesXml.java25
-rw-r--r--src/main/java/org/zwobble/mammoth/util/MammothMaps.java16
-rw-r--r--src/main/java/org/zwobble/mammoth/xml/XmlElement.java20
-rw-r--r--src/main/java/org/zwobble/mammoth/xml/XmlElementList.java17
-rw-r--r--src/main/java/org/zwobble/mammoth/xml/XmlNodes.java21
-rw-r--r--src/test/java/org/zwobble/mammoth/tests/docx/ContentTypesXmlTests.java27
7 files changed, 143 insertions, 2 deletions
diff --git a/src/main/java/org/zwobble/mammoth/docx/ContentTypes.java b/src/main/java/org/zwobble/mammoth/docx/ContentTypes.java
new file mode 100644
index 0000000..18aa8e4
--- /dev/null
+++ b/src/main/java/org/zwobble/mammoth/docx/ContentTypes.java
@@ -0,0 +1,19 @@
+package org.zwobble.mammoth.docx;
+
+import com.google.common.io.Files;
+
+import java.util.Map;
+import java.util.Optional;
+
+public class ContentTypes {
+ private final Map<String, String> extensionDefaults;
+
+ public ContentTypes(Map<String, String> extensionDefaults) {
+ this.extensionDefaults = extensionDefaults;
+ }
+
+ public Optional<String> findContentType(String path) {
+ String extension = Files.getFileExtension(path);
+ return Optional.ofNullable(extensionDefaults.get(extension));
+ }
+}
diff --git a/src/main/java/org/zwobble/mammoth/docx/ContentTypesXml.java b/src/main/java/org/zwobble/mammoth/docx/ContentTypesXml.java
new file mode 100644
index 0000000..6ecf8e2
--- /dev/null
+++ b/src/main/java/org/zwobble/mammoth/docx/ContentTypesXml.java
@@ -0,0 +1,25 @@
+package org.zwobble.mammoth.docx;
+
+import org.zwobble.mammoth.xml.XmlElement;
+import org.zwobble.mammoth.xml.XmlElementList;
+
+import java.util.AbstractMap;
+import java.util.Map;
+
+import static org.zwobble.mammoth.util.MammothMaps.toMap;
+
+public class ContentTypesXml {
+ public static ContentTypes readContentTypesXmlElement(XmlElement element) {
+ return new ContentTypes(readDefaults(element.findChildren("content-types:Default")));
+ }
+
+ private static Map<String, String> readDefaults(XmlElementList children) {
+ return toMap(children, ContentTypesXml::readDefault);
+ }
+
+ private static Map.Entry<String, String> readDefault(XmlElement element) {
+ return new AbstractMap.SimpleImmutableEntry<>(
+ element.getAttribute("Extension"),
+ element.getAttribute("ContentType"));
+ }
+}
diff --git a/src/main/java/org/zwobble/mammoth/util/MammothMaps.java b/src/main/java/org/zwobble/mammoth/util/MammothMaps.java
new file mode 100644
index 0000000..6e988dd
--- /dev/null
+++ b/src/main/java/org/zwobble/mammoth/util/MammothMaps.java
@@ -0,0 +1,16 @@
+package org.zwobble.mammoth.util;
+
+import com.google.common.collect.ImmutableMap;
+
+import java.util.Map;
+import java.util.function.Function;
+
+public class MammothMaps {
+ public static <T, K, V> Map<K, V> toMap(Iterable<T> iterable, Function<T, Map.Entry<K, V>> function) {
+ ImmutableMap.Builder<K, V> builder = ImmutableMap.builder();
+ for (T element : iterable) {
+ builder.put(function.apply(element));
+ }
+ return builder.build();
+ }
+}
diff --git a/src/main/java/org/zwobble/mammoth/xml/XmlElement.java b/src/main/java/org/zwobble/mammoth/xml/XmlElement.java
index f138917..a42a1b7 100644
--- a/src/main/java/org/zwobble/mammoth/xml/XmlElement.java
+++ b/src/main/java/org/zwobble/mammoth/xml/XmlElement.java
@@ -1,10 +1,14 @@
package org.zwobble.mammoth.xml;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Iterables;
+
import java.util.List;
import java.util.Map;
+import java.util.Optional;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableMap;
+import static com.google.common.collect.Iterables.filter;
public class XmlElement implements XmlNode {
public XmlElement(String name) {
@@ -33,6 +37,11 @@ public class XmlElement implements XmlNode {
return attributes;
}
+ public String getAttribute(String name) {
+ // TODO: throw more informative error message
+ return Optional.ofNullable(attributes.get(name)).get();
+ }
+
public List<XmlNode> getChildren() {
return children;
}
@@ -51,4 +60,11 @@ public class XmlElement implements XmlNode {
return "XmlElement(name=" + name + ", attributes=" + attributes
+ ", children=" + children + ")";
}
+
+ public XmlElementList findChildren(String name) {
+ Iterable<XmlElement> filtered = filter(
+ filter(children, XmlElement.class),
+ child -> child.getName().equals(name));
+ return new XmlElementList(ImmutableList.copyOf(filtered));
+ }
}
diff --git a/src/main/java/org/zwobble/mammoth/xml/XmlElementList.java b/src/main/java/org/zwobble/mammoth/xml/XmlElementList.java
new file mode 100644
index 0000000..9e46314
--- /dev/null
+++ b/src/main/java/org/zwobble/mammoth/xml/XmlElementList.java
@@ -0,0 +1,17 @@
+package org.zwobble.mammoth.xml;
+
+import java.util.Iterator;
+import java.util.List;
+
+public class XmlElementList implements Iterable<XmlElement> {
+ private final List<XmlElement> elements;
+
+ public XmlElementList(List<XmlElement> elements) {
+ this.elements = elements;
+ }
+
+ @Override
+ public Iterator<XmlElement> iterator() {
+ return elements.iterator();
+ }
+}
diff --git a/src/main/java/org/zwobble/mammoth/xml/XmlNodes.java b/src/main/java/org/zwobble/mammoth/xml/XmlNodes.java
new file mode 100644
index 0000000..272a710
--- /dev/null
+++ b/src/main/java/org/zwobble/mammoth/xml/XmlNodes.java
@@ -0,0 +1,21 @@
+package org.zwobble.mammoth.xml;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+
+import java.util.List;
+import java.util.Map;
+
+public class XmlNodes {
+ public static XmlElement element(String name, Map<String, String> attributes) {
+ return element(name, attributes, ImmutableList.of());
+ }
+
+ public static XmlElement element(String name, List<XmlNode> children) {
+ return element(name, ImmutableMap.of(), children);
+ }
+
+ public static XmlElement element(String name, Map<String, String> attributes, List<XmlNode> children) {
+ return new XmlElement(name, attributes, children);
+ }
+}
diff --git a/src/test/java/org/zwobble/mammoth/tests/docx/ContentTypesXmlTests.java b/src/test/java/org/zwobble/mammoth/tests/docx/ContentTypesXmlTests.java
new file mode 100644
index 0000000..6b46456
--- /dev/null
+++ b/src/test/java/org/zwobble/mammoth/tests/docx/ContentTypesXmlTests.java
@@ -0,0 +1,27 @@
+package org.zwobble.mammoth.tests.docx;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import org.junit.Test;
+import org.zwobble.mammoth.docx.ContentTypes;
+import org.zwobble.mammoth.xml.XmlElement;
+
+import java.util.Optional;
+
+import static org.junit.Assert.assertEquals;
+import static org.zwobble.mammoth.docx.ContentTypesXml.readContentTypesXmlElement;
+import static org.zwobble.mammoth.xml.XmlNodes.element;
+
+public class ContentTypesXmlTests {
+ @Test
+ public void contentTypesBasedOnDefaultForExtensionIfThereIsNoOverride() {
+ XmlElement element = element("content-types:Types", ImmutableList.of(
+ element("content-types:Default", ImmutableMap.of(
+ "Extension", "png",
+ "ContentType", "image/png"))));
+ ContentTypes contentTypes = readContentTypesXmlElement(element);
+ assertEquals(
+ Optional.of("image/png"),
+ contentTypes.findContentType("word/media/hat.png"));
+ }
+}