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.

YamlAliasStore.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (c) 2006-2015 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.commandparser.aliases;
  23. import com.dmdirc.util.BaseYamlStore;
  24. import java.nio.file.Path;
  25. import java.util.HashMap;
  26. import java.util.HashSet;
  27. import java.util.Map;
  28. import java.util.Optional;
  29. import java.util.Set;
  30. import org.slf4j.Logger;
  31. import org.slf4j.LoggerFactory;
  32. import static com.dmdirc.util.YamlReaderUtils.asMap;
  33. import static com.dmdirc.util.YamlReaderUtils.requiredString;
  34. /**
  35. * Store that reads and writes aliases from a YAML file on disk.
  36. * <p>
  37. * Aliases are written as a list of maps:
  38. * <pre><code>
  39. * ---
  40. * - name: quit
  41. * min_arguments: 0
  42. * substitution: exit $1-
  43. * </code></pre>
  44. */
  45. public class YamlAliasStore extends BaseYamlStore<Alias> implements AliasStore {
  46. private static final Logger LOG = LoggerFactory.getLogger(YamlAliasStore.class);
  47. /** The path to the file to read and write aliases in. */
  48. private final Path path;
  49. /** The factory to use to create aliases. */
  50. private final AliasFactory factory;
  51. /**
  52. * Creates a new YAML alias store.
  53. *
  54. * @param path The path to the YAML file to read and write aliases in.
  55. * @param factory The factory to use to create aliases.
  56. */
  57. public YamlAliasStore(final Path path, final AliasFactory factory) {
  58. this.path = path;
  59. this.factory = factory;
  60. }
  61. @Override
  62. public Set<Alias> readAliases() {
  63. return new HashSet<>(read(path));
  64. }
  65. @Override
  66. public void writeAliases(final Set<Alias> aliases) {
  67. write(path, aliases);
  68. }
  69. @Override
  70. protected Optional<Alias> convertFromYaml(final Object object) {
  71. try {
  72. final Map<Object, Object> map = asMap(object);
  73. final String name = requiredString(map, "name");
  74. final int minArguments = Integer.parseInt(requiredString(map, "min_arguments"));
  75. final String substitution = requiredString(map, "substitution");
  76. return Optional.of(factory.createAlias(name, minArguments, substitution));
  77. } catch (IllegalArgumentException ex) {
  78. LOG.info("Unable to read alias", ex);
  79. return Optional.empty();
  80. }
  81. }
  82. @Override
  83. protected Object convertToYaml(final Alias object) {
  84. final Map<Object, Object> map = new HashMap<>();
  85. map.put("name", object.getName());
  86. map.put("min_arguments", String.valueOf(object.getMinArguments()));
  87. map.put("substitution", object.getSubstitution());
  88. return map;
  89. }
  90. }