View Javadoc

1   //Copyright 2007-2008, by the California Institute of Technology.
2   //ALL RIGHTS RESERVED. United States Government Sponsorship acknowledged.
3   //Any commercial use must be negotiated with the Office of Technology Transfer
4   //at the California Institute of Technology.
5   //
6   //This software is subject to U. S. export control laws and regulations
7   //(22 C.F.R. 120-130 and 15 C.F.R. 730-774). To the extent that the software
8   //is subject to U.S. export control laws and regulations, the recipient has
9   //the responsibility to obtain export licenses or other export authority as
10  //may be required before exporting such information to foreign countries or
11  //providing access to foreign nationals.
12  
13  package gov.nasa.pds.ltdt.gui;
14  
15  import gov.nasa.pds.ltdt.commandLine.LTDTFlags;
16  import gov.nasa.pds.ltdt.gui.configuration.LTDTKeys;
17  import gov.nasa.pds.ltdt.gui.configuration.ToolsConfiguration;
18  import gov.nasa.pds.ltdt.gui.util.DictionaryUtility;
19  import gov.nasa.pds.ltdt.gui.util.Utility;
20  import gov.nasa.pds.tools.license.ToolsLicense;
21  import gov.nasa.pds.tools.options.ToolsOption;
22  
23  import java.io.File;
24  import java.io.IOException;
25  import java.net.MalformedURLException;
26  import java.net.URL;
27  import java.util.ArrayList;
28  import java.util.Arrays;
29  import java.util.HashMap;
30  import java.util.Iterator;
31  import java.util.List;
32  import java.util.Properties;
33  import java.util.logging.Level;
34  import java.util.logging.Logger;
35  
36  import javax.swing.UIManager;
37  
38  import org.apache.commons.cli.CommandLine;
39  import org.apache.commons.cli.GnuParser;
40  import org.apache.commons.cli.HelpFormatter;
41  import org.apache.commons.cli.Options;
42  import org.apache.commons.cli.ParseException;
43  import org.apache.commons.configuration.ConfigurationException;
44  import org.apache.commons.configuration.ConversionException;
45  
46  public class LTDTool implements LTDTFlags, LTDTKeys, ToolsLicense {
47  
48  	private List dict;
49  	private File projDir;
50  	private Options options;
51  
52  	/***
53  	 * Constructor
54  	 *
55  	 */
56  	public LTDTool() { 
57  		dict = null;
58  		projDir = null;
59  		buildOptions();
60  		Logger logger = Logger.getLogger("");
61  		logger.setLevel(Level.WARNING);
62  	}
63  	
64  	/***
65  	 * Show the version and disclaimer notice for LTDTool. 
66  	 * @throws IOException 
67  	 *
68  	 */	
69  	public void showVersion() throws IOException {
70  		URL propertyFile = this.getClass().getResource("ltdtool.properties");
71  		Properties p = Utility.getProperties(propertyFile);
72  		System.err.println("\n" + p.get(LTDTKeys.TOOLNAME));
73  		System.err.println(p.get(LTDTKeys.TOOLVERSION));
74  		System.err.println("Release Date: " + p.get(LTDTKeys.TOOLRELEASEDATE));
75  		System.err.println(p.get(LTDTKeys.TOOLCOPYRIGHT) + "\n");
76  	}
77  	
78  	/***
79  	 * Display LTDTool usage.
80  	 *
81  	 */
82  	public void showHelp(Options opts) {
83  		new HelpFormatter().printHelp("LTDTool", opts);
84  	}
85  	
86  	/***
87  	 * Set the list of dictionaries.
88  	 * 
89  	 * @param dictionaries A list of data dictionary files.
90  	 * @throws IOException Gets thrown if there was an invalid dictionary URL
91  	 * @throws MalformedURLException 
92  	 */
93  	public void setDict(List dictionaries) throws MalformedURLException,
94  	                                              IOException {
95  		this.dict = new ArrayList(dictionaries);
96  
97  		//Remove empty strings caused by the parsing method in
98  		//the Commons CLI library
99  		while(dict.remove("") == true);
100 
101 		// Check that each dictionary being passed in is a valid URL/File 
102 		for(Iterator i=dict.iterator(); i.hasNext();) {
103 			Utility.toURL(i.next().toString()).openStream().close();
104 		}
105 	}
106 	
107 	/***
108 	 * Set the project directory.
109 	 * 
110 	 * @param p The project directory.
111 	 * @throws IOException If the project directory does not exist.
112 	 */
113 	public void setProjDir(File p) throws IOException {
114 		if(!p.exists()) {
115 			throw new IOException("Project directory location does not exist: " + p.toString());
116 		}
117 		else {
118 			this.projDir = p;
119 		}
120 	}
121 	
122 	/***
123 	 * Builds the command-line option flags available to LTDTool.
124 	 * 
125 	 * @return an Options object containing the built options.
126 	 */
127 	private void buildOptions() {
128 		options = new Options();
129 		
130 		options.addOption(new ToolsOption(HELP[SHORT], HELP[LONG], WHATIS_HELP));
131 		options.addOption(new ToolsOption(VERSION[SHORT], VERSION[LONG], WHATIS_VERSION));
132 		
133 		ToolsOption project = new ToolsOption(PROJDIR[SHORT], PROJDIR[LONG], WHATIS_PROJDIR);
134 		project.hasArg(PROJDIR[ARGNAME], String.class);
135 		options.addOption(project);
136 		
137 		ToolsOption dict = new ToolsOption(DICT[SHORT], DICT[LONG], WHATIS_DICT);
138 		dict.hasArgs(DICT[ARGNAME], String.class);
139 		options.addOption(dict);
140 
141 		ToolsOption config = new ToolsOption(CONFIG[SHORT], CONFIG[LONG], WHATIS_CONFIG);
142 		config.hasArg(CONFIG[ARGNAME], String.class);
143 		options.addOption(config);
144 	}
145 	
146 	/***
147 	 * Parses the command line
148 	 * 
149 	 * @param args The arguments specified on the command line.
150 	 * @return A CommandLine object that has the results of the parsing.
151 	 * @throws ParseException Thrown if there was an invalid argument
152 	 * specified.
153 	 */
154 	public CommandLine parse(String[] args) 
155 	                                      throws ParseException {
156 		GnuParser parser = new GnuParser();
157 		return parser.parse(options, args);
158 	}
159 	
160 	/***
161 	 * Finds the different flag options specified during the execution run.
162 	 * 
163 	 * @param cmd Contains the option values specified on the command line.
164 	 * @return A HashMap containing values set for the different tool
165 	 * properties.
166 	 * @throws ConfigurationException Throws exception if a configuration
167 	 * file was specified and there was a problem during reading of the file.
168 	 * @throws IOException 
169 	 * @throws ConversionException 
170 	 */
171 	public HashMap query(CommandLine cmd) throws ConfigurationException, 
172 	                                      ConversionException, IOException {
173 		if(cmd.hasOption(HELP[SHORT])) {
174 			showHelp(options);
175 			System.exit(0);
176 		}
177 		else if(cmd.hasOption(VERSION[SHORT])) {
178 			showVersion();
179 			System.exit(0);
180 		}
181 		else {
182 			if(cmd.hasOption(CONFIG[SHORT])) {
183 				readConfig(Utility.toURL(cmd.getOptionValue(CONFIG[SHORT])));
184 			}
185 			if(cmd.hasOption(PROJDIR[SHORT])) {
186 				setProjDir(new File(cmd.getOptionValue(PROJDIR[SHORT])));			
187 			}
188 			if(cmd.hasOption(DICT[SHORT])) {
189 				setDict(Arrays.asList(cmd.getOptionValues(DICT[SHORT])));
190 			}
191 		}
192 		return setPropMap();
193 	}
194 	
195 	/***
196 	 * Sets the properties map.
197 	 * 
198 	 * @return A HashMap containing the values set for the different
199 	 * properties.
200 	 */
201 	private HashMap setPropMap() {
202 		HashMap map = new HashMap();
203 		
204 		//Pass in keyword values as a String
205 		if(projDir != null) {
206 			map.put(PROJDIRKEY, projDir.toString());
207 		}
208 		
209 		return map;
210 	}
211 	
212 	/***
213 	 * Reads a configuration file if it was passed into the tool.
214 	 * 
215 	 * @param url The URL supplied by the user.
216 	 * @throws ConfigurationException If there was a problem reading the file.
217 	 * @throws ConversionException If there was an invalid value entered for a
218 	 * property.
219 	 * @throws IOException 
220 	 */
221 	public void readConfig(URL url) throws ConfigurationException, 
222                                     ConversionException, IOException {
223 		ToolsConfiguration config = new ToolsConfiguration(url);
224 		
225 		if(config.containsKey(PROJDIRKEY)) {
226 			setProjDir(new File(config.getString(PROJDIRKEY)));
227 		}
228 		if(config.containsKey(DICTKEY)) {
229 			setDict(config.getList(DICTKEY));
230 		}
231 	}
232 		
233 	public static void main(String args[]) {
234 		HashMap propMap = null;
235 		final Properties props = new Properties();
236 		final ArrayList dictList;
237 		
238 		
239 		LTDTool ltdt = new LTDTool();
240 		
241 		try {
242 			CommandLine cmd = ltdt.parse(args);
243 			propMap = ltdt.query(cmd);
244 		} catch (Exception eX) {
245 			System.err.println(eX.getMessage());
246 			System.exit(1);
247 		}  
248 		
249         // MainWindow only takes final variables	
250         if(ltdt.dict != null) {
251         	dictList = new ArrayList(ltdt.dict);
252         }
253         else {
254         	dictList = null;
255         } 
256         
257         final HashMap pMap = propMap;
258 	
259         try { 
260         	// this allows the file chooser behaves better, but widgets are fixed for Windows-like look
261         	//UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
262         	UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());       
263         
264         	props.setProperty(LTDTKeys.DICTIONARIESFROMCOMMAND, DictionaryUtility.getDictionaryNameString(dictList));      
265         } catch (Exception e) { }
266         
267         //*** debug flag,i.e. when user specify -DDEBUG=true on command line
268         String v=System.getProperty("DEBUG");
269         if ("true".equalsIgnoreCase(v)) {
270         	props.setProperty("debug", "true");
271         }
272                
273 		javax.swing.SwingUtilities.invokeLater(new Runnable() {	
274 	        public void run() {
275 	        	new MainWindow(pMap, props);
276 	       
277 	        }
278 		});	
279 	}
280 	
281 }