2 * Copyright © 2016-2017 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.sdc.tosca.services;
19 import org.yaml.snakeyaml.DumperOptions;
20 import org.yaml.snakeyaml.TypeDescription;
21 import org.yaml.snakeyaml.Yaml;
22 import org.yaml.snakeyaml.constructor.Constructor;
23 import org.yaml.snakeyaml.introspector.BeanAccess;
24 import org.yaml.snakeyaml.introspector.Property;
25 import org.yaml.snakeyaml.introspector.PropertyUtils;
26 import org.yaml.snakeyaml.nodes.MappingNode;
27 import org.yaml.snakeyaml.nodes.NodeTuple;
28 import org.yaml.snakeyaml.nodes.Tag;
29 import org.yaml.snakeyaml.parser.ParserException;
30 import org.yaml.snakeyaml.representer.Representer;
32 import java.beans.IntrospectionException;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.util.AbstractMap;
36 import java.util.LinkedHashMap;
37 import java.util.LinkedHashSet;
44 @SuppressWarnings("unchecked")
45 public class YamlUtil {
47 private static final String DEFAULT = "default";
48 private static final String DEFAULT_STR = "_default";
53 * @param <T> the type parameter
54 * @param yamlContent the yaml content
55 * @param typClass the t class
58 public <T> T yamlToObject(String yamlContent, Class<T> typClass) {
59 Constructor constructor = getConstructor(typClass);
60 constructor.setPropertyUtils(getPropertyUtils());
61 TypeDescription yamlFileDescription = new TypeDescription(typClass);
62 constructor.addTypeDescription(yamlFileDescription);
63 Yaml yaml = new Yaml(constructor);
64 T yamlObj = (T) yaml.load(yamlContent);
65 //noinspection ResultOfMethodCallIgnored
70 public InputStream loadYamlFileIs(String yamlFullFileName) {
71 return YamlUtil.class.getResourceAsStream(yamlFullFileName);
77 * @param <T> the type parameter
78 * @param yamlContent the yaml content
79 * @param typClass the t class
82 public <T> T yamlToObject(InputStream yamlContent, Class<T> typClass) {
84 Constructor constructor = getConstructor(typClass);
85 constructor.setPropertyUtils(getPropertyUtils());
86 TypeDescription yamlFileDescription = new TypeDescription(typClass);
87 constructor.addTypeDescription(yamlFileDescription);
88 Yaml yaml = new Yaml(constructor);
89 T yamlObj = (T) yaml.load(yamlContent);
90 if (yamlObj != null) {
91 //noinspection ResultOfMethodCallIgnored
95 throw new RuntimeException();
97 } catch (Exception exception) {
98 throw new RuntimeException(exception);
101 if (yamlContent != null) {
104 } catch (IOException ignore) {
114 * @param <T> the type parameter
115 * @param typClass the t class
116 * @return the constructor
118 public <T> Constructor getConstructor(Class<T> typClass) {
119 return new StrictMapAppenderConstructor(typClass);
123 * Gets property utils.
125 * @return the property utils
127 protected PropertyUtils getPropertyUtils() {
128 return new MyPropertyUtils();
135 * @param yamlContent the yaml content
138 public Map<String, LinkedHashMap<String, Object>> yamlToMap(InputStream yamlContent) {
139 Yaml yaml = new Yaml();
140 return (Map<String, LinkedHashMap<String, Object>>) yaml.load(yamlContent);
144 * Object to yaml string.
148 public String objectToYaml(Object obj) {
149 DumperOptions options = new DumperOptions();
150 options.setPrettyFlow(true);
151 options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
152 Representer representer = new CustomRepresenter();
153 representer.addClassTag(obj.getClass(), Tag.MAP);
154 representer.setPropertyUtils(new MyPropertyUtils());
156 Yaml yaml = new Yaml(representer, options);
157 return yaml.dump(obj);
161 * Is yaml file content valid boolean.
163 * @param yamlFullFileName the yaml full file name
164 * @return the boolean
166 public boolean isYamlFileContentValid(String yamlFullFileName) {
167 Yaml yaml = new Yaml();
169 Object loadResult = yaml.load(yamlFullFileName);
170 return loadResult != null;
171 } catch (Exception exception) {
177 private class CustomRepresenter extends Representer {
179 protected MappingNode representJavaBean(Set<Property> properties, Object javaBean) {
180 //remove the bean type from the output yaml (!! ...)
181 if (!classTags.containsKey(javaBean.getClass())) {
182 addClassTag(javaBean.getClass(), Tag.MAP);
185 return super.representJavaBean(properties, javaBean);
189 protected NodeTuple representJavaBeanProperty(Object javaBean, Property property,
190 Object propertyValue, Tag customTag) {
191 if (propertyValue == null) {
194 NodeTuple defaultNode =
195 super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
197 return DEFAULT_STR.equals(property.getName())
198 ? new NodeTuple(representData(DEFAULT), defaultNode.getValueNode())
206 * The type My property utils.
208 public class MyPropertyUtils extends PropertyUtils {
209 //Unsorted properties
211 protected Set<Property> createPropertySet(Class<? extends Object> type, BeanAccess bnAccess)
212 throws IntrospectionException {
213 return new LinkedHashSet<>(getPropertiesMap(type,
214 BeanAccess.FIELD).values());
218 public Property getProperty(Class<?> type, String name) throws IntrospectionException {
219 String updatedName = name;
220 if (DEFAULT.equals(updatedName)) {
221 updatedName = DEFAULT_STR;
223 return super.getProperty(type, updatedName);
229 * The type Strict map appender constructor.
231 protected class StrictMapAppenderConstructor extends Constructor {
234 * Instantiates a new Strict map appender constructor.
236 * @param theRoot the the root
238 public StrictMapAppenderConstructor(Class<?> theRoot) {
243 protected Map<Object, Object> createDefaultMap() {
244 final Map<Object, Object> delegate = super.createDefaultMap();
245 return new AbstractMap<Object, Object>() {
247 public Object put(Object key, Object value) {
248 if (delegate.containsKey(key)) {
249 throw new IllegalStateException("duplicate key: " + key);
251 return delegate.put(key, value);
255 public Set<Entry<Object, Object>> entrySet() {
256 return delegate.entrySet();
262 protected Map<Object, Object> constructMapping(MappingNode node) {
264 return super.constructMapping(node);
265 } catch (IllegalStateException exception) {
266 throw new ParserException("while parsing MappingNode",
267 node.getStartMark(), exception.getMessage(),