package org.sonar.report.pdf; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.IOException; import java.net.URL; import java.util.Iterator; import java.util.Properties; import org.apache.commons.httpclient.HttpException; import org.sonar.report.pdf.entity.ComplexityDistribution; import org.sonar.report.pdf.entity.Measures; import org.sonar.report.pdf.entity.Project; import com.lowagie.text.Document; import com.lowagie.text.DocumentException; import com.lowagie.text.Image; import com.lowagie.text.PageSize; import com.lowagie.text.pdf.PdfWriter; /** * This is the superclass of concrete reporters. It provides the access to Sonar data * (project, measures, graphics) and report config data. * * The concrete reporter class will provide: sonar base URL, logo (it will be used * in yhe PDF document), the project key and the implementation of printPdfBody method. */ public abstract class PDFReporter { private Properties properties; public ByteArrayOutputStream getReport() throws DocumentException, IOException, org.dom4j.DocumentException { Document document = new Document(PageSize.A4, 50, 50, 110, 50); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfWriter writer = PdfWriter.getInstance(document, baos); writer.setPageEvent(new LogoHeader(this.getLogo(), this.getProject())); document.open(); this.printPdfBody(document); document.close(); return baos; } public Project getProject() throws HttpException, IOException, org.dom4j.DocumentException { Project project = Project.parse(getSonarUrl() + "/api/projects/" + getProjectKey() + "?includelinks=true&format=xml&includechildren=true"); project.setMeasures(getMeasures(project.getKey())); Iterator it = project.getSubprojects().iterator(); while (it.hasNext()) { Project subproject = it.next(); subproject.setMeasures(getMeasures(subproject.getKey())); } return project; } private Measures getMeasures(String projectKey) throws HttpException, IOException, org.dom4j.DocumentException { Measures measures = Measures.parse(getSonarUrl() + "/api/projects/" + projectKey + "/measures?includeparams=true&format=xml"); return measures; } // TODO: add xradar graphic (need ISO9126 measures for this, SONAR-563). public void getRadarGraphic() { // RadarGraphic graphics = new RadarGraphic(); // Image imageRadar = graphics.getGraphic(null, null); } public Image getCCNDistribution(Project project) { String data = project.getMeasureValue("ccn_classes_count_distribution"); ComplexityDistribution ccnDist = new ComplexityDistribution(data, getSonarUrl()); return ccnDist.getGraphic(); } public String getTextProperty(String key) throws IOException { if (properties == null) { properties = new Properties(); URL resource = this.getClass().getClassLoader().getResource("report-texts-en.properties"); properties.load(new FileInputStream(resource.getFile())); } return properties.getProperty(key); } protected abstract String getSonarUrl(); protected abstract void printPdfBody(Document document) throws DocumentException, IOException, org.dom4j.DocumentException; protected abstract URL getLogo(); protected abstract String getProjectKey(); }