aboutsummaryrefslogtreecommitdiffstats
path: root/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/services/YamlUtil.java
diff options
context:
space:
mode:
Diffstat (limited to 'common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/services/YamlUtil.java')
-rw-r--r--common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/services/YamlUtil.java91
1 files changed, 42 insertions, 49 deletions
diff --git a/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/services/YamlUtil.java b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/services/YamlUtil.java
index 8530846fae..d8e592d353 100644
--- a/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/services/YamlUtil.java
+++ b/common/onap-tosca-datatype/src/main/java/org/onap/sdc/tosca/services/YamlUtil.java
@@ -13,9 +13,15 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
package org.onap.sdc.tosca.services;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.DumperOptions;
@@ -30,19 +36,43 @@ import org.yaml.snakeyaml.nodes.NodeTuple;
import org.yaml.snakeyaml.nodes.Tag;
import org.yaml.snakeyaml.representer.Representer;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.*;
-
/**
* The type Yaml util.
*/
@SuppressWarnings("unchecked")
public class YamlUtil {
- private static final Logger LOGGER = LoggerFactory.getLogger(YamlUtil.class.getName());
static final String DEFAULT = "default";
static final String DEFAULT_STR = "_default";
+ private static final Logger LOGGER = LoggerFactory.getLogger(YamlUtil.class.getName());
+
+ /**
+ * Parse a YAML file to List
+ *
+ * @param yamlFileInputStream the YAML file input stream
+ * @return The YAML casted as a list
+ */
+ public static Optional<List<Object>> yamlToList(final InputStream yamlFileInputStream) {
+ List<Object> yamlList = null;
+ try {
+ yamlList = (List<Object>) read(yamlFileInputStream);
+ } catch (final ClassCastException ex) {
+ if (LOGGER.isWarnEnabled()) {
+ LOGGER.warn("Could not parse YAML to List.", ex);
+ }
+ }
+ return Optional.ofNullable(yamlList);
+ }
+
+ /**
+ * Parse a YAML file to Object
+ *
+ * @param yamlFileInputStream the YAML file input stream
+ * @return The YAML Object
+ */
+ public static Object read(final InputStream yamlFileInputStream) {
+ return new Yaml().load(yamlFileInputStream);
+ }
/**
* Yaml to object t.
@@ -57,7 +87,8 @@ public class YamlUtil {
constructor.setPropertyUtils(getPropertyUtils());
TypeDescription yamlFileDescription = new TypeDescription(typClass);
constructor.addTypeDescription(yamlFileDescription);
- T yamlObj = new Yaml(constructor, new Representer(), new DumperOptions(), getLoaderOptions()).load(yamlContent);;
+ T yamlObj = new Yaml(constructor, new Representer(), new DumperOptions(), getLoaderOptions()).load(yamlContent);
+ ;
//noinspection ResultOfMethodCallIgnored
yamlObj.toString();
return yamlObj;
@@ -111,7 +142,6 @@ public class YamlUtil {
return options;
}
-
/**
* Gets constructor.
*
@@ -132,7 +162,6 @@ public class YamlUtil {
return new MyPropertyUtils();
}
-
/**
* Yaml to map map.
*
@@ -143,35 +172,6 @@ public class YamlUtil {
return new Yaml().load(yamlContent);
}
-
- /**
- * Parse a YAML file to List
- *
- * @param yamlFileInputStream the YAML file input stream
- * @return The YAML casted as a list
- */
- public static Optional<List<Object>> yamlToList(final InputStream yamlFileInputStream) {
- List<Object> yamlList = null;
- try {
- yamlList = (List<Object>) read(yamlFileInputStream);
- } catch (final ClassCastException ex) {
- if (LOGGER.isWarnEnabled()) {
- LOGGER.warn("Could not parse YAML to List.", ex);
- }
- }
- return Optional.ofNullable(yamlList);
- }
-
- /**
- * Parse a YAML file to Object
- *
- * @param yamlFileInputStream the YAML file input stream
- * @return The YAML Object
- */
- public static Object read(final InputStream yamlFileInputStream) {
- return new Yaml().load(yamlFileInputStream);
- }
-
/**
* Object to yaml string.
*
@@ -185,7 +185,6 @@ public class YamlUtil {
Representer representer = new CustomRepresenter();
representer.addClassTag(obj.getClass(), Tag.MAP);
representer.setPropertyUtils(new MyPropertyUtils());
-
Yaml yaml = new Yaml(representer, options);
return yaml.dump(obj);
}
@@ -204,30 +203,24 @@ public class YamlUtil {
}
}
-
private class CustomRepresenter extends Representer {
+
@Override
protected MappingNode representJavaBean(Set<Property> properties, Object javaBean) {
//remove the bean type from the output yaml (!! ...)
if (!classTags.containsKey(javaBean.getClass())) {
addClassTag(javaBean.getClass(), Tag.MAP);
}
-
return super.representJavaBean(properties, javaBean);
}
@Override
- protected NodeTuple representJavaBeanProperty(Object javaBean, Property property,
- Object propertyValue, Tag customTag) {
+ protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, Object propertyValue, Tag customTag) {
if (propertyValue == null) {
return null;
} else {
- NodeTuple defaultNode =
- super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
-
- return DEFAULT_STR.equals(property.getName())
- ? new NodeTuple(representData(DEFAULT), defaultNode.getValueNode())
- : defaultNode;
+ NodeTuple defaultNode = super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
+ return DEFAULT_STR.equals(property.getName()) ? new NodeTuple(representData(DEFAULT), defaultNode.getValueNode()) : defaultNode;
}
}
}