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 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
235 }