1 package org.openecomp.sdc.tosca.services;
3 import org.yaml.snakeyaml.DumperOptions;
4 import org.yaml.snakeyaml.TypeDescription;
5 import org.yaml.snakeyaml.Yaml;
6 import org.yaml.snakeyaml.constructor.Constructor;
7 import org.yaml.snakeyaml.introspector.BeanAccess;
8 import org.yaml.snakeyaml.introspector.Property;
9 import org.yaml.snakeyaml.introspector.PropertyUtils;
10 import org.yaml.snakeyaml.nodes.MappingNode;
11 import org.yaml.snakeyaml.nodes.NodeTuple;
12 import org.yaml.snakeyaml.nodes.Tag;
13 import org.yaml.snakeyaml.parser.ParserException;
14 import org.yaml.snakeyaml.representer.Representer;
16 import java.beans.IntrospectionException;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.util.AbstractMap;
20 import java.util.LinkedHashMap;
21 import java.util.LinkedHashSet;
28 @SuppressWarnings("unchecked")
29 public class YamlUtil {
34 * @param <T> the type parameter
35 * @param yamlContent the yaml content
36 * @param typClass the t class
39 public <T> T yamlToObject(String yamlContent, Class<T> typClass) {
40 Constructor constructor = getConstructor(typClass);
41 constructor.setPropertyUtils(getPropertyUtils());
42 TypeDescription yamlFileDescription = new TypeDescription(typClass);
43 constructor.addTypeDescription(yamlFileDescription);
44 Yaml yaml = new Yaml(constructor);
45 T yamlObj = (T) yaml.load(yamlContent);
46 //noinspection ResultOfMethodCallIgnored
51 public InputStream loadYamlFileIs(String yamlFullFileName) {
52 return YamlUtil.class.getResourceAsStream(yamlFullFileName);
58 * @param <T> the type parameter
59 * @param yamlContent the yaml content
60 * @param typClass the t class
63 public <T> T yamlToObject(InputStream yamlContent, Class<T> typClass) {
65 Constructor constructor = getConstructor(typClass);
66 constructor.setPropertyUtils(getPropertyUtils());
67 TypeDescription yamlFileDescription = new TypeDescription(typClass);
68 constructor.addTypeDescription(yamlFileDescription);
69 Yaml yaml = new Yaml(constructor);
70 T yamlObj = (T) yaml.load(yamlContent);
71 if (yamlObj != null) {
72 //noinspection ResultOfMethodCallIgnored
76 throw new RuntimeException();
78 } catch (Exception exception) {
79 throw new RuntimeException(exception);
82 if (yamlContent != null) {
85 } catch (IOException ignore) {
95 * @param <T> the type parameter
96 * @param typClass the t class
97 * @return the constructor
99 public <T> Constructor getConstructor(Class<T> typClass) {
100 return new StrictMapAppenderConstructor(typClass);
104 * Gets property utils.
106 * @return the property utils
108 protected PropertyUtils getPropertyUtils() {
109 return new MyPropertyUtils();
116 * @param yamlContent the yaml content
119 public Map<String, LinkedHashMap<String, Object>> yamlToMap(InputStream yamlContent) {
120 Yaml yaml = new Yaml();
121 @SuppressWarnings("unchecked") Map<String, LinkedHashMap<String, Object>> yamlData =
122 (Map<String, LinkedHashMap<String, Object>>) yaml.load(yamlContent);
127 * Object to yaml string.
129 * @param <T> the type parameter
133 public <T> String objectToYaml(Object obj) {
134 DumperOptions options = new DumperOptions();
135 options.setPrettyFlow(true);
136 options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
137 Representer representer = new CustomRepresenter();
138 representer.addClassTag(obj.getClass(), Tag.MAP);
139 representer.setPropertyUtils(new MyPropertyUtils());
141 Yaml yaml = new Yaml(representer, options);
142 return yaml.dump(obj);
146 * Is yaml file content valid boolean.
148 * @param yamlFullFileName the yaml full file name
149 * @return the boolean
151 public boolean isYamlFileContentValid(String yamlFullFileName) {
152 Yaml yaml = new Yaml();
154 Object loadResult = yaml.load(yamlFullFileName);
155 return loadResult != null;
156 } catch (Exception exception) {
162 private class CustomRepresenter extends Representer {
164 protected MappingNode representJavaBean(Set<Property> properties, Object javaBean) {
165 //remove the bean type from the output yaml (!! ...)
166 if (!classTags.containsKey(javaBean.getClass())) {
167 addClassTag(javaBean.getClass(), Tag.MAP);
170 return super.representJavaBean(properties, javaBean);
174 protected NodeTuple representJavaBeanProperty(Object javaBean, Property property,
175 Object propertyValue, Tag customTag) {
176 if (propertyValue == null) {
179 NodeTuple defaultNode =
180 super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
182 return property.getName().equals("_default")
183 ? new NodeTuple(representData("default"), defaultNode.getValueNode())
191 * The type My property utils.
193 public class MyPropertyUtils extends PropertyUtils {
194 //Unsorted properties
196 protected Set<Property> createPropertySet(Class<? extends Object> type, BeanAccess bnAccess)
197 throws IntrospectionException {
198 return new LinkedHashSet<Property>(getPropertiesMap(type,
199 BeanAccess.FIELD).values());
203 public Property getProperty(Class<?> type, String name) throws IntrospectionException {
204 if (name.equals("default")) {
207 return super.getProperty(type, name);
213 * The type Strict map appender constructor.
215 protected class StrictMapAppenderConstructor extends Constructor {
218 * Instantiates a new Strict map appender constructor.
220 * @param theRoot the the root
222 public StrictMapAppenderConstructor(Class<?> theRoot) {
227 protected Map<Object, Object> createDefaultMap() {
228 final Map<Object, Object> delegate = super.createDefaultMap();
229 return new AbstractMap<Object, Object>() {
231 public Object put(Object key, Object value) {
232 if (delegate.containsKey(key)) {
233 throw new IllegalStateException("duplicate key: " + key);
235 return delegate.put(key, value);
239 public Set<Entry<Object, Object>> entrySet() {
240 return delegate.entrySet();
246 protected Map<Object, Object> constructMapping(MappingNode node) {
248 return super.constructMapping(node);
249 } catch (IllegalStateException exception) {
250 throw new ParserException("while parsing MappingNode",
251 node.getStartMark(), exception.getMessage(),