daily pastebin goal
48%
SHARE
TWEET

Untitled

a guest Jun 19th, 2017 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.config.parser.impl;
  2.  
  3. import com.client.util.FrameType;
  4. import com.config.object.Attribute;
  5. import com.config.parser.Provider;
  6. import com.config.parser.util.DefaultAttributes;
  7. import com.esotericsoftware.yamlbeans.YamlReader;
  8. import com.esotericsoftware.yamlbeans.YamlWriter;
  9. import com.logging.Logger;
  10.  
  11. import java.io.File;
  12. import java.io.FileReader;
  13. import java.io.FileWriter;
  14. import java.util.*;
  15. import java.util.logging.Level;
  16.  
  17. /**
  18.  * @author Daniel
  19.  */
  20. public class YAMLProvider implements Provider {
  21.  
  22.     @Override
  23.     public File getFile() {
  24.         return new File("./Settings.yml");
  25.     }
  26.  
  27.     @SuppressWarnings("unchecked")
  28.     @Override
  29.     public Collection<Attribute> getAll() {
  30.         final List<Attribute> list = new LinkedList<Attribute>();
  31.         try {
  32.             YamlReader reader = new YamlReader(new FileReader(getFile()));
  33.             Map<String, List<Map<String, Object>>> map = reader.read(HashMap.class);
  34.             for (Map<String, Object> set : map.get("objects")) {
  35.                 for (String name : set.keySet()) {
  36.                     Class<? extends Attribute> clazz = (Class<? extends Attribute>) Class.forName(name);
  37.                     if (clazz != null) {
  38.                         final Attribute attribute = clazz.newInstance();
  39.                         if (attribute != null) {
  40.                             final String value = (String) set.get(attribute.getClass().getCanonicalName());
  41.                             if (value != null) {
  42.                                 attribute.setValue(attribute.getValue() instanceof Boolean ? Boolean.valueOf(value) : attribute.getValue() instanceof Integer ? Integer.valueOf(value) : attribute.getValue() instanceof FrameType ? FrameType.valueOf(value) : value);
  43.                             }
  44.                         }
  45.                         list.add(attribute);
  46.                     }
  47.                 }
  48.             }
  49.             reader.close();
  50.             return list;
  51.         } catch (Exception ex) {
  52.             Logger.log(YAMLProvider.class, Level.WARNING, "exception encountered while loading settings", ex);
  53.             final Collection<Attribute> collection = new DefaultAttributes().getCollection();
  54.             write(collection);
  55.             return collection;
  56.         }
  57.     }
  58.  
  59.     @Override
  60.     public void write(Collection<Attribute> collection) {
  61.         try {
  62.             YamlWriter writer = new YamlWriter(new FileWriter(getFile()));
  63.             Map<String, List<Map<String, Object>>> map = new HashMap<String, List<Map<String, Object>>>();
  64.             map.put("objects", new ArrayList<Map<String, Object>>());
  65.             for (Attribute attribute : collection) {
  66.                 Map<String, Object> set = new HashMap<String, Object>();
  67.                 set.put(attribute.getClass().getCanonicalName(), attribute.getValue());
  68.                 map.get("objects").add(set);
  69.             }
  70.             writer.write(map);
  71.             writer.close();
  72.         } catch (Exception ex) {
  73.             Logger.log(YAMLProvider.class, Level.WARNING, "exception encountered while saving settings", ex);
  74.             write(new DefaultAttributes().getCollection());
  75.         }
  76.     }
  77. }
RAW Paste Data
Top