Skip to content

PPTXReportingJavaMain

angelozerr edited this page Aug 4, 2015 · 2 revisions

PPTX Reporting in Java Main

This section explains step by step how to generate a report created with MS PowerPoint pptx by using Velocity syntax to set fields to replace. You can download pptxandvelocity-*-sample.zip which is the project explained in this section.

PPTX reporting follow the same idea than PPTX reporting, except than you don't use Megefield to set field (because Mergefield doesn't exist in pptx), but you type directly your field name in the pptx slide. PPTX reporting is less powerfull than pptx reporting because you cannot use if, loop directives (because text typed are escaped by MS PowerPoint and XDocReport unespace only the field name and not the directive).

Here we will create a basic pptx document _PPTXProjectWithVelocity.pptx :

[The $project.Name is a text and project is Java model Project class which define Project#getName(). The field name start with project because we will register the Java Model with project key in the context like this :

Project project = new Project("XDocReport");
context.put("project", project);

After report process we will generate the document PPTXProjectWithVelocity_Out.pptx :

(http://wiki.xdocreport.googlecode.com/git/screenshots/PPTXProjectWithVelocity.png))

Steps section are :

  1. Add XDocReport JARs in your classpath project.
  2. Create Java model create your Java model.
  3. Design pptx report design your pptx report with MS PowerPoint by using Velocity syntax to set fields to replace.
  4. Generate pptx report generate pptx report by using XDocReport API.

If you wish to :

  • use PPTX with Freemarker, you could download *pptxandfreemarker-**-sample.zip.
  • use PPTX with Velocity, you could download *pptxandvelocity-**-sample.zip.

1. Add XDocReport JARs

XDocReport is very modular. It means that you can choose your document type (odt, pptx...) as source report and use template engine syntax to set fields name (Freemarker/Velocity...).

In our case we will generate report from pptx with Velocity. So we need to set well JARs in your classpath.

Required JARs

For any document type and template engine you need to add following JARs :

**JARs name** **Description** **OSGi Bundle/Fragment**
fr.opensagres.xdocreport.core XDocReport Core Bundle
fr.opensagres.xdocreport.document Document type API Bundle
fr.opensagres.xdocreport.template Template engine API Bundle
fr.opensagres.xdocreport.converter Converter API Bundle

PPTX Document JAR

In this section we wish using pptx as document source for report, so you need to add PPTX document implementation JAR:

**JARs name** **Description** **OSGi Bundle/Fragment**
fr.opensagres.xdocreport.document.pptx PPTX Document implementation Fragment -> fr.opensagres.xdocreport.document

Note for OSGi : this JAR is a fragment linked to the bundle OSGi fr.opensagres.xdocreport.document.

Velocity JARs

In this section we wish using Velocity as syntax to set fields, so you need to add Velocity template engine implementation and Velocity JARs.

Velocity Template JAR

Add Velocity template engine implementation JAR:

**JARs name** **Description** **OSGi Bundle/Fragment**
fr.opensagres.xdocreport.template.velocity Velocity template engine implementation Fragment -> fr.opensagres.xdocreport.template

Note for OSGi : this JAR is a fragment linked to the bundle OSGi fr.opensagres.xdocreport.template.

Velocity JARs

Add Velocity JARs :

**JARs name** **Description**
velocity-1.7 Velocity
commons-collections-3.2.1 Commons Collection
commons-lang-2.4 Commons Lang
oro-2.0.8 ORO

2. Create Java model

Now you can create your Java model that you wish use in your pptx report. In this section we will use Java Project model like this :

package fr.opensagres.xdocreport.samples.pptxandvelocity.model;

public class Project {

  private final String name;

  public Project(String name) {
    this.name = name;
  }

  public String getName() {
    return name;
  }
}

3. Design PPTX report

At this step you can create your pptx report with MS PowerPoint with the content :

Project: $project.Name

[For the field to replace $project.Name, in the pptx case, type directly the field* in your document.

You can note for this sample that you can style field.

4. Generate PPTX report

Once you have create pptx, save it in your project. In this sample the pptx report was saved with PPTXProjectWithVelocity.pptx name in the fr.opensagres.xdocreport.samples.pptxandvelocity package of the Java project.

Create Java class PPTXProjectWithVelocity to load PPTXProjectWithVelocity.pptx and generate report in file PPTXProjectWithVelocity_Out.pptx like this :

package fr.opensagres.xdocreport.samples.pptxandvelocity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import fr.opensagres.xdocreport.core.XDocReportException;
import fr.opensagres.xdocreport.document.IXDocReport;
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
import fr.opensagres.xdocreport.samples.pptxandvelocity.model.Project;
import fr.opensagres.xdocreport.template.IContext;
import fr.opensagres.xdocreport.template.TemplateEngineKind;

public class PPTXProjectWithVelocity {

  public static void main(String[](http://wiki.xdocreport.googlecode.com/git/screenshots/PPTXProjectWithVelocity.png)) args) {
    try {
      // 1) Load PPTX file by filling Velocity template engine and cache it to the registry
      InputStream in = PPTXProjectWithVelocity.class.getResourceAsStream("PPTXProjectWithVelocity.pptx");
      IXDocReport report = XDocReportRegistry.getRegistry().loadReport(in,TemplateEngineKind.Velocity);

      // 2) Create context Java model
      IContext context = report.createContext();
      Project project = new Project("XDocReport");
      context.put("project", project);

      // 3) Generate report by merging Java model with the PPTX
      OutputStream out = new FileOutputStream(new File("PPTXProjectWithVelocity_Out.pptx"));
      report.process(context, out);

    } catch (IOException e) {
      e.printStackTrace();
    } catch (XDocReportException e) {
      e.printStackTrace();
    }
  }
}

Your Eclipse project looks like this :

[Run PPTXProjectWithVelocity Java class and PPTXProjectWithVelocity_Out.pptx will be generated in your project home :

(http://wiki.xdocreport.googlecode.com/git/screenshots/PPTXProjectWithVelocity_Workspace.png))

If you open PPTXProjectWithVelocity_Out.pptx you will see :

[# 5. Test Performance

XDocReport use Velocity/Freemarker cache to improve a lot the performance. To test performance, create fr.opensagres.xdocreport.samples.pptxandvelocity.PPTXProjectWithVelocityTestPerf class like this which display time process for the first and second report generation :

package fr.opensagres.xdocreport.samples.pptxandvelocity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import fr.opensagres.xdocreport.core.XDocReportException;
import fr.opensagres.xdocreport.document.IXDocReport;
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
import fr.opensagres.xdocreport.samples.pptxandvelocity.model.Project;
import fr.opensagres.xdocreport.template.IContext;
import fr.opensagres.xdocreport.template.TemplateEngineKind;

public class PPTXProjectWithVelocityTestPerf {

	public static void main(String[](http://wiki.xdocreport.googlecode.com/git/screenshots/PPTXProjectWithVelocity_Out.png)) args) {
		try {

			String reportId = "MyId";

			// 1) Load PPTX file by filling Velocity template engine and cache
			// it to the registry
			InputStream in = PPTXProjectWithVelocityTestPerf.class
					.getResourceAsStream("PPTXProjectWithVelocity.pptx");
			IXDocReport report = XDocReportRegistry.getRegistry().loadReport(
					in, reportId, TemplateEngineKind.Velocity);

			// 2) Create context Java model
			IContext context = report.createContext();
			Project project = new Project("XDocReport");
			context.put("project", project);

			// 3) Generate report by merging Java model with the PPTX
			OutputStream out = new FileOutputStream(new File(
					"PPTXProjectWithVelocity_Out.pptx"));
			long start = System.currentTimeMillis();
			report.process(context, out);
			System.out.println("Report processed in "
					+ (System.currentTimeMillis() - start) + " ms");

			// 4) Regenerate report
			IXDocReport report2 = XDocReportRegistry.getRegistry().getReport(
					reportId);
			out = new FileOutputStream(new File(
					"PPTXProjectWithVelocity_Out.pptx"));
			start = System.currentTimeMillis();
			report2.process(context, out);
			System.out.println("Report processed in "
					+ (System.currentTimeMillis() - start) + " ms");

		} catch (IOException e) {
			e.printStackTrace();
		} catch (XDocReportException e) {
			e.printStackTrace();
		}
	}
}

Run PPTXProjectWithVelocityTestPerf Java class and PPTXProjectWithVelocity_Out.pptx will be generated twice and console display time process :

Report processed in 250 ms
Report processed in 16 ms

You can notice that the second report generation is very quick. This time is explained with use of velocity cache which cache the XML entry to merge with Java model.

Here report is loaded by filling an id :

String reportId = "MyId";

// 1) Load PPTX file by filling Velocity template engine and cache
// it to the registry
InputStream in = PPTXProjectWithVelocityTestPerf.class.getResourceAsStream("PPTXProjectWithVelocity.pptx");

The report is retrieved form the registry with this code :

IXDocReport report2 = XDocReportRegistry.getRegistry().getReport(reportId);

6. More features

Clone this wiki locally