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 5.3KB

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