You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2020/09/23 05:16:57 UTC
[isis] branch master updated: ISIS-2437: Commons: house keeping
This is an automated email from the ASF dual-hosted git repository.
ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git
The following commit(s) were added to refs/heads/master by this push:
new 10464d8 ISIS-2437: Commons: house keeping
10464d8 is described below
commit 10464d8e2a61f31487a7feb136aba52fcdfe777f
Author: Andi Huber <ah...@apache.org>
AuthorDate: Wed Sep 23 07:16:43 2020 +0200
ISIS-2437: Commons: house keeping
---
commons/pom.xml | 13 +--
.../isis/commons/internal/resources/_Yaml.java | 109 ++++++++++++++++++++-
2 files changed, 115 insertions(+), 7 deletions(-)
diff --git a/commons/pom.xml b/commons/pom.xml
index 9b00ca9..bfe438f 100644
--- a/commons/pom.xml
+++ b/commons/pom.xml
@@ -72,18 +72,18 @@
</dependency>
<dependency>
- <groupId>org.yaml</groupId>
- <artifactId>snakeyaml</artifactId>
+ <groupId>org.yaml</groupId>
+ <artifactId>snakeyaml</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
-
+
<dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-tx</artifactId>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-tx</artifactId>
</dependency>
<dependency>
@@ -105,7 +105,8 @@
<!-- Add Log4j2 Dependency, as a replacement for Spring's default 'logback'.
This requires for the 'spring-boot-starter' to exclude the default logging
- artifact 'org.springframework.boot:spring-boot-starter-logging' see https://www.callicoder.com/spring-boot-log4j-2-example/ -->
+ artifact 'org.springframework.boot:spring-boot-starter-logging'
+ see https://www.callicoder.com/spring-boot-log4j-2-example/ -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
diff --git a/commons/src/main/java/org/apache/isis/commons/internal/resources/_Yaml.java b/commons/src/main/java/org/apache/isis/commons/internal/resources/_Yaml.java
index 12ff1a1..3f9a076 100644
--- a/commons/src/main/java/org/apache/isis/commons/internal/resources/_Yaml.java
+++ b/commons/src/main/java/org/apache/isis/commons/internal/resources/_Yaml.java
@@ -18,6 +18,11 @@
*/
package org.apache.isis.commons.internal.resources;
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
import java.io.InputStream;
import org.yaml.snakeyaml.Yaml;
@@ -30,7 +35,7 @@ import lombok.val;
/**
* <h1>- internal use only -</h1>
* <p>
- * Utilities for the JSON format.
+ * Utilities for the YAML format.
* </p>
* <p>
* <b>WARNING</b>: Do <b>NOT</b> use any of the classes provided by this package! <br/>
@@ -40,6 +45,8 @@ import lombok.val;
*/
public class _Yaml {
+ // -- FROM INPUT STREAM
+
/**
* Deserialize YAML content from given YAML content InputStream into an instance of
* given {@code clazz} type.
@@ -69,4 +76,104 @@ public class _Yaml {
}
}
+ // -- FROM STRING
+
+ /**
+ * Deserialize YAML content from given YAML content String into an instance of
+ * given {@code clazz} type.
+ * @param <T>
+ * @param clazz
+ * @param content
+ * @return
+ */
+ public static <T> T readYaml(final Class<T> clazz, String content) {
+ val yaml = new Yaml(new Constructor(clazz));
+ return yaml.load(content);
+ }
+
+ /**
+ * Either deserialize YAML content from given YAML content String into an instance of
+ * given {@code clazz} type, or any exception that occurred during parsing.
+ * @param <T>
+ * @param clazz
+ * @param content
+ * @return
+ */
+ public static <T> _Either<T, Exception> tryReadYaml(final Class<T> clazz, String content) {
+ try {
+ return _Either.left(readYaml(clazz, content));
+ } catch (Exception e) {
+ return _Either.right(e);
+ }
+ }
+
+ // -- FROM FILE
+
+ /**
+ * Deserialize YAML content from given YAML content File into an instance of
+ * given {@code clazz} type.
+ * @param <T>
+ * @param clazz
+ * @param content
+ * @return
+ * @throws IOException
+ * @throws FileNotFoundException
+ */
+ public static <T> T readYaml(final Class<T> clazz, File content) throws FileNotFoundException, IOException {
+ try(val fis = new FileInputStream(content)) {
+ val yaml = new Yaml(new Constructor(clazz));
+ return yaml.load(fis);
+ }
+ }
+
+ /**
+ * Either deserialize YAML content from given YAML content File into an instance of
+ * given {@code clazz} type, or any exception that occurred during parsing.
+ * @param <T>
+ * @param clazz
+ * @param content
+ * @return
+ */
+ public static <T> _Either<T, Exception> tryReadYaml(final Class<T> clazz, File content) {
+ try {
+ return _Either.left(readYaml(clazz, content));
+ } catch (Exception e) {
+ return _Either.right(e);
+ }
+ }
+
+ // -- FROM BYTE ARRAY
+
+ /**
+ * Deserialize YAML content from given YAML content byte[] into an instance of
+ * given {@code clazz} type.
+ * @param <T>
+ * @param clazz
+ * @param content
+ * @return
+ * @throws IOException
+ */
+ public static <T> T readYaml(final Class<T> clazz, byte[] content) throws IOException {
+ try(val bais = new ByteArrayInputStream(content)) {
+ val yaml = new Yaml(new Constructor(clazz));
+ return yaml.load(bais);
+ }
+ }
+
+ /**
+ * Either deserialize YAML content from given YAML content byte[] into an instance of
+ * given {@code clazz} type, or any exception that occurred during parsing.
+ * @param <T>
+ * @param clazz
+ * @param content
+ * @return
+ */
+ public static <T> _Either<T, Exception> tryReadYaml(final Class<T> clazz, byte[] content) {
+ try {
+ return _Either.left(readYaml(clazz, content));
+ } catch (Exception e) {
+ return _Either.right(e);
+ }
+ }
+
}