Skip to main content
Windward

Advanced Programming Example

engine-wiki-logo.pngOverview

This article contains information on advanced Java Engine features.

Multiple Report Formats

Assume that all documents created by this method need to have their subject set to the company name, and reports need to be generated in different formats, depending on the user’s request. So we will add these features.

In the case of HTML reports, we also need to make an extra call to set it to use cascading style sheets. The default for HTML is basic HTML 3.2 with no style sheets, and this does not match the final report format as well as HTML with CSS can.

You can find the full command line example here.

 

Static {
    // Initialize Windward Reports. This only needs to be called
    // once so we place it here. This could also go in your
    // applications start-up code.
    ProcessReport.init();
}
public static void MultipleFormats(InputStream xml, InputStream rtf, OutputStream out, int typ) {
    // Create a report process
    // typ uses the ProcessReportAPI.TYP_* static final ints
    ProcessReportAPI proc;
    switch( typ ) {
        case ProcessReportAPI.TYP_PDF :
            proc = new ProcessPdf( xml, rtf, out );
    ​        break;
        case ProcessReportAPI.TYP_RTF :
            proc = new ProcessRtf( xml, rtf, out );
            break;
        case ProcessReportAPI.TYP_HTML :
            proc = new ProcessHtml( xml, rtf, out );
            // for html – use cascading style sheets
            ((ProcessHtmlAPI) proc).setCss(
            ProcessHtmlAPI.CSS_INCLUDE, null, null );
            break;
        case ProcessReportAPI.TYP_TXT :
            proc = new ProcessTxt( xml, rtf, out );
            break;
        default :
            throw new IllegalArgumentException();
    }
    // set the subject
    proc.setSubject( “An Acme, Inc. Report” );
    // run the report process
    proc.process();
    // ensure everything is written out to the stream
    out.flush();
}

 

HTML and Images

The HTML format cannot include embedded images. So when generating an HTML report that has images, you need to take a look at ProcessHtmlAPI..addImageName(), ProcessHtmlAPI.setImagePath(), and ProcessHtmlAPI.getImageNames().

ProcessReport.init()

You do not have to call ProcessReport.init() but you are strongly encouraged to do so when your application first installs. If you do not call it, it will be called when you create your first report object. This method checks your license, initializes logging, and configures the system. By calling it when first loading, you insure that all of these operations will succeed at that time.

You should also catch LicenseException if thrown by Init and handle that. If a LicenseException is thrown, you will not be able to generate reports.

Signing PDF Reports

If you wish to sign a PDF file using a Verisign certificate, please go to:

http://itextpdf.sourceforge.net/howtosign.html

Creating Uuencoded Images

If you are creating uuencoded bitmap images for the <wr:out … type=”BITMAP”/> command, a very useful reference is www.ietf.org/rfc/rfc2045.txt. The program we use for uudecoding is at jakarta.apache.org/commons/codec/.

Data Source Map

You can pass a map to the data source before processing the report. This allows you to add data to the report at runtime on a per report basis. For example, you could add a map entry where the key is username and the value is the name of the user creating the report. Then in the template, you could have

.../Program Files/Windward Studios/Windward Reports/Samples/sample12

JDBC Views

For the JDBC datasource, you can also add views. This can be set up to use the same template for different subsets of data. For example, you could create a view like addView( “customer”, “select * from CUSTOMERS where CUSTOMER_ID = 3”) and then in the template have <wr:query select=”select * from ${customer}”>.

Multiple Data Sources

You can also set Windward Reports to use multiple datasources when processing a template. In this case, the template is processed with one data source, creating an intermediate file. This intermediate file retains all tags that did not match data in the first data file.

The intermediate file is then processed a second time with a second datasource. This again produces an intermediate file. This processing can be repeated any number of times. Each time you process a datasource, you pass in the name of that datasource and all tags assigned to that datasource, and only tags assigned to that datasource, are processed. Any tag for a different datasource is treated as text.

No datasource=”” is a datasource

So what happens if a tag has no datasource=”” attribute? In that case it is applied to the default datasource, which has a name just like any other datasource. But its name is an empty string.

You can actually put datasource=”” in a tag and get the same result. And that is why for a single datasource, you call processData(…, “”);

If you have tags marked with a datasource and you never process that datasource, those tags will appear in the final output.

Here is our code, once again using just PDF output to keep it simple, showing part of a method:

// Create a report process

ProcessPdfAPI proc = new ProcessPdf( rtf, out );

// set the subject

proc.SetSubject( “An Acme, Inc. Report” );

// parse the template file

proc.processSetup();

// merge an xml file with the report

proc.processData( new Dom4jDataSource(new FileInputStream( “data.xml” )), false);

// merge a SQL database with the report.

// This is the last data source being merged.

Proc.processData( new JdbcDataSource("com.microsoft.jdbc.sqlserver.SQLServerDriver", "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Northwind", "mickey", "mouse"), “” );

// generate the final report

proc.processComplete();

// ensure everything is written out to the stream

out.flush();

That’s it. There are additional settings you can set, both in ProcessReportAPI and for each of the individual report types. You should also take a look at the sample programs which are complete implementations of everything shown here and more.

Buffered Input and Output Streams

If you have the option, do not pass buffered streams to Windward Reports. Windward Reports wraps all passed-in streams in a BufferedReader or BufferedWriter. So passing a buffered stream means the data will be buffered twice, which is slower. (A Reader/Writer API is coming but we are waiting for one necessary change in iText.)

The Report Locale

The report locale that is used when creating reports can affect the final report in tricky ways. The locale is used primarily to:

  • Set the default locale in the output report (RTF only)
  • Display the print time field
  • Display the OutTag when the type attribute is set.

If you do not call ProcessReportAPI.setLocale() and do not set report.locale=, then the report locale is taken from the template file. In the template file it is the settings \deflang1033\deflangfe1033 shown in the very first line of the file. The setting 1033 is English US. If you change these (for example to 1031 for German Germany) then it is in German. We have found no way to change this in Word itself!

If you set report.locale= in the WindwardReports.properties file, it will use this locale. This overrides the template file’s locale, but is overridden by a call to ProcessReport.setLocale(). This is set using the form “en_US”.

If you call ProcessReport.setLocale, that will override any other setting. If you pass LOCALE_DEFAULT, it will use the template’s locale. If you pass SYSTEM_LOCALE, then it will use the system locale. The system locale is the locale set by the jvm you are running under. If you call ProcessReport.setLocale( locale ) and the locale you pass in is a Locale object, it will use that locale. There is no way using setLocale() to set it back to the locale defined by report.locale=.

Other Settings

There are two other locale settings in WindwardReports.properties. These settings have no effect on the locale used to generate the report.

system.locale points to one of the resource property files. These determine the text printed to the console when Windward Reports runs.

report.charset is used when creating TXT and HTML reports only and sets the character set to use when generating the report. (And HTML can override this to use utf-8.) This is only used when the output format cannot handle Unicode.

  • Was this article helpful?