Skip to content

Commit

Permalink
added alias support, ability to get provider by filename/-extension
Browse files Browse the repository at this point in the history
  • Loading branch information
hohwille committed Jul 10, 2024
1 parent 1696f78 commit bd15aa9
Show file tree
Hide file tree
Showing 14 changed files with 153 additions and 10 deletions.
8 changes: 8 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
* eol=lf
*.bat eol=crlf
*.png binary
*.zip binary
*.tgz binary
*.tar binary
*.bz2 binary
*.gz binary
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
.*
!.github
!.gitignore
!.gitattributes
!.mvn
*.bak
*.log
*.diff
*.patch
*.iml
target
eclipse-target
war
war
Original file line number Diff line number Diff line change
@@ -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();

}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -18,28 +22,46 @@
*/
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<String, StructuredFormatProvider> providerMap;

private final Map<String, StructuredFormatProvider> aliasMap;

/**
* The constructor.
*/
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
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;
}
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

requires transitive io.github.mmm.base;

requires org.slf4j;

requires static io.github.mmm.scanner;

exports io.github.mmm.marshall;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,14 +15,20 @@
*
* @since 1.0.0
*/
public class JsonFormatProvider implements StructuredTextFormatProvider {
public class JsonFormatProvider extends AbstractStructuredFormatProvider implements StructuredTextFormatProvider {

@Override
public String getId() {

return StructuredFormat.ID_JSON;
}

@Override
public String[] getAliases() {

return new String[] { "json" };
}

@Override
public StructuredTextFormat create() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,14 +18,20 @@
*
* @since 1.0.0
*/
public class JsonpFormatProvider implements StructuredTextFormatProvider {
public class JsonpFormatProvider extends AbstractStructuredFormatProvider implements StructuredTextFormatProvider {

@Override
public String getId() {

return StructuredFormat.ID_JSON;
}

@Override
public String[] getAliases() {

return new String[] { "json" };
}

@Override
public StructuredTextFormat create() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -31,6 +33,12 @@ public String getId() {
return StructuredFormat.ID_PROTOBUF;
}

@Override
public String[] getAliases() {

return new String[] { "protobuf" };
}

@Override
public StructuredBinaryFormat create() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,14 +15,20 @@
*
* @since 1.0.0
*/
public class SnakeYamlFormatProvider implements StructuredTextFormatProvider {
public class SnakeYamlFormatProvider extends AbstractStructuredFormatProvider implements StructuredTextFormatProvider {

@Override
public String getId() {

return StructuredFormat.ID_YAML;
}

@Override
public String[] getAliases() {

return new String[] { "yaml", "yml" };
}

@Override
public StructuredTextFormat create() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,14 +18,20 @@
*
* @since 1.0.0
*/
public class StaxFormatProvider implements StructuredTextFormatProvider {
public class StaxFormatProvider extends AbstractStructuredFormatProvider implements StructuredTextFormatProvider {

@Override
public String getId() {

return StructuredFormat.ID_XML;
}

@Override
public String[] getAliases() {

return new String[] { "xml" };
}

@Override
public StructuredTextFormat create() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,14 +15,20 @@
*
* @since 1.0.0
*/
public class TvmXmlFormatProvider implements StructuredTextFormatProvider {
public class TvmXmlFormatProvider extends AbstractStructuredFormatProvider implements StructuredTextFormatProvider {

@Override
public String getId() {

return StructuredFormat.ID_XML;
}

@Override
public String[] getAliases() {

return new String[] { "xml" };
}

@Override
public StructuredTextFormat create() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,14 +15,20 @@
*
* @since 1.0.0
*/
public class YamlFormatProvider implements StructuredTextFormatProvider {
public class YamlFormatProvider extends AbstractStructuredFormatProvider implements StructuredTextFormatProvider {

@Override
public String getId() {

return StructuredFormat.ID_YAML;
}

@Override
public String[] getAliases() {

return new String[] { "yaml", "yml" };
}

@Override
public StructuredTextFormat create() {

Expand Down

0 comments on commit bd15aa9

Please sign in to comment.