View Javadoc

1   /*
2    * Copyright (c) 2001-2004,
3    * RedVerst Group, ISP RAS http://www.ispras.ru
4    * All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions are met:
8    *
9    * 1. Redistributions of source code must retain the above copyright notice, this
10   *    list of conditions and the following disclaimer.
11   *
12   * 2. Redistributions in binary form must reproduce the above copyright notice,
13   *    this list of conditions and the following disclaimer in the documentation
14   *    and/or other materials provided with the distribution.
15   *
16   * 3. The names "ATP", "TreeDL", "RedVerst", "ISP RAS"
17   *    may not be used to endorse or promote products derived from this software
18   *    without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
24   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
27   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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      // interface Function
55  
56      /***
57       * {@inheritDoc}
58       */
59      public void setGenerator( Generator generator )
60      {
61          if( generator != null )
62          {
63              // add
64              if( count > 0 )
65              {
66                  if( server != generator )
67                  {
68                      /*
69                       * function can't be used by more than one
70                       * generator simultaneously.
71                       */
72                      throw new IllegalArgumentException();
73                  }
74              } else {
75                  server = (TextGeneratorServer)generator;
76              }
77              count++;
78          } else {
79              // remove
80              if( --count < 0 )
81              {
82                  /*
83                   * can't free non-linked function
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     // interface Generator
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     // interface TextGenerator
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     // implemenation
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 }