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.tree.generation;
33
34 import com.unitesk.atp.tree.Node;
35 import com.unitesk.atp.text.generation.Function;
36 import com.unitesk.atp.text.generation.Generator;
37
38 /***
39 * The base client implementing interface of text generation.
40 * Delegates all text generation processing
41 * to underlying {@link TextGeneratorServer}
42 * that can be shared between several clients.
43 * Abstract {@link #visit(Node)} method should be overridden
44 * by inheritors to implement node processing.
45 *
46 * @author <A href="mailto:demakov@ispras.ru">Alexey Demakov</A>
47 * @version $Id: TextGeneratorClient.java,v 1.1 2005/07/17 19:35:16 all-x Exp $
48 * @since 3.5
49 */
50 public abstract class TextGeneratorClient
51 implements TextGenerator, Function
52 {
53
54
55
56 /***
57 * {@inheritDoc}
58 */
59 public void setGenerator( Generator generator )
60 {
61 if( generator != null )
62 {
63
64 if( count > 0 )
65 {
66 if( server != generator )
67 {
68
69
70
71
72 throw new IllegalArgumentException();
73 }
74 } else {
75 server = (TextGeneratorServer)generator;
76 }
77 count++;
78 } else {
79
80 if( --count < 0 )
81 {
82
83
84
85 throw new IllegalArgumentException();
86 }
87 if( count == 0 )
88 {
89 server = null;
90 }
91 }
92 }
93
94 /***
95 * Process node.
96 *
97 * @param node
98 */
99 public abstract void visit( Node node );
100
101 /***
102 * {@inheritDoc}
103 */
104 public void process( Object arg )
105 {
106 Node node = (Node)arg;
107 pushNode( node );
108
109 Function v = ((TreeDefaultFunction)getFunction( DEFAULT_NAME ))
110 .setNodeFunction( this );
111
112 try
113 {
114 visit( node );
115 }
116 finally
117 {
118 ((TreeDefaultFunction)getFunction( DEFAULT_NAME ))
119 .setNodeFunction( v );
120 popNode();
121 }
122 }
123
124
125
126
127 /***
128 * {@inheritDoc}
129 */
130 public final Function setFunction( String name, Function func )
131 {
132 return server.setFunction( name, func );
133 }
134
135 /***
136 * {@inheritDoc}
137 */
138 public final Function getFunction( String name )
139 {
140 return server.getFunction( name );
141 }
142
143 /***
144 * {@inheritDoc}
145 */
146 public final Object setVariable( String name, Object var )
147 {
148 return server.setVariable( name, var );
149 }
150
151 /***
152 * {@inheritDoc}
153 */
154 public final Object getVariable( String name )
155 {
156 return server.getVariable( name );
157 }
158
159 /***
160 * {@inheritDoc}
161 */
162 public final void txt( String t )
163 {
164 server.txt( t );
165 }
166
167 /***
168 * {@inheritDoc}
169 */
170 public final void txtAsIs( String t )
171 {
172 server.txtAsIs( t );
173 }
174
175
176
177
178 /***
179 * {@inheritDoc}
180 */
181 public final void pushNode( Node node )
182 {
183 server.pushNode( node );
184 }
185
186 /***
187 * {@inheritDoc}
188 */
189 public final Node getNode()
190 {
191 return server.getNode();
192 }
193
194 /***
195 * {@inheritDoc}
196 */
197 public final void popNode()
198 {
199 server.popNode();
200 }
201
202 /***
203 * {@inheritDoc}
204 */
205 public final void pushIndent( int indent )
206 {
207 server.pushIndent( indent );
208 }
209
210 /***
211 * {@inheritDoc}
212 */
213 public final int getIndent()
214 {
215 return server.getIndent();
216 }
217
218 /***
219 * {@inheritDoc}
220 */
221 public final void incIndent()
222 {
223 server.incIndent();
224 }
225
226 /***
227 * {@inheritDoc}
228 */
229 public final void decIndent()
230 {
231 server.decIndent();
232 }
233
234 /***
235 * {@inheritDoc}
236 */
237 public final void popIndent()
238 {
239 server.popIndent();
240 }
241
242 /***
243 * {@inheritDoc}
244 */
245 public final void list( String index
246 , int start
247 , int end
248 , String str
249 , String separator
250 )
251 {
252 server.list( index, start, end, str, separator );
253 }
254
255 /***
256 * {@inheritDoc}
257 */
258 public final void nl()
259 {
260 server.nl();
261 }
262
263
264
265 /***
266 * if <code>condition</code> is <code>true</code> calls
267 * {@link #txt(String) txt(t)}
268 *
269 * @throws NullPointerException
270 * if <code>t</code> is <code>null</code>
271 */
272 public final void txtif( boolean condition, String t )
273 {
274 if( condition )
275 {
276 server.txt( t );
277 }
278 }
279
280 /***
281 * if <code>condition</code> is <code>true</code> calls
282 * {@link #txt(String) txt(t)} else {@link #txt(String) txt(f)}
283 *
284 * @throws NullPointerException
285 * if <code>t</code> or <code>f</code> is <code>null</code>
286 */
287 public final void txtif( boolean condition, String t, String f )
288 {
289 if( condition )
290 {
291 server.txt( t );
292 } else {
293 server.txt( f );
294 }
295 }
296
297 /***
298 * if <code>object</code> is not <code>null</code> calls
299 * {@link #txt(String) txt(t)}
300 *
301 * @throws NullPointerException
302 * if <code>t</code> is <code>null</code>
303 */
304 public final void txtif( Object object, String t )
305 {
306 txtif( object != null, t );
307 }
308
309 /***
310 * if <code>object</code> is not <code>null</code> calls
311 * {@link #txt(String) txt(t)} else {@link #txt(String) txt(f)}
312 *
313 * @throws NullPointerException
314 * if <code>t</code> is <code>null</code>
315 */
316 public final void txtif( Object object, String t, String f )
317 {
318 txtif( object != null, t, f );
319 }
320
321
322
323
324 protected TextGeneratorServer server;
325 /***
326 * Counts how many times this function
327 * is used by server under different names
328 */
329 int count;
330 }