您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

YamlEventFormatProvider.java 6.2KB

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