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 MergedCheckStyleWidget implements Widget {
049: public String getDisplayName() {
050: return "Merged Check Style";
051: }
052:
053: public Object getOutput(Map parameters) {
054: File logFile = (File) parameters
055: .get(Widget.PARAM_BUILD_LOG_FILE);
056: try {
057: return parseCheckStyle(logFile);
058: } catch (Exception e) {
059: return null;
060: }
061: }
062:
063: private String parseCheckStyle(File logFile) {
064: SAXParserFactory factory = SAXParserFactory.newInstance();
065: SAXParser saxParser;
066: CheckStyleHandler handler = new CheckStyleHandler();
067: try {
068: saxParser = factory.newSAXParser();
069: saxParser.parse(logFile, handler);
070: } catch (Exception e) {
071: throw new RuntimeException(e);
072: }
073: return handler.getCheckStyleResult();
074: }
075:
076: class CheckStyleHandler extends DefaultHandler {
077: private StringBuffer errors = new StringBuffer();
078:
079: private String currentFileName = "";
080:
081: private boolean isCheckStyleStart;
082:
083: public void endElement(String uri, String localName,
084: String qName) throws SAXException {
085: if ("checkstyle".equals(qName)) {
086: this .isCheckStyleStart = false;
087: }
088: }
089:
090: public void startElement(String uri, String localName,
091: String qName, Attributes attributes)
092: throws SAXException {
093: if ("checkstyle".equals(qName)) {
094: this .isCheckStyleStart = true;
095: }
096: if (this .isCheckStyleStart) {
097: if ("file".equals(qName)) {
098: currentFileName = attributes.getValue("name");
099: }
100: if ("error".equals(qName)) {
101: errors.append("source = ").append('"').append(
102: currentFileName).append('"').append(" ")
103: .append("line = ").append('"').append(
104: attributes.getValue("line"))
105: .append('"').append(" ").append(
106: "severity = ").append('"').append(
107: attributes.getValue("severity"))
108: .append('"').append(" ").append(
109: "message = ").append('"').append(
110: attributes.getValue("message"))
111: .append('"').append("\n");
112: }
113: }
114: }
115:
116: public String getCheckStyleResult() {
117: return errors.toString().trim();
118: }
119: }
120: }
|