11package com .sandwich .koan .cmdline ;
22
3- import java .util .HashMap ;
3+ import static com .sandwich .koan .constant .KoanConstants .DATA_FOLDER ;
4+ import static com .sandwich .koan .constant .KoanConstants .FILESYSTEM_SEPARATOR ;
5+ import static com .sandwich .koan .constant .KoanConstants .PROJ_MAIN_FOLDER ;
6+
7+ import java .io .File ;
8+ import java .util .LinkedHashMap ;
49import java .util .Map ;
10+ import java .util .Map .Entry ;
511
612import com .sandwich .koan .constant .ArgumentType ;
7- import com .sandwich .util .Builder ;
13+ import com .sandwich .koan .constant .KoanConstants ;
14+ import com .sandwich .util .io .DynamicClassLoader ;
15+ import com .sandwich .util .io .FileUtils ;
16+
17+ public class CommandLineArgumentBuilder extends LinkedHashMap <ArgumentType , CommandLineArgument > {
18+
19+ private static final long serialVersionUID = 7635285665311420603L ;
820
9- public class CommandLineArgumentBuilder implements Builder <Map <ArgumentType , CommandLineArgument >>{
10- private String [] args ;
1121 public CommandLineArgumentBuilder (String ...args ){
12- this .args = args ;
13- }
14- public Map <ArgumentType , CommandLineArgument > build () {
15- Map <ArgumentType , CommandLineArgument > commandLineArguments = new HashMap <ArgumentType , CommandLineArgument >();
16- if (args != null ){
17- for (int index = 0 ; index < args .length ;){
18- String stringArg = args [index ];
19- if (stringArg == null || stringArg .trim ().length () == 0 ){
20- index ++;
22+ for (int index = 0 ; index < args .length ;){
23+ String stringArg = args [index ];
24+ if (stringArg == null || stringArg .trim ().length () == 0 ){
25+ index ++;
26+ continue ;
27+ }
28+ // is incremented in test of ternary
29+ String stringArgPlusOne = args .length <= ++index ? null : args [index ];
30+ stringArgPlusOne = stringArgPlusOne == null || stringArgPlusOne .trim ().length () == 0
31+ ? null : stringArgPlusOne ;
32+ ArgumentType argumentType = ArgumentType .findTypeByString (stringArg );
33+ ArgumentType argumentTypePlusOne = ArgumentType .findTypeByString (stringArgPlusOne );
34+ if (argumentType != null ){ // matches an anticipated argument string
35+ // so does next value, must be an argument too, reevaluate w/ index incremented only once above
36+ if (argumentTypePlusOne instanceof ArgumentType ){
37+ put (argumentType , new CommandLineArgument (argumentType , null ));
38+ // intentionally increment past next argument (only one) and
39+ // evaluate in next iteration with its following argument
40+ // - in other words, argumentType will be argumentTypePlusOne in next iteration
2141 continue ;
2242 }
23- // is incremented in test of ternary
24- String stringArgPlusOne = args .length <= ++index ? null : args [index ];
25- stringArgPlusOne = stringArgPlusOne == null || stringArgPlusOne .trim ().length () == 0
26- ? null : stringArgPlusOne ;
27- ArgumentType argumentType = ArgumentType .findTypeByString (stringArg );
28- ArgumentType argumentTypePlusOne = ArgumentType .findTypeByString (stringArgPlusOne );
29- if (argumentType != null ){ // matches an anticipated argument string
30- // so does next value, must be an argument too, reevaluate w/ index incremented only once above
31- if (argumentTypePlusOne instanceof ArgumentType ){
32- commandLineArguments .put (argumentType , new CommandLineArgument (argumentType , null ));
33- // intentionally increment past next argument (only one) and
34- // evaluate in next iteration with its following argument
35- // - in other words, argumentType will be argumentTypePlusOne in next iteration
36- continue ;
37- }
38- // ok 2nd argument wasn't a recongized argument type - go
39- // ahead and see if it's a class, if not, it must be a
40- // method - or bogus
41- else if (stringArgPlusOne != null ){
42- guessAtMethodAndClass (commandLineArguments , stringArgPlusOne );
43- }
44- else {
45- commandLineArguments .put (argumentType , new CommandLineArgument (argumentType , null ));
46- }
47- index ++;
48- continue ;
49- }else if (stringArg != null ){
50- // no flag, but a string - likely class or class and method
51- guessAtMethodAndClass (commandLineArguments , stringArg );
52- // do not increment again, bump stringArgPlusOne into stringArg from prior increment
53- continue ;
43+ // ok 2nd argument wasn't a recongized argument type - go
44+ // ahead and see if it's a class, if not, it must be a
45+ // method - or bogus
46+ else if (stringArgPlusOne != null ){
47+ guessAtMethodAndClass (this , stringArgPlusOne );
5448 }
49+ else {
50+ put (argumentType , new CommandLineArgument (argumentType , null ));
51+ }
52+ index ++;
53+ continue ;
54+ }else if (stringArg != null ){
55+ // no flag, but a string - likely class or class and method
56+ guessAtMethodAndClass (this , stringArg );
57+ // do not increment again, bump stringArgPlusOne into stringArg from prior increment
58+ continue ;
5559 }
5660 }
57- return commandLineArguments ;
61+ applyAssumedStartupBehaviors () ;
5862 }
5963
6064 private static void guessAtMethodAndClass (
@@ -66,13 +70,14 @@ private static void guessAtMethodAndClass(
6670 if (potentialClassOrMethod != null ){
6771 if (!hasClass ){
6872 commandLineArguments .put (ArgumentType .CLASS_ARG ,
69- new CommandLineArgument (ArgumentType .CLASS_ARG , Class .forName (potentialClassOrMethod ).getName ()));
73+ new CommandLineArgument (ArgumentType .CLASS_ARG ,
74+ new DynamicClassLoader ().loadClass (potentialClassOrMethod ).getName ()));
7075 }else if (!hasMethod ){
7176 commandLineArguments .put (ArgumentType .METHOD_ARG ,
7277 new CommandLineArgument (ArgumentType .METHOD_ARG , potentialClassOrMethod ));
7378 }
7479 }
75- }catch (ClassNotFoundException cnfe2 ){
80+ }catch (Exception cnfe2 ){
7681 if (!hasMethod ){
7782 commandLineArguments .put (ArgumentType .METHOD_ARG ,
7883 new CommandLineArgument (ArgumentType .METHOD_ARG , potentialClassOrMethod ));
@@ -82,4 +87,30 @@ private static void guessAtMethodAndClass(
8287 }
8388 }
8489 }
90+
91+ static private boolean isFirstTimeAppHasBeenRun () {
92+ File dataDirectory = new File (FileUtils .makeAbsoluteRelativeTo (
93+ new StringBuilder (PROJ_MAIN_FOLDER ).append (FILESYSTEM_SEPARATOR ).append (DATA_FOLDER ).toString ()));
94+ return !dataDirectory .exists ();
95+ }
96+
97+ void applyAssumedStartupBehaviors () {
98+ if (isFirstTimeAppHasBeenRun ()){
99+ ArgumentType .BACKUP .run (null );
100+ // really hackish - but this will clear console for first run, not necessary after
101+ for (int i = 0 ; i < 80 ; i ++){
102+ System .out .println ();
103+ }
104+ }
105+ if (isEmpty () || !containsKey (ArgumentType .RUN_KOANS ) && containsKey (ArgumentType .CLASS_ARG )){
106+ if (KoanConstants .DEBUG ){
107+ System .out .println ("Planting default run target." );
108+ for (Entry <ArgumentType , CommandLineArgument > argEntry : entrySet ()){
109+ System .out .println ("Key: '" +argEntry .getKey ()+"'" );
110+ System .out .println ("Value: '" +argEntry .getValue ()+"'" );
111+ }
112+ }
113+ put (ArgumentType .RUN_KOANS , new CommandLineArgument (ArgumentType .RUN_KOANS , null , true ));
114+ }
115+ }
85116}
0 commit comments