001: /********************************************************************************
002: * CruiseControl, a Continuous Integration Toolkit
003: * Copyright (c) 2007, ThoughtWorks, Inc.
004: * 200 E. Randolph, 25th Floor
005: * Chicago, IL 60601 USA
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * + Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * + Redistributions in binary form must reproduce the above
016: * copyright notice, this list of conditions and the following
017: * disclaimer in the documentation and/or other materials provided
018: * with the distribution.
019: *
020: * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
021: * names of its contributors may be used to endorse or promote
022: * products derived from this software without specific prior
023: * written permission.
024: *
025: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
026: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
027: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
028: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
029: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
030: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
031: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
032: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
033: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
034: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
035: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
036: ********************************************************************************/package net.sourceforge.cruisecontrol.dashboard.widgets;
037:
038: import java.io.File;
039: import java.util.Map;
040:
041: import javax.xml.parsers.SAXParser;
042: import javax.xml.parsers.SAXParserFactory;
043:
044: import org.xml.sax.Attributes;
045: import org.xml.sax.SAXException;
046: import org.xml.sax.helpers.DefaultHandler;
047:
048: public class EmmaArtifactWidget implements Widget {
049: public String getDisplayName() {
050: return "Emma Artifact";
051: }
052:
053: public Object getOutput(Map parameters) {
054: File artifactosRoot = (File) parameters
055: .get(Widget.PARAM_BUILD_ARTIFACTS_ROOT);
056: try {
057: return parseEmma(new File(artifactosRoot, "coverage.xml"));
058: } catch (Exception e) {
059: return null;
060: }
061: }
062:
063: private String parseEmma(File logFile) {
064: SAXParserFactory factory = SAXParserFactory.newInstance();
065: SAXParser saxParser;
066: EmmaHandler handler = new EmmaHandler();
067: try {
068: saxParser = factory.newSAXParser();
069: saxParser.parse(logFile, handler);
070: } catch (Exception e) {
071: throw new RuntimeException(e);
072: }
073: return handler.getEmmaResult();
074: }
075:
076: private static class EmmaHandler extends DefaultHandler {
077: private StringBuffer coverages = new StringBuffer();
078:
079: private boolean isLevelAll = true;
080:
081: public void startElement(String uri, String localName,
082: String qName, Attributes attributes)
083: throws SAXException {
084: if ("all".equals(qName)) {
085: this .isLevelAll = true;
086: }
087: if ("package".equals(qName)) {
088: this .isLevelAll = false;
089: }
090: if (this .isLevelAll) {
091: if ("coverage".equals(qName)) {
092: coverages.append(attributes.getValue("type"))
093: .append(" : ").append(
094: attributes.getValue("value"))
095: .append("\n");
096: }
097: }
098: }
099:
100: public String getEmmaResult() {
101: return coverages.toString().trim();
102: }
103: }
104: }
|