KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > junit > viewer > server > ReportXml


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.junit.viewer.server;
17
18 import com.google.gwt.junit.viewer.client.Report;
19
20 import org.w3c.dom.Element JavaDoc;
21 import org.w3c.dom.Node JavaDoc;
22 import org.w3c.dom.NodeList JavaDoc;
23
24 import java.text.DateFormat JavaDoc;
25 import java.text.ParseException JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Date JavaDoc;
28 import java.util.List JavaDoc;
29
30 /**
31  * Hydrates a Report from its XML representation.
32  */

33 class ReportXml {
34
35   /**
36    * Hydrates a Report from its XML representation.
37    *
38    * @param element The XML element to hydrate from.
39    * @return a new report (with null id)
40    */

41   public static Report fromXml(Element JavaDoc element) {
42
43     Report report = new Report();
44     String JavaDoc dateString = element.getAttribute("date");
45
46     try {
47       DateFormat JavaDoc format = DateFormat.getDateTimeInstance();
48       Date JavaDoc d = format.parse(dateString);
49       report.setDate(d);
50       report.setDateString(format.format(d));
51     } catch (ParseException JavaDoc e) {
52       // let date remain null if it doesn't parse correctly
53
}
54
55     report.setGwtVersion(element.getAttribute("gwt_version"));
56
57     List JavaDoc/* <Element> */children = getElementChildren(element, "category");
58     report.setCategories(new ArrayList JavaDoc/* <Category> */(children.size()));
59     for (int i = 0; i < children.size(); ++i) {
60       report.getCategories().add(CategoryXml.fromXml((Element JavaDoc) children.get(i)));
61     }
62
63     return report;
64   }
65
66   static Element JavaDoc getElementChild(Element JavaDoc e, String JavaDoc name) {
67     NodeList JavaDoc children = e.getElementsByTagName(name);
68     return children.getLength() == 0 ? null : (Element JavaDoc) children.item(0);
69   }
70
71   static List JavaDoc/* <Element> */getElementChildren(Element JavaDoc e, String JavaDoc name) {
72     NodeList JavaDoc children = e.getElementsByTagName(name);
73     int numElements = children.getLength();
74     List JavaDoc/* <Element> */elements = new ArrayList JavaDoc/* <Element> */(numElements);
75     for (int i = 0; i < children.getLength(); ++i) {
76       Node JavaDoc n = children.item(i);
77       elements.add((Element JavaDoc) n);
78     }
79     return elements;
80   }
81
82   static String JavaDoc getText(Element JavaDoc e) {
83     Node JavaDoc n = e.getFirstChild();
84     return n == null ? null : n.getNodeValue();
85   }
86 }
87
Popular Tags