diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-04-30 17:20:53 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-04-30 17:20:53 +0100 |
| commit | ec5037c01868fecaec2eba89b975f3e53aded89a (patch) | |
| tree | e0d5a092419bace577a90eb29699f5be57f36078 /Mammoth/Couscous/mammoth | |
| parent | 17d1a1f710edd8dfb92e75bcd273e04cee7c8146 (diff) | |
Export from java-mammoth 5f538798b147f0e76
Diffstat (limited to 'Mammoth/Couscous/mammoth')
15 files changed, 501 insertions, 0 deletions
diff --git a/Mammoth/Couscous/mammoth/internal/docx/InMemoryDocxFile.cs b/Mammoth/Couscous/mammoth/internal/docx/InMemoryDocxFile.cs new file mode 100644 index 0000000..9b7efd0 --- /dev/null +++ b/Mammoth/Couscous/mammoth/internal/docx/InMemoryDocxFile.cs @@ -0,0 +1,13 @@ +using Mammoth.Couscous.java.io; +using Mammoth.Couscous.java.util; +using System.IO; + +namespace Mammoth.Couscous.org.zwobble.mammoth.@internal.docx { + internal class InMemoryDocxFile { + internal static DocxFile fromStream(InputStream stream) { + var memoryStream = new MemoryStream(); + stream.Stream.CopyTo(memoryStream); + return new ZippedDocxFile(memoryStream); + } + } +} diff --git a/Mammoth/Couscous/mammoth/internal/docx/ZippedDocxFile.cs b/Mammoth/Couscous/mammoth/internal/docx/ZippedDocxFile.cs new file mode 100644 index 0000000..cf39e63 --- /dev/null +++ b/Mammoth/Couscous/mammoth/internal/docx/ZippedDocxFile.cs @@ -0,0 +1,30 @@ +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); + } + + internal ZippedDocxFile(System.IO.Stream stream) { + _zipFile = new ZipArchive(stream); + } + + 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/Mammoth/Couscous/mammoth/internal/styles/parsing/LineParseException.cs b/Mammoth/Couscous/mammoth/internal/styles/parsing/LineParseException.cs new file mode 100644 index 0000000..6ca9381 --- /dev/null +++ b/Mammoth/Couscous/mammoth/internal/styles/parsing/LineParseException.cs @@ -0,0 +1,15 @@ +using Mammoth.Couscous.java.lang; + +namespace Mammoth.Couscous.org.zwobble.mammoth.@internal.styles.parsing { + internal class LineParseException : RuntimeException { + private readonly Token _token; + + internal LineParseException(Token token, string message) : base(message) { + _token = token; + } + + internal Token getToken() { + return _token; + } + } +} diff --git a/Mammoth/Couscous/mammoth/internal/styles/parsing/ParseException.cs b/Mammoth/Couscous/mammoth/internal/styles/parsing/ParseException.cs new file mode 100644 index 0000000..73f349c --- /dev/null +++ b/Mammoth/Couscous/mammoth/internal/styles/parsing/ParseException.cs @@ -0,0 +1,8 @@ +using Mammoth.Couscous.java.lang; + +namespace Mammoth.Couscous.org.zwobble.mammoth.@internal.styles.parsing { + internal class ParseException : RuntimeException { + internal ParseException(string message) : base(message) { + } + } +} diff --git a/Mammoth/Couscous/mammoth/internal/styles/parsing/StyleMapParser.cs b/Mammoth/Couscous/mammoth/internal/styles/parsing/StyleMapParser.cs new file mode 100644 index 0000000..53315f3 --- /dev/null +++ b/Mammoth/Couscous/mammoth/internal/styles/parsing/StyleMapParser.cs @@ -0,0 +1,22 @@ +using Mammoth.Couscous.java.util; +using Mammoth.Couscous.org.zwobble.mammoth.@internal.styles; + +namespace Mammoth.Couscous.org.zwobble.mammoth.@internal.styles.parsing { + internal static class StyleMapParser { + internal static StyleMap parseStyleMappings(List<string> mappings) { + return DefaultStyleMap(); + } + + internal static StyleMap parse(string styleMap) { + return DefaultStyleMap(); + } + + private static StyleMap DefaultStyleMap() { + return StyleMap.builder() + + .mapParagraph(ParagraphMatcher.styleName("Normal"), HtmlPath.element("p")) + + .build(); + } + } +} diff --git a/Mammoth/Couscous/mammoth/internal/util/Base64Encoding.cs b/Mammoth/Couscous/mammoth/internal/util/Base64Encoding.cs new file mode 100644 index 0000000..24d2f9c --- /dev/null +++ b/Mammoth/Couscous/mammoth/internal/util/Base64Encoding.cs @@ -0,0 +1,18 @@ +using Mammoth.Couscous.java.lang; +using Mammoth.Couscous.java.io; + +namespace Mammoth.Couscous.org.zwobble.mammoth.@internal.util { + internal static class Base64Encoding { + 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/Mammoth/Couscous/mammoth/internal/util/Casts.cs b/Mammoth/Couscous/mammoth/internal/util/Casts.cs new file mode 100644 index 0000000..c342e20 --- /dev/null +++ b/Mammoth/Couscous/mammoth/internal/util/Casts.cs @@ -0,0 +1,13 @@ +using Mammoth.Couscous.java.util; + +namespace Mammoth.Couscous.org.zwobble.mammoth.@internal.util { + internal static class Casts { + internal static Optional<T> tryCast<T>(System.Type type, object value) { + if (value is T) { + return new Some<T>((T) value); + } else { + return new None<T>(); + } + } + } +} diff --git a/Mammoth/Couscous/mammoth/internal/util/Iterables.cs b/Mammoth/Couscous/mammoth/internal/util/Iterables.cs new file mode 100644 index 0000000..b1e1477 --- /dev/null +++ b/Mammoth/Couscous/mammoth/internal/util/Iterables.cs @@ -0,0 +1,60 @@ +using System.Collections.Generic; +using System.Linq; +using Mammoth.Couscous.java.lang; +using Mammoth.Couscous.java.util; +using Mammoth.Couscous.java.util.function; + +namespace Mammoth.Couscous.org.zwobble.mammoth.@internal.util +{ + internal static class Iterables + { + internal static T getFirst<T>(Iterable<T> iterable, T defaultValue) { + var iterator = iterable.iterator(); + if (iterator.hasNext()) { + return iterator.next(); + } else { + return defaultValue; + } + } + + internal static Optional<T> tryFind<T>(Iterable<T> iterable, Predicate<T> predicate) { + var iterator = iterable.iterator(); + while (iterator.hasNext()) { + T value = iterator.next(); + if (predicate.test(value)) { + return new Some<T>(value); + } + } + return new None<T>(); + } + + internal static Iterable<T> lazyFilter<T>(Iterable<T> iterable, Predicate<T> predicate) { + var enumerable = FromJava.IterableToEnumerable(iterable).Where(predicate.test); + return ToJava.EnumerableToIterable(enumerable); + } + + internal static Iterable<U> lazyFilter<T, U>(Iterable<T> iterable, System.Type type) { + var enumerable = FromJava.IterableToEnumerable(iterable).OfType<U>(); + return ToJava.EnumerableToIterable(enumerable); + } + + internal static Iterable<U> lazyMap<T, U>(Iterable<T> iterable, Function<T, U> function) { + var enumerable = FromJava.IterableToEnumerable(iterable) + .Select(function.apply); + return ToJava.EnumerableToIterable(enumerable); + } + + internal static Iterable<U> lazyFlatMap<T, U>(Iterable<T> iterable, Function<T, Iterable<U>> function) { + var enumerable = FromJava.IterableToEnumerable(iterable) + .SelectMany(element => FromJava.IterableToEnumerable(function.apply(element))); + return ToJava.EnumerableToIterable(enumerable); + } + + internal static Iterable<T> lazyConcat<T>(Iterable<T> first, Iterable<T> second) { + var enumerable = FromJava.IterableToEnumerable(first) + .Concat(FromJava.IterableToEnumerable(second)); + return ToJava.EnumerableToIterable(enumerable); + } + } +} + diff --git a/Mammoth/Couscous/mammoth/internal/util/Lists.cs b/Mammoth/Couscous/mammoth/internal/util/Lists.cs new file mode 100644 index 0000000..b914a34 --- /dev/null +++ b/Mammoth/Couscous/mammoth/internal/util/Lists.cs @@ -0,0 +1,97 @@ +using Mammoth.Couscous.java.lang; +using Mammoth.Couscous.java.util; +using Mammoth.Couscous.java.util.function; +using System.Linq; + +namespace Mammoth.Couscous.org.zwobble.mammoth.@internal.util { + internal static class Lists { + internal static List<T> list<T>(params T[] values) { + return ToJava.ListToList(values); + } + + internal static List<T> toList<T>(Iterable<T> iterable) { + return ToJava.ListToList(FromJava.IterableToEnumerable(iterable).ToList()); + } + + internal static List<U> eagerMap<T, U>(Iterable<T> iterable, Function<T, U> function) { + var result = FromJava.IterableToEnumerable(iterable) + .Select(function.apply) + .ToList(); + return ToJava.ListToList(result); + } + + internal static List<U> eagerFlatMap<T, U>(Iterable<T> iterable, Function<T, Iterable<U>> function) { + var result = FromJava.IterableToEnumerable(iterable) + .SelectMany(element => FromJava.IterableToEnumerable(function.apply(element))) + .ToList(); + return ToJava.ListToList(result); + } + + internal static List<T> cons<T>(T head, Iterable<T> tail) { + var result = new T[] { head } + .Concat(FromJava.IterableToEnumerable(tail)) + .ToList(); + return ToJava.ListToList(result); + } + + internal static List<T> eagerConcat<T>(Iterable<T> first, Iterable<T> second) { + var result = FromJava.IterableToEnumerable(first) + .Concat(FromJava.IterableToEnumerable(second)) + .ToList(); + return ToJava.ListToList(result); + } + + internal static List<T> orderedBy<T, R>(Iterable<T> iterable, Function<T, R> getKey) { + var result = FromJava.IterableToEnumerable(iterable) + .OrderBy(getKey.apply) + .ToList(); + return ToJava.ListToList(result); + } + + internal static Optional<T> tryGetLast<T>(List<T> list) { + if (list.isEmpty()) { + return new None<T>(); + } else { + return new Some<T>(list.get(list.size() - 1)); + } + } + + internal static Iterable<T> reversed<T>(List<T> list) { + return new ReversedList<T>(list); + } + + private class ReversedList<T> : Iterable<T> { + private readonly List<T> _list; + + internal ReversedList(List<T> list) { + _list = list; + } + + public Iterator<T> iterator() { + return new ReverseIterator<T>(_list); + } + } + + private class ReverseIterator<T> : Iterator<T> { + private readonly List<T> _list; + private int _nextIndex; + + internal ReverseIterator(List<T> list) { + _list = list; + _nextIndex = _list.size() - 1; + } + + public bool hasNext() { + return _nextIndex >= 0; + } + + public T next() { + if (hasNext()) { + return _list.get(_nextIndex--); + } else { + throw new NoSuchElementException(); + } + } + } + } +} diff --git a/Mammoth/Couscous/mammoth/internal/util/Maps.cs b/Mammoth/Couscous/mammoth/internal/util/Maps.cs new file mode 100644 index 0000000..bee2dac --- /dev/null +++ b/Mammoth/Couscous/mammoth/internal/util/Maps.cs @@ -0,0 +1,110 @@ +using System.Linq; +using Mammoth.Couscous.java.util; +using Mammoth.Couscous.java.lang; +using Mammoth.Couscous.java.util.function; +using Mammoth.Couscous; + +namespace Mammoth.Couscous.org.zwobble.mammoth.@internal.util +{ + internal static class Maps + { + internal static Map<K, V> map<K, V>() { + return ToJava.DictionaryToMap(new System.Collections.Generic.Dictionary<K, V>()); + } + + internal static Map<K, V> map<K, V>(K key1, V value1) { + return ToJava.DictionaryToMap(new System.Collections.Generic.Dictionary<K, V> { + {key1, value1} + }); + } + + internal static Map<K, V> map<K, V>(K key1, V value1, K key2, V value2) { + return ToJava.DictionaryToMap(new System.Collections.Generic.Dictionary<K, V> { + {key1, value1}, + {key2, value2} + }); + } + + internal static Map<K, V> toMap<T, K, V>(Iterable<T> iterable, Function<T, Map__Entry<K, V>> function) { + var dictionary = FromJava.IterableToEnumerable(iterable) + .Select(function.apply) + .ToDictionary(entry => entry.getKey(), entry => entry.getValue()); + return ToJava.DictionaryToMap(dictionary); + + } + + internal static Map<K, T> toMapWithKey<T, K>(Iterable<T> iterable, Function<T, K> function) { + var dictionary = FromJava.IterableToEnumerable(iterable) + .ToDictionary(function.apply); + return ToJava.DictionaryToMap(dictionary); + } + + internal static Map<K, java.util.List<T>> toMultiMapWithKey<T, K>(Iterable<T> iterable, Function<T, K> function) { + var dictionary = FromJava.IterableToEnumerable(iterable) + .GroupBy(value => function.apply(value)) + .ToDictionary( + grouping => grouping.Key, + grouping => ToJava.ListToList(grouping.ToList())); + return ToJava.DictionaryToMap(dictionary); + } + + internal static Optional<V> lookup<K, V>(Map<K, V> map, K key) { + return map._lookup(key); + } + + internal static Map<K2, V> eagerMapKeys<K1, K2, V>(Map<K1, V> map, Function<K1, K2> function) { + var dictionary = FromJava.MapToDictionary(map) + .ToDictionary( + entry => function.apply(entry.Key), + entry => entry.Value); + return ToJava.DictionaryToMap(dictionary); + } + + internal static Map<K, V2> eagerMapValues<K, V1, V2>(Map<K, V1> map, Function<V1, V2> function) { + var dictionary = FromJava.MapToDictionary(map) + .ToDictionary( + entry => entry.Key, + entry => function.apply(entry.Value)); + return ToJava.DictionaryToMap(dictionary); + } + + internal static Map__Entry<K, V> entry<K, V>(K key, V value) { + return new Entry<K, V>(key, value); + } + + private class Entry<K, V> : Map__Entry<K, V> { + private readonly K _key; + private readonly V _value; + + internal Entry(K key, V value) { + this._key = key; + this._value = value; + } + + public K getKey() { + return _key; + } + + public V getValue() { + return _value; + } + } + + internal static Builder<K, V> builder<K, V>() { + return new Builder<K, V>(); + } + + internal class Builder<K, V> { + private readonly System.Collections.Generic.IDictionary<K, V> _values = new System.Collections.Generic.Dictionary<K, V>(); + + internal Builder<K, V> put(K key, V value) { + _values[key] = value; + return this; + } + + internal Map<K, V> build() { + return ToJava.DictionaryToMap(_values); + } + } + } +} diff --git a/Mammoth/Couscous/mammoth/internal/util/PassThroughException.cs b/Mammoth/Couscous/mammoth/internal/util/PassThroughException.cs new file mode 100644 index 0000000..88597e3 --- /dev/null +++ b/Mammoth/Couscous/mammoth/internal/util/PassThroughException.cs @@ -0,0 +1,29 @@ +using Mammoth.Couscous.java.lang; +using Mammoth.Couscous.java.io; + +namespace Mammoth.Couscous.org.zwobble.mammoth.@internal.util { + internal class PassThroughException : RuntimeException { + internal static T wrap<T>(SupplierWithException<T, IOException> supplier) { + try { + return supplier.get(); + } catch (IOException exception) { + 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; + + internal PassThroughException(IOException exception) { + _exception = exception; + } + } +} + diff --git a/Mammoth/Couscous/mammoth/internal/util/Paths.cs b/Mammoth/Couscous/mammoth/internal/util/Paths.cs new file mode 100644 index 0000000..ca876eb --- /dev/null +++ b/Mammoth/Couscous/mammoth/internal/util/Paths.cs @@ -0,0 +1,8 @@ +namespace Mammoth.Couscous.org.zwobble.mammoth.@internal.util { + internal static class Paths { + internal static string getExtension(string path) { + int dotIndex = path.LastIndexOf('.'); + return dotIndex == -1 ? "" : path.Substring(dotIndex + 1); + } + } +} diff --git a/Mammoth/Couscous/mammoth/internal/util/Sets.cs b/Mammoth/Couscous/mammoth/internal/util/Sets.cs new file mode 100644 index 0000000..bfce619 --- /dev/null +++ b/Mammoth/Couscous/mammoth/internal/util/Sets.cs @@ -0,0 +1,18 @@ +using Mammoth.Couscous.java.lang; +using Mammoth.Couscous.java.util; +using Mammoth.Couscous.java.nio.file; +using System; + +namespace Mammoth.Couscous.org.zwobble.mammoth.@internal.util +{ + internal static class Sets { + internal static Set<T> set<T>(T[] values) { + return new HashSet<T>(new System.Collections.Generic.HashSet<T>(values)); + } + + internal static Set<T> toSet<T>(Iterable<T> iterable) { + return new HashSet<T>(new System.Collections.Generic.HashSet<T>(FromJava.IterableToEnumerable(iterable))); + } + } +} + diff --git a/Mammoth/Couscous/mammoth/internal/util/Strings.cs b/Mammoth/Couscous/mammoth/internal/util/Strings.cs new file mode 100644 index 0000000..65f5e0b --- /dev/null +++ b/Mammoth/Couscous/mammoth/internal/util/Strings.cs @@ -0,0 +1,18 @@ +using System; + +namespace Mammoth.Couscous.org.zwobble.mammoth.@internal.util { + internal static class Strings { + internal static string trimLeft(string value, int character) { + int index = 0; + while (index < value.Length && value[index] == character) { + index++; + } + return value.Substring(index); + } + + internal static bool isNullOrEmpty(string value) { + return string.IsNullOrEmpty(value); + } + } +} + diff --git a/Mammoth/Couscous/mammoth/internal/xml/parsing/SimpleSax.cs b/Mammoth/Couscous/mammoth/internal/xml/parsing/SimpleSax.cs new file mode 100644 index 0000000..65f63c3 --- /dev/null +++ b/Mammoth/Couscous/mammoth/internal/xml/parsing/SimpleSax.cs @@ -0,0 +1,42 @@ +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) { + 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) { + throw new System.NotImplementedException(); + } + } +} |
