You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

YamlProfileStore.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.config.profiles;
  23. import com.google.common.base.Splitter;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.io.InputStreamReader;
  27. import java.io.OutputStream;
  28. import java.io.OutputStreamWriter;
  29. import java.nio.file.Files;
  30. import java.nio.file.Path;
  31. import java.util.ArrayList;
  32. import java.util.Collection;
  33. import java.util.HashMap;
  34. import java.util.HashSet;
  35. import java.util.List;
  36. import java.util.Map;
  37. import java.util.Optional;
  38. import java.util.Set;
  39. import org.slf4j.Logger;
  40. import org.slf4j.LoggerFactory;
  41. import com.esotericsoftware.yamlbeans.YamlReader;
  42. import com.esotericsoftware.yamlbeans.YamlWriter;
  43. import static com.dmdirc.util.YamlReaderUtils.asList;
  44. import static com.dmdirc.util.YamlReaderUtils.asMap;
  45. import static com.dmdirc.util.YamlReaderUtils.optionalString;
  46. import static com.dmdirc.util.YamlReaderUtils.requiredString;
  47. import static com.dmdirc.util.YamlReaderUtils.uncheckedCast;
  48. /**
  49. * Store that reads and writes profiles from a YAML file on disk.
  50. */
  51. public class YamlProfileStore implements ProfileStore {
  52. private static final Logger LOG = LoggerFactory.getLogger(YamlProfileStore.class);
  53. /** The charset to use when reading and writing files. */
  54. private static final String CHARSET = "UTF-8";
  55. /** The path to the file to read and write auto commands in. */
  56. private final Path path;
  57. /**
  58. * Creates a new YAML profiles store.
  59. *
  60. * @param path The path to the YAML file to read and write profiles in.
  61. */
  62. public YamlProfileStore(final Path path) {
  63. this.path = path;
  64. }
  65. @Override
  66. public Collection<Profile> readProfiles() {
  67. final Set<Profile> profiles = new HashSet<>();
  68. if (Files.exists(path)) {
  69. try (final InputStream stream = Files.newInputStream(path);
  70. final InputStreamReader reader = new InputStreamReader(stream, CHARSET)) {
  71. final YamlReader yamlReader = new YamlReader(reader);
  72. final Object root = yamlReader.read();
  73. profiles.addAll(readProfilesFromYaml(asList(root)));
  74. yamlReader.close();
  75. } catch (IOException | IllegalArgumentException ex) {
  76. LOG.warn("Unable to read auto commands", ex);
  77. }
  78. }
  79. return profiles;
  80. }
  81. @Override
  82. public void writeProfiles(final Collection<Profile> profiles) {
  83. final List<Object> list = getProfilesForYaml(profiles);
  84. try (final OutputStream stream = Files.newOutputStream(path);
  85. final OutputStreamWriter writer = new OutputStreamWriter(stream, CHARSET)) {
  86. final YamlWriter yamlWriter = new YamlWriter(writer);
  87. yamlWriter.write(list);
  88. yamlWriter.close();
  89. } catch (IOException ex) {
  90. LOG.error("Unable to write auto commands", ex);
  91. }
  92. }
  93. /**
  94. * Builds a set of auto commands from the given YAML representations.
  95. *
  96. * @param objects The raw objects read from the YAML file.
  97. *
  98. * @return A list of corresponding auto commands.
  99. */
  100. private Collection<Profile> readProfilesFromYaml(final Collection<Object> objects) {
  101. final Collection<Profile> res = new ArrayList<>(objects.size());
  102. for (Object profile : objects) {
  103. try {
  104. final Map<Object, Object> map = asMap(profile);
  105. final String name = requiredString(map, "name");
  106. final String realname = requiredString(map, "realname");
  107. final Optional<String> ident = Optional.ofNullable(optionalString(map, "ident"));
  108. final List<String> nicknames = uncheckedCast(asList(map.get("nicknames")));
  109. res.add(new Profile(name, realname, ident, nicknames));
  110. } catch (IllegalArgumentException ex) {
  111. LOG.info("Unable to read alias", ex);
  112. }
  113. }
  114. return res;
  115. }
  116. /**
  117. * Gets a set of maps that can be written to a YAML file.
  118. *
  119. * @param profiles The profiles to be stored.
  120. *
  121. * @return A list of corresponding maps.
  122. */
  123. private List<Object> getProfilesForYaml(final Collection<Profile> profiles) {
  124. final List<Object> res = new ArrayList<>(profiles.size());
  125. for (Profile profile : profiles) {
  126. final Map<Object, Object> map = new HashMap<>();
  127. map.put("name", profile.getName());
  128. map.put("realname", profile.getRealname());
  129. if (profile.getIdent().isPresent()) {
  130. map.put("ident", profile.getIdent().get());
  131. }
  132. map.put("nicknames", profile.getNicknames().toArray());
  133. res.add(map);
  134. }
  135. return res;
  136. }
  137. }