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;
40 @SuppressWarnings("unchecked")
41 public class YamlUtil {
43 private static final String DEFAULT = "default";
44 private static final String DEFAULT_STR = "_default";
49 * @param <T> the type parameter
50 * @param yamlContent the yaml content
51 * @param typClass the t class
54 public <T> T yamlToObject(String yamlContent, Class<T> typClass) {
55 Constructor constructor = getConstructor(typClass);
56 constructor.setPropertyUtils(getPropertyUtils());
57 TypeDescription yamlFileDescription = new TypeDescription(typClass);
58 constructor.addTypeDescription(yamlFileDescription);
59 Yaml yaml = new Yaml(constructor);
60 T yamlObj = (T) yaml.load(yamlContent);
61 //noinspection ResultOfMethodCallIgnored
66 public InputStream loadYamlFileIs(String yamlFullFileName) {
67 return YamlUtil.class.getResourceAsStream(yamlFullFileName);
73 * @param <T> the type parameter
74 * @param yamlContent the yaml content
75 * @param typClass the t class
78 public <T> T yamlToObject(InputStream yamlContent, Class<T> typClass) {
80 Constructor constructor = getConstructor(typClass);
81 constructor.setPropertyUtils(getPropertyUtils());
82 TypeDescription yamlFileDescription = new TypeDescription(typClass);
83 constructor.addTypeDescription(yamlFileDescription);
84 Yaml yaml = new Yaml(constructor);
85 T yamlObj = (T) yaml.load(yamlContent);
86 if (yamlObj != null) {
87 //noinspection ResultOfMethodCallIgnored
91 throw new RuntimeException();
93 } catch (Exception exception) {
94 throw new RuntimeException(exception);
97 if (yamlContent != null) {
100 } catch (IOException ignore) {
110 * @param <T> the type parameter
111 * @param typClass the t class
112 * @return the constructor
114 public <T> Constructor getConstructor(Class<T> typClass) {
115 return new StrictMapAppenderConstructor(typClass);
119 * Gets property utils.
121 * @return the property utils
123 protected PropertyUtils getPropertyUtils() {
124 return new MyPropertyUtils();
131 * @param yamlContent the yaml content
134 public Map<String, LinkedHashMap<String, Object>> yamlToMap(InputStream yamlContent) {
135 Yaml yaml = new Yaml();
136 return (Map<String, LinkedHashMap<String, Object>>) yaml.load(yamlContent);
140 * Object to yaml string.
144 public String objectToYaml(Object obj) {
145 DumperOptions options = new DumperOptions();
146 options.setPrettyFlow(true);
147 options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
148 Representer representer = new CustomRepresenter();
149 representer.addClassTag(obj.getClass(), Tag.MAP);
150 representer.setPropertyUtils(new MyPropertyUtils());
152 Yaml yaml = new Yaml(representer, options);
153 return yaml.dump(obj);
157 * Is yaml file content valid boolean.
159 * @param yamlFullFileName the yaml full file name
160 * @return the boolean
162 public boolean isYamlFileContentValid(String yamlFullFileName) {
163 Yaml yaml = new Yaml();
165 Object loadResult = yaml.load(yamlFullFileName);
166 return loadResult != null;
167 } catch (Exception exception) {
173 private class CustomRepresenter extends Representer {
175 protected MappingNode representJavaBean(Set<Property> properties, Object javaBean) {
176 //remove the bean type from the output yaml (!! ...)
177 if (!classTags.containsKey(javaBean.getClass())) {
178 addClassTag(javaBean.getClass(), Tag.MAP);
181 return super.representJavaBean(properties, javaBean);
185 protected NodeTuple representJavaBeanProperty(Object javaBean, Property property,
186 Object propertyValue, Tag customTag) {
187 if (propertyValue == null) {
190 NodeTuple defaultNode =
191 super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
193 return DEFAULT_STR.equals(property.getName())
194 ? new NodeTuple(representData(DEFAULT), defaultNode.getValueNode())
202 * The type My property utils.
204 public class MyPropertyUtils extends PropertyUtils {
205 //Unsorted properties
207 protected Set<Property> createPropertySet(Class<? extends Object> type, BeanAccess bnAccess)
208 throws IntrospectionException {
209 return new LinkedHashSet<>(getPropertiesMap(type,
210 BeanAccess.FIELD).values());
214 public Property getProperty(Class<?> type, String name) throws IntrospectionException {
215 String updatedName = name;
216 if (DEFAULT.equals(updatedName)) {
217 updatedName = DEFAULT_STR;
219 return super.getProperty(type, updatedName);
225 * The type Strict map appender constructor.
227 protected class StrictMapAppenderConstructor extends Constructor {
230 * Instantiates a new Strict map appender constructor.
232 * @param theRoot the the root
234 public StrictMapAppenderConstructor(Class<?> theRoot) {
239 protected Map<Object, Object> createDefaultMap() {
240 final Map<Object, Object> delegate = super.createDefaultMap();
241 return new AbstractMap<Object, Object>() {
243 public Object put(Object key, Object value) {
244 if (delegate.containsKey(key)) {
245 throw new IllegalStateException("duplicate key: " + key);
247 return delegate.put(key, value);
251 public Set<Entry<Object, Object>> entrySet() {
252 return delegate.entrySet();
258 protected Map<Object, Object> constructMapping(MappingNode node) {
260 return super.constructMapping(node);
261 } catch (IllegalStateException exception) {
262 throw new ParserException("while parsing MappingNode",
263 node.getStartMark(), exception.getMessage(),