From 52140d52210eda0f25ecab9f89b5c73473e9d8e0 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 17 Apr 2016 18:57:29 +0100 Subject: Add CLI for .NET version --- dotnet/Mammoth/Couscous/Adapters.cs | 13 +++++++++ dotnet/Mammoth/Couscous/java/io/Closeable.cs | 7 +++-- dotnet/Mammoth/Couscous/java/io/File.cs | 15 +++++++++++ dotnet/Mammoth/Couscous/java/io/InputStream.cs | 2 +- dotnet/Mammoth/Couscous/java/nio/file/Path.cs | 11 +++++--- .../mammoth/internal/docx/InMemoryDocxFile.cs | 17 ++++++++++++ .../mammoth/internal/docx/ZippedDocxFile.cs | 26 ++++++++++++++++++ .../mammoth/internal/util/Base64Encoding.cs | 12 +++++++-- .../mammoth/internal/util/PassThroughException.cs | 8 ++++++ .../Mammoth/Couscous/mammoth/internal/util/Sets.cs | 3 +-- .../mammoth/internal/xml/parsing/SimpleSax.cs | 31 +++++++++++++++++++++- 11 files changed, 131 insertions(+), 14 deletions(-) create mode 100644 dotnet/Mammoth/Couscous/java/io/File.cs create mode 100644 dotnet/Mammoth/Couscous/mammoth/internal/docx/InMemoryDocxFile.cs create mode 100644 dotnet/Mammoth/Couscous/mammoth/internal/docx/ZippedDocxFile.cs (limited to 'dotnet/Mammoth/Couscous') 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 ListToList(IList list) { return new ArrayList(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 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 tryGetInputStream(string name) { + var entry = _zipFile.GetEntry(name); + if (entry == null) { + return Optional.empty(); + } 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 stream) { - throw new UnsupportedOperationException(); + internal static string streamToBase64(SupplierWithException 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(SupplierWithException 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 set(T[] values) { return new HashSet(new System.Collections.Generic.HashSet(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(); + 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) { -- cgit v1.2.3