aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org')
-rw-r--r--src/main/java/org/zwobble/mammoth/Mammoth.java11
-rw-r--r--src/main/java/org/zwobble/mammoth/xml/ElementName.java20
-rw-r--r--src/main/java/org/zwobble/mammoth/xml/SimpleSax.java15
-rw-r--r--src/main/java/org/zwobble/mammoth/xml/XmlElement.java64
-rw-r--r--src/main/java/org/zwobble/mammoth/xml/XmlParser.java12
-rw-r--r--src/main/java/org/zwobble/mammoth/xml/XmlTextNode.java43
6 files changed, 127 insertions, 38 deletions
diff --git a/src/main/java/org/zwobble/mammoth/Mammoth.java b/src/main/java/org/zwobble/mammoth/Mammoth.java
index aa3514f..c425280 100644
--- a/src/main/java/org/zwobble/mammoth/Mammoth.java
+++ b/src/main/java/org/zwobble/mammoth/Mammoth.java
@@ -3,6 +3,7 @@ package org.zwobble.mammoth;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
+import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.zwobble.mammoth.xml.XmlElement;
@@ -16,15 +17,13 @@ import com.google.common.collect.ImmutableBiMap;
import static com.google.common.collect.Iterables.transform;
-import lombok.val;
-
public class Mammoth {
public static String convertToHtml(File file) {
- try (val zipFile = new ZipFile(file)) {
- val entry = zipFile.getEntry("word/document.xml");
- val documentXml = parseXml(zipFile.getInputStream(entry));
- val text = extractText(documentXml);
+ try (ZipFile zipFile = new ZipFile(file)) {
+ ZipEntry entry = zipFile.getEntry("word/document.xml");
+ XmlElement documentXml = parseXml(zipFile.getInputStream(entry));
+ String text = extractText(documentXml);
if (Strings.isNullOrEmpty(text)) {
return "";
} else {
diff --git a/src/main/java/org/zwobble/mammoth/xml/ElementName.java b/src/main/java/org/zwobble/mammoth/xml/ElementName.java
index 9e51e0c..76b1c93 100644
--- a/src/main/java/org/zwobble/mammoth/xml/ElementName.java
+++ b/src/main/java/org/zwobble/mammoth/xml/ElementName.java
@@ -1,9 +1,19 @@
package org.zwobble.mammoth.xml;
-import lombok.Value;
-
-@Value
public class ElementName {
- private String uri;
- private String localName;
+ private final String uri;
+ private final String localName;
+
+ public ElementName(String uri, String localName) {
+ this.uri = uri;
+ this.localName = localName;
+ }
+
+ public String getUri() {
+ return uri;
+ }
+
+ public String getLocalName() {
+ return localName;
+ }
}
diff --git a/src/main/java/org/zwobble/mammoth/xml/SimpleSax.java b/src/main/java/org/zwobble/mammoth/xml/SimpleSax.java
index bea752a..54bd614 100644
--- a/src/main/java/org/zwobble/mammoth/xml/SimpleSax.java
+++ b/src/main/java/org/zwobble/mammoth/xml/SimpleSax.java
@@ -3,20 +3,21 @@ package org.zwobble.mammoth.xml;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
+import java.util.Map;
import java.util.stream.IntStream;
import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
+import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import static java.util.stream.Collectors.toMap;
-import lombok.val;
-
public class SimpleSax {
public static void parseStream(InputStream input, SimpleSaxHandler handler) {
parseInputSource(new InputSource(input), handler);
@@ -27,16 +28,16 @@ public class SimpleSax {
}
private static void parseInputSource(InputSource inputSource, SimpleSaxHandler handler) {
- val parserFactory = SAXParserFactory.newInstance();
+ SAXParserFactory parserFactory = SAXParserFactory.newInstance();
parserFactory.setNamespaceAware(true);
try {
- val saxParser = parserFactory.newSAXParser();
- val xmlReader = saxParser.getXMLReader();
+ SAXParser saxParser = parserFactory.newSAXParser();
+ XMLReader xmlReader = saxParser.getXMLReader();
xmlReader.setContentHandler(new DefaultHandler() {
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
- final ElementName name = new ElementName(uri, localName);
- val attributesMap = IntStream.range(0, attributes.getLength())
+ ElementName name = new ElementName(uri, localName);
+ Map<ElementName, String> attributesMap = IntStream.range(0, attributes.getLength())
.boxed()
.collect(toMap(
index -> new ElementName(attributes.getURI(index), attributes.getLocalName(index)),
diff --git a/src/main/java/org/zwobble/mammoth/xml/XmlElement.java b/src/main/java/org/zwobble/mammoth/xml/XmlElement.java
index b5e8f1b..659be2d 100644
--- a/src/main/java/org/zwobble/mammoth/xml/XmlElement.java
+++ b/src/main/java/org/zwobble/mammoth/xml/XmlElement.java
@@ -6,14 +6,6 @@ import java.util.Map;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
-import lombok.EqualsAndHashCode;
-import lombok.Getter;
-import lombok.RequiredArgsConstructor;
-import lombok.ToString;
-
-@RequiredArgsConstructor
-@EqualsAndHashCode
-@ToString
public class XmlElement implements XmlNode {
public XmlElement(String name) {
this(name, ImmutableMap.of());
@@ -23,11 +15,20 @@ public class XmlElement implements XmlNode {
this(name, attributes, ImmutableList.of());
}
- @Getter
private final String name;
private final Map<String, String> attributes;
private final List<XmlNode> children;
+ public XmlElement(String name, Map<String, String> attributes, List<XmlNode> children) {
+ this.name = name;
+ this.attributes = attributes;
+ this.children = children;
+ }
+
+ public String getName() {
+ return name;
+ }
+
@Override
public <T> T accept(XmlNodeVisitor<T> visitor) {
return visitor.visit(this);
@@ -36,4 +37,49 @@ public class XmlElement implements XmlNode {
public Iterable<XmlNode> children() {
return children;
}
+
+ @Override
+ public String toString() {
+ return "XmlElement(name=" + name + ", attributes=" + attributes
+ + ", children=" + children + ")";
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result
+ + ((attributes == null) ? 0 : attributes.hashCode());
+ result = prime * result
+ + ((children == null) ? 0 : children.hashCode());
+ result = prime * result + ((name == null) ? 0 : name.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ XmlElement other = (XmlElement) obj;
+ if (attributes == null) {
+ if (other.attributes != null)
+ return false;
+ } else if (!attributes.equals(other.attributes))
+ return false;
+ if (children == null) {
+ if (other.children != null)
+ return false;
+ } else if (!children.equals(other.children))
+ return false;
+ if (name == null) {
+ if (other.name != null)
+ return false;
+ } else if (!name.equals(other.name))
+ return false;
+ return true;
+ }
}
diff --git a/src/main/java/org/zwobble/mammoth/xml/XmlParser.java b/src/main/java/org/zwobble/mammoth/xml/XmlParser.java
index 219d02c..cf00586 100644
--- a/src/main/java/org/zwobble/mammoth/xml/XmlParser.java
+++ b/src/main/java/org/zwobble/mammoth/xml/XmlParser.java
@@ -10,8 +10,6 @@ import com.google.common.collect.BiMap;
import static java.util.stream.Collectors.toMap;
-import lombok.val;
-
public class XmlParser {
private final BiMap<String, String> namespaces;
@@ -20,13 +18,13 @@ public class XmlParser {
}
public XmlElement parseStream(InputStream inputStream) {
- val nodeGenerator = new NodeGenerator();
+ NodeGenerator nodeGenerator = new NodeGenerator();
SimpleSax.parseStream(inputStream, nodeGenerator);
return nodeGenerator.getRoot();
}
public XmlElement parseString(String value) {
- val nodeGenerator = new NodeGenerator();
+ NodeGenerator nodeGenerator = new NodeGenerator();
SimpleSax.parseString(value, nodeGenerator);
return nodeGenerator.getRoot();
}
@@ -44,13 +42,13 @@ public class XmlParser {
@Override
public void startElement(ElementName name, Map<ElementName, String> attributes) {
- val simpleAttributes = attributes
+ Map<String, String> simpleAttributes = attributes
.entrySet()
.stream()
.collect(toMap(
entry -> readName(entry.getKey()),
entry -> entry.getValue()));
- val element = new XmlElementBuilder(readName(name), simpleAttributes);
+ XmlElementBuilder element = new XmlElementBuilder(readName(name), simpleAttributes);
elementStack.add(element);
}
@@ -68,7 +66,7 @@ public class XmlParser {
@Override
public void endElement() {
if (elementStack.size() > 1) {
- val element = elementStack.removeLast();
+ XmlElementBuilder element = elementStack.removeLast();
elementStack.getLast().addChild(element.build());
}
}
diff --git a/src/main/java/org/zwobble/mammoth/xml/XmlTextNode.java b/src/main/java/org/zwobble/mammoth/xml/XmlTextNode.java
index 4d47626..1dd0275 100644
--- a/src/main/java/org/zwobble/mammoth/xml/XmlTextNode.java
+++ b/src/main/java/org/zwobble/mammoth/xml/XmlTextNode.java
@@ -1,13 +1,48 @@
package org.zwobble.mammoth.xml;
-import lombok.Value;
-
-@Value
public class XmlTextNode implements XmlNode {
- private String value;
+ private final String value;
+ public XmlTextNode(String value) {
+ this.value = value;
+ }
+
+ public String getValue() {
+ return value;
+ }
+
@Override
public <T> T accept(XmlNodeVisitor<T> visitor) {
return visitor.Visit(this);
}
+
+ @Override
+ public String toString() {
+ return "XmlTextNode(value=" + value + ")";
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((value == null) ? 0 : value.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ XmlTextNode other = (XmlTextNode) obj;
+ if (value == null) {
+ if (other.value != null)
+ return false;
+ } else if (!value.equals(other.value))
+ return false;
+ return true;
+ }
}