001    package com.khubla.pragmatach.plugin.yaml;
002    
003    import java.io.ByteArrayInputStream;
004    import java.io.ByteArrayOutputStream;
005    import java.io.InputStream;
006    import java.io.OutputStream;
007    import java.lang.reflect.Field;
008    import java.util.HashMap;
009    import java.util.Map;
010    
011    import org.apache.commons.beanutils.BeanUtils;
012    import org.apache.commons.io.IOUtils;
013    import org.yaml.snakeyaml.Yaml;
014    
015    import com.khubla.pragmatach.framework.api.PragmatachException;
016    
017    /**
018     * @author tome
019     */
020    public class PragmatachYAML {
021       /**
022        * convert object from YAML
023        */
024       public static void fromYAML(Object object, InputStream inputStream) throws PragmatachException {
025          try {
026             final ByteArrayOutputStream baos = new ByteArrayOutputStream();
027             IOUtils.copy(inputStream, baos);
028             fromYAML(object, baos.toString());
029          } catch (final Exception e) {
030             throw new PragmatachException("Exception in fromYAML", e);
031          }
032       }
033    
034       /**
035        * convert object from YAML
036        */
037       public static void fromYAML(Object object, String YAML) throws PragmatachException {
038          try {
039             final Map<String, String> values = parseYAML(YAML);
040             if ((null != values) && (values.size() > 0)) {
041                for (final Field field : object.getClass().getDeclaredFields()) {
042                   final String value = values.get(field.getName());
043                   BeanUtils.setProperty(object, field.getName(), value);
044                }
045             }
046          } catch (final Exception e) {
047             throw new PragmatachException("Exception in fromYAML", e);
048          }
049       }
050    
051       /**
052        * parse YAML to map
053        */
054       public static Map<String, String> parseYAML(InputStream inputStream) throws PragmatachException {
055          try {
056             final ByteArrayOutputStream baos = new ByteArrayOutputStream();
057             IOUtils.copy(inputStream, baos);
058             return parseYAML(baos.toString());
059          } catch (final Exception e) {
060             throw new PragmatachException("Exception in parseYAML", e);
061          }
062       }
063    
064       /**
065        * parse YAML to map
066        */
067       @SuppressWarnings("unchecked")
068       public static Map<String, String> parseYAML(String YAML) throws PragmatachException {
069          try {
070             final Yaml yaml = new Yaml();
071             return (Map<String, String>) yaml.load(YAML);
072          } catch (final Exception e) {
073             throw new PragmatachException("Exception in parseYAML", e);
074          }
075       }
076    
077       /**
078        * convert object to YAML
079        */
080       public static String toYAML(Object object) throws PragmatachException {
081          try {
082             final Map<String, String> map = new HashMap<String, String>();
083             for (final Field field : object.getClass().getDeclaredFields()) {
084                map.put(field.getName(), BeanUtils.getSimpleProperty(object, field.getName()));
085             }
086             final Yaml yaml = new Yaml();
087             return yaml.dump(map);
088          } catch (final Exception e) {
089             throw new PragmatachException("Exception in toYAML", e);
090          }
091       }
092    
093       /**
094        * convert object to YAML
095        */
096       public static void toYAML(Object object, OutputStream outputStream) throws PragmatachException {
097          try {
098             final String YAML = toYAML(object);
099             final ByteArrayInputStream bais = new ByteArrayInputStream(YAML.getBytes());
100             IOUtils.copy(bais, outputStream);
101          } catch (final Exception e) {
102             throw new PragmatachException("Exception in toYAML", e);
103          }
104       }
105    }