Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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. /**
  48. * Store that reads and writes profiles from a YAML file on disk.
  49. */
  50. public class YamlProfileStore implements ProfileStore {
  51. private static final Logger LOG = LoggerFactory.getLogger(YamlProfileStore.class);
  52. /** The charset to use when reading and writing files. */
  53. private static final String CHARSET = "UTF-8";
  54. /** The path to the file to read and write auto commands in. */
  55. private final Path path;
  56. /**
  57. * Creates a new YAML profiles store.
  58. *
  59. * @param path The path to the YAML file to read and write profiles in.
  60. */
  61. public YamlProfileStore(final Path path) {
  62. this.path = path;
  63. }
  64. @Override
  65. public Collection<Profile> readProfiles() {
  66. final Set<Profile> profiles = new HashSet<>();
  67. if (Files.exists(path)) {
  68. try (final InputStream stream = Files.newInputStream(path);
  69. final InputStreamReader reader = new InputStreamReader(stream, CHARSET)) {
  70. final YamlReader yamlReader = new YamlReader(reader);
  71. final Object root = yamlReader.read();
  72. profiles.addAll(readProfilesFromYaml(asList(root)));
  73. yamlReader.close();
  74. } catch (IOException | IllegalArgumentException ex) {
  75. LOG.warn("Unable to read auto commands", ex);
  76. }
  77. }
  78. return profiles;
  79. }
  80. @Override
  81. public void writeProfiles(final Collection<Profile> profiles) {
  82. final List<Object> list = getProfilesForYaml(profiles);
  83. try (final OutputStream stream = Files.newOutputStream(path);
  84. final OutputStreamWriter writer = new OutputStreamWriter(stream, CHARSET)) {
  85. final YamlWriter yamlWriter = new YamlWriter(writer);
  86. yamlWriter.write(list);
  87. yamlWriter.close();
  88. } catch (IOException ex) {
  89. LOG.error("Unable to write auto commands", ex);
  90. }
  91. }
  92. /**
  93. * Builds a set of auto commands from the given YAML representations.
  94. *
  95. * @param objects The raw objects read from the YAML file.
  96. *
  97. * @return A list of corresponding auto commands.
  98. */
  99. private Collection<Profile> readProfilesFromYaml(final Collection<Object> objects) {
  100. final Collection<Profile> res = new ArrayList<>(objects.size());
  101. for (Object profile : objects) {
  102. try {
  103. final Map<Object, Object> map = asMap(profile);
  104. final String name = requiredString(map, "name");
  105. final String realname = requiredString(map, "realname");
  106. final Optional<String> ident = Optional.ofNullable(optionalString(map, "ident"));
  107. final List<String> nicknames = Splitter.on('\n')
  108. .splitToList(requiredString(map, "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());
  133. res.add(map);
  134. }
  135. return res;
  136. }
  137. }