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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 DisplayLocationManager displayLocationManager;
  46. private final Map<String, EventFormat> formats = new HashMap<>();
  47. public YamlEventFormatProvider(final Path path, final ColourManager colourManager,
  48. final DisplayLocationManager displayLocationManager) {
  49. this.path = path;
  50. this.colourManager = colourManager;
  51. this.displayLocationManager = displayLocationManager;
  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. return EventFormat.create(
  90. template, beforeTemplate, afterTemplate, iterateProperty,
  91. getDisplayProperties(info));
  92. }
  93. private DisplayPropertyMap getDisplayProperties(final Map<Object, Object> info) {
  94. final DisplayPropertyMap map = new DisplayPropertyMap();
  95. if (info.containsKey("colour")) {
  96. map.put(DisplayProperty.FOREGROUND_COLOUR,
  97. colourManager.getColourFromString(
  98. info.get("colour").toString(), Colour.BLACK));
  99. }
  100. if (info.containsKey("background")) {
  101. map.put(DisplayProperty.BACKGROUND_COLOUR,
  102. colourManager.getColourFromString(
  103. info.get("background").toString(), Colour.BLACK));
  104. }
  105. if (info.containsKey("timestamp")) {
  106. map.put(DisplayProperty.NO_TIMESTAMPS,
  107. !info.get("timestamp").toString().toLowerCase().matches("y|yes|true|1|on"));
  108. }
  109. if (info.containsKey("displaywindow")) {
  110. try {
  111. map.put(DisplayProperty.DISPLAY_LOCATION, displayLocationManager.getDisplayLocation(
  112. info.get("displaywindow").toString()).orElseThrow(() -> new IllegalArgumentException()));
  113. } catch (final IllegalArgumentException iae) {
  114. LOG.info(USER_ERROR, "Invalid displaywindow specified for: {}.\nValid values are: {}",
  115. info.get("displaywindow").toString(),
  116. displayLocationManager.getDisplayLocations().toString());
  117. }
  118. }
  119. return map;
  120. }
  121. @Override
  122. public Optional<EventFormat> getFormat(final Class<? extends DisplayableEvent> eventType) {
  123. return Optional.ofNullable(formats.get(eventType.getSimpleName()));
  124. }
  125. @Override
  126. public void reload() {
  127. load();
  128. }
  129. }