1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.reactive.beans.factory.config;
18
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
21
22 /***
23 * The default implementation of {@link PlaceholderMatcher} that uses the "standard" format of "${valueToBeEveluated}".
24 * <p>The default format can be overwritten by using the {@link #setPlaceholderRegex(String)} and {@link #setPlaceholderSuffix(String)} methods.</p>
25 *
26 * @author Dan Washusen
27 * @version $Id: DefaultPlaceholderMatcher.java,v 1.1 2004/12/26 23:42:29 dan_washusen Exp $
28 * @since 16.12.2004
29 */
30 public class DefaultPlaceholderMatcher
31 implements PlaceholderMatcher {
32 /*** The default regex to determine if the value contains a placeholder. */
33 public static final String DEFAULT_PLACEHOLDER_REGEX = "//$//{.*?//}";
34 public static final String DEFAULT_PLACEHOLDER_PREFIX = "${";
35 public static final String DEFAULT_PLACEHOLDER_SUFFIX = "}";
36
37 /*** The prefix for a placeholder value. */
38 protected String placeholderRegex = DEFAULT_PLACEHOLDER_REGEX;
39 private String placeholderPrefix = DEFAULT_PLACEHOLDER_PREFIX;
40 private String placeholderSuffix = DEFAULT_PLACEHOLDER_SUFFIX;
41
42 protected String value;
43 protected Pattern pattern;
44 protected Matcher matcher;
45 protected boolean placeholderPresent = false;
46 /*** If multple placeholders are present in the value then it must evaluate to a string */
47 protected boolean multiplePlaceholdersPresent = false;
48 protected Placeholder placeholder = null;
49 protected int proceedingTextBeginIndex = 0;
50 protected String proceedingText = null;
51 protected String trailingText = null;
52
53 /***
54 * Returns the prefix for a placeholder value.
55 * @return the prefix for a placeholder value
56 */
57 public String getPlaceholderRegex() {
58 return placeholderRegex;
59 }
60
61 /***
62 * Sets the prefix for a placeholder value.
63 * @param placeholderRegex the prefix for a placeholder value
64 */
65 public void setPlaceholderRegex(String placeholderRegex) {
66 this.placeholderRegex = placeholderRegex;
67 }
68
69 public String getPlaceholderPrefix() {
70 return placeholderPrefix;
71 }
72
73 public void setPlaceholderPrefix(String placeholderPrefix) {
74 this.placeholderPrefix = placeholderPrefix;
75 }
76
77 public String getPlaceholderSuffix() {
78 return placeholderSuffix;
79 }
80
81 public void setPlaceholderSuffix(String placeholderSuffix) {
82 this.placeholderSuffix = placeholderSuffix;
83 }
84
85 public String getValue() {
86 return value;
87 }
88
89 public void init(String value) {
90
91 this.placeholder = null;
92 this.proceedingText = null;
93 this.trailingText = null;
94 this.proceedingTextBeginIndex = 0;
95
96 this.value = value;
97 this.pattern = Pattern.compile(getPlaceholderRegex());
98 this.matcher = this.pattern.matcher(getValue());
99
100
101 this.placeholderPresent = this.matcher.find();
102
103
104 this.multiplePlaceholdersPresent = this.matcher.find();
105
106
107 this.matcher.reset();
108 }
109
110 public boolean isPlaceholderPresent() {
111 return this.placeholderPresent;
112 }
113
114 public boolean isMultiplePlaceholdersPresent() {
115 return this.multiplePlaceholdersPresent;
116 }
117
118 public Placeholder next() {
119 return this.placeholder;
120 }
121
122 public boolean hasNext() {
123 boolean hasNext = this.matcher.find();
124 if (hasNext) {
125 this.placeholder = new DefaultPlaceholder(stripPlaceholderWrappers(this.matcher.group()));
126
127
128 int proceedingTextEndIndex = this.matcher.start();
129 if (proceedingTextBeginIndex != proceedingTextEndIndex)
130 this.proceedingText = value.substring(proceedingTextBeginIndex, proceedingTextEndIndex);
131
132
133 int trailingTextBeginIndex = this.matcher.end();
134 int trailingTextEndIndex = this.value.length();
135 if (trailingTextBeginIndex != trailingTextEndIndex)
136 this.trailingText = value.substring(this.matcher.end(), this.value.length());
137
138
139 proceedingTextBeginIndex = this.matcher.end();
140 }
141 else {
142 this.placeholder = null;
143 }
144
145 return hasNext;
146 }
147
148 /***
149 * This method returns the placeholder with the prefix and suffix removed.
150 * <p>e.g. a value of "${placeholder}" would return the value "placeholder".</p>
151 * @param placeholderText The placeholder text
152 * @return The placeholder text without the prefix and suffix
153 */
154 protected String stripPlaceholderWrappers(String placeholderText) {
155 return placeholderText.substring(getPlaceholderPrefix().length(), (placeholderText.length() - getPlaceholderSuffix().length()));
156 }
157
158 public String getProceedingText() {
159 return this.proceedingText;
160 }
161
162 public String getTrailingText() {
163 return this.trailingText;
164 }
165
166 }