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  package com.unitesk.atp.tree;
32  
33  import java.io.File;
34  import java.io.FileWriter;
35  import java.io.IOException;
36  import java.io.Writer;
37  import java.util.Iterator;
38  import java.util.List;
39  
40  import com.unitesk.atp.dynattrs.Accessor;
41  import com.unitesk.atp.text.filters.ExceptionHandler;
42  import com.unitesk.atp.text.filters.IndentFilter;
43  import com.unitesk.atp.text.filters.WriterTextReceiver;
44  
45  /***
46   * Dump tree structure for debugging
47   * 
48   * @author <A href="mailto:demakov@ispras.ru">Alexey Demakov</A>
49   * @version $Id: TreePrinter.java,v 1.4 2005/11/30 10:06:18 all-x Exp $
50   */
51  public class TreePrinter
52  {
53      protected Writer writer;
54      protected IndentFilter indentFilter;
55      protected ExceptionHandler exceptionHandler;
56  
57      public static TreePrinter create( String file, ExceptionHandler exceptionHandler )
58      {
59          return create( new File( file ), exceptionHandler );
60      }
61  
62      public static 
63      TreePrinter create( String file, ExceptionHandler exceptionHandler, int indentStep )
64      {
65          return create( new File( file ), exceptionHandler, indentStep );
66      }
67  
68      public static TreePrinter create( File file, ExceptionHandler exceptionHandler )
69      {
70          return create( file, exceptionHandler, IndentFilter.DEFAULT_INDENT_STEP );
71      }
72      
73      public static TreePrinter create( File file, ExceptionHandler exceptionHandler, int indentStep )
74      {
75          try
76          {
77              return create( new FileWriter( file.getCanonicalPath() )
78                           , exceptionHandler
79                           , indentStep 
80                           );
81          }
82          catch( IOException e )
83          {
84              exceptionHandler.process( e );
85              return null;
86          }
87      }
88  
89      public static TreePrinter create( Writer writer
90                                      , ExceptionHandler exceptionHandler
91                                      )
92      {
93          return create( writer, exceptionHandler, IndentFilter.DEFAULT_INDENT_STEP );
94      }
95  
96      public static TreePrinter create( Writer writer
97                                      , ExceptionHandler exceptionHandler
98                                      , int indentStep
99                                      )
100     {
101         return new TreePrinter( writer, exceptionHandler, indentStep );
102     }
103 
104     /***
105      * Close output stream
106      */
107     public void close()
108     {
109         try
110         {
111             writer.close();
112         }
113         catch( IOException e )
114         {
115             exceptionHandler.process( e );
116         }
117     }
118      
119     /***
120      * Dump tree
121      * 
122      * @param tree     Tree to be dumped
123      */
124     public void dumpTree( Tree tree )
125     {
126         txt( "tree " + tree.getFile() ); nl();
127         block_nl();
128         dumpTree( tree.getRootNode() );
129         unblock_nl();
130     }
131      
132     /***
133      * Dump subtree.
134      * 
135      * @param node     Subtree root node.
136      */
137     public void dumpTree( Node node )
138     {
139         dumpNode( node );
140     }
141 
142     protected TreePrinter( Writer writer, ExceptionHandler exceptionHandler, int indentStep )
143     {
144         this.writer = writer;
145         WriterTextReceiver out = new WriterTextReceiver( writer, exceptionHandler );
146         indentFilter = new IndentFilter( out, indentStep );
147     }
148 
149     protected final void incIndent()
150     {
151         indentFilter.pushIndent(   indentFilter.getIndent()
152                                  + indentFilter.getIndentStep()
153                                );
154     }
155 
156     protected final void popIndent()
157     {
158         indentFilter.popIndent();
159     }
160 
161     protected final void txt( String s )
162     {
163         indentFilter.txt( s );
164     }
165 
166     protected final void nl()
167     {
168         indentFilter.nl();
169     }
170 
171     protected final void block()
172     {
173         txt( "{" );
174         incIndent();
175     }
176 
177     protected final void unblock()
178     {
179         popIndent();
180         txt( "}" );
181     }
182 
183     protected final void block_nl()
184     {
185         block();
186         nl();
187     }
188 
189     protected final void unblock_nl()
190     {
191         unblock();
192         nl();
193     }
194 
195     protected void dumpNode( Node node )
196     {
197         txt( node.toString() ); nl();
198         block_nl();
199 
200         for( Iterator attr_name_iterator = Accessor.getAttributeNames( node ).iterator();
201              attr_name_iterator.hasNext();
202            )
203         {
204             String name = (String)attr_name_iterator.next();
205             Object value = Accessor.getAttribute( node, name );
206 
207             if( value instanceof List )
208             {
209                 txt( name + "[" + ((List)value).size() + "] =" ); nl();
210                 block_nl();
211                 for( int i = 0; i < ((List)value).size(); i++ )
212                 {
213                     dumpAttribute( node
214                                  , name + "[" + i + "]"
215                                  , ((List)value).get( i )
216                                  );
217                 }
218                 unblock_nl();
219             } else {
220                 dumpAttribute( node, name, value );
221             }
222         }
223         unblock_nl();
224     }
225 
226     protected void dumpAttribute( Node node, String name, Object value )
227     {
228         txt( name + " = " );
229 
230         if( value instanceof Node && ((Node)value).getParent() == node )
231         {
232             // child
233             dumpNode( (Node)value );
234         } else {
235             txt( "'" + value + "'" );
236             nl();
237         }
238     }
239 }