| 1 | |
package org.kuali.spring.util; |
| 2 | |
|
| 3 | |
import java.util.ArrayList; |
| 4 | |
import java.util.Collection; |
| 5 | |
import java.util.HashMap; |
| 6 | |
import java.util.HashSet; |
| 7 | |
import java.util.List; |
| 8 | |
import java.util.Map; |
| 9 | |
import java.util.Set; |
| 10 | |
|
| 11 | |
import org.slf4j.Logger; |
| 12 | |
import org.slf4j.LoggerFactory; |
| 13 | |
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; |
| 14 | |
import org.springframework.util.Assert; |
| 15 | |
import org.springframework.util.StringUtils; |
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
public class PlaceholderStringResolver { |
| 72 | |
|
| 73 | 1 | private static final Logger logger = LoggerFactory.getLogger(PlaceholderStringResolver.class); |
| 74 | |
|
| 75 | 1 | private static final Map<String, String> wellKnownSimplePrefixes = new HashMap<String, String>(4); |
| 76 | |
public static final boolean DEFAULT_IS_IGNORE_UNRESOLVABLE_PLACEHOLDERS = false; |
| 77 | |
|
| 78 | |
static { |
| 79 | 1 | wellKnownSimplePrefixes.put("}", "{"); |
| 80 | 1 | wellKnownSimplePrefixes.put("]", "["); |
| 81 | 1 | wellKnownSimplePrefixes.put(")", "("); |
| 82 | 1 | } |
| 83 | |
|
| 84 | 15 | PropertyLogger plogger = new PropertyLogger(); |
| 85 | |
String placeholderPrefix; |
| 86 | |
String placeholderSuffix; |
| 87 | |
String valueSeparator; |
| 88 | |
boolean ignoreUnresolvablePlaceholders; |
| 89 | |
String simplePrefix; |
| 90 | |
|
| 91 | |
public PlaceholderStringResolver() { |
| 92 | 13 | this(DEFAULT_IS_IGNORE_UNRESOLVABLE_PLACEHOLDERS); |
| 93 | 13 | } |
| 94 | |
|
| 95 | |
public PlaceholderStringResolver(boolean ignoreUnresolvablePlaceholders) { |
| 96 | 13 | this(PropertyPlaceholderConfigurer.DEFAULT_PLACEHOLDER_PREFIX, |
| 97 | |
PropertyPlaceholderConfigurer.DEFAULT_PLACEHOLDER_SUFFIX, null, ignoreUnresolvablePlaceholders); |
| 98 | 13 | } |
| 99 | |
|
| 100 | |
public PlaceholderStringResolver(String placeholderPrefix, String placeholderSuffix) { |
| 101 | 1 | this(placeholderPrefix, placeholderSuffix, null, DEFAULT_IS_IGNORE_UNRESOLVABLE_PLACEHOLDERS); |
| 102 | 1 | } |
| 103 | |
|
| 104 | |
public PlaceholderStringResolver(String placeholderPrefix, String placeholderSuffix, String valueSeparator, |
| 105 | 15 | boolean ignoreUnresolvablePlaceholders) { |
| 106 | |
|
| 107 | 15 | Assert.notNull(placeholderPrefix, "placeholderPrefix must not be null"); |
| 108 | 15 | Assert.notNull(placeholderSuffix, "placeholderSuffix must not be null"); |
| 109 | |
|
| 110 | 15 | this.placeholderPrefix = placeholderPrefix; |
| 111 | 15 | this.placeholderSuffix = placeholderSuffix; |
| 112 | |
|
| 113 | 15 | String simplePrefixForSuffix = wellKnownSimplePrefixes.get(this.placeholderSuffix); |
| 114 | 15 | if (simplePrefixForSuffix != null && this.placeholderPrefix.endsWith(simplePrefixForSuffix)) { |
| 115 | 13 | this.simplePrefix = simplePrefixForSuffix; |
| 116 | |
} else { |
| 117 | 2 | this.simplePrefix = this.placeholderPrefix; |
| 118 | |
} |
| 119 | 15 | this.valueSeparator = valueSeparator; |
| 120 | 15 | this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders; |
| 121 | 15 | } |
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
protected boolean isEmpty(Collection<?> c) { |
| 127 | 37 | return c == null || c.size() == 0; |
| 128 | |
} |
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
protected String getTrimmedText(String text) { |
| 134 | 52 | int prefixLength = this.placeholderPrefix.length(); |
| 135 | 52 | int suffixLength = this.placeholderSuffix.length(); |
| 136 | 52 | int beginIndex = prefixLength; |
| 137 | 52 | int endIndex = text.length() - suffixLength; |
| 138 | 52 | return text.substring(beginIndex, endIndex); |
| 139 | |
} |
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
protected String getDefaultValue(String key, ValueRetriever retriever) { |
| 145 | |
|
| 146 | 5 | if (this.valueSeparator == null) { |
| 147 | 2 | return null; |
| 148 | |
} |
| 149 | |
|
| 150 | |
|
| 151 | 3 | int separatorIndex = key.indexOf(this.valueSeparator); |
| 152 | 3 | if (separatorIndex == -1) { |
| 153 | 1 | return null; |
| 154 | |
} |
| 155 | |
|
| 156 | |
|
| 157 | 2 | String actualKey = key.substring(0, separatorIndex); |
| 158 | |
|
| 159 | |
|
| 160 | 2 | String defaultValue = key.substring(separatorIndex + this.valueSeparator.length()); |
| 161 | |
|
| 162 | |
|
| 163 | 2 | String value = retriever.retrieveValue(actualKey); |
| 164 | |
|
| 165 | |
|
| 166 | 2 | if (value != null) { |
| 167 | 1 | return value; |
| 168 | |
} else { |
| 169 | |
|
| 170 | 1 | return defaultValue; |
| 171 | |
} |
| 172 | |
} |
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
protected String getRawValue(String key, ValueRetriever retriever) { |
| 178 | |
|
| 179 | 37 | String value = retriever.retrieveValue(key); |
| 180 | |
|
| 181 | |
|
| 182 | 37 | if (value != null) { |
| 183 | 32 | return value; |
| 184 | |
} |
| 185 | |
|
| 186 | |
|
| 187 | 5 | return getDefaultValue(key, retriever); |
| 188 | |
} |
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
protected void handleUnresolvedPlaceholder(String placeholderText) { |
| 194 | |
|
| 195 | 3 | if (!this.ignoreUnresolvablePlaceholders) { |
| 196 | |
|
| 197 | 1 | throw new IllegalArgumentException("Could not resolve a value for " + placeholderText); |
| 198 | |
} else { |
| 199 | 2 | logger.trace("Ignoring unresolvable placeholder: '" + placeholderText + "'"); |
| 200 | |
} |
| 201 | 2 | } |
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
protected String getValue(String key, Placeholder placeholder, ValueRetriever retriever, Set<String> resolving) { |
| 207 | |
|
| 208 | 37 | String value = getRawValue(key, retriever); |
| 209 | |
|
| 210 | 37 | if (value == null) { |
| 211 | |
|
| 212 | 3 | String placeholderText = getPlaceholderPrefix() + placeholder.getPlaceholderString().getText() |
| 213 | |
+ getPlaceholderSuffix(); |
| 214 | 3 | handleUnresolvedPlaceholder(placeholderText); |
| 215 | 2 | return placeholderText; |
| 216 | |
} else { |
| 217 | |
|
| 218 | 34 | PlaceholderString phs = new PlaceholderString(value); |
| 219 | 34 | resolvePlaceholderString(phs, retriever, resolving); |
| 220 | 32 | return phs.getResolvedText(); |
| 221 | |
} |
| 222 | |
} |
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
public String resolve(String text, ValueRetriever retriever) { |
| 228 | 295 | PlaceholderString phs = new PlaceholderString(text); |
| 229 | 295 | resolve(phs, retriever); |
| 230 | 293 | return phs.getResolvedText(); |
| 231 | |
} |
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
public void resolve(PlaceholderString placeholderString, ValueRetriever retriever) { |
| 238 | 295 | resolvePlaceholderString(placeholderString, retriever, new HashSet<String>()); |
| 239 | 293 | } |
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
protected void resolvePlaceholderString(PlaceholderString phs, ValueRetriever retriever, Set<String> resolving) { |
| 245 | |
|
| 246 | |
|
| 247 | 341 | parse(phs); |
| 248 | |
|
| 249 | |
|
| 250 | 341 | StringBuilder buffer = new StringBuilder(phs.getText()); |
| 251 | 341 | int offset = 0; |
| 252 | 341 | for (Placeholder pholder : phs.getPlaceholders()) { |
| 253 | |
|
| 254 | 38 | resolvePlaceholder(pholder, retriever, resolving); |
| 255 | |
|
| 256 | 34 | offset += updateBuffer(buffer, pholder, offset); |
| 257 | |
} |
| 258 | |
|
| 259 | 337 | phs.setResolvedText(buffer.toString()); |
| 260 | 337 | } |
| 261 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
protected int updateBuffer(StringBuilder buffer, Placeholder pholder, int offset) { |
| 266 | 34 | int prefixLength = this.placeholderPrefix.length(); |
| 267 | 34 | int suffixLength = this.placeholderSuffix.length(); |
| 268 | 34 | int startIndex = pholder.getStartIndex() + offset; |
| 269 | 34 | int endIndex = pholder.getEndIndex() + offset; |
| 270 | 34 | buffer.replace(startIndex, endIndex, pholder.getValue()); |
| 271 | 34 | int textLength = pholder.getPlaceholderString().getText().length(); |
| 272 | 34 | int originalLength = prefixLength + textLength + suffixLength; |
| 273 | 34 | int newLength = pholder.getValue().length(); |
| 274 | 34 | int diff = newLength - originalLength; |
| 275 | 34 | return diff; |
| 276 | |
} |
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
protected void circularReferenceCheck(String placeholderText, Set<String> resolving) { |
| 288 | 38 | logger.trace("Adding '{}' to the list of placeholders being resolved", placeholderText); |
| 289 | 38 | boolean added = resolving.add(placeholderText); |
| 290 | |
|
| 291 | 38 | if (!added) { |
| 292 | |
|
| 293 | |
|
| 294 | 1 | throw new IllegalArgumentException("Circular reference detected on " + placeholderText); |
| 295 | |
} |
| 296 | 37 | } |
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
|
| 302 | |
protected void resolvePlaceholder(Placeholder placeholder, ValueRetriever retriever, Set<String> resolving) { |
| 303 | |
|
| 304 | 38 | PlaceholderString phs = placeholder.getPlaceholderString(); |
| 305 | |
|
| 306 | |
|
| 307 | 38 | circularReferenceCheck(phs.getText(), resolving); |
| 308 | |
|
| 309 | |
|
| 310 | 37 | if (isEmpty(phs.getPlaceholders())) { |
| 311 | 25 | phs.setResolvedText(phs.getText()); |
| 312 | |
} else { |
| 313 | |
|
| 314 | 12 | resolvePlaceholderString(phs, retriever, resolving); |
| 315 | |
} |
| 316 | |
|
| 317 | |
|
| 318 | 37 | String key = phs.getResolvedText(); |
| 319 | |
|
| 320 | |
|
| 321 | 37 | String value = getValue(key, placeholder, retriever, resolving); |
| 322 | |
|
| 323 | |
|
| 324 | 34 | placeholder.setValue(value); |
| 325 | |
|
| 326 | |
|
| 327 | 34 | logger.trace("Removing '{}' from the list of placeholders being resolved", phs.getText()); |
| 328 | 34 | resolving.remove(phs.getText()); |
| 329 | 34 | } |
| 330 | |
|
| 331 | |
|
| 332 | |
|
| 333 | |
|
| 334 | |
|
| 335 | |
protected void parse(PlaceholderString phs) { |
| 336 | |
|
| 337 | |
|
| 338 | 393 | String text = phs.getText(); |
| 339 | |
|
| 340 | |
|
| 341 | 393 | int startIndex = text.indexOf(this.placeholderPrefix); |
| 342 | 393 | if (startIndex == -1) { |
| 343 | 347 | logger.trace("Skip parsing. No placeholders found in [{}]", text); |
| 344 | |
} |
| 345 | |
|
| 346 | |
|
| 347 | 393 | List<Placeholder> placeholders = new ArrayList<Placeholder>(); |
| 348 | |
|
| 349 | |
|
| 350 | 445 | while (startIndex != -1) { |
| 351 | |
|
| 352 | 53 | Placeholder placeholder = getPlaceholder(text, startIndex); |
| 353 | |
|
| 354 | |
|
| 355 | 53 | if (placeholder == null) { |
| 356 | 1 | break; |
| 357 | |
} |
| 358 | |
|
| 359 | |
|
| 360 | 52 | placeholders.add(placeholder); |
| 361 | |
|
| 362 | |
|
| 363 | 52 | int newFromIndex = placeholder.getEndIndex(); |
| 364 | |
|
| 365 | |
|
| 366 | 52 | startIndex = text.indexOf(this.placeholderPrefix, newFromIndex); |
| 367 | 52 | } |
| 368 | |
|
| 369 | |
|
| 370 | 393 | phs.setPlaceholders(placeholders); |
| 371 | 393 | } |
| 372 | |
|
| 373 | |
|
| 374 | |
|
| 375 | |
|
| 376 | |
protected Placeholder getPlaceholder(String source, int startIndex) { |
| 377 | 53 | int suffixLength = this.placeholderSuffix.length(); |
| 378 | |
|
| 379 | |
|
| 380 | 53 | int endIndex = findPlaceholderEndIndex(source, startIndex); |
| 381 | |
|
| 382 | 53 | if (endIndex == -1) { |
| 383 | |
|
| 384 | 1 | return null; |
| 385 | |
} else { |
| 386 | |
|
| 387 | 52 | endIndex = endIndex + suffixLength; |
| 388 | |
} |
| 389 | |
|
| 390 | |
|
| 391 | 52 | String text = source.substring(startIndex, endIndex); |
| 392 | |
|
| 393 | |
|
| 394 | 52 | String trimmedText = getTrimmedText(text); |
| 395 | |
|
| 396 | |
|
| 397 | 52 | PlaceholderString phs = new PlaceholderString(trimmedText); |
| 398 | |
|
| 399 | |
|
| 400 | 52 | parse(phs); |
| 401 | |
|
| 402 | |
|
| 403 | 52 | Placeholder placeholder = new Placeholder(); |
| 404 | 52 | placeholder.setStartIndex(startIndex); |
| 405 | 52 | placeholder.setEndIndex(endIndex); |
| 406 | 52 | placeholder.setPlaceholderString(phs); |
| 407 | 52 | return placeholder; |
| 408 | |
} |
| 409 | |
|
| 410 | |
|
| 411 | |
|
| 412 | |
|
| 413 | |
protected boolean haveArrivedAtSuffix(CharSequence buf, int index) { |
| 414 | 284 | return StringUtils.substringMatch(buf, index, this.placeholderSuffix); |
| 415 | |
} |
| 416 | |
|
| 417 | |
|
| 418 | |
|
| 419 | |
|
| 420 | |
protected boolean haveArrivedAtPrefix(CharSequence buf, int index) { |
| 421 | 218 | return StringUtils.substringMatch(buf, index, this.simplePrefix); |
| 422 | |
} |
| 423 | |
|
| 424 | |
|
| 425 | |
|
| 426 | |
|
| 427 | |
|
| 428 | |
protected int findPlaceholderEndIndex(CharSequence buf, int startIndex) { |
| 429 | |
|
| 430 | 53 | int index = startIndex + this.placeholderPrefix.length(); |
| 431 | |
|
| 432 | |
|
| 433 | 53 | int nestedPlaceHolderCount = 0; |
| 434 | |
|
| 435 | |
|
| 436 | 285 | while (index < buf.length()) { |
| 437 | |
|
| 438 | 284 | if (haveArrivedAtSuffix(buf, index)) { |
| 439 | |
|
| 440 | |
|
| 441 | 66 | if (nestedPlaceHolderCount == 0) { |
| 442 | 52 | return index; |
| 443 | |
} |
| 444 | |
|
| 445 | |
|
| 446 | 14 | nestedPlaceHolderCount--; |
| 447 | |
|
| 448 | |
|
| 449 | 14 | index = index + this.placeholderSuffix.length(); |
| 450 | |
|
| 451 | 218 | } else if (haveArrivedAtPrefix(buf, index)) { |
| 452 | |
|
| 453 | |
|
| 454 | |
|
| 455 | 14 | nestedPlaceHolderCount++; |
| 456 | |
|
| 457 | |
|
| 458 | 14 | index = index + this.simplePrefix.length(); |
| 459 | |
} else { |
| 460 | |
|
| 461 | 204 | index++; |
| 462 | |
} |
| 463 | |
} |
| 464 | |
|
| 465 | |
|
| 466 | 1 | return -1; |
| 467 | |
} |
| 468 | |
|
| 469 | |
public String getPlaceholderPrefix() { |
| 470 | 9 | return placeholderPrefix; |
| 471 | |
} |
| 472 | |
|
| 473 | |
public void setPlaceholderPrefix(String placeholderPrefix) { |
| 474 | 2 | this.placeholderPrefix = placeholderPrefix; |
| 475 | 2 | } |
| 476 | |
|
| 477 | |
public String getPlaceholderSuffix() { |
| 478 | 8 | return placeholderSuffix; |
| 479 | |
} |
| 480 | |
|
| 481 | |
public void setPlaceholderSuffix(String placeholderSuffix) { |
| 482 | 2 | this.placeholderSuffix = placeholderSuffix; |
| 483 | 2 | } |
| 484 | |
|
| 485 | |
public String getValueSeparator() { |
| 486 | 3 | return valueSeparator; |
| 487 | |
} |
| 488 | |
|
| 489 | |
public void setValueSeparator(String valueSeparator) { |
| 490 | 2 | this.valueSeparator = valueSeparator; |
| 491 | 2 | } |
| 492 | |
|
| 493 | |
public boolean isIgnoreUnresolvablePlaceholders() { |
| 494 | 2 | return ignoreUnresolvablePlaceholders; |
| 495 | |
} |
| 496 | |
|
| 497 | |
public void setIgnoreUnresolvablePlaceholders(boolean ignoreUnresolvablePlaceholders) { |
| 498 | 3 | this.ignoreUnresolvablePlaceholders = ignoreUnresolvablePlaceholders; |
| 499 | 3 | } |
| 500 | |
|
| 501 | |
public PropertyLogger getPlogger() { |
| 502 | 2 | return plogger; |
| 503 | |
} |
| 504 | |
|
| 505 | |
public void setPlogger(PropertyLogger plogger) { |
| 506 | 3 | this.plogger = plogger; |
| 507 | 3 | } |
| 508 | |
|
| 509 | |
public String getSimplePrefix() { |
| 510 | 3 | return simplePrefix; |
| 511 | |
} |
| 512 | |
|
| 513 | |
public void setSimplePrefix(String simplePrefix) { |
| 514 | 2 | this.simplePrefix = simplePrefix; |
| 515 | 2 | } |
| 516 | |
|
| 517 | |
} |