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

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