View Javadoc

1   /*
2    * Copyright (c) 2001-2005,
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.tool;
33  
34  import java.io.File;
35  import java.util.ArrayList;
36  import java.util.List;
37  
38  import com.unitesk.atp.tree.Node;
39  import com.unitesk.atp.messages.*;
40  
41  /***
42   * The framework of an application that processes Attributed Tree (AT).
43   * The inheritor must implement abstract methods {@link #parse(File)},
44   * {@link #initName()}
45   * and define method
46   *
47   * <pre>public static void main( String[] args )
48   * {
49   *     new MyTool().run( args );
50   * }</pre>
51   *
52   * @author <A href="mailto:demakov@ispras.ru">Alexey Demakov</A>
53   * @version $Id: Tool.java,v 1.6 2005/06/01 06:40:22 all-x Exp $
54   */
55  public abstract class Tool extends com.unitesk.atp.tool.Tool
56                             implements PluginManager
57  {
58      /***
59       * Parses the input file and sets tree root node.
60       *
61       * @see #setTree(Tree) setTree( Tree )
62       * @see #getTree()
63       */
64      protected abstract void parse( File inputFile );
65  
66      /***
67       * Processes command line arguments.
68       *
69       * @param  args    The array of command line parameters from main() method.
70       */
71      protected int run( String[] args )
72      {
73          try
74          {
75              onToolStart();            
76              if( args.length < 1 )
77              {
78                  usage();
79                  return 1;
80              }
81  
82              String inputFileName = args[0];
83  
84              if( inputFileName.equals( "-" ) )
85              {
86                  if( args.length == 1 )
87                  {
88                      help( null );
89                  } else {
90                      List/*String*/ l = new ArrayList/*String*/();
91  
92                      for( int i = 1; i < args.length; i++ )
93                      {
94                          l.add( args[i] );
95                      }
96                      help( l );
97                  }
98                  return 1;
99              } else {
100                 mbox.info( "" );
101                 onParserStart( inputFileName );
102                 parse( new File( inputFileName ) );
103                 mbox.status();
104                 onParserFinish( inputFileName );
105                 for ( int i = 1; i < args.length; i++ )
106                 {
107                     process( args[i], null );
108                 }
109                 onToolFinish();
110                 return 0;
111             }
112         }
113         catch( FatalErrorException e )
114         {
115             return e.getCode();
116         }
117     }
118 
119     //---- interface PluginManager ---------------------------------------------
120 
121     protected Tree tree;
122 
123     /***
124      * {@inheritDoc}
125      */
126     public Tree getTree() { return tree; }
127 
128     /***
129      * {@inheritDoc}
130      */
131     public void setTree( Tree tree ) { this.tree = tree; }
132 
133     /***
134      * {@inheritDoc}
135      */
136     public void process( String name, Tree tree )
137     {
138         Object plugin = getPlugin( name );
139 
140         if( plugin != null && plugin instanceof Action )
141         {
142             Action action = (Action)plugin;
143             onActionStart( name, action );
144             action.process( tree != null ? tree : this.tree );
145             mbox.status();
146             onActionFinish( name, action );
147         } else {
148             mbox.fatal( new NameNotFoundMessage( null
149                                                , null
150                                                ,  mbox
151                                                  .getStringManager()
152                                                  .getString( Action.class
153                                                            , "message.action"
154                                                            )
155                                                , name
156                                                )
157                       );
158         }
159     }
160 
161     /***
162      * {@inheritDoc}
163      */
164     public Object get( String name, Tree tree, Node node )
165     {
166         Object plugin = getPlugin( name );
167 
168         if( plugin != null && plugin instanceof Attribute )
169         {
170             return ((Attribute)plugin).get( tree != null ? tree : this.tree
171                                           , node
172                                           );
173         } else {
174             mbox.fatal( new NameNotFoundMessage( null
175                                                , null
176                                                ,  mbox
177                                                  .getStringManager()
178                                                  .getString( Attribute.class
179                                                            , "message.attribute"
180                                                            )
181                                                , name
182                                                )
183                       );
184             return null;
185         }
186     }
187 
188     /***
189      * Invoked at start of the tool.
190      * Prints out a name and a version of the tool.
191      */
192     protected void onToolStart()
193     {
194         mbox.info( new ToolInfoMessage( this ) );
195         mbox.info( new FileStatusMessage( properties_file_status
196                                         , properties_file_name
197                                         )
198                  );
199         mbox.info( new FileStatusMessage( plugins_file_status
200                                         , plugins_file_name
201                                         )
202                  );
203     }
204 
205     /***
206      * Invoked at finish of the tool.
207      * Prints out statistics.
208      */
209     protected void onToolFinish()
210     {
211         mbox.statistics();
212     }
213 
214     /***
215      * Invoked before start of parsing.
216      * Prints out a name of the input file.
217      *
218      * @param  inputFileName    The name of the input file.
219      */
220     protected void onParserStart( String inputFileName )
221     {
222         mbox.info( new FileStatusMessage( InputFileStatus.status
223                                         , inputFileName
224                                         )
225                  );
226     }
227     
228     /***
229      * Invoked before start of parsing.
230      * Does nothing.
231      *
232      * @param  inputFileName    The name of the input file.
233      */
234     protected void onParserFinish( String inputFileName )
235     {
236     }
237     
238     /***
239      * Invoked before start of the action.
240      * Prints out a name and a version of the action.
241      *
242      * @param  name      The name of the action.
243      * @param  action    The action.
244      */
245     protected void onActionStart( String name, Action action )
246     {
247         mbox.info( new PluginInfoMessage( name, action) );
248     }
249     
250     /***
251      * Invoked before start of the action.
252      * Does nothing.
253      *
254      * @param  name      The name of the action.
255      * @param  action    The action.
256      */
257     protected void onActionFinish( String name, Action action )
258     {
259     }
260 }