Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

EventFormatter.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 java.util.Optional;
  25. import javax.inject.Inject;
  26. import javax.inject.Singleton;
  27. /**
  28. * Formats an event into a text string, based on a user-defined template.
  29. *
  30. * <p>Template tags start with <code>{{</code> and end with <code>}}</code>. The start of the
  31. * tag should be the property of the event that will be displayed. Properties can be chained using
  32. * a <code>.</code> character, e.g. <code>{{user.hostname}}</code>. One or more functions can
  33. * be applied to the result of this, to change the appearance of the output. Functions are
  34. * separated from their argument with a <code>|</code> character,
  35. * e.g. <code>{{user.hostname|uppercase}}</code>.
  36. *
  37. * <p>Properties and functions are case-insensitive.
  38. */
  39. @Singleton
  40. public class EventFormatter {
  41. private static final String ERROR_STRING = "<FormatError>";
  42. private final EventPropertyManager propertyManager;
  43. private final EventFormatProvider formatProvider;
  44. @Inject
  45. public EventFormatter(final EventPropertyManager propertyManager,
  46. final EventFormatProvider formatProvider) {
  47. this.propertyManager = propertyManager;
  48. this.formatProvider = formatProvider;
  49. }
  50. public Optional<String> format(final DisplayableEvent event) {
  51. final Optional<EventFormat> format = formatProvider.getFormat(event.getClass());
  52. format.map(EventFormat::getDisplayProperties)
  53. .ifPresent(event.getDisplayProperties()::putAll);
  54. return format.map(f -> format(f, event));
  55. }
  56. private String format(final EventFormat format, final DisplayableEvent event) {
  57. final StringBuilder builder = new StringBuilder();
  58. format.getBeforeTemplate().ifPresent(
  59. before -> builder.append(doSubstitutions(event, before)).append('\n'));
  60. builder.append(
  61. format.getIterateProperty()
  62. .map(iterate -> formatIterable(event, iterate, format.getTemplate()))
  63. .orElseGet(() -> doSubstitutions(event, format.getTemplate())));
  64. format.getAfterTemplate().ifPresent(
  65. after -> builder.append('\n').append(doSubstitutions(event, after)));
  66. return builder.toString();
  67. }
  68. private String doSubstitutions(final Object dataSource, final String line) {
  69. final StringBuilder builder = new StringBuilder(line);
  70. int tagStart = builder.indexOf("{{");
  71. while (tagStart > -1) {
  72. final int tagEnd = builder.indexOf("}}", tagStart);
  73. final String tag = builder.substring(tagStart + 2, tagEnd);
  74. final String replacement = getReplacement(dataSource, tag);
  75. builder.replace(tagStart, tagEnd + 2, replacement);
  76. tagStart = builder.indexOf("{{", tagStart + replacement.length());
  77. }
  78. return builder.toString();
  79. }
  80. private String formatIterable(final DisplayableEvent event, final String property,
  81. final String template) {
  82. final Optional<Object> value
  83. = propertyManager.getProperty(event, event.getClass(), property);
  84. if (!value.isPresent() || !(value.get() instanceof Iterable<?>)) {
  85. return ERROR_STRING;
  86. }
  87. @SuppressWarnings("unchecked")
  88. final Iterable<Object> collection = (Iterable<Object>) value.get();
  89. final StringBuilder res = new StringBuilder();
  90. for (Object line : collection) {
  91. if (res.length() > 0) {
  92. res.append('\n');
  93. }
  94. res.append(doSubstitutions(line, template));
  95. }
  96. return res.toString();
  97. }
  98. private String getReplacement(final Object dataSource, final String tag) {
  99. final String[] functionParts = tag.split("\\|");
  100. final String[] dataParts = functionParts[0].split("\\.");
  101. Object target = dataSource;
  102. for (String part : dataParts) {
  103. final Optional<Object> result =
  104. propertyManager.getProperty(target, target.getClass(), part);
  105. if (result.isPresent()) {
  106. target = result.get();
  107. } else {
  108. return ERROR_STRING;
  109. }
  110. }
  111. String value = target.toString();
  112. for (int i = 1; i < functionParts.length; i++) {
  113. value = propertyManager.applyFunction(value, functionParts[i]);
  114. }
  115. return value;
  116. }
  117. }