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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.commandparser.aliases;
  18. import com.dmdirc.util.io.yaml.BaseYamlStore;
  19. import java.nio.file.Path;
  20. import java.util.HashMap;
  21. import java.util.HashSet;
  22. import java.util.Map;
  23. import java.util.Optional;
  24. import java.util.Set;
  25. import org.slf4j.Logger;
  26. import org.slf4j.LoggerFactory;
  27. import static com.dmdirc.util.io.yaml.YamlReaderUtils.asMap;
  28. import static com.dmdirc.util.io.yaml.YamlReaderUtils.requiredString;
  29. /**
  30. * Store that reads and writes aliases from a YAML file on disk.
  31. * <p>
  32. * Aliases are written as a list of maps:
  33. * <pre><code>
  34. * ---
  35. * - name: quit
  36. * min_arguments: 0
  37. * substitution: exit $1-
  38. * </code></pre>
  39. */
  40. public class YamlAliasStore extends BaseYamlStore<Alias> implements AliasStore {
  41. private static final Logger LOG = LoggerFactory.getLogger(YamlAliasStore.class);
  42. /** The path to the file to read and write aliases in. */
  43. private final Path path;
  44. /** The factory to use to create aliases. */
  45. private final AliasFactory factory;
  46. /**
  47. * Creates a new YAML alias store.
  48. *
  49. * @param path The path to the YAML file to read and write aliases in.
  50. * @param factory The factory to use to create aliases.
  51. */
  52. public YamlAliasStore(final Path path, final AliasFactory factory) {
  53. this.path = path;
  54. this.factory = factory;
  55. }
  56. @Override
  57. public Set<Alias> readAliases() {
  58. return new HashSet<>(read(path));
  59. }
  60. @Override
  61. public void writeAliases(final Set<Alias> aliases) {
  62. write(path, aliases);
  63. }
  64. @Override
  65. protected Optional<Alias> convertFromYaml(final Object object) {
  66. try {
  67. final Map<Object, Object> map = asMap(object);
  68. final String name = requiredString(map, "name");
  69. final int minArguments = Integer.parseInt(requiredString(map, "min_arguments"));
  70. final String substitution = requiredString(map, "substitution");
  71. return Optional.of(factory.createAlias(name, minArguments, substitution));
  72. } catch (IllegalArgumentException ex) {
  73. LOG.info("Unable to read alias", ex);
  74. return Optional.empty();
  75. }
  76. }
  77. @Override
  78. protected Object convertToYaml(final Alias object) {
  79. final Map<Object, Object> map = new HashMap<>();
  80. map.put("name", object.getName());
  81. map.put("min_arguments", String.valueOf(object.getMinArguments()));
  82. map.put("substitution", object.getSubstitution());
  83. return map;
  84. }
  85. }