1 package org.apache.torque.generator.control;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.torque.generator.processor.string.StringProcessor;
23
24
25
26
27
28
29
30 public class TokenReplacer implements StringProcessor
31 {
32
33 public static final char TOKEN_START_1 = '$';
34
35
36 public static final char TOKEN_START_2 = '{';
37
38
39 public static final char TOKEN_END = '}';
40
41
42 public static final char ESCAPE = '\\';
43
44
45 public static final String OPTION_PREFIX = "option";
46
47
48 public static final char PREFIX_SEPARATOR = ':';
49
50
51
52
53 private ControllerState controllerState;
54
55 public TokenReplacer(ControllerState controllerState)
56 {
57 if (controllerState == null)
58 {
59 throw new NullPointerException("controllerState must not be null");
60 }
61 this.controllerState = controllerState;
62 }
63
64
65
66
67
68
69
70 public String process(String toProcess)
71 {
72 if (toProcess == null)
73 {
74 return null;
75 }
76 StringBuilder result = new StringBuilder();
77 StringBuilder tokenName = new StringBuilder();
78 boolean escape = false;
79 boolean inTokenStart = false;
80 boolean inToken = false;
81 for (char currentChar : toProcess.toCharArray())
82 {
83 if (currentChar == ESCAPE && !escape)
84 {
85 escape = true;
86 if (inTokenStart)
87 {
88 result.append(TOKEN_START_1);
89 inTokenStart = false;
90 }
91 continue;
92 }
93 if (escape)
94 {
95 if (inToken)
96 {
97 tokenName.append(currentChar);
98 }
99 else
100 {
101 result.append(currentChar);
102 }
103 escape = false;
104 continue;
105 }
106 escape = false;
107 if (currentChar == TOKEN_START_1 && !inTokenStart && !inToken)
108 {
109 inTokenStart = true;
110 continue;
111 }
112 if (inTokenStart)
113 {
114 if (currentChar == TOKEN_START_2)
115 {
116 inTokenStart = false;
117 inToken = true;
118 continue;
119 }
120 else
121 {
122 result.append(TOKEN_START_1);
123 result.append(currentChar);
124 inTokenStart = false;
125 continue;
126 }
127 }
128 if (currentChar == TOKEN_END && inToken)
129 {
130 result.append(resolveToken(tokenName.toString()));
131 tokenName = new StringBuilder();
132 inToken = false;
133 continue;
134 }
135 if (inToken)
136 {
137 tokenName.append(currentChar);
138 }
139 else
140 {
141 result.append(currentChar);
142 }
143 }
144 if (escape)
145 {
146 throw new IllegalArgumentException("Single escape character "
147 + ESCAPE
148 + " encountered at end of String "
149 + toProcess);
150 }
151 if (inTokenStart)
152 {
153 result.append(TOKEN_START_1);
154 }
155 if (inToken)
156 {
157 throw new IllegalArgumentException("Token end "
158 + TOKEN_END
159 + " missing at end of String "
160 + toProcess);
161 }
162 return result.toString();
163 }
164
165 private String resolveToken(String tokenName)
166 {
167 if (!tokenName.startsWith(OPTION_PREFIX + PREFIX_SEPARATOR))
168 {
169 throw new IllegalArgumentException("Token name must start with"
170 + OPTION_PREFIX + PREFIX_SEPARATOR);
171 }
172 String optionName = tokenName.substring(
173 (OPTION_PREFIX + PREFIX_SEPARATOR).length());
174 Object optionValue = controllerState.getOption(optionName);
175 if (optionValue == null)
176 {
177 return "";
178 }
179 return optionValue.toString();
180 }
181 }