| 1 | package com.renomad.minum.templating; | |
| 2 | ||
| 3 | ||
| 4 | import java.util.*; | |
| 5 | import java.util.stream.Collectors; | |
| 6 | ||
| 7 | /** | |
| 8 | * This class provides methods for working with templates. | |
| 9 | * <p> | |
| 10 | * The first step is to write a template. Here is an example: | |
| 11 | * </p> | |
| 12 | * <pre> | |
| 13 | * Hello, my name is {{name}} | |
| 14 | * </pre> | |
| 15 | * <p> | |
| 16 | * Then, feed that string into the {@link #buildProcessor} method, like | |
| 17 | * this: | |
| 18 | * </p> | |
| 19 | * <pre> | |
| 20 | * {@code | |
| 21 | * String input = "Hello, my name is {{name}}" | |
| 22 | * TemplateProcessor helloProcessor = TemplateProcessor.buildProcessor(input); | |
| 23 | * } | |
| 24 | * </pre> | |
| 25 | * <p> | |
| 26 | * The returned value ("helloProcessor") can be rendered with different values. For | |
| 27 | * example: | |
| 28 | * </p> | |
| 29 | * <pre> | |
| 30 | * {@code | |
| 31 | * Map<String,String> myMap = Map.of("name", "Susanne"); | |
| 32 | * String fullyRenderedString = helloProcessor.renderTemplate(myMap); | |
| 33 | * } | |
| 34 | * </pre> | |
| 35 | * <p> | |
| 36 | * The result is: | |
| 37 | * </p> | |
| 38 | * <pre> | |
| 39 | * {@code Hello, my name is Susanne} | |
| 40 | * </pre> | |
| 41 | */ | |
| 42 | public final class TemplateProcessor { | |
| 43 | ||
| 44 | /** | |
| 45 | * The template is made up of a list of these, which are the static (unchanging) | |
| 46 | * parts, along with the dynamic parts the user fills in later. | |
| 47 | */ | |
| 48 | private final List<TemplateSection> templateSections; | |
| 49 | ||
| 50 | /** | |
| 51 | * This value is used to calculate a quick estimate of how many | |
| 52 | * bytes of memory we will need for the buffer holding our generated string | |
| 53 | */ | |
| 54 | private final static double SIZE_ESTIMATE_MODIFIER = 1.1; | |
| 55 | ||
| 56 | /** | |
| 57 | * The String value given to us by the developer when creating this template | |
| 58 | */ | |
| 59 | private final String originalText; | |
| 60 | ||
| 61 | /** | |
| 62 | * A general estimate of the resulting size of the output from the | |
| 63 | * template process, for one rendering of the template, to provide | |
| 64 | * quick information when building a buffer to contain the results | |
| 65 | * which is appropriately sized, which greatly improves performance | |
| 66 | */ | |
| 67 | private final int estimatedSizeOfSingleTemplate; | |
| 68 | ||
| 69 | /** | |
| 70 | * Used when checking correctness - we check that the user is providing | |
| 71 | * key->value pairs that match the expected template keys. | |
| 72 | */ | |
| 73 | private final Set<String> keysFoundInTemplate; | |
| 74 | ||
| 75 | ||
| 76 | /** | |
| 77 | * Instantiate a new object with a list of {@link TemplateSection}. | |
| 78 | */ | |
| 79 | private TemplateProcessor(List<TemplateSection> templateSections, String originalText) { | |
| 80 | this.templateSections = templateSections; | |
| 81 | this.originalText = originalText; | |
| 82 |
1
1. <init> : Replaced double multiplication with division → KILLED |
this.estimatedSizeOfSingleTemplate = (int) (originalText.length() * SIZE_ESTIMATE_MODIFIER); |
| 83 | keysFoundInTemplate = templateSections.stream() | |
| 84 |
2
1. lambda$new$0 : replaced boolean return with false for com/renomad/minum/templating/TemplateProcessor::lambda$new$0 → KILLED 2. lambda$new$0 : replaced boolean return with true for com/renomad/minum/templating/TemplateProcessor::lambda$new$0 → KILLED |
.filter(x -> x.templateType.equals(TemplateType.DYNAMIC_TEXT)) |
| 85 |
1
1. lambda$new$1 : replaced return value with "" for com/renomad/minum/templating/TemplateProcessor::lambda$new$1 → KILLED |
.map(x -> x.key) |
| 86 | .collect(Collectors.toUnmodifiableSet()); | |
| 87 | } | |
| 88 | ||
| 89 | /** | |
| 90 | * Given a map of key names -> value, render a template. | |
| 91 | */ | |
| 92 | public String renderTemplate(Map<String, String> myMap) { | |
| 93 |
1
1. renderTemplate : negated conditional → KILLED |
if (myMap == null) throw new TemplateRenderException("Input to renderTemplate(Map myMap) was null"); |
| 94 |
1
1. renderTemplate : replaced return value with "" for com/renomad/minum/templating/TemplateProcessor::renderTemplate → KILLED |
return renderTemplate(List.of(myMap), ""); |
| 95 | } | |
| 96 | ||
| 97 | /** | |
| 98 | * Given a list, map of key names -> value, render one template | |
| 99 | * for each, joined by a newline. Use {@link #renderTemplate(List, String)} | |
| 100 | * for control over the delimiter. | |
| 101 | */ | |
| 102 | public String renderTemplate(List<Map<String, String>> data) { | |
| 103 |
1
1. renderTemplate : replaced return value with "" for com/renomad/minum/templating/TemplateProcessor::renderTemplate → KILLED |
return renderTemplate(data, "\n"); |
| 104 | } | |
| 105 | ||
| 106 | /** | |
| 107 | * Render a list of maps. | |
| 108 | * <p> | |
| 109 | * Similar to {@link #renderTemplate(Map)} but takes a list of maps instead | |
| 110 | * of just one. The "delimiter" argument is inserted between each rendered | |
| 111 | * template. | |
| 112 | * </p> | |
| 113 | */ | |
| 114 | public String renderTemplate(List<Map<String, String>> data, String delimiter) { | |
| 115 |
1
1. renderTemplate : negated conditional → KILLED |
if (data == null) throw new TemplateRenderException("Input to renderTemplate(List data) was null"); |
| 116 | ||
| 117 | // if we are given no information, return an empty string. | |
| 118 |
1
1. renderTemplate : negated conditional → KILLED |
if (data.isEmpty()) { |
| 119 | return ""; | |
| 120 | } | |
| 121 | ||
| 122 |
1
1. renderTemplate : removed call to com/renomad/minum/templating/TemplateProcessor::correctnessCheck → KILLED |
correctnessCheck(data); |
| 123 | ||
| 124 | // build an appropriately-sized buffer for output | |
| 125 |
1
1. renderTemplate : Replaced integer multiplication with division → SURVIVED |
int capacity = estimatedSizeOfSingleTemplate * data.size(); |
| 126 | StringBuilder stringBuilder = new StringBuilder(capacity); | |
| 127 | ||
| 128 |
2
1. renderTemplate : changed conditional boundary → KILLED 2. renderTemplate : negated conditional → KILLED |
for (int i = 0; i < data.size(); i++) { |
| 129 | for (TemplateSection templateSection : templateSections) { | |
| 130 |
1
1. renderTemplate : removed call to com/renomad/minum/templating/TemplateSection::render → KILLED |
templateSection.render(data.get(i), stringBuilder); |
| 131 | } | |
| 132 |
2
1. renderTemplate : negated conditional → KILLED 2. renderTemplate : Replaced integer subtraction with addition → KILLED |
if (i != data.size() - 1) { |
| 133 | stringBuilder.append(delimiter); | |
| 134 | } | |
| 135 | } | |
| 136 |
1
1. renderTemplate : replaced return value with "" for com/renomad/minum/templating/TemplateProcessor::renderTemplate → KILLED |
return stringBuilder.toString(); |
| 137 | } | |
| 138 | ||
| 139 | ||
| 140 | /** | |
| 141 | * Builds a {@link TemplateProcessor} from a string | |
| 142 | * containing a proper template. Templated values | |
| 143 | * are surrounded by double-curly-braces, i.e. {{foo}} or {{ foo }} | |
| 144 | */ | |
| 145 | public static TemplateProcessor buildProcessor(String template) { | |
| 146 | // this value holds the entire template after processing, comprised | |
| 147 | // of an ordered list of TemplateSections | |
| 148 | var tSections = new ArrayList<TemplateSection>(); | |
| 149 | ||
| 150 | // these values are used for logging and setting proper indentation | |
| 151 | int rowNumber = 1; | |
| 152 | int columnNumber = 1; | |
| 153 | ||
| 154 | // this value records the indent of the beginning of template keys, | |
| 155 | // so we can properly indent the values later. | |
| 156 | int startOfKey = 0; | |
| 157 | ||
| 158 | StringBuilder builder = new StringBuilder(); | |
| 159 | ||
| 160 | // this flag is to help us understand whether we are currently reading the | |
| 161 | // name of a template literal. | |
| 162 | // e.g. in the case of hello {{ name }}, "name" is the literal. | |
| 163 | boolean isInsideTemplateKeyLiteral = false; | |
| 164 |
2
1. buildProcessor : changed conditional boundary → KILLED 2. buildProcessor : negated conditional → KILLED |
for (int i = 0; i < template.length(); i++) { |
| 165 | char charAtCursor = template.charAt(i); | |
| 166 | ||
| 167 |
1
1. buildProcessor : negated conditional → KILLED |
if (justArrivedInside(template, charAtCursor, i)) { |
| 168 | isInsideTemplateKeyLiteral = true; | |
| 169 | startOfKey = columnNumber; | |
| 170 |
1
1. buildProcessor : Changed increment from 1 to -1 → TIMED_OUT |
i += 1; |
| 171 | builder = processSectionInside(builder, tSections); | |
| 172 |
1
1. buildProcessor : negated conditional → KILLED |
} else if (justArrivedOutside(template, charAtCursor, i, isInsideTemplateKeyLiteral)) { |
| 173 | isInsideTemplateKeyLiteral = false; | |
| 174 |
1
1. buildProcessor : Changed increment from 1 to -1 → KILLED |
i += 1; |
| 175 | builder = processSectionOutside(builder, tSections, startOfKey); | |
| 176 | startOfKey = 0; | |
| 177 | } else { | |
| 178 | builder.append(charAtCursor); | |
| 179 | ||
| 180 | /* | |
| 181 | if we're at the end of the template, it's our last chance to | |
| 182 | add a substring (we can't be adding to a key, since if we're | |
| 183 | at the end, and it's not a closing brace, it's a malformed | |
| 184 | template. | |
| 185 | */ | |
| 186 |
2
1. buildProcessor : Replaced integer subtraction with addition → KILLED 2. buildProcessor : negated conditional → KILLED |
if (i == template.length() - 1) { |
| 187 |
1
1. buildProcessor : negated conditional → KILLED |
if (isInsideTemplateKeyLiteral) { |
| 188 | // if we're exiting this string while inside a template literal, then | |
| 189 | // we're reading a corrupted input, and we should make that clear | |
| 190 | // to our caller. | |
| 191 |
2
1. buildProcessor : negated conditional → KILLED 2. buildProcessor : changed conditional boundary → KILLED |
String templateSample = template.length() > 10 ? template.substring(0, 10) + "..." : template; |
| 192 | throw new TemplateParseException( | |
| 193 | "parsing failed for string starting with \"" + templateSample + "\" at line " + rowNumber + " and column " + columnNumber); | |
| 194 | } | |
| 195 | tSections.add(new TemplateSection(null, builder.toString(), 0, TemplateType.STATIC_TEXT)); | |
| 196 | } | |
| 197 | } | |
| 198 | ||
| 199 |
1
1. buildProcessor : negated conditional → KILLED |
if (charAtCursor == '\n') { |
| 200 |
1
1. buildProcessor : Changed increment from 1 to -1 → KILLED |
rowNumber += 1; |
| 201 | columnNumber = 1; | |
| 202 | } else { | |
| 203 |
1
1. buildProcessor : Changed increment from 1 to -1 → KILLED |
columnNumber += 1; |
| 204 | } | |
| 205 | ||
| 206 | } | |
| 207 | ||
| 208 |
1
1. buildProcessor : replaced return value with null for com/renomad/minum/templating/TemplateProcessor::buildProcessor → KILLED |
return new TemplateProcessor(tSections, template); |
| 209 | } | |
| 210 | ||
| 211 | /** | |
| 212 | * Returns the raw template string provided at creation. | |
| 213 | */ | |
| 214 | public String getOriginalText() { | |
| 215 |
1
1. getOriginalText : replaced return value with "" for com/renomad/minum/templating/TemplateProcessor::getOriginalText → KILLED |
return this.originalText; |
| 216 | } | |
| 217 | ||
| 218 | static StringBuilder processSectionInside(StringBuilder builder, List<TemplateSection> tSections) { | |
| 219 |
1
1. processSectionInside : negated conditional → KILLED |
if (!builder.isEmpty()) { |
| 220 | tSections.add(new TemplateSection(null, builder.toString(), 0, TemplateType.STATIC_TEXT)); | |
| 221 | builder = new StringBuilder(); | |
| 222 | } | |
| 223 |
1
1. processSectionInside : replaced return value with null for com/renomad/minum/templating/TemplateProcessor::processSectionInside → KILLED |
return builder; |
| 224 | } | |
| 225 | ||
| 226 | static StringBuilder processSectionOutside(StringBuilder builder, List<TemplateSection> tSections, int indent) { | |
| 227 |
1
1. processSectionOutside : negated conditional → KILLED |
if (!builder.isEmpty()) { |
| 228 | String trimmedKey = builder.toString().trim(); | |
| 229 | tSections.add(new TemplateSection(trimmedKey, null, indent, TemplateType.DYNAMIC_TEXT)); | |
| 230 | builder = new StringBuilder(); | |
| 231 | } | |
| 232 |
1
1. processSectionOutside : replaced return value with null for com/renomad/minum/templating/TemplateProcessor::processSectionOutside → KILLED |
return builder; |
| 233 | } | |
| 234 | ||
| 235 | /** | |
| 236 | * Just left a template key value. | |
| 237 | * <pre> | |
| 238 | * hello {{ world }} | |
| 239 | * ^ | |
| 240 | * +------Template key | |
| 241 | * | |
| 242 | * </pre> | |
| 243 | */ | |
| 244 | static boolean justArrivedOutside(String template, char charAtCursor, int i, boolean isInsideTemplateKeyLiteral) { | |
| 245 |
8
1. justArrivedOutside : negated conditional → KILLED 2. justArrivedOutside : Replaced integer addition with subtraction → KILLED 3. justArrivedOutside : changed conditional boundary → KILLED 4. justArrivedOutside : negated conditional → KILLED 5. justArrivedOutside : Replaced integer addition with subtraction → KILLED 6. justArrivedOutside : negated conditional → KILLED 7. justArrivedOutside : replaced boolean return with true for com/renomad/minum/templating/TemplateProcessor::justArrivedOutside → KILLED 8. justArrivedOutside : negated conditional → KILLED |
return charAtCursor == '}' && (i + 1) < template.length() && template.charAt(i + 1) == '}' && isInsideTemplateKeyLiteral; |
| 246 | } | |
| 247 | ||
| 248 | /** | |
| 249 | * Just arrived inside a template key value. | |
| 250 | * <pre> | |
| 251 | * hello {{ world }} | |
| 252 | * ^ | |
| 253 | * +------Template key | |
| 254 | * | |
| 255 | * </pre> | |
| 256 | */ | |
| 257 | static boolean justArrivedInside(String template, char charAtCursor, int i) { | |
| 258 |
7
1. justArrivedInside : negated conditional → KILLED 2. justArrivedInside : Replaced integer addition with subtraction → KILLED 3. justArrivedInside : Replaced integer addition with subtraction → KILLED 4. justArrivedInside : negated conditional → KILLED 5. justArrivedInside : replaced boolean return with true for com/renomad/minum/templating/TemplateProcessor::justArrivedInside → KILLED 6. justArrivedInside : negated conditional → KILLED 7. justArrivedInside : changed conditional boundary → KILLED |
return charAtCursor == '{' && (i + 1) < template.length() && template.charAt(i + 1) == '{'; |
| 259 | } | |
| 260 | ||
| 261 | /** | |
| 262 | * This examines the currently registered data lists and template keys | |
| 263 | * and confirms they are aligned. It will throw an exception if they | |
| 264 | * are not perfectly correlated. | |
| 265 | * <br> | |
| 266 | * | |
| 267 | */ | |
| 268 | private void correctnessCheck(List<Map<String, String>> dataList) { | |
| 269 | HashSet<String> copyOfKeysInTemplate = new HashSet<>(keysFoundInTemplate); | |
| 270 | ||
| 271 | // check for inconsistencies between maps in the data list | |
| 272 | Set<String> keysInFirstMap = dataList.getFirst().keySet(); | |
| 273 | ||
| 274 | for (Map<String, String> data : dataList) { | |
| 275 |
1
1. correctnessCheck : negated conditional → KILLED |
if (!data.keySet().equals(keysInFirstMap)) { |
| 276 | Set<String> result = differenceBetweenSets(data.keySet(), keysInFirstMap); | |
| 277 | throw new TemplateRenderException("In registered data, the maps were inconsistent on these keys: " + result); | |
| 278 | } | |
| 279 | } | |
| 280 | ||
| 281 | // ensure consistency between the registered data and the template keys | |
| 282 | HashSet<String> copyOfTemplateKeys = new HashSet<>(copyOfKeysInTemplate); | |
| 283 | copyOfTemplateKeys.removeAll(keysInFirstMap); | |
| 284 |
1
1. correctnessCheck : negated conditional → KILLED |
if (!copyOfTemplateKeys.isEmpty()) { |
| 285 | throw new TemplateRenderException("These keys in the template were not provided data: " + copyOfTemplateKeys); | |
| 286 | } | |
| 287 | ||
| 288 | HashSet<String> copyOfDataKeys = new HashSet<>(keysInFirstMap); | |
| 289 | copyOfDataKeys.removeAll(copyOfKeysInTemplate); | |
| 290 |
1
1. correctnessCheck : negated conditional → KILLED |
if (!copyOfDataKeys.isEmpty()) { |
| 291 | throw new TemplateRenderException("These keys in the data did not match anything in the template: " + copyOfDataKeys); | |
| 292 | } | |
| 293 | } | |
| 294 | ||
| 295 | private static Set<String> differenceBetweenSets(Set<String> set1, Set<String> set2) { | |
| 296 | Set<String> union = new HashSet<>(set2); | |
| 297 | union.addAll(set1); | |
| 298 | Set<String> intersection = new HashSet<>(set2); | |
| 299 | intersection.retainAll(set1); | |
| 300 | ||
| 301 | Set<String> result = new HashSet<>(union); | |
| 302 | result.removeAll(intersection); | |
| 303 |
1
1. differenceBetweenSets : replaced return value with Collections.emptySet for com/renomad/minum/templating/TemplateProcessor::differenceBetweenSets → KILLED |
return result; |
| 304 | } | |
| 305 | ||
| 306 | } | |
| 307 | ||
Mutations | ||
| 82 |
1.1 |
|
| 84 |
1.1 2.2 |
|
| 85 |
1.1 |
|
| 93 |
1.1 |
|
| 94 |
1.1 |
|
| 103 |
1.1 |
|
| 115 |
1.1 |
|
| 118 |
1.1 |
|
| 122 |
1.1 |
|
| 125 |
1.1 |
|
| 128 |
1.1 2.2 |
|
| 130 |
1.1 |
|
| 132 |
1.1 2.2 |
|
| 136 |
1.1 |
|
| 164 |
1.1 2.2 |
|
| 167 |
1.1 |
|
| 170 |
1.1 |
|
| 172 |
1.1 |
|
| 174 |
1.1 |
|
| 186 |
1.1 2.2 |
|
| 187 |
1.1 |
|
| 191 |
1.1 2.2 |
|
| 199 |
1.1 |
|
| 200 |
1.1 |
|
| 203 |
1.1 |
|
| 208 |
1.1 |
|
| 215 |
1.1 |
|
| 219 |
1.1 |
|
| 223 |
1.1 |
|
| 227 |
1.1 |
|
| 232 |
1.1 |
|
| 245 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 |
|
| 258 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 |
|
| 275 |
1.1 |
|
| 284 |
1.1 |
|
| 290 |
1.1 |
|
| 303 |
1.1 |