1 | |
package org.apache.onami.configuration.variables; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import static java.lang.String.format; |
23 | |
|
24 | |
import java.util.ArrayList; |
25 | |
import java.util.List; |
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 | 21638 | public class AntStyleParser |
51 | |
implements Parser |
52 | |
{ |
53 | |
|
54 | |
|
55 | |
static final String VAR_START = "${"; |
56 | |
|
57 | 2 | static final int VAR_START_LEN = VAR_START.length(); |
58 | |
|
59 | |
static final char VAR_CLOSE = '}'; |
60 | |
|
61 | |
static final int VAR_CLOSE_LEN = 1; |
62 | |
|
63 | |
static final char PIPE_SEPARATOR = '|'; |
64 | |
|
65 | |
static final int PIPE_SEPARATOR_LEN = 1; |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
public Appender parse( String pattern ) |
71 | |
{ |
72 | 22596 | List<Appender> appenders = new ArrayList<Appender>(); |
73 | 22596 | int prev = 0; |
74 | 22596 | int pos = 0; |
75 | 25078 | while ( ( pos = pattern.indexOf( VAR_START, pos ) ) >= 0 ) |
76 | |
{ |
77 | |
|
78 | 2494 | if ( pos > prev ) |
79 | |
{ |
80 | 434 | appenders.add( new TextAppender( pattern.substring( prev, pos ) ) ); |
81 | |
} |
82 | |
|
83 | |
|
84 | 2494 | pos += VAR_START_LEN; |
85 | |
|
86 | |
|
87 | |
|
88 | 2494 | int endVariable = pattern.indexOf( VAR_CLOSE, pos ); |
89 | 2494 | if ( endVariable < 0 ) |
90 | |
{ |
91 | 6 | throw new IllegalArgumentException( format( "Syntax error in property value '%s', missing close bracket '%s' for variable beginning at col %s: '%s'", |
92 | |
pattern, VAR_CLOSE, pos - VAR_START_LEN, pattern.substring( pos - VAR_START_LEN ) ) ); |
93 | |
} |
94 | |
|
95 | |
|
96 | 2488 | int nextVariable = pattern.indexOf( VAR_START, pos ); |
97 | |
|
98 | 2488 | int lastEndVariable = endVariable; |
99 | 2488 | boolean hasNested = false; |
100 | 4048 | while ( nextVariable >= 0 && nextVariable < endVariable ) |
101 | |
{ |
102 | 1566 | hasNested = true; |
103 | 1566 | endVariable = pattern.indexOf( VAR_CLOSE, endVariable + VAR_CLOSE_LEN ); |
104 | |
|
105 | 1566 | if ( endVariable < 0 ) |
106 | |
{ |
107 | 6 | throw new IllegalArgumentException( format( "Syntax error in property value '%s', missing close bracket '%s' for variable beginning at col %s: '%s'", |
108 | |
pattern, VAR_CLOSE, nextVariable, pattern.substring( nextVariable, lastEndVariable ) ) ); |
109 | |
} |
110 | 1560 | nextVariable = pattern.indexOf( VAR_START, nextVariable + VAR_START_LEN ); |
111 | 1560 | lastEndVariable = endVariable; |
112 | |
} |
113 | |
|
114 | 2482 | final String rawKey = pattern.substring( pos - VAR_START_LEN, endVariable + VAR_CLOSE_LEN ); |
115 | |
|
116 | 2482 | final String key = pattern.substring( pos, endVariable ); |
117 | |
|
118 | 2482 | int pipeIndex = key.indexOf( PIPE_SEPARATOR ); |
119 | |
|
120 | 2482 | boolean hasKeyVariables = false; |
121 | 2482 | boolean hasDefault = false; |
122 | 2482 | boolean hasDefaultVariables = false; |
123 | |
|
124 | |
|
125 | 2482 | if ( pipeIndex >= 0 ) |
126 | |
{ |
127 | |
|
128 | 1260 | if ( !hasNested ) |
129 | |
{ |
130 | 460 | hasDefault = true; |
131 | 460 | hasDefaultVariables = false; |
132 | |
} |
133 | |
|
134 | |
|
135 | |
|
136 | |
else |
137 | |
{ |
138 | 800 | int nextStartKeyVariable = key.indexOf( VAR_START ); |
139 | 800 | hasKeyVariables = pipeIndex > nextStartKeyVariable; |
140 | 800 | if ( hasKeyVariables ) |
141 | |
{ |
142 | |
|
143 | 250 | int nextEndKeyVariable = key.indexOf( VAR_CLOSE, nextStartKeyVariable + VAR_START_LEN ); |
144 | 250 | pipeIndex = key.indexOf( PIPE_SEPARATOR, pipeIndex + PIPE_SEPARATOR_LEN ); |
145 | 400 | while ( pipeIndex >= 0 && pipeIndex > nextStartKeyVariable ) |
146 | |
{ |
147 | 200 | pipeIndex = key.indexOf( PIPE_SEPARATOR, nextEndKeyVariable + VAR_CLOSE_LEN ); |
148 | 200 | nextStartKeyVariable = key.indexOf( VAR_START, nextStartKeyVariable + VAR_START_LEN ); |
149 | |
|
150 | 200 | if ( nextStartKeyVariable < 0 ) |
151 | |
{ |
152 | 50 | break; |
153 | |
} |
154 | 150 | nextEndKeyVariable = key.indexOf( VAR_CLOSE, nextEndKeyVariable + VAR_CLOSE_LEN ); |
155 | 150 | if ( nextEndKeyVariable < 0 ) |
156 | |
{ |
157 | 0 | throw new IllegalArgumentException( format( "Syntax error in property value '%s', missing close bracket '%s' for variable beginning at col %s: '%s'", |
158 | |
pattern, VAR_CLOSE, nextStartKeyVariable, key.substring( nextStartKeyVariable ) ) ); |
159 | |
} |
160 | |
} |
161 | |
} |
162 | |
|
163 | |
|
164 | |
|
165 | 800 | if ( pipeIndex >= 0 ) |
166 | |
{ |
167 | 650 | hasDefault = true; |
168 | 650 | hasDefaultVariables = key.indexOf( VAR_START, pipeIndex ) >= 0; |
169 | |
} |
170 | |
|
171 | 800 | } |
172 | |
} |
173 | |
|
174 | |
|
175 | |
else |
176 | |
{ |
177 | 1222 | hasKeyVariables = hasNested; |
178 | |
} |
179 | |
|
180 | |
|
181 | 2482 | String keyPart = null; |
182 | 2482 | String defaultPart = null; |
183 | 2482 | if ( hasDefault ) |
184 | |
{ |
185 | 1110 | keyPart = key.substring( 0, pipeIndex ).trim(); |
186 | 1110 | defaultPart = key.substring( pipeIndex + PIPE_SEPARATOR_LEN ).trim(); |
187 | |
} |
188 | |
else |
189 | |
{ |
190 | 1372 | keyPart = key.trim(); |
191 | |
} |
192 | |
|
193 | 2482 | appenders.add( new KeyAppender( this, |
194 | |
rawKey, |
195 | |
hasKeyVariables ? parse( keyPart ) : new TextAppender( keyPart ), |
196 | |
!hasDefault ? null : ( hasDefaultVariables ? parse( defaultPart ) : new TextAppender( defaultPart ) ) ) ); |
197 | |
|
198 | 2482 | prev = endVariable + VAR_CLOSE_LEN; |
199 | 2482 | pos = prev; |
200 | 2482 | } |
201 | |
|
202 | 22584 | if ( prev < pattern.length() ) |
203 | |
{ |
204 | 20586 | appenders.add( new TextAppender( pattern.substring( prev ) ) ); |
205 | |
} |
206 | |
|
207 | 22584 | return appenders.size() == 1 ? appenders.get( 0 ) : new MixinAppender( pattern, appenders ); |
208 | |
} |
209 | |
|
210 | |
} |