1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| |
16 |
| |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| package com.unitesk.atp.text.generation; |
33 |
| |
34 |
| |
35 |
| |
36 |
| import java.util.HashMap; |
37 |
| import java.util.Map; |
38 |
| |
39 |
| import com.unitesk.atp.dynattrs.Accessor; |
40 |
| import com.unitesk.atp.dynattrs.Attributed; |
41 |
| import com.unitesk.atp.dynattrs.MapAttributed; |
42 |
| import com.unitesk.atp.text.filters.PatternFilter; |
43 |
| import com.unitesk.atp.text.filters.TextReceiver; |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| public class BasicGenerator |
55 |
| implements Generator, PatternFilter.ParameterProcessor |
56 |
| { |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
0
| public BasicGenerator( TextReceiver client )
|
66 |
| { |
67 |
0
| this( client, new DefaultFunction(), "${", "}" );
|
68 |
| } |
69 |
| |
70 |
| |
71 |
| |
72 |
| |
73 |
| |
74 |
| |
75 |
| |
76 |
| |
77 |
| |
78 |
| |
79 |
| |
80 |
0
| public BasicGenerator( TextReceiver client
|
81 |
| , Function default_func |
82 |
| , String par_start |
83 |
| , String par_end |
84 |
| ) |
85 |
| { |
86 |
0
| if( client == null )
|
87 |
| { |
88 |
0
| throw new NullPointerException();
|
89 |
| } |
90 |
| |
91 |
0
| this.client = client;
|
92 |
| |
93 |
0
| pattern_filter = new PatternFilter( client, this, par_start, par_end );
|
94 |
| |
95 |
0
| setFunction( DEFAULT_NAME, default_func );
|
96 |
| } |
97 |
| |
98 |
| |
99 |
| |
100 |
| |
101 |
| |
102 |
| |
103 |
| |
104 |
0
| public Function setFunction( String name, Function func )
|
105 |
| { |
106 |
0
| mustBeID( name );
|
107 |
| |
108 |
0
| Function res;
|
109 |
| |
110 |
0
| if( func != null )
|
111 |
| { |
112 |
0
| func.setGenerator( this );
|
113 |
0
| res = (Function)name_to_func.put( name, func );
|
114 |
| } else { |
115 |
0
| res = (Function)name_to_func.remove( name );
|
116 |
| } |
117 |
0
| if( res != null && res != func )
|
118 |
| { |
119 |
| |
120 |
| |
121 |
| |
122 |
0
| res.setGenerator( null );
|
123 |
| } |
124 |
0
| return res;
|
125 |
| } |
126 |
| |
127 |
| |
128 |
| |
129 |
| |
130 |
0
| public Function getFunction( String name )
|
131 |
| { |
132 |
0
| mustBeID( name );
|
133 |
| |
134 |
0
| return (Function)name_to_func.get( name );
|
135 |
| } |
136 |
| |
137 |
| |
138 |
| |
139 |
| |
140 |
0
| public Object setVariable( String name, Object var )
|
141 |
| { |
142 |
0
| mustBeID( name );
|
143 |
| |
144 |
0
| if( var != null )
|
145 |
| { |
146 |
0
| return name_to_var.put( name, var );
|
147 |
| } else { |
148 |
0
| return name_to_var.remove( name );
|
149 |
| } |
150 |
| } |
151 |
| |
152 |
| |
153 |
| |
154 |
| |
155 |
0
| public Object getVariable( String name )
|
156 |
| { |
157 |
0
| mustBeID( name );
|
158 |
| |
159 |
0
| return name_to_var.get( name );
|
160 |
| } |
161 |
| |
162 |
| |
163 |
| |
164 |
| |
165 |
0
| public void txt( String str )
|
166 |
| { |
167 |
0
| pattern_filter.txt( str );
|
168 |
| } |
169 |
| |
170 |
| |
171 |
| |
172 |
| |
173 |
0
| public void txtAsIs( String str )
|
174 |
| { |
175 |
0
| client.txt( str );
|
176 |
| } |
177 |
| |
178 |
| |
179 |
| |
180 |
| |
181 |
| |
182 |
| |
183 |
| |
184 |
0
| public void param( String param )
|
185 |
| { |
186 |
0
| int colon = param.indexOf( ':' );
|
187 |
| |
188 |
0
| String func;
|
189 |
0
| int arg_pos;
|
190 |
| |
191 |
0
| if( colon == -1 )
|
192 |
| { |
193 |
0
| func = DEFAULT_NAME;
|
194 |
0
| arg_pos = 0;
|
195 |
| } else { |
196 |
0
| func = param.substring( 0, colon );
|
197 |
0
| arg_pos = colon + 1;
|
198 |
| } |
199 |
| |
200 |
0
| Function f;
|
201 |
0
| try
|
202 |
| { |
203 |
0
| f = getFunction( func );
|
204 |
| } |
205 |
| catch( IllegalArgumentException e ) |
206 |
| { |
207 |
0
| f = null;
|
208 |
| } |
209 |
| |
210 |
0
| if( f == null )
|
211 |
| { |
212 |
0
| throw new UndefinedFunctionException( param, func );
|
213 |
| } |
214 |
| |
215 |
0
| Object var_obj;
|
216 |
| |
217 |
0
| int param_len = param.length();
|
218 |
0
| if( arg_pos < param_len && param.charAt( arg_pos ) == '.' )
|
219 |
| { |
220 |
| |
221 |
| |
222 |
| |
223 |
| |
224 |
0
| var_obj = variable_map;
|
225 |
0
| arg_pos++;
|
226 |
| } else { |
227 |
| |
228 |
0
| var_obj = getVariable( DEFAULT_NAME );
|
229 |
| } |
230 |
| |
231 |
0
| Object arg_obj;
|
232 |
| |
233 |
0
| if( arg_pos == param_len )
|
234 |
| { |
235 |
0
| arg_obj = var_obj;
|
236 |
| } else { |
237 |
0
| int question = param.indexOf( "?", arg_pos );
|
238 |
0
| String arg;
|
239 |
| |
240 |
0
| if( question != -1 )
|
241 |
| { |
242 |
0
| arg = param.substring( arg_pos, question );
|
243 |
| } else { |
244 |
0
| arg = param.substring( arg_pos );
|
245 |
| } |
246 |
| |
247 |
0
| arg_obj = Accessor.getAttribute( var_obj
|
248 |
| , arg |
249 |
| , variable_map |
250 |
| ); |
251 |
| |
252 |
0
| if( arg_obj == null && question != -1 )
|
253 |
| { |
254 |
0
| arg_obj = param.substring( question + 1 );
|
255 |
| } |
256 |
| } |
257 |
| |
258 |
0
| f.process( arg_obj );
|
259 |
| } |
260 |
| |
261 |
| |
262 |
| |
263 |
| |
264 |
0
| protected void mustBeID( String id )
|
265 |
| { |
266 |
0
| if( id.length() > 0 && !Accessor.isID( id ) )
|
267 |
| { |
268 |
0
| throw new IllegalArgumentException( id );
|
269 |
| } |
270 |
| } |
271 |
| |
272 |
| protected Map name_to_func |
273 |
| = new HashMap(); |
274 |
| |
275 |
| protected Map name_to_var |
276 |
| = new HashMap(); |
277 |
| |
278 |
| protected Attributed variable_map = new MapAttributed( name_to_var ); |
279 |
| |
280 |
| protected PatternFilter pattern_filter; |
281 |
| |
282 |
| protected TextReceiver client; |
283 |
| } |