Skip to content

Commit

Permalink
Added "fromContent" and "fromPath" methods to BeanMapper.Format.
Browse files Browse the repository at this point in the history
  • Loading branch information
essiembre committed Nov 20, 2024
1 parent e02fcfb commit e61a27a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 26 deletions.
82 changes: 71 additions & 11 deletions src/main/java/com/norconex/commons/lang/bean/BeanMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -175,6 +176,76 @@ public enum Format {

final Supplier<MapperBuilder<?, ?>> builder;
final Function<MapperBuilder<?, ?>, ObjectMapper> mapper;

/**
* Resolve the format based on the file extension for a given path.
* Supported extensions are ".json", ".yaml", ".yml", and ".xml"
* (all case-insensitive).
* @param path the path to evaluate
* @return the detected format or <code>null</code> if path is
* <code>null</code> or does not have a supported extension.
*/
public static Format fromPath(Path path) {
return fromPath(path, null);
}

/**
* Resolve the format based on the file extension for a given path.
* Supported extensions are ".json", ".yaml", ".yml", and ".xml"
* (all case-insensitive).
* @param path the path to evaluate
* @param defaultFormat format returned if the path is <code>null</code>
* or does not have a supported extension
* @return the detected or default format
*/
public static Format fromPath(Path path, Format defaultFormat) {
if (path == null) {
return defaultFormat;
}
Format format;
var asStr = path.toString().toLowerCase();
if (asStr.endsWith(".json")) {
format = Format.JSON;
} else if (asStr.endsWith(".yaml") || asStr.endsWith(".yml")) {
format = Format.YAML;
} else if (asStr.endsWith(".xml")) {
format = Format.XML;
} else {
format = defaultFormat;
}
return format;
}

/**
* Resolve the format based on supplied content.
* @param content the content to evaluate
* @return the detected format or <code>null</code> if the content is
* blank or the format could not be detected
*/
public static Format fromContent(String content) {
return fromContent(content, null);
}

/**
* Resolve the format based on supplied content.
* @param content the content to evaluate
* @param defaultFormat format returned if the content is blank
* or the format could not be detected
* @return the detected or default format
*/
public static Format fromContent(String content, Format defaultFormat) {
if (StringUtils.isBlank(content)) {
return defaultFormat;
}
var cfg = content.stripLeading();
if (cfg.startsWith("{")) {
return Format.JSON;
}
if (cfg.startsWith("<")) {
return Format.XML;
}
return Format.YAML;
}
}

/**
Expand Down Expand Up @@ -389,17 +460,6 @@ public void assertWriteRead(@NonNull Object obj, Format... formats) {
}
}

public static Format detectFormat(@NonNull String str) {
var cfg = str.stripLeading();
if (cfg.startsWith("{")) {
return Format.JSON;
}
if (cfg.startsWith("<")) {
return Format.XML;
}
return Format.YAML;
}

/**
* Gets a Jackson {@link ObjectMapper} for the given format.
* @param format format for which to get the mapper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ public <T> T toObject(@NonNull Path configFile, Class<T> type) {
return beanMapper.read(
type,
new StringReader(toString(configFile)),
resolveFormat(configFile));
Format.fromPath(configFile, Format.XML));
}

/**
Expand All @@ -359,7 +359,7 @@ public void toObject(@NonNull Path configFile, @NonNull Object object) {
beanMapper.read(
object,
new StringReader(toString(configFile)),
resolveFormat(configFile));
Format.fromPath(configFile, Format.XML));
}

/**
Expand Down Expand Up @@ -526,19 +526,6 @@ public String loadString(Path configFile) {

//--- Private methods ----------------------------------------------------

private Format resolveFormat(@NonNull Path path) {
Format format;
var asStr = path.toString();
if (asStr.endsWith(".json")) {
format = Format.JSON;
} else if (asStr.endsWith(".yaml") || asStr.endsWith(".yml")) {
format = Format.YAML;
} else {
format = Format.XML;
}
return format;
}

private static VelocityEngine createDefaultVelocityEngine() {
var engine = new VelocityEngine();
engine.setProperty(RuntimeConstants.EVENTHANDLER_INCLUDE,
Expand Down

0 comments on commit e61a27a

Please sign in to comment.