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.

YamlEventFormatProvider.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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.ui.messages;
  23. import com.dmdirc.events.DisplayableEvent;
  24. import com.dmdirc.util.colours.Colour;
  25. import java.io.IOException;
  26. import java.io.InputStream;
  27. import java.io.InputStreamReader;
  28. import java.nio.file.Files;
  29. import java.nio.file.Path;
  30. import java.util.HashMap;
  31. import java.util.Map;
  32. import java.util.Optional;
  33. import org.slf4j.Logger;
  34. import org.slf4j.LoggerFactory;
  35. import com.esotericsoftware.yamlbeans.YamlReader;
  36. import static com.dmdirc.util.LogUtils.FATAL_APP_ERROR;
  37. import static com.dmdirc.util.LogUtils.USER_ERROR;
  38. import static com.dmdirc.util.YamlReaderUtils.asMap;
  39. /**
  40. * YAML-backed supplier of event formats.
  41. */
  42. public class YamlEventFormatProvider implements EventFormatProvider {
  43. private static final Logger LOG = LoggerFactory.getLogger(YamlEventFormatProvider.class);
  44. /** The charset to use when reading and writing files. */
  45. private static final String CHARSET = "UTF-8";
  46. private final Path path;
  47. private final ColourManager colourManager;
  48. private final Map<String, EventFormat> formats = new HashMap<>();
  49. public YamlEventFormatProvider(final Path path, final ColourManager colourManager) {
  50. this.path = path;
  51. this.colourManager = colourManager;
  52. }
  53. public void load() {
  54. formats.clear();
  55. try (final InputStream stream = getClass().getResourceAsStream("format.yml")) {
  56. load(stream);
  57. } catch (IOException e) {
  58. LOG.error(FATAL_APP_ERROR, "Unable to load default event templates", e);
  59. }
  60. if (Files.exists(path)) {
  61. try (final InputStream stream = Files.newInputStream(path)) {
  62. load(stream);
  63. } catch (IOException e) {
  64. LOG.info(USER_ERROR, "Unable to load event templates from {}", path, e);
  65. }
  66. }
  67. }
  68. private void load(final InputStream stream) throws IOException {
  69. try (final InputStreamReader reader = new InputStreamReader(stream, CHARSET)) {
  70. final YamlReader yamlReader = new YamlReader(reader);
  71. final Object root = yamlReader.read();
  72. final Map<Object, Object> entries = asMap(root);
  73. entries.forEach((k, v) -> formats.put(k.toString(), readFormat(v)));
  74. yamlReader.close();
  75. }
  76. }
  77. private EventFormat readFormat(final Object format) {
  78. final Map<Object, Object> info = asMap(format);
  79. final String template = info.get("format").toString();
  80. final Optional<String> beforeTemplate = info.containsKey("before")
  81. ? Optional.of(info.get("before").toString())
  82. : Optional.empty();
  83. final Optional<String> afterTemplate = info.containsKey("after")
  84. ? Optional.of(info.get("after").toString())
  85. : Optional.empty();
  86. final Optional<String> iterateProperty = info.containsKey("iterate")
  87. ? Optional.of(info.get("iterate").toString())
  88. : Optional.empty();
  89. final Optional<Colour> foregroundColour = info.containsKey("colour")
  90. ? Optional.of(colourManager.getColourFromIrcCode(
  91. Integer.parseInt(info.get("colour").toString())))
  92. : Optional.empty();
  93. return EventFormat.create(
  94. template, beforeTemplate, afterTemplate, iterateProperty, foregroundColour);
  95. }
  96. @Override
  97. public Optional<EventFormat> getFormat(final Class<? extends DisplayableEvent> eventType) {
  98. return Optional.ofNullable(formats.get(eventType.getSimpleName()));
  99. }
  100. }