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