Create plugin point for csar generation
[sdc.git] / common / onap-tosca-datatype / src / main / java / org / onap / sdc / tosca / services / YamlUtil.java
index 8530846..25407f1 100644 (file)
  * 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()), new DumperOptions(), getLoaderOptions()).load(yamlContent);
+
         //noinspection ResultOfMethodCallIgnored
         yamlObj.toString();
         return yamlObj;
@@ -83,7 +114,7 @@ public class YamlUtil {
             TypeDescription yamlFileDescription = new TypeDescription(typClass);
             constructor.addTypeDescription(yamlFileDescription);
             //No Yaml Constructor takes only Constructor and LoaderOptions, that is why I had to pass anonymous Representer and DumperOptions objects
-            T yamlObj = new Yaml(constructor, new Representer(), new DumperOptions(), getLoaderOptions()).load(yamlContent);
+            T yamlObj = new Yaml(constructor, new Representer(new DumperOptions()), new DumperOptions(), getLoaderOptions()).load(yamlContent);
             if (yamlObj != null) {
                 //noinspection ResultOfMethodCallIgnored
                 yamlObj.toString();
@@ -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;
             }
         }
     }