587
|
1 |
/*
|
|
2 |
* Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
package com.nokia.helium.internaldata.ant.listener;
|
|
19 |
|
628
|
20 |
import java.io.ByteArrayOutputStream;
|
|
21 |
import java.io.IOException;
|
|
22 |
import java.io.UnsupportedEncodingException;
|
|
23 |
import java.util.Enumeration;
|
|
24 |
import java.util.Hashtable;
|
587
|
25 |
import java.util.Iterator;
|
|
26 |
import java.util.Vector;
|
|
27 |
|
628
|
28 |
import org.apache.tools.ant.BuildEvent;
|
587
|
29 |
import org.dom4j.Document;
|
|
30 |
import org.dom4j.DocumentHelper;
|
|
31 |
import org.dom4j.Element;
|
628
|
32 |
import org.dom4j.io.OutputFormat;
|
587
|
33 |
import org.dom4j.io.XMLWriter;
|
|
34 |
/**
|
|
35 |
* This xml render object does the following -
|
|
36 |
* Generates target only for TargetNode type of node
|
|
37 |
* Creates the targets section.
|
|
38 |
* Generates task only for TargetNode type of node
|
|
39 |
* Creates the task section.
|
|
40 |
* Creates execution tree recursively, visiting the DataNodes.
|
|
41 |
* Creates the execution tree section.
|
|
42 |
* Creates the property section.
|
|
43 |
* Renders the build node into XML string.
|
|
44 |
*/
|
|
45 |
public class XMLRenderer {
|
|
46 |
|
|
47 |
// Dump of properties
|
|
48 |
private Hashtable<String, String> properties;
|
|
49 |
// the toplevel node.
|
|
50 |
private BuildNode root;
|
|
51 |
// Helium content database.
|
|
52 |
private Document database;
|
|
53 |
|
|
54 |
// Deps hashes: helper to remove duplicates.
|
|
55 |
private Vector<String> targetList = new Vector<String>();
|
|
56 |
private Vector<String> assertList = new Vector<String>();
|
|
57 |
|
|
58 |
public XMLRenderer(BuildNode root, Document database, Hashtable<String, String> properties, BuildEvent event) {
|
|
59 |
this.root = root;
|
|
60 |
this.database = database;
|
|
61 |
this.properties = properties;
|
|
62 |
}
|
|
63 |
|
|
64 |
/**
|
|
65 |
* Generating target only for TargetNode type of node
|
|
66 |
* @param node
|
|
67 |
* @param targets
|
|
68 |
*/
|
|
69 |
protected void createTarget(DataNode node, Element targets) {
|
|
70 |
if (node instanceof TargetNode) {
|
|
71 |
TargetNode targetNode = (TargetNode)node;
|
|
72 |
if (!targetList.contains(targetNode.getName() + targetNode.getFilename())) {
|
|
73 |
targetList.add(targetNode.getName() + targetNode.getFilename());
|
|
74 |
Element target = targets.addElement("target");
|
|
75 |
target.addAttribute("id", "target@" + targetList.indexOf(targetNode.getName() + targetNode.getFilename()));
|
|
76 |
target.addAttribute("name", targetNode.getName());
|
|
77 |
target.addAttribute("file", targetNode.getFilename());
|
|
78 |
target.addAttribute("line", "" + targetNode.getLine());
|
|
79 |
}
|
|
80 |
}
|
|
81 |
for (Iterator<DataNode> i = node.iterator() ; i.hasNext() ; ) {
|
|
82 |
createTarget(i.next(), targets);
|
|
83 |
}
|
|
84 |
}
|
|
85 |
|
|
86 |
/**
|
|
87 |
* Creating the targets section.
|
|
88 |
* @param statistics
|
|
89 |
*/
|
|
90 |
protected void createTargets(Element statistics) {
|
|
91 |
Element targets = statistics.addElement("targets");
|
|
92 |
if (root != null) {
|
|
93 |
createTarget(root, targets);
|
|
94 |
}
|
|
95 |
}
|
|
96 |
|
|
97 |
/**
|
|
98 |
* Creating the assert section.
|
|
99 |
* @param statistics
|
|
100 |
*/
|
|
101 |
protected void createAsserts(Element statistics) {
|
|
102 |
Element asserts = statistics.addElement("asserts");
|
|
103 |
if (root != null) {
|
|
104 |
createAssert(root, asserts);
|
|
105 |
}
|
|
106 |
}
|
|
107 |
|
|
108 |
/**
|
|
109 |
* Generating assert only for TargetNode type of node
|
|
110 |
* @param node
|
|
111 |
* @param targets
|
|
112 |
*/
|
|
113 |
protected void createAssert(DataNode node, Element targets) {
|
|
114 |
if (node instanceof AssertNode) {
|
|
115 |
AssertNode assertNode = (AssertNode)node;
|
|
116 |
if (assertNode.getAssertName() != null) {
|
|
117 |
assertList.add(assertNode.getAssertName());
|
|
118 |
Element target = targets.addElement("assert");
|
|
119 |
target.addAttribute("id", "assert@" + assertList.indexOf(assertNode.getAssertName()));
|
|
120 |
target.addAttribute("name", assertNode.getAssertName());
|
|
121 |
target.addAttribute("file", assertNode.getFilename());
|
|
122 |
target.addAttribute("line", "" + assertNode.getLine());
|
|
123 |
target.addAttribute("message", "" + assertNode.getMessage());
|
|
124 |
}
|
|
125 |
}
|
|
126 |
for (Iterator<DataNode> i = node.iterator() ; i.hasNext() ; ) {
|
|
127 |
createAssert(i.next(), targets);
|
|
128 |
}
|
|
129 |
}
|
|
130 |
|
|
131 |
/**
|
|
132 |
* Creating execution tree recursively, visiting the DataNodes.
|
|
133 |
* @param node
|
|
134 |
* @param tree
|
|
135 |
*/
|
|
136 |
protected void createTree(DataNode node, Element tree) {
|
|
137 |
Element elt = null;
|
|
138 |
if (node instanceof BuildNode) {
|
|
139 |
BuildNode buildNode = (BuildNode)node;
|
|
140 |
elt = tree.addElement("build");
|
|
141 |
elt.addAttribute("name", buildNode.getName());
|
|
142 |
elt.addAttribute("startTime", "" + buildNode.getStartTime().getTime());
|
|
143 |
elt.addAttribute("endTime", "" + buildNode.getEndTime().getTime());
|
|
144 |
elt.addAttribute("status", buildNode.getSuccessful() ? "successful" : "failed");
|
|
145 |
elt.addAttribute("thread", "" + buildNode.getThreadId());
|
|
146 |
} else if (node instanceof TargetNode) {
|
|
147 |
TargetNode targetNode = (TargetNode)node;
|
|
148 |
elt = tree.addElement("targetRef");
|
|
149 |
elt.addAttribute("reference", "target@" + targetList.indexOf(targetNode.getName() + targetNode.getFilename()));
|
|
150 |
elt.addAttribute("startTime", "" + targetNode.getStartTime().getTime());
|
|
151 |
elt.addAttribute("endTime", "" + targetNode.getEndTime().getTime());
|
|
152 |
elt.addAttribute("thread", "" + targetNode.getThreadId());
|
|
153 |
elt.addAttribute("startUsedHeap", "" + targetNode.getStartUsedHeap());
|
|
154 |
elt.addAttribute("startCommittedHeap", "" + targetNode.getStartCommittedHeap());
|
|
155 |
elt.addAttribute("endUsedHeap", "" + targetNode.getEndUsedHeap());
|
|
156 |
elt.addAttribute("endCommittedHeap", "" + targetNode.getEndCommittedHeap());
|
|
157 |
} else if (node instanceof AssertNode) {
|
|
158 |
AssertNode assertNode = (AssertNode)node;
|
|
159 |
if (assertNode.getAssertName() != null) {
|
|
160 |
elt = tree.addElement("assertRef");
|
|
161 |
elt.addAttribute("reference", "assert@" + assertList.indexOf(assertNode.getAssertName()));
|
|
162 |
elt.addAttribute("startTime", "" + assertNode.getStartTime().getTime());
|
|
163 |
elt.addAttribute("endTime", "" + assertNode.getEndTime().getTime());
|
|
164 |
elt.addAttribute("thread", "" + assertNode.getThreadId());
|
|
165 |
}
|
|
166 |
}
|
|
167 |
|
|
168 |
if (elt != null) {
|
|
169 |
for (Iterator<DataNode> i = node.iterator() ; i.hasNext() ; ) {
|
|
170 |
createTree(i.next(), elt);
|
|
171 |
}
|
|
172 |
}
|
|
173 |
}
|
|
174 |
|
|
175 |
/**
|
|
176 |
* Creating the execution tree section.
|
|
177 |
* @param statistics
|
|
178 |
*/
|
|
179 |
protected void createExecutionTree(Element statistics) {
|
|
180 |
Element executionTree = statistics.addElement("executionTree");
|
|
181 |
if (root != null) {
|
|
182 |
createTree(root, executionTree);
|
|
183 |
}
|
|
184 |
}
|
|
185 |
|
|
186 |
/**
|
|
187 |
* Creating the property section.
|
|
188 |
* @param statistics
|
|
189 |
*/
|
|
190 |
protected void createProperties(Element statistics) {
|
|
191 |
Element propertiesElt = statistics.addElement("properties");
|
|
192 |
if (properties != null) {
|
588
|
193 |
for (Enumeration<String> propertyEnum = properties.keys(); propertyEnum.hasMoreElements() ; ) {
|
|
194 |
String key = propertyEnum.nextElement();
|
587
|
195 |
Element propertyElt = propertiesElt.addElement("property");
|
|
196 |
propertyElt.addAttribute("name", key);
|
|
197 |
propertyElt.addAttribute("value", properties.get(key));
|
|
198 |
}
|
|
199 |
}
|
|
200 |
}
|
|
201 |
|
|
202 |
protected void insertDatabase(Element statistics) {
|
|
203 |
if (database != null) {
|
|
204 |
Element databaseElt = statistics.addElement("database");
|
|
205 |
databaseElt.add(database.getRootElement().detach());
|
|
206 |
}
|
|
207 |
}
|
|
208 |
|
|
209 |
/**
|
|
210 |
* Rendering the build node into XML string.
|
|
211 |
*/
|
|
212 |
public String toString() {
|
|
213 |
// Creating the XML document
|
|
214 |
Document document = DocumentHelper.createDocument();
|
|
215 |
Element statistics = document.addElement( "statistics" );
|
|
216 |
statistics.addAttribute("version", "1.1");
|
|
217 |
|
|
218 |
// Creating the document content.
|
|
219 |
insertDatabase(statistics);
|
|
220 |
createTargets(statistics);
|
|
221 |
createAsserts(statistics);
|
|
222 |
createExecutionTree(statistics);
|
|
223 |
createProperties(statistics);
|
|
224 |
try {
|
|
225 |
ByteArrayOutputStream output = new ByteArrayOutputStream();
|
|
226 |
XMLWriter out = new XMLWriter(output, OutputFormat.createPrettyPrint());
|
|
227 |
out.write(document);
|
|
228 |
return output.toString();
|
|
229 |
} catch (UnsupportedEncodingException exc) {
|
|
230 |
return document.asXML();
|
|
231 |
} catch (IOException exc) {
|
|
232 |
return document.asXML();
|
|
233 |
}
|
|
234 |
}
|
|
235 |
}
|