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
54 * @param <T> the type parameter
55 * @param yamlContent the yaml content
56 * @param typClass the t class
59 public <T> T yamlToObject(InputStream yamlContent, Class<T> typClass) {
61 Constructor constructor = getConstructor(typClass);
62 constructor.setPropertyUtils(getPropertyUtils());
63 TypeDescription yamlFileDescription = new TypeDescription(typClass);
64 constructor.addTypeDescription(yamlFileDescription);
65 Yaml yaml = new Yaml(constructor);
66 T yamlObj = (T) yaml.load(yamlContent);
67 if (yamlObj != null) {
68 //noinspection ResultOfMethodCallIgnored
72 throw new RuntimeException();
74 } catch (Exception exception) {
75 throw new RuntimeException(exception);
78 if (yamlContent != null) {
81 } catch (IOException ignore) {
91 * @param <T> the type parameter
92 * @param typClass the t class
93 * @return the constructor
95 public <T> Constructor getConstructor(Class<T> typClass) {
96 return new StrictMapAppenderConstructor(typClass);
100 * Gets property utils.
102 * @return the property utils
104 protected PropertyUtils getPropertyUtils() {
105 return new MyPropertyUtils();
112 * @param yamlContent the yaml content
115 public Map<String, LinkedHashMap<String, Object>> yamlToMap(InputStream yamlContent) {
116 Yaml yaml = new Yaml();
117 @SuppressWarnings("unchecked") Map<String, LinkedHashMap<String, Object>> yamlData =
118 (Map<String, LinkedHashMap<String, Object>>) yaml.load(yamlContent);
123 * Object to yaml string.
125 * @param <T> the type parameter
129 public <T> String objectToYaml(Object obj) {
130 DumperOptions options = new DumperOptions();
131 options.setPrettyFlow(true);
132 options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
133 Representer representer = new CustomRepresenter();
134 representer.addClassTag(obj.getClass(), Tag.MAP);
135 representer.setPropertyUtils(new MyPropertyUtils());
137 Yaml yaml = new Yaml(representer, options);
138 return yaml.dump(obj);
142 * Is yaml file content valid boolean.
144 * @param yamlFullFileName the yaml full file name
145 * @return the boolean
147 public boolean isYamlFileContentValid(String yamlFullFileName) {
148 Yaml yaml = new Yaml();
150 Object loadResult = yaml.load(yamlFullFileName);
151 return loadResult != null;
152 } catch (Exception exception) {
158 private class CustomRepresenter extends Representer {
160 protected MappingNode representJavaBean(Set<Property> properties, Object javaBean) {
161 //remove the bean type from the output yaml (!! ...)
162 if (!classTags.containsKey(javaBean.getClass())) {
163 addClassTag(javaBean.getClass(), Tag.MAP);
166 return super.representJavaBean(properties, javaBean);
170 protected NodeTuple representJavaBeanProperty(Object javaBean, Property property,
171 Object propertyValue, Tag customTag) {
172 if (propertyValue == null) {
175 NodeTuple defaultNode =
176 super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
178 return property.getName().equals("_default")
179 ? new NodeTuple(representData("default"), defaultNode.getValueNode())
187 * The type My property utils.
189 public class MyPropertyUtils extends PropertyUtils {
190 //Unsorted properties
192 protected Set<Property> createPropertySet(Class<? extends Object> type, BeanAccess bnAccess)
193 throws IntrospectionException {
194 return new LinkedHashSet<Property>(getPropertiesMap(type,
195 BeanAccess.FIELD).values());
199 public Property getProperty(Class<?> type, String name) throws IntrospectionException {
200 if (name.equals("default")) {
203 return super.getProperty(type, name);
209 * The type Strict map appender constructor.
211 protected class StrictMapAppenderConstructor extends Constructor {
214 * Instantiates a new Strict map appender constructor.
216 * @param theRoot the the root
218 public StrictMapAppenderConstructor(Class<?> theRoot) {
223 protected Map<Object, Object> createDefaultMap() {
224 final Map<Object, Object> delegate = super.createDefaultMap();
225 return new AbstractMap<Object, Object>() {
227 public Object put(Object key, Object value) {
228 if (delegate.containsKey(key)) {
229 throw new IllegalStateException("duplicate key: " + key);
231 return delegate.put(key, value);
235 public Set<Entry<Object, Object>> entrySet() {
236 return delegate.entrySet();
242 protected Map<Object, Object> constructMapping(MappingNode node) {
244 return super.constructMapping(node);
245 } catch (IllegalStateException exception) {
246 throw new ParserException("while parsing MappingNode",
247 node.getStartMark(), exception.getMessage(),