001/****************************************************************************
002 * Copyright/Copyleft:
003 *
004 * For this source the LGPL Lesser General Public License,
005 * published by the Free Software Foundation is valid.
006 * It means:
007 * 1) You can use this source without any restriction for any desired purpose.
008 * 2) You can redistribute copies of this source to everybody.
009 * 3) Every user of this source, also the user of redistribute copies
010 *    with or without payment, must accept this license for further using.
011 * 4) But the LPGL ist not appropriate for a whole software product,
012 *    if this source is only a part of them. It means, the user
013 *    must publish this part of source,
014 *    but don't need to publish the whole source of the own product.
015 * 5) You can study and modify (improve) this source
016 *    for own using or for redistribution, but you have to license the
017 *    modified sources likewise under this LGPL Lesser General Public License.
018 *    You mustn't delete this Copyright/Copyleft inscription in this source file.
019 *
020 * @author JcHartmut: hartmut.schorrig@vishia.de
021 * @version 2008-04-06  (year-month-day)
022 * list of changes:
023 * 2008-04-06 JcHartmut: some correction
024 * 2008-03-15 JcHartmut: creation
025 *
026 ****************************************************************************/
027package org.vishia.java2C;
028
029import java.text.ParseException;
030
031import org.vishia.mainCmd.MainCmd;
032import org.vishia.mainCmd.MainCmd_ifc;
033import org.vishia.mainCmd.Report;
034
035/**Main class of Java2C-translator to organize call from cmd line.
036 * TODO call from ANT should be implemented.
037 * @author JcHartmut
038 *
039 */
040public class Java2C extends MainCmd
041{
042
043  /**Aggregation to the Console ouput (implementation class of Report).*/
044  Report console;
045
046
047
048
049  /*---------------------------------------------------------------------------------------------*/
050  /** main started from cmd line. A {@link Java2C_Main#singleton} is instanciated,
051   * all cmd line arguments are setted to it. The special cmd line arguments are:
052   * <table>
053   * <tr><td><code>-if:CONFIGFILE </code></td><td>file contains configuration for translating, 
054   *                              syntax see {@link Java2C_Main#setConfigFile(String)}.</td></tr>
055   * <tr><td><code>-o:OUTPUT   </code></td><td>outputpath, to this folder the file are written</td></tr>
056   * <tr><td><code>-syntax:SYNTAXDIR </code></td><td>path to the directory, for Java2C.zbnf, Java2Cstc.zbnf</td></tr>
057   * </table> 
058   * All standard cmd line arguments are the same described in {@link org.vishia.mainCmd.MainCmd#addStandardHelpInfo()}.
059  */
060  public static void main(String [] args)
061  { Java2C mainCmdLine = new Java2C(args); //the instance to parse arguments and others.
062    //Java2C_Main main = new Java2C_Main(mainCmdLine);     //the main instance
063    Java2C_Main.CmdlineArgs argData = new Java2C_Main.CmdlineArgs();
064    boolean bOk = true;
065    try{ mainCmdLine.parseArguments(argData, args); }
066    catch(Exception exception)
067    { mainCmdLine.setExitErrorLevel(MainCmd_ifc.exitWithArgumentError);
068      if(mainCmdLine.console != null){ mainCmdLine.console.writeError("cmd line parameter fault", exception); }
069      else{ System.out.println(exception); }
070      bOk = false;
071    }
072    if(bOk)
073    { /** The execution class knows the Java2C Main class in form of the MainCmd super class
074          to hold the contact to the command line execution.
075      */
076        Java2C_Main.instanciateSingleton(argData, mainCmdLine);     //the main instance
077      try{ Java2C_Main.singleton.execute(); }
078      catch(Exception exception)
079      { //catch the last level of error. No error is reported direct on command line!
080        Java2C_Main.singleton.console.report("Uncatched Exception on main level:", exception);
081        //exception.printStackTrace(System.err);
082        Java2C_Main.singleton.console.setExitErrorLevel(MainCmd_ifc.exitWithErrors);
083      }
084    }
085    mainCmdLine.exit();
086  }
087
088
089
090  /**The main instance for the Java2C translator. This is the {@link Java2C_Main#singleton} - instance.
091   * Such an instance is also used if the translator is settled outside calling per cmdLine. 
092   * The cmd line arguments are set to this instance. 
093   * The instance is created before the command line arguments are parsed, 
094   * because the cmd line argument values are set directly to this instance. 
095   */
096  private Java2C_Main.CmdlineArgs main;
097
098  /*---------------------------------------------------------------------------------------------*/
099  /** Constructor of the main class.
100      The command line arguments are parsed here. After them the execute class is created as composition of Java2C.
101  */
102  private Java2C(String[] args)
103  { super(args);
104    //:TODO: user, add your help info!
105    //super.addHelpInfo(getAboutInfo());
106    super.addAboutInfo("Java2C translator");
107    super.addAboutInfo("made by Hartmut Schorrig, since 2007-09-28, Version 0.94 2011-02-09");
108    super.addStandardHelpInfo();
109    //super.addHelpInfo("-i:INPUT    inputfilepathes, example -i:path/org/myPackage/*.java");
110    super.addHelpInfo("-cfg=CFGFILE file contains some configuration for input, one per line, see examples.");
111    super.addHelpInfo("-oc=OUTPUT_C   outputpath, to this folder the c-files are written.");
112    super.addHelpInfo("-oh=OUTPUTHEADER   outputpath, to this folder the h-files are written.");
113    super.addHelpInfo("-os=OUTPUTSTC   outputpath, to this folder the stc-files are written.");
114    super.addHelpInfo("-syntax=SYNTAXDIR path to the directory, for Java2C.zbnf, Java2Cstc.zbnf.");
115
116  }
117
118
119  /**parses all cmd line arguments. This is a wrapper for parseArguments()-method from {@link MainCmd},
120   * the MainCmd.parseArguments() is called inside. 
121   * Before it is called, the aggregation to the Java2C-implementation class is set.
122   */
123  public void parseArguments(Java2C_Main.CmdlineArgs argData, String [] args)
124  throws ParseException
125  { this.main = argData;
126    super.parseArguments(args);
127  }
128
129
130
131  /*---------------------------------------------------------------------------------------------*/
132  /** Tests one argument. This method is invoked from {@link MainCmd.parseArgument(String[])}. 
133   * It is abstract in the superclass MainCmd and have to be overwritten from the user.
134
135      @param argc String of the actual parsed argument from cmd line
136      @param nArg number of the argument in order of the command line, the first argument is number 1.
137      @return true is okay,
138              false if the argument doesn't match. The parseArgument method in MainCmd throws an exception,
139              the application should be aborted.
140  */
141  @Override
142  protected boolean testArgument(String arg, int nArg)
143  { boolean bOk = true;  //set to false if the argc is not passed
144
145    //if(arg.startsWith("-i:"))      main.addInputFilemask(getArgument(3),null, null);
146    //else 
147        if(arg.startsWith("-if:")) main.setConfigFile(getArgument(4));
148        else if(arg.startsWith("-cfg=")) main.setConfigFile(getArgument(5));
149        else if(arg.startsWith("-o:")) main.setPathOut(getArgument(3));
150    else if(arg.startsWith("-oc=")) main.setPathOut(getArgument(4));
151    else if(arg.startsWith("-oh=")) main.setPathOutHeader(getArgument(4));
152    else if(arg.startsWith("-os=")) main.setPathOutStcFiles(getArgument(4));
153    else if(arg.startsWith("-testJavaXml=")) main.setPathOutJavaXml(getArgument(13));
154    else if(arg.startsWith("-syntax:")) main.setSyntaxPath(getArgument(8));
155    else if(arg.startsWith("-syntax=")) main.setSyntaxPath(getArgument(8));
156    else bOk=false;
157
158    return bOk;
159  }
160
161  /**invoked from parseArguments if no argument is given. In the default implementation a help info is written
162   * and the application is terminated. The user should overwrite this method if the call without command line arguments
163   * is meaningfully.
164   * @throws ParseException
165   *
166   */
167  @Override
168  protected void callWithoutArguments() throws ParseException
169  { //:TODO: overwrite with empty method - if the calling without arguments
170    //having equal rights than the calling with arguments - no special action.
171    super.callWithoutArguments();  //it needn't be overwritten if it is unnecessary
172  }
173
174
175
176
177  /*---------------------------------------------------------------------------------------------*/
178  /**Checks the cmdline arguments relation together.
179     If there is an inconsistent, a message should be written. It may be also a warning.
180  */
181  @Override
182  protected boolean checkArguments()
183  { 
184        return main.checkArguments();
185
186  }
187
188
189}