aboutsummaryrefslogtreecommitdiff
path: root/dotnet/Mammoth
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-04-17 18:57:29 +0100
committerMichael Williamson <mike@zwobble.org>2016-04-17 18:58:12 +0100
commit52140d52210eda0f25ecab9f89b5c73473e9d8e0 (patch)
treecb383edb71e44b8ae7b2b0d0890f13574d1922f0 /dotnet/Mammoth
parent0f9ef005f063198ea2e16ca36beaead4bd5e3824 (diff)
Add CLI for .NET version
Diffstat (limited to 'dotnet/Mammoth')
-rw-r--r--dotnet/Mammoth/Couscous/Adapters.cs13
-rw-r--r--dotnet/Mammoth/Couscous/java/io/Closeable.cs7
-rw-r--r--dotnet/Mammoth/Couscous/java/io/File.cs15
-rw-r--r--dotnet/Mammoth/Couscous/java/io/InputStream.cs2
-rw-r--r--dotnet/Mammoth/Couscous/java/nio/file/Path.cs11
-rw-r--r--dotnet/Mammoth/Couscous/mammoth/internal/docx/InMemoryDocxFile.cs17
-rw-r--r--dotnet/Mammoth/Couscous/mammoth/internal/docx/ZippedDocxFile.cs26
-rw-r--r--dotnet/Mammoth/Couscous/mammoth/internal/util/Base64Encoding.cs12
-rw-r--r--dotnet/Mammoth/Couscous/mammoth/internal/util/PassThroughException.cs8
-rw-r--r--dotnet/Mammoth/Couscous/mammoth/internal/util/Sets.cs3
-rw-r--r--dotnet/Mammoth/Couscous/mammoth/internal/xml/parsing/SimpleSax.cs31
-rw-r--r--dotnet/Mammoth/DocumentConverter.cs57
-rw-r--r--dotnet/Mammoth/IResult.cs8
-rw-r--r--dotnet/Mammoth/Mammoth.csproj9
-rw-r--r--dotnet/Mammoth/SimpleResult.cs22
15 files changed, 227 insertions, 14 deletions
diff --git a/dotnet/Mammoth/Couscous/Adapters.cs b/dotnet/Mammoth/Couscous/Adapters.cs
index a613473..c45f35b 100644
--- a/dotnet/Mammoth/Couscous/Adapters.cs
+++ b/dotnet/Mammoth/Couscous/Adapters.cs
@@ -2,6 +2,7 @@ using System.Collections.Generic;
using System.Linq;
using Mammoth.Couscous.java.lang;
using Mammoth.Couscous.java.util;
+using Mammoth.Couscous.java.io;
namespace Mammoth.Couscous {
internal static class FromJava {
@@ -82,5 +83,17 @@ namespace Mammoth.Couscous {
internal static java.util.List<T> ListToList<T>(IList<T> list) {
return new ArrayList<T>(list);
}
+
+ internal static java.io.InputStream StreamToInputStream(System.IO.Stream stream) {
+ return new StreamToInputStreamAdapter(stream);
+ }
+
+ private class StreamToInputStreamAdapter : InputStream {
+ public System.IO.Stream Stream { get; }
+
+ internal StreamToInputStreamAdapter(System.IO.Stream stream) {
+ Stream = stream;
+ }
+ }
}
}
diff --git a/dotnet/Mammoth/Couscous/java/io/Closeable.cs b/dotnet/Mammoth/Couscous/java/io/Closeable.cs
index 36b2a80..9dc10c5 100644
--- a/dotnet/Mammoth/Couscous/java/io/Closeable.cs
+++ b/dotnet/Mammoth/Couscous/java/io/Closeable.cs
@@ -1,9 +1,8 @@
using System;
-namespace Mammoth.Couscous.java.io
-{
- internal interface Closeable
- {
+namespace Mammoth.Couscous.java.io {
+ internal interface Closeable {
+ void close();
}
}
diff --git a/dotnet/Mammoth/Couscous/java/io/File.cs b/dotnet/Mammoth/Couscous/java/io/File.cs
new file mode 100644
index 0000000..abff40a
--- /dev/null
+++ b/dotnet/Mammoth/Couscous/java/io/File.cs
@@ -0,0 +1,15 @@
+using Mammoth.Couscous.java.nio.file;
+
+namespace Mammoth.Couscous.java.io {
+ internal class File {
+ internal string Path { get; }
+
+ internal File(string path) {
+ Path = path;
+ }
+
+ internal Path toPath() {
+ return new Path(Path);
+ }
+ }
+}
diff --git a/dotnet/Mammoth/Couscous/java/io/InputStream.cs b/dotnet/Mammoth/Couscous/java/io/InputStream.cs
index 9609a2b..b474bb8 100644
--- a/dotnet/Mammoth/Couscous/java/io/InputStream.cs
+++ b/dotnet/Mammoth/Couscous/java/io/InputStream.cs
@@ -1,5 +1,5 @@
namespace Mammoth.Couscous.java.io {
internal interface InputStream {
-
+ System.IO.Stream Stream { get; }
}
}
diff --git a/dotnet/Mammoth/Couscous/java/nio/file/Path.cs b/dotnet/Mammoth/Couscous/java/nio/file/Path.cs
index 6c85cc8..032b4bb 100644
--- a/dotnet/Mammoth/Couscous/java/nio/file/Path.cs
+++ b/dotnet/Mammoth/Couscous/java/nio/file/Path.cs
@@ -1,9 +1,12 @@
using System;
-namespace Mammoth.Couscous.java.nio.file
-{
- internal class Path
- {
+namespace Mammoth.Couscous.java.nio.file {
+ internal class Path {
+ private readonly string _path;
+
+ internal Path(string path) {
+ _path = path;
+ }
}
}
diff --git a/dotnet/Mammoth/Couscous/mammoth/internal/docx/InMemoryDocxFile.cs b/dotnet/Mammoth/Couscous/mammoth/internal/docx/InMemoryDocxFile.cs
new file mode 100644
index 0000000..a475acd
--- /dev/null
+++ b/dotnet/Mammoth/Couscous/mammoth/internal/docx/InMemoryDocxFile.cs
@@ -0,0 +1,17 @@
+using Mammoth.Couscous.java.io;
+using Mammoth.Couscous.java.util;
+
+namespace Mammoth.Couscous.org.zwobble.mammoth.@internal.docx {
+ internal class InMemoryDocxFile : DocxFile {
+ internal static DocxFile fromStream(InputStream stream) {
+ return new InMemoryDocxFile();
+ }
+
+ public Optional<InputStream> tryGetInputStream(string name) {
+ throw new System.NotImplementedException();
+ }
+
+ public void close() {
+ }
+ }
+}
diff --git a/dotnet/Mammoth/Couscous/mammoth/internal/docx/ZippedDocxFile.cs b/dotnet/Mammoth/Couscous/mammoth/internal/docx/ZippedDocxFile.cs
new file mode 100644
index 0000000..cad7a2f
--- /dev/null
+++ b/dotnet/Mammoth/Couscous/mammoth/internal/docx/ZippedDocxFile.cs
@@ -0,0 +1,26 @@
+using System.IO.Compression;
+using Mammoth.Couscous.java.io;
+using Mammoth.Couscous.java.util;
+
+namespace Mammoth.Couscous.org.zwobble.mammoth.@internal.docx {
+ internal class ZippedDocxFile : DocxFile {
+ private readonly ZipArchive _zipFile;
+
+ internal ZippedDocxFile(File file) {
+ _zipFile = ZipFile.OpenRead(file.Path);
+ }
+
+ public Optional<InputStream> tryGetInputStream(string name) {
+ var entry = _zipFile.GetEntry(name);
+ if (entry == null) {
+ return Optional.empty<InputStream>();
+ } else {
+ return Optional.of(ToJava.StreamToInputStream(entry.Open()));
+ }
+ }
+
+ public void close() {
+ _zipFile.Dispose();
+ }
+ }
+}
diff --git a/dotnet/Mammoth/Couscous/mammoth/internal/util/Base64Encoding.cs b/dotnet/Mammoth/Couscous/mammoth/internal/util/Base64Encoding.cs
index f8270b0..24d2f9c 100644
--- a/dotnet/Mammoth/Couscous/mammoth/internal/util/Base64Encoding.cs
+++ b/dotnet/Mammoth/Couscous/mammoth/internal/util/Base64Encoding.cs
@@ -3,8 +3,16 @@ using Mammoth.Couscous.java.io;
namespace Mammoth.Couscous.org.zwobble.mammoth.@internal.util {
internal static class Base64Encoding {
- internal static string streamToBase64(SupplierWithException<InputStream, IOException> stream) {
- throw new UnsupportedOperationException();
+ internal static string streamToBase64(SupplierWithException<InputStream, IOException> streamSupplier) {
+ using (var stream = streamSupplier.get().Stream) {
+ return streamToBase64(stream);
+ }
+ }
+
+ internal static string streamToBase64(System.IO.Stream stream) {
+ var memoryStream = new System.IO.MemoryStream();
+ stream.CopyTo(memoryStream);
+ return System.Convert.ToBase64String(memoryStream.ToArray());
}
}
}
diff --git a/dotnet/Mammoth/Couscous/mammoth/internal/util/PassThroughException.cs b/dotnet/Mammoth/Couscous/mammoth/internal/util/PassThroughException.cs
index 4bd2a2a..88597e3 100644
--- a/dotnet/Mammoth/Couscous/mammoth/internal/util/PassThroughException.cs
+++ b/dotnet/Mammoth/Couscous/mammoth/internal/util/PassThroughException.cs
@@ -10,6 +10,14 @@ namespace Mammoth.Couscous.org.zwobble.mammoth.@internal.util {
throw new PassThroughException(exception);
}
}
+
+ internal static T unwrap<T>(SupplierWithException<T, IOException> supplier) {
+ try {
+ return supplier.get();
+ } catch (PassThroughException exception) {
+ throw exception._exception;
+ }
+ }
private readonly IOException _exception;
diff --git a/dotnet/Mammoth/Couscous/mammoth/internal/util/Sets.cs b/dotnet/Mammoth/Couscous/mammoth/internal/util/Sets.cs
index 7843537..bfce619 100644
--- a/dotnet/Mammoth/Couscous/mammoth/internal/util/Sets.cs
+++ b/dotnet/Mammoth/Couscous/mammoth/internal/util/Sets.cs
@@ -5,8 +5,7 @@ using System;
namespace Mammoth.Couscous.org.zwobble.mammoth.@internal.util
{
- internal static class Sets
- {
+ internal static class Sets {
internal static Set<T> set<T>(T[] values) {
return new HashSet<T>(new System.Collections.Generic.HashSet<T>(values));
}
diff --git a/dotnet/Mammoth/Couscous/mammoth/internal/xml/parsing/SimpleSax.cs b/dotnet/Mammoth/Couscous/mammoth/internal/xml/parsing/SimpleSax.cs
index 8ebdf28..65f63c3 100644
--- a/dotnet/Mammoth/Couscous/mammoth/internal/xml/parsing/SimpleSax.cs
+++ b/dotnet/Mammoth/Couscous/mammoth/internal/xml/parsing/SimpleSax.cs
@@ -1,9 +1,38 @@
using Mammoth.Couscous.java.io;
+using Mammoth.Couscous.java.util;
+using System.Xml;
namespace Mammoth.Couscous.org.zwobble.mammoth.@internal.xml.parsing {
internal static class SimpleSax {
internal static void parseStream(InputStream input, SimpleSaxHandler handler) {
- throw new System.NotImplementedException();
+ var reader = XmlReader.Create(input.Stream);
+ while (reader.Read()) {
+ switch (reader.NodeType) {
+ case XmlNodeType.Element:
+ var name = new ElementName(reader.NamespaceURI, reader.LocalName);
+ var attributes = new HashMap<ElementName, string>();
+ var isEmpty = reader.IsEmptyElement;
+ for (int attributeIndex = 0; attributeIndex < reader.AttributeCount; attributeIndex++) {
+ reader.MoveToAttribute(attributeIndex);
+ attributes.put(new ElementName(reader.NamespaceURI, reader.LocalName), reader.Value);
+ }
+ handler.startElement(name, attributes);
+ if (isEmpty) {
+ handler.endElement();
+ }
+ break;
+ case XmlNodeType.CDATA:
+ case XmlNodeType.Text:
+ case XmlNodeType.SignificantWhitespace:
+ handler.characters(reader.Value);
+ break;
+ case XmlNodeType.EntityReference:
+ throw new System.NotImplementedException();
+ case XmlNodeType.EndElement:
+ handler.endElement();
+ break;
+ }
+ }
}
internal static void parseString(string value, SimpleSaxHandler handler) {
diff --git a/dotnet/Mammoth/DocumentConverter.cs b/dotnet/Mammoth/DocumentConverter.cs
new file mode 100644
index 0000000..eb8c939
--- /dev/null
+++ b/dotnet/Mammoth/DocumentConverter.cs
@@ -0,0 +1,57 @@
+using System.IO;
+using Mammoth.Couscous;
+using Mammoth.Couscous.org.zwobble.mammoth.@internal;
+using Mammoth.Couscous.org.zwobble.mammoth.@internal.conversion;
+
+namespace Mammoth {
+ public class DocumentConverter {
+ private readonly DocumentToHtmlOptions options;
+
+ public DocumentConverter() : this(DocumentToHtmlOptions._DEFAULT) {
+ }
+
+ private DocumentConverter(DocumentToHtmlOptions options) {
+ this.options = options;
+ }
+
+ public DocumentConverter IdPrefix(string idPrefix) {
+ return new DocumentConverter(options.idPrefix(idPrefix));
+ }
+
+ public DocumentConverter PreserveEmptyParagraphs() {
+ return new DocumentConverter(options.preserveEmptyParagraphs());
+ }
+
+ public DocumentConverter AddStyleMap(string styleMap) {
+ return new DocumentConverter(options.addStyleMap(styleMap));
+ }
+
+ public DocumentConverter DisableDefaultStyleMap() {
+ return new DocumentConverter(options.disableDefaultStyleMap());
+ }
+
+ public IResult<string> ConvertToHtml(Stream stream) {
+ return new InternalDocumentConverter(options)
+ .convertToHtml(ToJava.StreamToInputStream(stream))
+ .ToResult();
+ }
+
+ public IResult<string> ConvertToHtml(string path) {
+ return new InternalDocumentConverter(options)
+ .convertToHtml(new Couscous.java.io.File(path))
+ .ToResult();
+ }
+
+ public IResult<string> ExtractRawText(Stream stream) {
+ return new InternalDocumentConverter(options)
+ .extractRawText(ToJava.StreamToInputStream(stream))
+ .ToResult();
+ }
+
+ public IResult<string> ExtractRawText(string path) {
+ return new InternalDocumentConverter(options)
+ .extractRawText(new Couscous.java.io.File(path))
+ .ToResult();
+ }
+ }
+}
diff --git a/dotnet/Mammoth/IResult.cs b/dotnet/Mammoth/IResult.cs
new file mode 100644
index 0000000..7088f3a
--- /dev/null
+++ b/dotnet/Mammoth/IResult.cs
@@ -0,0 +1,8 @@
+using System.Collections.Generic;
+
+namespace Mammoth {
+ public interface IResult<T> {
+ T Value { get; }
+ ISet<string> Warnings { get; }
+ }
+}
diff --git a/dotnet/Mammoth/Mammoth.csproj b/dotnet/Mammoth/Mammoth.csproj
index ac380f8..1fe296d 100644
--- a/dotnet/Mammoth/Mammoth.csproj
+++ b/dotnet/Mammoth/Mammoth.csproj
@@ -29,14 +29,21 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
+ <Reference Include="System.Xml" />
+ <Reference Include="System.IO.Compression" />
+ <Reference Include="System.IO.Compression.FileSystem" />
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="DocumentConverter.cs" />
+ <Compile Include="IResult.cs" />
+ <Compile Include="SimpleResult.cs" />
<Compile Include="generated.cs" />
<Compile Include="Couscous\Adapters.cs" />
<Compile Include="Couscous\HorribleHacks.cs" />
<Compile Include="Couscous\IterableExtensions.cs" />
<Compile Include="Couscous\JavaStringExtensions.cs" />
+ <Compile Include="Couscous\java\io\File.cs" />
<Compile Include="Couscous\java\io\InputStream.cs" />
<Compile Include="Couscous\java\io\IOException.cs" />
<Compile Include="Couscous\java\io\Closeable.cs" />
@@ -61,7 +68,9 @@
<Compile Include="Couscous\java\util\NoSuchElementException.cs" />
<Compile Include="Couscous\java\util\Optional.cs" />
<Compile Include="Couscous\java\util\Set.cs" />
+ <Compile Include="Couscous\mammoth\internal\docx\InMemoryDocxFile.cs" />
<Compile Include="Couscous\mammoth\internal\docx\PathRelativeFileReader.cs" />
+ <Compile Include="Couscous\mammoth\internal\docx\ZippedDocxFile.cs" />
<Compile Include="Couscous\mammoth\internal\styles\parsing\StyleMapParser.cs" />
<Compile Include="Couscous\mammoth\internal\util\Base64Encoding.cs" />
<Compile Include="Couscous\mammoth\internal\util\Casts.cs" />
diff --git a/dotnet/Mammoth/SimpleResult.cs b/dotnet/Mammoth/SimpleResult.cs
new file mode 100644
index 0000000..f20cd42
--- /dev/null
+++ b/dotnet/Mammoth/SimpleResult.cs
@@ -0,0 +1,22 @@
+using System.Collections.Generic;
+using Mammoth.Couscous;
+using Mammoth.Couscous.org.zwobble.mammoth.@internal.results;
+
+namespace Mammoth {
+ internal static class SimpleResult {
+ internal static IResult<T> ToResult<T>(this InternalResult<T> result) {
+ var warnings = new HashSet<string>(FromJava.IterableToEnumerable(result.getWarnings()));
+ return new SimpleResult<T>(result.getValue(), warnings);
+ }
+ }
+
+ internal class SimpleResult<T> : IResult<T> {
+ public T Value { get; }
+ public ISet<string> Warnings { get; }
+
+ internal SimpleResult(T value, ISet<string> warnings) {
+ Value = value;
+ Warnings = warnings;
+ }
+ }
+}