1 package org.overturetool.umltrans.Main;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.Hashtable;
6 import java.util.List;
7 import java.util.Vector;
8
9 public abstract class CmdLineHelper
10 {
11 public void processCommand(String[] args) throws Exception
12 {
13 Object[] tmp = exstractParameters(args);
14 Hashtable<String, String> par = (Hashtable<String, String>) tmp[0];
15 List<File> files = (List<File>) tmp[1];
16
17 if (args.length == 0 || args[0].replace("--", "-").startsWith("-help")
18 || args[0].startsWith("?") || args[0].startsWith("-?")
19 || args[0].startsWith("/?"))
20 {
21 printHelp();
22 return;
23
24
25
26 } else
27 {
28 handleCommand(par, files);
29 }
30 }
31
32 protected abstract void handleCommand(Hashtable<String, String> parameters,
33 List<File> files) throws Exception;
34
35 protected String[] paramterTypes;
36
37 protected final static String HelpParameter = "-help";
38
39 protected static boolean containsKeys(Hashtable<String, String> parameters,
40 String[] keys)
41 {
42 boolean containsAll = true;
43 for (String string : keys)
44 {
45 if (!parameters.containsKey(string))
46 containsAll = false;
47 }
48 return containsAll;
49 }
50
51
52
53
54
55
56
57
58
59
60 public abstract void printHelp();
61
62 private Object[] exstractParameters(String[] parameters)
63 {
64
65 Hashtable<String, String> pars = new Hashtable<String, String>();
66 List<File> specFiles = new Vector<File>();
67 try
68 {
69
70 int lastFoundParameter = 0;
71 for (int i = 0; i < parameters.length; i++)
72 {
73 for (String parType : paramterTypes)
74 {
75 if (parameters[i].equals(parType)
76 && parameters.length > i + 1)
77 {
78 pars.put(parType, parameters[i + 1]);
79 lastFoundParameter = i;
80 }
81 }
82 }
83
84
85 if (lastFoundParameter + 2 < parameters.length)
86 for (int i = lastFoundParameter + 2; i < parameters.length; i++)
87 {
88 specFiles.add(new File(parameters[i]));
89 }
90
91
92
93
94
95 } catch (Exception e)
96 {
97
98 }
99 return new Object[] { pars, specFiles };
100 }
101 }