01: /*
02: * Copyright 2007 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
21: import org.w3c.dom.Node;
22: import org.w3c.dom.NodeList;
23:
24: import java.text.DateFormat;
25: import java.text.ParseException;
26: import java.util.ArrayList;
27: import java.util.Date;
28: import java.util.List;
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 element) {
42:
43: Report report = new Report();
44: String dateString = element.getAttribute("date");
45:
46: try {
47: DateFormat format = DateFormat.getDateTimeInstance();
48: Date d = format.parse(dateString);
49: report.setDate(d);
50: report.setDateString(format.format(d));
51: } catch (ParseException e) {
52: // let date remain null if it doesn't parse correctly
53: }
54:
55: report.setGwtVersion(element.getAttribute("gwt_version"));
56:
57: List/* <Element> */children = getElementChildren(element,
58: "category");
59: report.setCategories(new ArrayList/* <Category> */(children
60: .size()));
61: for (int i = 0; i < children.size(); ++i) {
62: report.getCategories().add(
63: CategoryXml.fromXml((Element) children.get(i)));
64: }
65:
66: return report;
67: }
68:
69: static Element getElementChild(Element e, String name) {
70: NodeList children = e.getElementsByTagName(name);
71: return children.getLength() == 0 ? null : (Element) children
72: .item(0);
73: }
74:
75: static List/* <Element> */getElementChildren(Element e, String name) {
76: NodeList children = e.getElementsByTagName(name);
77: int numElements = children.getLength();
78: List/* <Element> */elements = new ArrayList/* <Element> */(
79: numElements);
80: for (int i = 0; i < children.getLength(); ++i) {
81: Node n = children.item(i);
82: elements.add((Element) n);
83: }
84: return elements;
85: }
86:
87: static String getText(Element e) {
88: Node n = e.getFirstChild();
89: return n == null ? null : n.getNodeValue();
90: }
91: }
|