001: /*
002: * @(#)XmlReportStyle.java
003: *
004: * Copyright (C) 2004 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a
009: * copy of this software and associated documentation files (the "Software"),
010: * to deal in the Software without restriction, including without limitation
011: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
012: * and/or sell copies of the Software, and to permit persons to whom the
013: * Software is furnished to do so, subject to the following conditions:
014: *
015: * The above copyright notice and this permission notice shall be included in
016: * all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
021: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024: * DEALINGS IN THE SOFTWARE.
025: */
026:
027: package net.sourceforge.groboutils.codecoverage.v2.ant;
028:
029: import java.io.File;
030: import java.io.FileOutputStream;
031: import java.io.IOException;
032: import java.io.OutputStreamWriter;
033:
034: import net.sourceforge.groboutils.codecoverage.v2.report.OutputXml;
035:
036: import org.apache.tools.ant.BuildException;
037: import org.apache.tools.ant.Project;
038: import org.w3c.dom.Document;
039:
040: /**
041: * Generates the XML file for the report.
042: *
043: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
044: * @version $Date: 2004/04/15 05:48:25 $
045: * @since March 15, 2003
046: */
047: public class XmlReportStyle implements IReportStyle {
048: private File outdir;
049: private String prefix = "CoverageReport-";
050: private String suffix = ".xml";
051:
052: public void setPrefix(String p) {
053: if (p == null) {
054: p = "";
055: }
056: this .prefix = p;
057: }
058:
059: public void setSuffix(String s) {
060: if (s == null) {
061: s = "";
062: }
063: this .suffix = s;
064: }
065:
066: public void setDestDir(File dir) {
067: this .outdir = dir;
068: }
069:
070: /**
071: * Called when the task is finished generating all the reports. This
072: * may be useful for styles that join all the reports together.
073: */
074: public void reportComplete(Project project,
075: java.util.Vector errorList) throws BuildException,
076: IOException {
077: // do nothing
078: }
079:
080: public void generateReport(Project project, Document doc,
081: String moduleName) throws BuildException, IOException {
082: if (this .outdir == null) {
083: throw new BuildException(
084: "Must set the 'destdir' attribute.");
085: }
086: project.log("Generating XML report for module " + moduleName,
087: Project.MSG_VERBOSE);
088: File outFile = new File(this .outdir, this .prefix + moduleName
089: + this .suffix);
090: File parent = outFile.getParentFile();
091: if (!parent.exists()) {
092: parent.mkdirs();
093: }
094: OutputStreamWriter osw = null;
095: try {
096: osw = new OutputStreamWriter(new FileOutputStream(outFile),
097: "UTF8");
098: (new OutputXml())
099: .write(doc.getDocumentElement(), osw, null);
100: } finally {
101: if (osw != null) {
102: osw.close();
103: }
104: }
105: }
106: }
|