diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..f883011 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,8 @@ +* eol=lf +*.bat eol=crlf +*.png binary +*.zip binary +*.tgz binary +*.tar binary +*.bz2 binary +*.gz binary diff --git a/.gitignore b/.gitignore index 5393520..c9883d9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,8 @@ .* +!.github +!.gitignore +!.gitattributes +!.mvn *.bak *.log *.diff @@ -6,4 +10,4 @@ *.iml target eclipse-target -war \ No newline at end of file +war diff --git a/core/src/main/java/io/github/mmm/marshall/AbstractStructuredFormatProvider.java b/core/src/main/java/io/github/mmm/marshall/AbstractStructuredFormatProvider.java new file mode 100644 index 0000000..98f8976 --- /dev/null +++ b/core/src/main/java/io/github/mmm/marshall/AbstractStructuredFormatProvider.java @@ -0,0 +1,17 @@ +/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 + * http://www.apache.org/licenses/LICENSE-2.0 */ +package io.github.mmm.marshall; + +/** + * Abstract base implementation of {@link StructuredFormatProvider}. + */ +public abstract class AbstractStructuredFormatProvider implements StructuredFormatProvider { + + /** + * @return an array of aliases of this provider. + * @see #getId() + * @see StructuredFormatFactory#getProvider(String) + */ + public abstract String[] getAliases(); + +} diff --git a/core/src/main/java/io/github/mmm/marshall/StructuredFormatFactory.java b/core/src/main/java/io/github/mmm/marshall/StructuredFormatFactory.java index a670327..ca4e1af 100644 --- a/core/src/main/java/io/github/mmm/marshall/StructuredFormatFactory.java +++ b/core/src/main/java/io/github/mmm/marshall/StructuredFormatFactory.java @@ -10,7 +10,8 @@ public interface StructuredFormatFactory { /** - * @param formatId the {@link StructuredFormatProvider#getId() format Id}. + * @param formatId the {@link StructuredFormatProvider#getId() format Id}. May also be a + * {@link AbstractStructuredFormatProvider#getAliases() alias}. * @return a new {@link StructuredFormatProvider} for the given {@code formatId} or {@code null} if no such provider * is registered. * @throws io.github.mmm.base.exception.ObjectNotFoundException if no such provider could be found. diff --git a/core/src/main/java/io/github/mmm/marshall/StructuredFormatHelper.java b/core/src/main/java/io/github/mmm/marshall/StructuredFormatHelper.java new file mode 100644 index 0000000..86372de --- /dev/null +++ b/core/src/main/java/io/github/mmm/marshall/StructuredFormatHelper.java @@ -0,0 +1,39 @@ +/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0 + * http://www.apache.org/licenses/LICENSE-2.0 */ +package io.github.mmm.marshall; + +import java.nio.file.Path; + +/** + * Helper to {@link #getProvider(Path) get} a {@link StructuredFormatProvider} from a {@link Path} or its filename. + * + * @since 1.0.0 + */ +public interface StructuredFormatHelper { + + /** + * @param path the {@link Path} pointing to a file where to store {@link StructuredFormat} payload (a marshalled + * object). + * @return the {@link StructuredFormatProvider} determined from the file extension. + */ + static StructuredFormatProvider getProvider(Path path) { + + return getProvider(path.getFileName().toString()); + } + + /** + * + * @param filename the {@link Path#getFileName() file name} as {@link String}. + * @return the {@link StructuredFormatProvider} determined from the file extension. + */ + static StructuredFormatProvider getProvider(String filename) { + + int lastDot = filename.lastIndexOf('.'); + String id = filename; + if (lastDot >= 0) { + id = filename.substring(lastDot + 1); // extension + } + return StructuredFormatFactory.get().getProvider(id); + } + +} diff --git a/core/src/main/java/io/github/mmm/marshall/impl/StructuredFormatFactoryImpl.java b/core/src/main/java/io/github/mmm/marshall/impl/StructuredFormatFactoryImpl.java index ce01dba..e7d7c93 100644 --- a/core/src/main/java/io/github/mmm/marshall/impl/StructuredFormatFactoryImpl.java +++ b/core/src/main/java/io/github/mmm/marshall/impl/StructuredFormatFactoryImpl.java @@ -6,8 +6,12 @@ import java.util.Map; import java.util.ServiceLoader; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import io.github.mmm.base.exception.ObjectNotFoundException; import io.github.mmm.base.service.ServiceHelper; +import io.github.mmm.marshall.AbstractStructuredFormatProvider; import io.github.mmm.marshall.StructuredFormatFactory; import io.github.mmm.marshall.StructuredFormatProvider; @@ -18,11 +22,15 @@ */ public class StructuredFormatFactoryImpl implements StructuredFormatFactory { + private static final Logger LOG = LoggerFactory.getLogger(StructuredFormatFactoryImpl.class); + /** The singleton instance. */ public static final StructuredFormatFactoryImpl INSTANCE = new StructuredFormatFactoryImpl(); private final Map providerMap; + private final Map aliasMap; + /** * The constructor. */ @@ -30,8 +38,19 @@ protected StructuredFormatFactoryImpl() { super(); this.providerMap = new HashMap<>(); + this.aliasMap = new HashMap<>(); ServiceHelper.all(ServiceLoader.load(StructuredFormatProvider.class), this.providerMap, StructuredFormatProvider::getId); + for (StructuredFormatProvider provider : this.providerMap.values()) { + if (provider instanceof AbstractStructuredFormatProvider asfp) { + for (String alias : asfp.getAliases()) { + StructuredFormatProvider duplicate = this.aliasMap.put(alias, provider); + if (duplicate != null) { + LOG.warn("Duplicate alias {} used by {} and {}", alias, duplicate, provider); + } + } + } + } } @Override @@ -39,7 +58,10 @@ public StructuredFormatProvider getProvider(String format) { StructuredFormatProvider provider = this.providerMap.get(format); if (provider == null) { - throw new ObjectNotFoundException("StructuredFormatProvider", format); + provider = this.aliasMap.get(format); + if (provider == null) { + throw new ObjectNotFoundException("StructuredFormatProvider", format); + } } return provider; } diff --git a/core/src/main/java/module-info.java b/core/src/main/java/module-info.java index a2570cf..fb1f6a2 100644 --- a/core/src/main/java/module-info.java +++ b/core/src/main/java/module-info.java @@ -12,6 +12,8 @@ requires transitive io.github.mmm.base; + requires org.slf4j; + requires static io.github.mmm.scanner; exports io.github.mmm.marshall; diff --git a/impl/json/src/main/java/io/github/mmm/marshall/json/JsonFormatProvider.java b/impl/json/src/main/java/io/github/mmm/marshall/json/JsonFormatProvider.java index 548a5d0..0b618fa 100644 --- a/impl/json/src/main/java/io/github/mmm/marshall/json/JsonFormatProvider.java +++ b/impl/json/src/main/java/io/github/mmm/marshall/json/JsonFormatProvider.java @@ -2,6 +2,7 @@ * http://www.apache.org/licenses/LICENSE-2.0 */ package io.github.mmm.marshall.json; +import io.github.mmm.marshall.AbstractStructuredFormatProvider; import io.github.mmm.marshall.MarshallingConfig; import io.github.mmm.marshall.StructuredFormat; import io.github.mmm.marshall.StructuredFormatProvider; @@ -14,7 +15,7 @@ * * @since 1.0.0 */ -public class JsonFormatProvider implements StructuredTextFormatProvider { +public class JsonFormatProvider extends AbstractStructuredFormatProvider implements StructuredTextFormatProvider { @Override public String getId() { @@ -22,6 +23,12 @@ public String getId() { return StructuredFormat.ID_JSON; } + @Override + public String[] getAliases() { + + return new String[] { "json" }; + } + @Override public StructuredTextFormat create() { diff --git a/impl/jsonp/src/main/java/io/github/mmm/marshall/jsonp/JsonpFormatProvider.java b/impl/jsonp/src/main/java/io/github/mmm/marshall/jsonp/JsonpFormatProvider.java index 2ab5954..664e6d8 100644 --- a/impl/jsonp/src/main/java/io/github/mmm/marshall/jsonp/JsonpFormatProvider.java +++ b/impl/jsonp/src/main/java/io/github/mmm/marshall/jsonp/JsonpFormatProvider.java @@ -5,6 +5,7 @@ import javax.json.stream.JsonGeneratorFactory; import javax.json.stream.JsonParserFactory; +import io.github.mmm.marshall.AbstractStructuredFormatProvider; import io.github.mmm.marshall.MarshallingConfig; import io.github.mmm.marshall.StructuredFormat; import io.github.mmm.marshall.StructuredFormatProvider; @@ -17,7 +18,7 @@ * * @since 1.0.0 */ -public class JsonpFormatProvider implements StructuredTextFormatProvider { +public class JsonpFormatProvider extends AbstractStructuredFormatProvider implements StructuredTextFormatProvider { @Override public String getId() { @@ -25,6 +26,12 @@ public String getId() { return StructuredFormat.ID_JSON; } + @Override + public String[] getAliases() { + + return new String[] { "json" }; + } + @Override public StructuredTextFormat create() { diff --git a/impl/protobuf/src/main/java/io/github/mmm/marshall/protobuf/ProtoBufFormatProvider.java b/impl/protobuf/src/main/java/io/github/mmm/marshall/protobuf/ProtoBufFormatProvider.java index 181ccea..64c41e0 100644 --- a/impl/protobuf/src/main/java/io/github/mmm/marshall/protobuf/ProtoBufFormatProvider.java +++ b/impl/protobuf/src/main/java/io/github/mmm/marshall/protobuf/ProtoBufFormatProvider.java @@ -3,6 +3,7 @@ package io.github.mmm.marshall.protobuf; import io.github.mmm.base.variable.VariableDefinition; +import io.github.mmm.marshall.AbstractStructuredFormatProvider; import io.github.mmm.marshall.MarshallingConfig; import io.github.mmm.marshall.StructuredBinaryFormat; import io.github.mmm.marshall.StructuredFormat; @@ -16,7 +17,8 @@ * * @since 1.0.0 */ -public class ProtoBufFormatProvider implements StructuredBinaryIdBasedFormatProvider { +public class ProtoBufFormatProvider extends AbstractStructuredFormatProvider + implements StructuredBinaryIdBasedFormatProvider { /** * {@link VariableDefinition} to configure if {@link StructuredState#START_OBJECT objects} should be encoded as @@ -31,6 +33,12 @@ public String getId() { return StructuredFormat.ID_PROTOBUF; } + @Override + public String[] getAliases() { + + return new String[] { "protobuf" }; + } + @Override public StructuredBinaryFormat create() { diff --git a/impl/snakeyaml/src/main/java/io/github/mmm/marshall/snakeyaml/SnakeYamlFormatProvider.java b/impl/snakeyaml/src/main/java/io/github/mmm/marshall/snakeyaml/SnakeYamlFormatProvider.java index 4dbbb26..0f6fb4f 100644 --- a/impl/snakeyaml/src/main/java/io/github/mmm/marshall/snakeyaml/SnakeYamlFormatProvider.java +++ b/impl/snakeyaml/src/main/java/io/github/mmm/marshall/snakeyaml/SnakeYamlFormatProvider.java @@ -2,6 +2,7 @@ * http://www.apache.org/licenses/LICENSE-2.0 */ package io.github.mmm.marshall.snakeyaml; +import io.github.mmm.marshall.AbstractStructuredFormatProvider; import io.github.mmm.marshall.MarshallingConfig; import io.github.mmm.marshall.StructuredFormat; import io.github.mmm.marshall.StructuredFormatProvider; @@ -14,7 +15,7 @@ * * @since 1.0.0 */ -public class SnakeYamlFormatProvider implements StructuredTextFormatProvider { +public class SnakeYamlFormatProvider extends AbstractStructuredFormatProvider implements StructuredTextFormatProvider { @Override public String getId() { @@ -22,6 +23,12 @@ public String getId() { return StructuredFormat.ID_YAML; } + @Override + public String[] getAliases() { + + return new String[] { "yaml", "yml" }; + } + @Override public StructuredTextFormat create() { diff --git a/impl/stax/src/main/java/io/github/mmm/marshall/stax/StaxFormatProvider.java b/impl/stax/src/main/java/io/github/mmm/marshall/stax/StaxFormatProvider.java index 74709d8..7553645 100644 --- a/impl/stax/src/main/java/io/github/mmm/marshall/stax/StaxFormatProvider.java +++ b/impl/stax/src/main/java/io/github/mmm/marshall/stax/StaxFormatProvider.java @@ -5,6 +5,7 @@ import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLOutputFactory; +import io.github.mmm.marshall.AbstractStructuredFormatProvider; import io.github.mmm.marshall.MarshallingConfig; import io.github.mmm.marshall.StructuredFormat; import io.github.mmm.marshall.StructuredFormatProvider; @@ -17,7 +18,7 @@ * * @since 1.0.0 */ -public class StaxFormatProvider implements StructuredTextFormatProvider { +public class StaxFormatProvider extends AbstractStructuredFormatProvider implements StructuredTextFormatProvider { @Override public String getId() { @@ -25,6 +26,12 @@ public String getId() { return StructuredFormat.ID_XML; } + @Override + public String[] getAliases() { + + return new String[] { "xml" }; + } + @Override public StructuredTextFormat create() { diff --git a/impl/tvm-xml/src/main/java/io/github/mmm/marshall/tvm/xml/TvmXmlFormatProvider.java b/impl/tvm-xml/src/main/java/io/github/mmm/marshall/tvm/xml/TvmXmlFormatProvider.java index 8f3348b..78b44e8 100644 --- a/impl/tvm-xml/src/main/java/io/github/mmm/marshall/tvm/xml/TvmXmlFormatProvider.java +++ b/impl/tvm-xml/src/main/java/io/github/mmm/marshall/tvm/xml/TvmXmlFormatProvider.java @@ -2,6 +2,7 @@ * http://www.apache.org/licenses/LICENSE-2.0 */ package io.github.mmm.marshall.tvm.xml; +import io.github.mmm.marshall.AbstractStructuredFormatProvider; import io.github.mmm.marshall.MarshallingConfig; import io.github.mmm.marshall.StructuredFormat; import io.github.mmm.marshall.StructuredFormatProvider; @@ -14,7 +15,7 @@ * * @since 1.0.0 */ -public class TvmXmlFormatProvider implements StructuredTextFormatProvider { +public class TvmXmlFormatProvider extends AbstractStructuredFormatProvider implements StructuredTextFormatProvider { @Override public String getId() { @@ -22,6 +23,12 @@ public String getId() { return StructuredFormat.ID_XML; } + @Override + public String[] getAliases() { + + return new String[] { "xml" }; + } + @Override public StructuredTextFormat create() { diff --git a/impl/yaml/src/main/java/io/github/mmm/marshall/yaml/YamlFormatProvider.java b/impl/yaml/src/main/java/io/github/mmm/marshall/yaml/YamlFormatProvider.java index d3e8c6e..be83e94 100644 --- a/impl/yaml/src/main/java/io/github/mmm/marshall/yaml/YamlFormatProvider.java +++ b/impl/yaml/src/main/java/io/github/mmm/marshall/yaml/YamlFormatProvider.java @@ -2,6 +2,7 @@ * http://www.apache.org/licenses/LICENSE-2.0 */ package io.github.mmm.marshall.yaml; +import io.github.mmm.marshall.AbstractStructuredFormatProvider; import io.github.mmm.marshall.MarshallingConfig; import io.github.mmm.marshall.StructuredFormat; import io.github.mmm.marshall.StructuredFormatProvider; @@ -14,7 +15,7 @@ * * @since 1.0.0 */ -public class YamlFormatProvider implements StructuredTextFormatProvider { +public class YamlFormatProvider extends AbstractStructuredFormatProvider implements StructuredTextFormatProvider { @Override public String getId() { @@ -22,6 +23,12 @@ public String getId() { return StructuredFormat.ID_YAML; } + @Override + public String[] getAliases() { + + return new String[] { "yaml", "yml" }; + } + @Override public StructuredTextFormat create() {