1 package net.sourceforge.selfesteem;
2
3 import org.jdom.Element;
4 import org.jdom.Document;
5 import org.jdom.JDOMException;
6 import org.jdom.input.SAXBuilder;
7
8 import java.io.*;
9 import java.util.HashMap;
10 import java.util.Map;
11 import java.util.Iterator;
12 import java.text.ParseException;
13
14 public class StoryParser {
15 private Map _stories = new HashMap();
16 private RootNode _root = new RootNode();
17
18 public void parseStoryFile(File file) throws IOException {
19 FileReader reader = new FileReader(file);
20 parseStoryFile(reader);
21 reader.close();
22 }
23
24 public void parseStoryFile(Reader reader) throws IOException {
25 IterationNode currentIteration = null;
26
27 BufferedReader bufferedReader = new BufferedReader(reader);
28 String line = null;
29 while ((line = bufferedReader.readLine()) != null) {
30 line = line.trim();
31
32 if (line.startsWith("#") || line.equals("")) {
33 // ignore
34 } else if (line.startsWith("-")) {
35 StoryNode storyNode = new StoryNode(line.substring(1).trim());
36 add(storyNode);
37 currentIteration.add(storyNode);
38 } else {
39 currentIteration = new IterationNode(line);
40 _root.add(currentIteration);
41 }
42 }
43 }
44
45 private void add(StoryNode story) {
46 if (_stories.containsKey(story.getName())) {
47 throw new SelfEsteemException("there can only be one story named (" + story.getName() + ")");
48 }
49 _stories.put(story.getName(), story);
50 }
51
52 public RootNode getRoot() {
53 return _root;
54 }
55
56 public void addTest(String name, boolean passing, double time) {
57 Element element = new Element("testcase");
58 element.setAttribute("name", name);
59 element.setAttribute("time", String.valueOf(time));
60 if (!passing) {
61 element.addContent(new Element("failure"));
62 }
63 addTest(element);
64 }
65
66 private void addTest(Element element) {
67 TestNode testNode = new TestNode(element);
68 StoryNode storyNode = (StoryNode) _stories.get(testNode.getStoryName());
69 if (storyNode == null) {
70 throw new SelfEsteemException("Unknown story (" + testNode.getStoryName() + ") please define it");
71 }
72 storyNode.add(testNode);
73 }
74
75 public void parseTestResults(File file) throws JDOMException {
76 try {
77 parseTestResults(new SAXBuilder().build(file));
78
79 } catch (SelfEsteemException e) {
80 e.prependToMessage("error while reading " + file + "\n");
81 throw e;
82 }
83 }
84
85 public void parseTestResults(Document document) {
86 for (Iterator i = document.getRootElement().getChildren("testcase").iterator(); i.hasNext();) {
87 addTest((Element) i.next());
88 }
89 }
90 }
This page was automatically generated by Maven