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.core.ant.conditions;
|
|
19 |
|
|
20 |
import java.io.File;
|
628
|
21 |
|
587
|
22 |
import javax.xml.parsers.SAXParser;
|
|
23 |
import javax.xml.parsers.SAXParserFactory;
|
628
|
24 |
|
|
25 |
import org.apache.tools.ant.BuildException;
|
|
26 |
import org.apache.tools.ant.taskdefs.condition.Condition;
|
|
27 |
import org.apache.tools.ant.types.DataType;
|
|
28 |
import org.xml.sax.Attributes;
|
|
29 |
import org.xml.sax.SAXException;
|
|
30 |
import org.xml.sax.helpers.DefaultHandler;
|
587
|
31 |
|
|
32 |
/**
|
|
33 |
* This class implements a Ant Condition which report true if it finds any
|
|
34 |
* matching severity inside an XML log.
|
|
35 |
*
|
|
36 |
* Example:
|
|
37 |
* <pre>
|
|
38 |
* <target name="fail-on-build-error">
|
|
39 |
* <fail message="The build contains errors">
|
|
40 |
* <hlm:hasSeverity file="build_log.log.xml" severity="error"/>
|
|
41 |
* </fail>
|
|
42 |
* </target>
|
|
43 |
* </pre>
|
|
44 |
*
|
|
45 |
* The condition will eval as true if the build_log.log.xml contains any error message in the log.
|
|
46 |
*
|
|
47 |
* @ant.type name="hasSeverity" category="Core"
|
|
48 |
*/
|
628
|
49 |
public class XMLLogCondition extends DataType implements Condition {
|
587
|
50 |
|
|
51 |
// The severity to count
|
|
52 |
private String severity;
|
|
53 |
private String logRegexp;
|
|
54 |
private File fileName;
|
|
55 |
|
|
56 |
/**
|
|
57 |
* Sets which severity will be counted.
|
|
58 |
*
|
|
59 |
* @param severity
|
|
60 |
* @ant.required
|
|
61 |
*/
|
|
62 |
public void setSeverity(String severity) {
|
|
63 |
this.severity = severity;
|
|
64 |
}
|
|
65 |
|
|
66 |
public void setFile(File file) {
|
|
67 |
fileName = file;
|
|
68 |
}
|
|
69 |
|
|
70 |
/**
|
|
71 |
* Regular expression which used to match a specific log filename.
|
|
72 |
*
|
|
73 |
* @param regex
|
|
74 |
* @ant.not-required
|
|
75 |
*/
|
|
76 |
public void setLogMatcher(String regex) {
|
|
77 |
this.logRegexp = regex;
|
|
78 |
}
|
|
79 |
|
|
80 |
/**
|
|
81 |
* Get the number of a particular severity.
|
|
82 |
*
|
|
83 |
* @return the number of a particular severity.
|
|
84 |
*/
|
|
85 |
public int getSeverity() {
|
|
86 |
int messageCount = 0;
|
|
87 |
if (fileName == null || !fileName.exists()) {
|
|
88 |
//this.log("Error: Log file does not exist " + fileName);
|
|
89 |
return -1;
|
|
90 |
}
|
628
|
91 |
if (severity == null) {
|
587
|
92 |
throw new BuildException("'severity' attribute is not defined");
|
628
|
93 |
}
|
587
|
94 |
|
|
95 |
//this.log("Looking for severity '" + severity + "' under '" + fileName.getAbsolutePath() + "'");
|
|
96 |
SAXParserFactory factory = SAXParserFactory.newInstance();
|
|
97 |
try {
|
|
98 |
SAXParser saxParser = factory.newSAXParser();
|
|
99 |
MessageHandler handler = new MessageHandler();
|
|
100 |
saxParser.parse(fileName, handler);
|
|
101 |
this.log("Found " + handler.getMessageCount() + " " + severity
|
|
102 |
+ "(s).");
|
|
103 |
messageCount += handler.getMessageCount();
|
|
104 |
} catch (javax.xml.parsers.ParserConfigurationException exc) {
|
|
105 |
throw new BuildException(exc);
|
|
106 |
} catch (SAXException exc) {
|
|
107 |
throw new BuildException(exc);
|
|
108 |
} catch (java.io.IOException exc) {
|
|
109 |
throw new BuildException(exc);
|
|
110 |
}
|
|
111 |
return messageCount;
|
|
112 |
}
|
|
113 |
|
|
114 |
/**
|
|
115 |
* This method open the defined file and count the number of message tags
|
|
116 |
* with their severity attribute matching the configured one.
|
|
117 |
*
|
|
118 |
* @return if true if message with the defined severity have been found.
|
|
119 |
*/
|
|
120 |
public boolean eval() {
|
|
121 |
int severity = getSeverity();
|
|
122 |
if (severity < 0) {
|
|
123 |
return false;
|
|
124 |
}
|
|
125 |
return severity > 0;
|
|
126 |
}
|
|
127 |
|
|
128 |
/**
|
|
129 |
* Implements a SAX handler specialized in counting message tag with a
|
|
130 |
* specific severity.
|
|
131 |
*/
|
|
132 |
class MessageHandler extends DefaultHandler {
|
|
133 |
|
|
134 |
private int count;
|
|
135 |
|
|
136 |
public MessageHandler() {
|
|
137 |
}
|
|
138 |
|
|
139 |
@Override
|
|
140 |
public void startElement(String uri, String localName, String name,
|
|
141 |
Attributes attributes) throws SAXException {
|
|
142 |
super.startElement(uri, localName, name, attributes);
|
|
143 |
if (name.equals(severity) && count == 0) {
|
|
144 |
count = Integer.valueOf(attributes.getValue("count"));
|
|
145 |
}
|
|
146 |
}
|
|
147 |
|
|
148 |
public int getMessageCount() {
|
|
149 |
return count;
|
|
150 |
}
|
|
151 |
}
|
|
152 |
} |