diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-04-17 18:57:29 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-04-17 18:58:12 +0100 |
| commit | 52140d52210eda0f25ecab9f89b5c73473e9d8e0 (patch) | |
| tree | cb383edb71e44b8ae7b2b0d0890f13574d1922f0 /dotnet | |
| parent | 0f9ef005f063198ea2e16ca36beaead4bd5e3824 (diff) | |
Add CLI for .NET version
Diffstat (limited to 'dotnet')
19 files changed, 315 insertions, 14 deletions
diff --git a/dotnet/Mammoth.Cli/Mammoth.Cli.csproj b/dotnet/Mammoth.Cli/Mammoth.Cli.csproj new file mode 100644 index 0000000..f7e125e --- /dev/null +++ b/dotnet/Mammoth.Cli/Mammoth.Cli.csproj @@ -0,0 +1,44 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <ProjectGuid>{20602E78-EEBD-407B-B8C3-EDB6254BD663}</ProjectGuid> + <OutputType>Exe</OutputType> + <RootNamespace>Mammoth.Cli</RootNamespace> + <AssemblyName>Mammoth.Cli</AssemblyName> + <TargetFrameworkVersion>v4.5</TargetFrameworkVersion> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> + <DebugSymbols>true</DebugSymbols> + <DebugType>full</DebugType> + <Optimize>false</Optimize> + <OutputPath>bin\Debug</OutputPath> + <DefineConstants>DEBUG;</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + <Externalconsole>true</Externalconsole> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> + <DebugType>full</DebugType> + <Optimize>true</Optimize> + <OutputPath>bin\Release</OutputPath> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + <Externalconsole>true</Externalconsole> + </PropertyGroup> + <ItemGroup> + <Reference Include="System" /> + </ItemGroup> + <ItemGroup> + <Compile Include="Program.cs" /> + <Compile Include="Properties\AssemblyInfo.cs" /> + </ItemGroup> + <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> + <ItemGroup> + <ProjectReference Include="..\Mammoth\Mammoth.csproj"> + <Project>{51C5CBD6-8809-4D5E-BABE-1787A16F5ED8}</Project> + <Name>Mammoth</Name> + </ProjectReference> + </ItemGroup> +</Project>
\ No newline at end of file diff --git a/dotnet/Mammoth.Cli/Program.cs b/dotnet/Mammoth.Cli/Program.cs new file mode 100644 index 0000000..f45b100 --- /dev/null +++ b/dotnet/Mammoth.Cli/Program.cs @@ -0,0 +1,11 @@ +using System; +using Mammoth; + +namespace Mammoth.Cli { + internal class Program { + public static void Main(string[] args) { + var result = new DocumentConverter().ConvertToHtml(args[0]); + Console.WriteLine(result.Value); + } + } +} diff --git a/dotnet/Mammoth.Cli/Properties/AssemblyInfo.cs b/dotnet/Mammoth.Cli/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..e68abd2 --- /dev/null +++ b/dotnet/Mammoth.Cli/Properties/AssemblyInfo.cs @@ -0,0 +1,27 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +// Information about this assembly is defined by the following attributes. +// Change them to the values specific to your project. + +[assembly: AssemblyTitle ("Mammoth.Cli")] +[assembly: AssemblyDescription ("")] +[assembly: AssemblyConfiguration ("")] +[assembly: AssemblyCompany ("")] +[assembly: AssemblyProduct ("")] +[assembly: AssemblyCopyright ("mick")] +[assembly: AssemblyTrademark ("")] +[assembly: AssemblyCulture ("")] + +// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}". +// The form "{Major}.{Minor}.*" will automatically update the build and revision, +// and "{Major}.{Minor}.{Build}.*" will update just the revision. + +[assembly: AssemblyVersion ("1.0.*")] + +// The following attributes are used to specify the signing key for the assembly, +// if desired. See the Mono documentation for more information about signing. + +//[assembly: AssemblyDelaySign(false)] +//[assembly: AssemblyKeyFile("")] + diff --git a/dotnet/Mammoth.sln b/dotnet/Mammoth.sln index cd369cf..7dae37b 100644 --- a/dotnet/Mammoth.sln +++ b/dotnet/Mammoth.sln @@ -3,12 +3,18 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 2012
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mammoth", "Mammoth\Mammoth.csproj", "{51C5CBD6-8809-4D5E-BABE-1787A16F5ED8}"
EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mammoth.Cli", "Mammoth.Cli\Mammoth.Cli.csproj", "{20602E78-EEBD-407B-B8C3-EDB6254BD663}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {20602E78-EEBD-407B-B8C3-EDB6254BD663}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {20602E78-EEBD-407B-B8C3-EDB6254BD663}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {20602E78-EEBD-407B-B8C3-EDB6254BD663}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {20602E78-EEBD-407B-B8C3-EDB6254BD663}.Release|Any CPU.Build.0 = Release|Any CPU
{51C5CBD6-8809-4D5E-BABE-1787A16F5ED8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{51C5CBD6-8809-4D5E-BABE-1787A16F5ED8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{51C5CBD6-8809-4D5E-BABE-1787A16F5ED8}.Release|Any CPU.ActiveCfg = Release|Any CPU
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; + } + } +} |
