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 java.util.Stack;
35  
36  import com.unitesk.atp.tree.Node;
37  import com.unitesk.atp.text.generation.BasicGenerator;
38  import com.unitesk.atp.text.filters.TextReceiver;
39  import com.unitesk.atp.text.filters.LineSplitFilter;
40  import com.unitesk.atp.text.filters.IndentFilter;
41  
42  /***
43   * Text Generator Server implements {@link TextGenerator} functionality.
44   *
45   * @author <A href="mailto:demakov@ispras.ru">Alexey Demakov</A>
46   * @version $Id: TextGeneratorServer.java,v 1.1 2004/10/09 06:28:48 all-x Exp $
47   */
48  public class TextGeneratorServer extends BasicGenerator implements TextGenerator
49  {
50      /***
51       * Constructs Text Generator Server with the given client
52       * and default indent step.
53       *
54       * @param  client  The client receiving results of text generation.
55       */
56      public TextGeneratorServer( TextReceiver client )
57      {
58          this( client, IndentFilter.DEFAULT_INDENT_STEP );
59      }
60  
61      /***
62       * Constructs Text Generator Server with the given client and indent step.
63       *
64       * @param  client     The client receiving results of text generation.
65       * @param  indentStep Indent step.
66       */
67      public TextGeneratorServer( TextReceiver client, int indentStep )
68      {
69          super( new LineSplitFilter( new IndentFilter( client, indentStep ) ) );
70  
71          lineSplitFilter = (LineSplitFilter)this.client;
72          indentFilter = (IndentFilter)lineSplitFilter.getClient();
73          out = client;
74  
75          setFunction( DEFAULT_NAME, new TreeDefaultFunction() );
76      }
77  
78      /***
79       * {@inheritDoc}
80       */
81      public void pushNode( Node node )
82      {
83          setVariable( DEFAULT_NAME, node );
84          nodeStack.push( node );
85      }
86  
87      /***
88       * {@inheritDoc}
89       */
90      public Node getNode()
91      {
92  
93          return (Node)nodeStack.peek();
94      }
95  
96      /***
97       * {@inheritDoc}
98       */
99      public void popNode()
100     {
101         nodeStack.pop();
102 
103         if( nodeStack.empty() )
104         {
105             setVariable( DEFAULT_NAME, null );
106         } else {
107             setVariable( DEFAULT_NAME, nodeStack.peek() );
108         }
109     }
110 
111     /***
112      * {@inheritDoc}
113      */
114     public void pushIndent( int indent )
115     {
116         indentFilter.pushIndent( indent );
117     }
118 
119     /***
120      * {@inheritDoc}
121      */
122     public int getIndent()
123     {
124         return indentFilter.getIndent();
125     }
126 
127     /***
128      * {@inheritDoc}
129      */
130     public void incIndent()
131     {
132         indentFilter.pushIndent(   indentFilter.getIndent()
133                                  + indentFilter.getIndentStep()
134                                );
135     }
136 
137     /***
138      * {@inheritDoc}
139      */
140     public void decIndent()
141     {
142         indentFilter.pushIndent(   indentFilter.getIndent()
143                                  - indentFilter.getIndentStep()
144                                );
145     }
146 
147     /***
148      * {@inheritDoc}
149      */
150     public void popIndent()
151     {
152         indentFilter.popIndent();
153     }
154 
155     /***
156      * {@inheritDoc}
157      */
158     public void list( String index
159                     , int start
160                     , int end
161                     , String str
162                     , String separator
163                     )
164     {
165         int from;
166         int to;
167         int step;
168 
169         if( start < end )
170         {
171             from = start;
172             to = end;
173             step = 1;
174         } else
175         if( end < start )
176         {
177             from = start - 1;
178             to = end - 1;
179             step = -1;
180         } else {
181             return;
182         }
183         for( int i = from; i != to; i += step )
184         {
185             Object old_var = setVariable( index, new Integer( i ) );
186 
187             txt( str );
188 
189             if( i + step != to && separator != null )
190             {
191                 txt( separator );
192             }
193 
194             setVariable( index, old_var );
195         }
196     }
197 
198     /***
199      * {@inheritDoc}
200      */
201     public void nl()
202     {
203         lineSplitFilter.nl();
204     }
205 
206     /***
207      * Print string as is, don't treat it as pattern and don't split lines.
208      * This method is used for printing values of parameters.
209      *
210      * @param str   The string to pass as is.
211      */
212     public void txtAsIs( String str )
213     {
214         indentFilter.txt( str );
215     }
216 
217     /***
218      * Line splitter.
219      */
220     protected LineSplitFilter lineSplitFilter;
221     /***
222      * Indent manager filter. Client of {@link #lineSplitFilter}
223      */
224     protected IndentFilter indentFilter;
225     /***
226      * Client receiving results of text generation.
227      * Client of {@link #indentFilter}
228      */
229     protected TextReceiver out;
230 
231     /***
232      * Stack of used nodes. The top element is current node.
233      */
234     private Stack/*Node*/ nodeStack = new Stack/*Node*/();
235 }