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 ****************************************************************************/
027 package org.vishia.java2C;
028
029 import java.text.ParseException;
030
031 import org.vishia.mainCmd.MainCmd;
032 import org.vishia.mainCmd.MainCmd_ifc;
033 import 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 */
040 public 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 JcHartmut, 2008-03-24, Version 0.83 2008-11-09");
108 super.addStandardHelpInfo();
109 //super.addHelpInfo("-i:INPUT inputfilepathes, example -i:path/org/myPackage/*.java");
110 super.addHelpInfo("-if:CFGFILE file contains some configuration for input, one per line, see examples.");
111 super.addHelpInfo("-o:OUTPUT outputpath, to this folder the file are written.");
112 super.addHelpInfo("-syntax:SYNTAXDIR path to the directory, for Java2C.zbnf, Java2Cstc.zbnf.");
113
114 }
115
116
117 /**parses all cmd line arguments. This is a wrapper for parseArguments()-method from {@link MainCmd},
118 * the MainCmd.parseArguments() is called inside.
119 * Before it is called, the aggregation to the Java2C-implementation class is set.
120 */
121 public void parseArguments(Java2C_Main.CmdlineArgs argData, String [] args)
122 throws ParseException
123 { this.main = argData;
124 super.parseArguments(args);
125 }
126
127
128
129 /*---------------------------------------------------------------------------------------------*/
130 /** Tests one argument. This method is invoked from {@link MainCmd.parseArgument(String[])}.
131 * It is abstract in the superclass MainCmd and have to be overwritten from the user.
132
133 @param argc String of the actual parsed argument from cmd line
134 @param nArg number of the argument in order of the command line, the first argument is number 1.
135 @return true is okay,
136 false if the argument doesn't match. The parseArgument method in MainCmd throws an exception,
137 the application should be aborted.
138 */
139 protected boolean testArgument(String arg, int nArg)
140 { boolean bOk = true; //set to false if the argc is not passed
141
142 //if(arg.startsWith("-i:")) main.addInputFilemask(getArgument(3),null, null);
143 //else
144 if(arg.startsWith("-if:")) main.setConfigFile(getArgument(4));
145 else if(arg.startsWith("-o:")) main.setPathOut(getArgument(3));
146 else if(arg.startsWith("-syntax:")) main.setSyntaxPath(getArgument(8));
147 else bOk=false;
148
149 return bOk;
150 }
151
152 /**invoked from parseArguments if no argument is given. In the default implementation a help info is written
153 * and the application is terminated. The user should overwrite this method if the call without command line arguments
154 * is meaningfully.
155 * @throws ParseException
156 *
157 */
158 protected void callWithoutArguments() throws ParseException
159 { //:TODO: overwrite with empty method - if the calling without arguments
160 //having equal rights than the calling with arguments - no special action.
161 super.callWithoutArguments(); //it needn't be overwritten if it is unnecessary
162 }
163
164
165
166
167 /*---------------------------------------------------------------------------------------------*/
168 /**Checks the cmdline arguments relation together.
169 If there is an inconsistent, a message should be written. It may be also a warning.
170 */
171 protected boolean checkArguments()
172 { return true;
173
174 }
175
176
177 }