|
2 | 2 |
|
3 | 3 | import java.lang.reflect.Method; |
4 | 4 | import java.util.ArrayList; |
5 | | -import java.util.Arrays; |
6 | | -import java.util.Collections; |
7 | 5 | import java.util.HashMap; |
8 | 6 | import java.util.Iterator; |
9 | 7 | import java.util.LinkedHashMap; |
10 | 8 | import java.util.List; |
11 | 9 | import java.util.Map; |
12 | 10 | import java.util.Map.Entry; |
13 | | -import java.util.logging.Logger; |
14 | 11 |
|
15 | 12 | import com.sandwich.koan.KoanMethod; |
16 | 13 | import com.sandwich.koan.constant.KoanConstants; |
17 | 14 | import com.sandwich.koan.path.xmltransformation.XmlToPathTransformer; |
18 | | -import com.sandwich.koan.path.xmltransformation.XmlToPathTransformer.KoanElementAttributes; |
19 | 15 |
|
20 | 16 | public abstract class PathToEnlightenment { |
21 | 17 |
|
22 | 18 | static Path theWay; |
| 19 | + static String suiteName; |
| 20 | + static String koanMethod; |
23 | 21 |
|
24 | | - static Path createPath(){ |
25 | | - try{ |
26 | | - return new XmlToPathTransformer(KoanConstants.PATH_XML_LOCATION).transform(); |
27 | | - }catch(Throwable t){ |
28 | | - throw new RuntimeException(t); |
29 | | - } |
30 | | - } |
31 | | - |
32 | | - public static Path getPathToEnlightment(){ |
33 | | - if(theWay == null){ |
34 | | - theWay = createPath(); |
35 | | - } |
36 | | - return theWay; |
37 | | - } |
| 22 | + private PathToEnlightenment(){} // non instantiable |
38 | 23 |
|
39 | | - public static void removeAllKoanMethodsExcept(String koanName) { |
40 | | - Path koans = getPathToEnlightment(); |
41 | | - // if more than 1 pkg, or more than 1 suite - something is likely broken before this point |
42 | | - Map<Object, List<KoanMethod>> lessonsBySuiteMap = koans.iterator().next().getValue(); |
43 | | - koanName = koanName.trim(); |
44 | | - for(Entry<Object, List<KoanMethod>> methodsBySuite : lessonsBySuiteMap.entrySet()){ |
45 | | - KoanMethod keeper = null; |
46 | | - for(KoanMethod method : methodsBySuite.getValue()){ |
47 | | - if(koanName.equals(method.getMethod().getName())){ |
48 | | - keeper = method; |
49 | | - break; |
50 | | - } |
51 | | - } |
52 | | - if(keeper == null){ |
53 | | - // default is to assume method if not a class or recognized arg |
54 | | - // no sense throwing exception, might be user error |
55 | | - System.err.println(koanName+" was not a recognized parameter."); |
56 | | - System.exit(-3); |
57 | | - } |
58 | | - methodsBySuite.setValue(Arrays.asList(keeper)); |
59 | | - } |
60 | | - // defer the warning until after potential user error is in console and app is exited |
61 | | - if(koans.size() != 1 || lessonsBySuiteMap.size() != 1){ |
62 | | - Logger.getAnonymousLogger().warning("not just one koansuite remains, " + |
63 | | - "check koan suite name argument - not likely that filtering by method will work."); |
64 | | - } |
65 | | - } |
66 | | - |
67 | | - static void stagePathToEnlightenment(String pkg, String koanSuite){ |
68 | | - try{ |
69 | | - Class<?> koanClass; |
70 | | - if(koanSuite.contains(KoanConstants.PERIOD)){ |
71 | | - koanClass = Class.forName(koanSuite); |
72 | | - }else{ |
73 | | - koanClass = Class.forName( |
74 | | - new StringBuilder(pkg).append(KoanConstants.PERIOD).append(koanSuite).toString()); |
75 | | - } |
76 | | - stagePathToEnlightenment(koanClass); |
77 | | - }catch(Throwable t){ |
78 | | - throw new RuntimeException(t); |
79 | | - } |
80 | | - } |
81 | | - |
82 | | - public static void stagePathToEnlightenment(String koanSuite){ |
| 24 | + static Path createPath(){ |
83 | 25 | try{ |
84 | | - Class<?> koanClass; |
85 | | - if(koanSuite.contains(KoanConstants.PERIOD)){ |
86 | | - koanClass = Class.forName(koanSuite); |
87 | | - }else{ |
88 | | - throw new RuntimeException("need package to instantiate the class"); |
89 | | - } |
90 | | - stagePathToEnlightenment(koanClass); |
| 26 | + return new XmlToPathTransformer(KoanConstants.PATH_XML_LOCATION, suiteName, koanMethod) |
| 27 | + .transform(); |
91 | 28 | }catch(Throwable t){ |
92 | 29 | throw new RuntimeException(t); |
93 | 30 | } |
94 | 31 | } |
95 | 32 |
|
96 | | - static void stagePathToEnlightenment(Class<?> koanSuite) { |
97 | | - try { |
98 | | - stagePathToEnlightenment(koanSuite.newInstance()); |
99 | | - } catch (Exception e) { |
100 | | - throw new RuntimeException(e); |
101 | | - } |
| 33 | + public static void filterBySuite(String suite){ |
| 34 | + suiteName = suite; |
102 | 35 | } |
103 | 36 |
|
104 | | - static void stagePathToEnlightenment(Object koanSuite) { |
105 | | - if(koanSuite instanceof Class<?>){ |
106 | | - stagePathToEnlightenment((Class<?>)koanSuite); |
107 | | - } |
108 | | - Map<String, Map<Object, List<KoanMethod>>> koans = new HashMap<String, Map<Object,List<KoanMethod>>>(); |
109 | | - Map<Object, List<KoanMethod>> suiteAndMethods = new HashMap<Object, List<KoanMethod>>(); |
110 | | - List<KoanMethod> methods = XmlToPathTransformer.getKoanMethods(koanSuite.getClass(), |
111 | | - createStubbedLessonMapForSuite(koanSuite.getClass())); |
112 | | - suiteAndMethods.put(koanSuite, methods); |
113 | | - koans.put(koanSuite.toString(), suiteAndMethods); |
114 | | - PathToEnlightenment.theWay = new Path(koans); |
| 37 | + public static void filterByKoan(String koan){ |
| 38 | + koanMethod = koan; |
115 | 39 | } |
116 | 40 |
|
117 | | - private static Map<String, KoanElementAttributes> createStubbedLessonMapForSuite( |
118 | | - Class<? extends Object> clazz) { |
119 | | - Map<String, KoanElementAttributes> partialLessonByKoanMap = new HashMap<String, KoanElementAttributes>(); |
120 | | - Map<String, String> lessonsByMethodName = Collections.emptyMap(); |
121 | | - try{ |
122 | | - Path path = PathToEnlightenment.createPath(); |
123 | | - Map<String, Map<Object, List<KoanMethod>>> koanMethodsBySuiteByPackage = path.koanMethodsBySuiteByPackage; |
124 | | - Map<Object, List<KoanMethod>> methodsBySuite = Collections.emptyMap(); |
125 | | - for(Entry<String, Map<Object, List<KoanMethod>>> e : koanMethodsBySuiteByPackage.entrySet()){ |
126 | | - for(Entry<Object, List<KoanMethod>> e1 : e.getValue().entrySet()){ |
127 | | - if(clazz.isInstance(e1.getKey())){ |
128 | | - methodsBySuite = e.getValue(); |
129 | | - break; |
130 | | - } |
131 | | - } |
132 | | - } |
133 | | - for(Entry<Object, List<KoanMethod>> e : methodsBySuite.entrySet()){ |
134 | | - if(clazz.isInstance(e.getKey())){ |
135 | | - lessonsByMethodName = new HashMap<String, String>(); |
136 | | - for(KoanMethod method : e.getValue()){ |
137 | | - lessonsByMethodName.put(method.getMethod().getName(), method.getLesson()); |
138 | | - } |
139 | | - break; |
140 | | - } |
141 | | - } |
142 | | - }catch(Throwable x){ |
143 | | - Logger.getAnonymousLogger().severe(x.getMessage()); |
144 | | - } |
145 | | - for(Method m : clazz.getMethods()){ |
146 | | - String methodName = m.getName(); |
147 | | - partialLessonByKoanMap.put(methodName, new KoanElementAttributes( |
148 | | - lessonsByMethodName.get(methodName), methodName, "true")); |
| 41 | + public static Path getPathToEnlightment(){ |
| 42 | + if(theWay == null){ |
| 43 | + theWay = createPath(); |
149 | 44 | } |
150 | | - return partialLessonByKoanMap; |
| 45 | + return theWay; |
151 | 46 | } |
152 | | - |
153 | | - private PathToEnlightenment(){} // non instantiable |
154 | 47 |
|
155 | 48 | public static class FileFormatException extends RuntimeException{ |
156 | 49 | private static final long serialVersionUID = -1343169944770684376L; |
|
0 commit comments