|
1 | 1 | package com.sandwich.koan.path.xmltransformation; |
2 | 2 |
|
3 | | -import java.io.File; |
4 | | -import java.io.FileNotFoundException; |
5 | | -import java.io.IOException; |
6 | | -import java.lang.reflect.Method; |
7 | | -import java.util.ArrayList; |
8 | | -import java.util.Collections; |
9 | | -import java.util.LinkedHashMap; |
10 | | -import java.util.List; |
11 | | -import java.util.Map; |
12 | | - |
13 | | -import javax.xml.parsers.DocumentBuilder; |
14 | | -import javax.xml.parsers.DocumentBuilderFactory; |
15 | | -import javax.xml.parsers.ParserConfigurationException; |
16 | | - |
17 | | -import org.w3c.dom.DOMException; |
18 | | -import org.w3c.dom.Document; |
19 | | -import org.w3c.dom.NamedNodeMap; |
20 | | -import org.w3c.dom.Node; |
21 | | -import org.w3c.dom.NodeList; |
22 | | -import org.xml.sax.SAXException; |
23 | | - |
24 | | -import com.sandwich.koan.Koan; |
25 | | -import com.sandwich.koan.KoanMethod; |
26 | 3 | import com.sandwich.koan.path.PathToEnlightenment.Path; |
27 | | -import com.sandwich.util.KoanComparator; |
28 | | -import com.sandwich.util.io.DynamicClassLoader; |
29 | | - |
30 | | -public class XmlToPathTransformer { |
31 | | - |
32 | | - private final File xmlFile; |
33 | | - private final String suiteName; |
34 | | - private final String methodName; |
35 | | - |
36 | | - public XmlToPathTransformer(){ |
37 | | - xmlFile = null; |
38 | | - suiteName = null; |
39 | | - methodName = null; |
40 | | - } |
41 | | - |
42 | | - public XmlToPathTransformer(String xmlFileLocation, String suiteName, String methodName) throws FileNotFoundException { |
43 | | - this.xmlFile = new File(xmlFileLocation); |
44 | | - this.suiteName = suiteName; |
45 | | - this.methodName = methodName; |
46 | | - if(!xmlFile.exists()){ |
47 | | - throw new FileNotFoundException(xmlFile.getAbsolutePath() |
48 | | - + " was not found. it may have been deleted, renamed."); |
49 | | - } |
50 | | - } |
51 | | - |
52 | | - public Path transform() throws ParserConfigurationException, SAXException, |
53 | | - IOException, DOMException, ClassNotFoundException, |
54 | | - InstantiationException, IllegalAccessException { |
55 | | - DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); |
56 | | - DocumentBuilder db = dbf.newDocumentBuilder(); |
57 | | - Document doc = db.parse(xmlFile); |
58 | | - doc.getDocumentElement().normalize(); |
59 | | - NodeList nodeLst = doc.getElementsByTagName("package"); |
60 | | - Map<String, Map<Object, List<KoanMethod>>> koans = new LinkedHashMap<String, Map<Object, List<KoanMethod>>>(); |
61 | | - for(int i = 0; i < nodeLst.getLength(); i++){ |
62 | | - Node node = nodeLst.item(i); |
63 | | - String packageTitle = node.getAttributes().getNamedItem("name").getNodeValue(); |
64 | | - String pkg = node.getAttributes().getNamedItem("pkg").getNodeValue(); |
65 | | - koans.put(packageTitle, createSuitesAndKoans(pkg, node.getChildNodes())); |
66 | | - } |
67 | | - return new Path(koans); |
68 | | - } |
69 | | - |
70 | | - Map<Object, List<KoanMethod>> createSuitesAndKoans(String pkg, NodeList childNodes) throws DOMException, ClassNotFoundException, InstantiationException, IllegalAccessException { |
71 | | - Map<Object, List<KoanMethod>> suitesAndKoans = new LinkedHashMap<Object, List<KoanMethod>>(); |
72 | | - for(int i = 0; i < childNodes.getLength(); i++){ |
73 | | - Node node = childNodes.item(i); |
74 | | - if("suite".equalsIgnoreCase(node.getNodeName())){ |
75 | | - String className = pkg + '.' + node.getAttributes().getNamedItem("class").getNodeValue(); |
76 | | - if(suiteName == null || suiteName.equalsIgnoreCase(className)){ |
77 | | - DynamicClassLoader dynamicClassLoader = new DynamicClassLoader(); |
78 | | - Class<?> koanSuiteClass = dynamicClassLoader.loadClass(className); |
79 | | - suitesAndKoans.put( |
80 | | - koanSuiteClass.newInstance(), Collections.unmodifiableList( |
81 | | - createKoanMethods(koanSuiteClass, node.getChildNodes()))); |
82 | | - } |
83 | | - } |
84 | | - } |
85 | | - return suitesAndKoans; |
86 | | - } |
87 | | - |
88 | | - List<KoanMethod> createKoanMethods(Class<?> koanSuiteClass, NodeList nodes) { |
89 | | - Map<String, KoanElementAttributes> rawKoanLessonByMethodName = extractKoansAndRawLessons(koanSuiteClass.getName(), nodes); |
90 | | - List<KoanMethod> koanMethods = getKoanMethods(koanSuiteClass, rawKoanLessonByMethodName); |
91 | | - KoanComparator koanComparator = new KoanComparator(rawKoanLessonByMethodName.keySet()); |
92 | | - Collections.sort(koanMethods, koanComparator); |
93 | | - return koanMethods; |
94 | | - } |
95 | | - |
96 | | - Map<String, KoanElementAttributes> extractKoansAndRawLessons( |
97 | | - String className, NodeList nodes) { |
98 | | - Map<String, KoanElementAttributes> rawKoanAttributesByMethodName = new LinkedHashMap<String, KoanElementAttributes>(); |
99 | | - for(int i = 0; i < nodes.getLength(); i++){ |
100 | | - Node node = nodes.item(i); |
101 | | - if("koan".equalsIgnoreCase(node.getNodeName())){ |
102 | | - NamedNodeMap attributes = node.getAttributes(); |
103 | | - String name = attributes.getNamedItem("name").getNodeValue(); |
104 | | - Node lesson = attributes.getNamedItem("lesson"); |
105 | | - String rawLesson = lesson == null ? null : lesson.getNodeValue(); |
106 | | - Node displayKoanIncompleteExceptionNode = attributes.getNamedItem("displayIncompleteKoanException"); |
107 | | - String displayIncompleteKoanException = displayKoanIncompleteExceptionNode == null ? |
108 | | - null : displayKoanIncompleteExceptionNode.getNodeValue(); |
109 | | - if(rawKoanAttributesByMethodName.containsKey(name)){ |
110 | | - throw new DuplicateKoanException(className, name); |
111 | | - } |
112 | | - rawKoanAttributesByMethodName.put(name, new KoanElementAttributes( |
113 | | - rawLesson, name, displayIncompleteKoanException)); |
114 | | - } |
115 | | - } |
116 | | - return rawKoanAttributesByMethodName; |
117 | | - } |
118 | | - |
119 | | - static class DuplicateKoanException extends RuntimeException { |
120 | | - private static final long serialVersionUID = 3846796580641690961L; |
121 | 4 |
|
122 | | - public DuplicateKoanException(String className, String name){ |
123 | | - super("Duplicate koans in the same suite: "+className+" with the name "+name); |
124 | | - } |
125 | | - } |
| 5 | +public interface XmlToPathTransformer { |
126 | 6 |
|
127 | | - public List<KoanMethod> getKoanMethods(Class<?> koanSuiteClass, Map<String, KoanElementAttributes> attributesByKoanName) { |
128 | | - List<KoanMethod> koanMethods = new ArrayList<KoanMethod>(); |
129 | | - for(final Method koan : koanSuiteClass.getMethods()){ |
130 | | - final Koan annotation = koan.getAnnotation(Koan.class); |
131 | | - if(annotation != null){ |
132 | | - if(methodName == null || methodName.equalsIgnoreCase(koan.getName())){ |
133 | | - KoanElementAttributes koanMethodAttributes = attributesByKoanName.get(koan.getName()); |
134 | | - koanMethodAttributes = koanMethodAttributes == null ? KoanElementAttributes.NON_EXISTENT : koanMethodAttributes; |
135 | | - String incompleteKoanException = koanMethodAttributes.displayIncompleteKoanException; |
136 | | - boolean displayIncompleteKoanException = incompleteKoanException == null || |
137 | | - "true".equalsIgnoreCase(incompleteKoanException.trim()); |
138 | | - koanMethods.add(new KoanMethod(koanMethodAttributes.lesson, koan, displayIncompleteKoanException)); |
139 | | - } |
140 | | - } |
141 | | - } |
142 | | - return koanMethods; |
143 | | - } |
| 7 | + public Path transform(); |
144 | 8 |
|
145 | | - public static class KoanElementAttributes{ |
146 | | - public static final KoanElementAttributes NON_EXISTENT = new KoanElementAttributes( |
147 | | - "", "", "true"); |
148 | | - String lesson, name, displayIncompleteKoanException; |
149 | | - public KoanElementAttributes(String lesson, String name, String displayIncompleteKoanException){ |
150 | | - this.lesson = lesson; |
151 | | - this.name = name; |
152 | | - this.displayIncompleteKoanException = displayIncompleteKoanException; |
153 | | - } |
154 | | - } |
155 | 9 | } |
156 | 10 |
|
0 commit comments