Sie sind auf Seite 1von 51

XmlEngine users manual

7th April 2006 Revision 1.0

Visit us at www.openbravo.com

Table of Contents
I.Introduction......................................................................................................................... 3 II.Requirements.................................................................................................................... 4 III.License............................................................................................................................. 5 IV.Installation........................................................................................................................ 6 V.System overview............................................................................................................... 7 VI.Building XmlEngine from the sources............................................................................ 11 VII.Example reports............................................................................................................ 12 VII.1Example 1: Initial sample........................................................................................ 12 VII.2Example 2: Multiple queries.................................................................................... 15 VII.3Example 3: Sum and count functions..................................................................... 16 VII.4Example 4: Defining the database driver and URL in the web.xml......................... 18 VII.5Example 5: Data grouping...................................................................................... 21 VII.6Example 6: Functions at different grouping levels.................................................. 23 VII.7Example 7: Using dynamic SQL queries................................................................ 25 VII.8Example 8: Using parameters................................................................................. 27 VII.9Example 9: Changing attributes with parameters................................................... 29 VII.10Example 10: Replacing a part of a value on an attribute...................................... 31 VII.11Example 11: Using Boolean attributes.................................................................. 33 VII.12Example 12: Creating subreports......................................................................... 35 VII.13Example 13: More functions................................................................................. 38 VII.14Example 14: the EQUAL function......................................................................... 40 VII.15Example 15: Functions and fields format.............................................................. 42 VIII.Application examples................................................................................................... 44 VIII.1Demo application without database....................................................................... 44 VIII.2Demo application with a database connection....................................................... 49

XmlEngine_tutorial - Apr 7, 2006

2/51

I.Introduction

I.

Introduction
XmlEngine is a tool that produces forms and reports according to templates defined in XML, including FOP (Formatting Objects Processor), or HTML mark-up languages. These templates combined with a configuration file define the design of forms and reports and let users to have full control of how the information is organized and displayed. XmlEngine can be used as part of other applications, as we do in Openbravo ERP, or as a servlet as in the examples included in this manual. XmlEngine produces highly customized reports and has advanced report features such as headers and footers, data grouping and data functions.

XmlEngine_tutorial - Apr 7, 2006

3/51

II.Requirements

II.

Requirements
Operating systems supported: Microsoft Windows XP, 2000 or 2003 server.

Linux. It has been successfully tested in Red Hat and Fedora systems.

To run XmlEngine, you need to install the following software: Jakarta-Tomcat version 5.5 or better. It can be downloaded from http://tomcat.apache.org/ Additionally, if you also plan to build applications you need to install this additional software: Java 2 Platform Enterprise Edition 1.5 SDK or better. It can be downloaded from http://java.sun.com/javaee/downloads/ Additionally, if you also plan to build XmlEngine from the sources you need to install this additional software: Apache-ant 1.5. It can be downloaded from http://ant.apache.org/ Operating systems supported: Microsoft Windows XP, 2000 or 2003 server.

Linux. It has been successfully tested in Red Hat and Fedora systems. from

To run Openbravo, you need to install the following software: Jakarta-Tomcat version 5.5. It can be downloaded http://tomcat.apache.org/

Java 2 Platform Standard Edition 5.0. It can be downloaded from http://java.sun.com/j2se/1.5.0/download.jsp Oracle 10g release 2. It can be downloaded from http://www.oracle.com Apache-ant 1.5. It can be downloaded from http://ant.apache.org/

XmlEngine_tutorial - Apr 7, 2006

4/51

III.License

III.

License
The contents of this file are subject to the Openbravo Public License Version 1.0 (the "License"), being the Mozilla Public License version 1.1 with a permitted attribution clause; you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.openbravo.com/legal/license.html Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. The Original Code is Openbravo ERP. The Initial Developer of the Original Code is Openbravo SL All portions are Copyright (C) 2001-2006 Openbravo SL All Rights Reserved.

XmlEngine_tutorial - Apr 7, 2006

5/51

IV.Installation

IV.

Installation
This is a quick-start installation that explains how to install XmlEngine step by step: Make sure that you have installed all the required software from third parties (Tomcat) described in the requirements section of this document.

Unzip the downloaded XmlEngine classes file and copy the files into your Tomcat directory. The examples can be executed using the following URL: webapps\XmlEngine\example-urls.html that contains a list of URL to the different samples

XmlEngine_tutorial - Apr 7, 2006

6/51

V.System overview

V.

System overview
XmlEngine can work as a servlet. There are few examples in this manual that describe in detail its features and possibilities when running in this mode of operation. The following table shows the output generated by a sample using XmlEngine as a servlet. Screen capture of an XmlEngine used as a servlet

Hours of labour
Employee Johann Johann Peter Peter Peter Peter Year 2002 2002 2001 2002 2002 2002 Month 4 5 12 4 5 5 Day 1 15 26 14 2 15 Hours 7 8 5 7 8 3

XmlEngine can also be integrated in external applications. In this scenario, it receives the input parameters from the application (such as a configuration file parameter) and returns an output string. It uses a configuration file and a template, but instead of building an SQL query, the data is taken from the application. Screen capture of an XmlEngine used by an external application

XmlEngine_tutorial - Apr 7, 2006

7/51

V.System overview

Screen capture of an XmlEngine generated report

XmlEngine needs two files to generate reports: An HTML/XML template file that contains the visual representation of how the data is displayed.

An XML configuration file that contains the parameters needed to process the template correctly.

The template can be created using any HTML editor, so the designer can work using a WYSIWYG (What You See Is What You Get) and see how the document looks instead of editing complex template files. This is a simple XML configuration file: <?xml version="1.0"?> <REPORT> <template file="example01.html" /> <structure name="structure1"> <CONNECTION driver = "org.apache.derby.jdbc.EmbeddedDriver" URL = "jdbc:derby:../webapps/XmlEngine/WEBINF/database/tutorialDB"/> <SQL> SELECT Employee, ReportYear, ReportMonth, ReportDay, Hours FROM Hours </SQL> <FIELD id="fieldEmployee">Employee</FIELD> <FIELD id="fieldYear">ReportYear</FIELD> <SECTION id="sectionDetail"/> </structure> <DISCARD id="example"/> </REPORT>
XmlEngine_tutorial - Apr 7, 2006 8/51

V.System overview

These are some of tags commonly used in XmlEngine configuration files: The <report> tag indicates that the block that is going to be defined is a report definition.

The <template> tag indicates through the file attribute which is the name of the template file that is used for this configuration file. The <connection> tag describes how the connection to the database is performed. The driver attribute indicates which database driver is used and the url attribute indicates the location of the database. This connection details can be also specified in the server configuration file. The <sql> tag indicates the SQL query that needs to be performed to get the data from the database. The <field> tag describes the mapping between the database fields and the templates are done.

There are also some common tips and rules for creating XML configuration and HTML/XML template files: You can use the HTML <span> tag to place an id if there is no previous element to place it.

The id attribute values on the document must be unique, so each element can be identified correctly.

XmlEngine_tutorial - Apr 7, 2006

9/51

VI.Building XmlEngine from the sources

VI.

Building XmlEngine from the sources


To build XmlEngine from the sources, we need to compile the Java sources and move to the compiled Java classes to the XmlEngine Tomcats context directory. A build.xml file for Ant is provided with the source distribution. Before executing Ant, check the initial parameters on the build.xml file are the correct for you configuration.

XmlEngine_tutorial - Apr 7, 2006

10/51

VII.Example reports

VII. Example reports


In the following pages we are going to introduce some samples that show how to use XmlEngine as servlet and in an application. The examples have been developed using Derby database but they can be modified to use any database that has a JDBC driver.

VII.1 Example 1: Initial sample


In this first sample, we use a simple query to obtain a table that contains the hours worked by a group of different employees. Output of m example 1

Hours of labour
Employee Johann Johann Peter Peter Peter Peter Year 2002 2002 2001 2002 2002 2002 Month 4 5 12 4 5 5 Day 1 15 26 14 2 15 Hours 7 8 5 7 8 3

Template file: example01.html <html> <head> <title>Hours of labour</title> <META http-equiv="Content-Type" content="text/html" charset=iso-8859-1"> </head> <body bgcolor="#FFFFFF" text="#000000"> <h1 align="center">Hours of labour</h1> <table width="75%" border="1" align="center"> <tr> <td>Employee</td> <td>Year</td> <td>Month</td> <td>Day</td> <td>Hours</td> </tr> <div class="sectionDetail">
XmlEngine_tutorial - Apr 7, 2006 11/51

VII.Example reports

<tr> <td id="fieldEmployee">xFrank</td> <td id="fieldYear">x2002</td> <td id="fieldMonth">x02</td> <td id="fieldDay">x1</td> <td id="fieldHours">x8</td> </tr> </div> <div class="example"> <tr> <td>xJohann</td> <td>x2002</td> <td>x03</td> <td>x2</td> <td>x6</td> </tr> <tr> <td>xAlfred</td> <td>x2002</td> <td>x03</td> <td>x4</td> <td>x8</td> </tr> </div> </table> </body> </html> Configuration file: example01.xml <?xml version="1.0"?> <REPORT> <template file="example01.html" /> <structure name="structure1"> <CONNECTION driver = "org.apache.derby.jdbc.EmbeddedDriver" URL = "jdbc:derby:../webapps/XmlEngine/WEBINF/database/tutorialDB"/> <SQL> SELECT Employee, ReportYear, ReportMonth, ReportDay, Hours FROM Hours </SQL> <FIELD id="fieldEmployee">Employee</FIELD> <FIELD id="fieldYear">ReportYear</FIELD>

XmlEngine_tutorial - Apr 7, 2006

12/51

VII.Example reports

<FIELD id="fieldMonth">ReportMonth</FIELD> <FIELD id="fieldDay">ReportDay</FIELD> <FIELD id="fieldHours">Hours</FIELD> <SECTION id="sectionDetail"/> </structure> <DISCARD id="example"/> </REPORT> In this example, the SQL sentence contained between <sql> tags obtains all the records of the Hours table: <SQL> SELECT Employee, ReportYear, ReportMonth, ReportDay, Hours FROM Hours </SQL> The mapping between fields and columns is described by the field tag as follows: <FIELD id="fieldEmployee">Employee</FIELD> <FIELD id="fieldYear">ReportYear</FIELD> <FIELD id="fieldMonth">ReportMonth</FIELD> <FIELD id="fieldDay">ReportDay</FIELD> <FIELD id="fieldHours">Hours</FIELD> The first line indicates that the Employee column will we displayed in the field with id fieldEmployee. The SQL query can return any number of records. For each one, the code of the template that is inside the <div> tag has the same identifier than the <section> tag of the configuration file. <SECTION id="sectionDetail"/> If there are more detail datas in the template than the strictly need these can be ignored from final result using the <discard> tag in the configuration file. <DISCARD id="example"/> This tag indicates that all lines in the template which are between tags with the same id of the <discard> tag are ignored.

VII.2 Example 2: Multiple queries


This example shows how multiple queries can be integrated in a single output document.

XmlEngine_tutorial - Apr 7, 2006

13/51

VII.Example reports

Output of example 2

Hours of labour
Employee Johann Johann Peter Peter Peter Peter Year 2002 2002 2001 2002 2002 2002 Month 4 5 12 4 5 5 Day 1 15 26 14 2 15 Hours 7 8 5 7 8 3

Configuration file: example02.xml <?xml version="1.0"?> <REPORT> <template file="example02.html" /> <structure name="structure1"> <CONNECTION driver = "org.apache.derby.jdbc.EmbeddedDriver" URL = "jdbc:derby:../webapps/XmlEngine/WEBINF/database/tutorialDB"/> <SQL> SELECT Employee, ReportYear, ReportMonth, ReportDay, Hours FROM Hours </SQL> <FIELD <FIELD <FIELD <FIELD <FIELD id="fieldEmployee">Employee</FIELD> id="fieldYear">ReportYear</FIELD> id="fieldMonth">ReportMonth</FIELD> id="fieldDay">ReportDay</FIELD> id="fieldHours">Hours</FIELD>

<SECTION id="sectionDetail"/> </structure> <structure name="structure2"> <CONNECTION driver = "org.apache.derby.jdbc.EmbeddedDriver" URL = "jdbc:derby:../webapps/XmlEngine/WEBINF/database/tutorialDB"/> <SQL>
XmlEngine_tutorial - Apr 7, 2006 14/51

VII.Example reports

SELECT distinct Employee FROM Hours </SQL> <FIELD id="fieldEmployee2">Employee</FIELD> <SECTION id="sectionDetailEmployee"/> </structure> <DISCARD id="example"/> </REPORT> Each query is contained between a <structure> tag and can be performed on different databases. On the HTML/XML template document, there is a list of employees that refer to the elements of the new structure. Template file: example02.html <center> <h2>Employees</h2> <ul> <div class="sectionDetailEmployee"> <li id="fieldEmployee2">xFrank</li> </ul> </center>

VII.3 Example 3: Sum and count functions


This example shows how sum and count functions work. Output of example 3

Hours of labour - 6 parts


Employee Johann Johann Peter Peter Peter Peter Total Year 2002 2002 2001 2002 2002 2002 Month 4 5 12 4 5 5 Day 1 15 26 14 2 15 Hours 7 8 5 7 8 3 38.0

Configuration file: example03.xml <?xml version="1.0"?> <REPORT> <template file="example03.html" />


XmlEngine_tutorial - Apr 7, 2006 15/51

VII.Example reports

<structure name="structure1"> <CONNECTION driver = "org.apache.derby.jdbc.EmbeddedDriver" URL = "jdbc:derby:../webapps/XmlEngine/WEBINF/database/tutorialDB"/> <SQL> SELECT Employee, ReportYear, ReportMonth, ReportDay, Hours FROM Hours </SQL> <FIELD <FIELD <FIELD <FIELD <FIELD id="fieldEmployee">Employee</FIELD> id="fieldYear">ReportYear</FIELD> id="fieldMonth">ReportMonth</FIELD> id="fieldDay">ReportDay</FIELD> id="fieldHours">Hours</FIELD>

<FUNCTION id="funcCountParts" name="COUNT">Hours</FUNCTION> <FUNCTION id="funcSumHours" name="SUM">Hours</FUNCTION> <SECTION id="sectionDetail"/> </structure> <structure name="structure2"> <CONNECTION driver = "org.apache.derby.jdbc.EmbeddedDriver" URL = "jdbc:derby:../webapps/XmlEngine/WEBINF/database/tutorialDB"/> <SQL> SELECT distinct Employee FROM Hours </SQL> <FIELD id="fieldEmployee2">Employee</FIELD> <SECTION id="sectionDetailEmployee"/> </structure> <DISCARD id="example"/> </REPORT>

XmlEngine_tutorial - Apr 7, 2006

16/51

VII.Example reports

In this example, we use the SUM and COUNT functions to perform operations on data retrieved from the database and include the results in the report. To use this kind of functionality, we use the <function> tag as shown below: <FUNCTION id="funcCountParts" name="COUNT">Hours</FUNCTION> <FUNCTION id="funcSumHours" name="SUM">Hours</FUNCTION> You can get also an average value instead of a sum using the MED function: <FUNCTION id="funcMedHours" name="MED">Hours</FUNCTION>

VII.4 Example 4: Defining the database driver and URL in the web.xml
This example shows how to use define the driver and URL used to connect to the database in the initializing parameters of the XmlEngine servlet. Output of example 4

Hours of labour - 6 parts


Employee Johann Johann Peter Peter Peter Peter Total Year 2002 2002 2001 2002 2002 2002 Month 4 5 12 4 5 5 Day 1 15 26 14 2 15 Hours 7 8 5 7 8 3 38.0

The default database connection parameters can be setup in the web.xml file. For example, to indicate that we want to use the driver org.apache.derby.jdbc.EmbeddedDriver and the URL of the database is jdbc:derby:../webapps/XmlEngine/WEB-INF/database/tutorialDB we add the following lines to the web.xml configuration file: <init-param> <param-name>driver</param-name> <paramvalue>org.apache.derby.jdbc.EmbeddedDriver</param -value> </init-param>
XmlEngine_tutorial - Apr 7, 2006 17/51

VII.Example reports

<init-param> <param-name>URL</param-name> <paramvalue>jdbc:derby:../webapps/XmlEngine/WEBINF/database/tutorialDB</param-value> </init-param> Using this approach, there is no need to specify the connection details in our servlets. The following example uses the connection details specified in web.xml to perform the connection. Configuration file: Example04.xml <?xml version="1.0"?> <REPORT> <template file="list4.html" /> <structure name="structure1"> <SQL> SELECT Employee, ReportYear, ReportMonth, ReportDay, Hours FROM Hours </SQL> <FIELD <FIELD <FIELD <FIELD <FIELD id="fieldEmployee">Employee</FIELD> id="fieldYear">ReportYear</FIELD> id="fieldMonth">ReportMonth</FIELD> id="fieldDay">ReportDay</FIELD> id="fieldHours">Hours</FIELD>

<FUNCTION id="funcCountParts" name="COUNT">Hours</FUNCTION> <FUNCTION id="funcSumHours" name="SUM">Hours</FUNCTION> <SECTION id="sectionDetail"/> </structure> </REPORT> Template file: example04.html <html> <head> <title>Hours of labour</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
XmlEngine_tutorial - Apr 7, 2006 18/51

VII.Example reports

</head> <body bgcolor="#FFFFFF" text="#000000"> <h1 align="center">Hours of labour - <span id="funcCountParts">xx2</span> parts</h1> <table width="75%" border="1" align="center"> <tr> <td>Employee</td> <td>Year</td> <td>Month</td> <td>Day</td> <td>Hours</td> </tr> <div class="sectionDetail"> <tr> <td id="fieldEmployee">xFrank</td> <td id="fieldYear">x2002</td&g; <td id="fieldMonth">x02</td> <td id="fieldDay">x1</td> <td id="fieldHours">x8</td> </tr> </div> <tr> <td>Total</td> <td colspan="4" id="funcSumHours" align="right">x34</td> </tr> </table> </body> </html>

VII.5 Example 5: Data grouping


This example shows how to use data grouping in our output reports allow grouping by different criteria. Output of example 5

Hours of labour - 6 parts


Year: 2001 Month: 12

Employee

Year

Month

Day

Hours

XmlEngine_tutorial - Apr 7, 2006

19/51

VII.Example reports

Peter Total

2001

12

26

5 5.0

Year: 2002
Month: 4

Employee Peter Johann Total

Year 2002 2002

Month 4 4

Day 14 1

Hours 7 7 14.0

Month: 5

Employee Peter Peter Johann Total

Year 2002 2002 2002

Month 5 5 5

Day 15 2 15

Hours 3 8 8

On the previous documents, there was just a section in the XML configuration file called sectionDetail. This section was repeated for each data record on the query. However, it is possible to have more than a section on a structure. The field attribute on the <section> tag indicates how the data is grouped. Notice from the output of this example that the sum function is now applied to each month. Configuration file: Example05.xml <?xml version="1.0"?> <REPORT> <template file="example05.html" /> <structure name="structure1"> <SQL> SELECT Employee, ReportYear, ReportMonth, ReportDay, Hours FROM Hours ORDER BY ReportYear, ReportMonth </SQL> <FIELD <FIELD <FIELD <FIELD <FIELD
XmlEngine_tutorial - Apr 7, 2006

id="fieldEmployee">Employee</FIELD> id="fieldYear">ReportYear</FIELD> id="fieldMonth">ReportMonth</FIELD> id="fieldDay">ReportDay</FIELD> id="fieldHours">Hours</FIELD>


20/51

VII.Example reports

<FUNCTION id="funcCountParts" name="COUNT">Hours</FUNCTION> <FUNCTION id="funcSumHours" name="SUM">Hours</FUNCTION> <SECTION id="sectionYear" field="ReportYear"/> <SECTION id="sectionMonth" field="ReportMonth"/> <SECTION id="sectionDetail"/> </structure> </REPORT>

VII.6 Example 6: Functions at different grouping levels


This example shows how to combine functions and grouping. It computes a result per every year and then sums the results of all years. Output of example 6

Hours of labour - 6 parts Year: 2001 Total year: 5.0


Month: 12

Employee Peter Total

Year 2001

Month 12

Day 26

Hours 5 5.0

Year: 2002 Total year: 33.0


Month: 4

Employee Peter Johann Total

Year 2002 2002

Month 4 4

Day 14 1

Hours 7 7 14.0

Month: 5

XmlEngine_tutorial - Apr 7, 2006

21/51

VII.Example reports

Employee Peter Peter Johann Total


Total general: 38.0

Year 2002 2002 2002

Month 5 5 5

Day 15 2 15

Hours 3 8 8 19.0

Configuration file: Example06.xml <?xml version="1.0"?> <REPORT> <template file="example06.html" /> <structure name="structure1"> <SQL> SELECT Employee, ReportYear, ReportMonth, ReportDay, Hours FROM Hours ORDER BY ReportYear, ReportMonth </SQL> <FIELD <FIELD <FIELD <FIELD <FIELD id="fieldEmployee">Employee</FIELD> id="fieldYear">ReportYear</FIELD> id="fieldMonth">ReportMonth</FIELD> id="fieldDay">ReportDay</FIELD> id="fieldHours">Hours</FIELD>

<FUNCTION id="funcCountParts" name="COUNT">Hours</FUNCTION> <FUNCTION id="funcSumHours" name="SUM">Hours</FUNCTION> <FUNCTION id="funcSumHoursYear" name="SUM">Hours</FUNCTION> <FUNCTION id="funcSumHoursTotal" name="SUM">Hours</FUNCTION> <SECTION id="sectionYear" field=ReportYear/> <SECTION id="sectionMonth" field=ReportMonth/> <SECTION id="sectionDetail"/> </structure> </REPORT>

XmlEngine_tutorial - Apr 7, 2006

22/51

VII.Example reports

In the example 5 there was only a sum function. In this new example, there are two sum functions at different levels of the structure. Those operations perform sums per year and generate a total result. To include a sum per year it is necessary to define a new function in the XML configuration file: <FUNCTION id="funcSumHoursYear" name="SUM">Hours</FUNCTION> In the template we have to include the id of this new sum between the blocks that enclose the <div> tags of the sections sectionMonth and sectionYear. <div class="sectionYear"> <h2 align="center">Year: <span id="fieldYear">x2000</span><h2> Total year: <span id="funcSumHoursYear">xx10</span> <div id="sectionMonth"> If a general total result is also need it, we just need to insert another sum function in the XML configuration file: <FUNCTION id="funcSumHoursTotal" name="SUM">Hours</FUNCTION> The id of this sum must be placed out of the <div> tag of the sectionYear. In the HTML/XML template, the following line needs to be added: <h3>Total general: <span id="funcSumHoursTotal">xx10</span></h3> In the HTML file, the id is placed in a new <span> tag because there is no previous element that can be use to include it.

VII.7 Example 7: Using dynamic SQL queries


This example shows how to use dynamic SQL queries. Until now the SQL queries that we used were static. XmlEngine can also work with dynamic SQL queries. This can be archived adding the argument name and its values to the URL address. Output of example 7

Hours of labour - 1 parts


Year: 2001 Total year: 5.0 Month: 12
XmlEngine_tutorial - Apr 7, 2006 23/51

VII.Example reports

Employee Peter Total


Total general: 5.0

Year 2001

Month 12

Day 26

Hours 5 5.0

Configuration file: Example07.xml <?xml version="1.0"?> <REPORT> <template file="example07.html" /> <structure name="structure1"> <SQL> SELECT Employee, ReportYear, ReportMonth, ReportDay, Hours FROM Hours WHERE Year=? ORDER BY ReportYear, ReportMonth </SQL> <PARAMETER_SQL name = "year" type = "string" default = "2000" /> <FIELD <FIELD <FIELD <FIELD <FIELD id="fieldEmployee">Employee</FIELD> id="fieldYear">ReportYear</FIELD> id="fieldMonth">ReportMonth</FIELD> id="fieldDay">ReportDay</FIELD> id="fieldHours">Hours</FIELD>

<FUNCTION id="funcCountParts" name="COUNT">Hours</FUNCTION> <FUNCTION id="funcSumHours" name="SUM">Hours</FUNCTION> <FUNCTION id="funcSumHoursYear" name="SUM">Hours</FUNCTION> <FUNCTION id="funcSumHoursTotal" name="SUM">Hours</FUNCTION> <SECTION id="sectionYear" field=ReportYear/> <SECTION id="sectionMonth" field=ReportMonth/> <SECTION id="sectionDetail"/> </structure> </REPORT> In the previous sample, we had the following XML configuration file: <SQL>
XmlEngine_tutorial - Apr 7, 2006 24/51

VII.Example reports

SELECT Employee, ReportYear, ReportMonth, ReportDay, Hours FROM Hours WHERE Year=? ORDER BY ReportYear, ReportMonth </SQL> <PARAMETER_SQL name = "year" type = "string" default = "2000" /> <FIELD id="fieldEmployee">Employee</FIELD> The <parameter_sql> defines dynamic SQL parameters. It accepts the following parameters: name: indicates the parameter on the query (as appears in the URL).

type: the argument type: string or integer. default: the default value of the parameter in case it is not defined in the URL.

For example: <PARAMETER_SQL name = "year" type = "string" default = "2000" /> The URL without arguments: http://localhost:8080/XmlEngine/ XmlEngine.html?report=examples\example07 Returns no results in the output because the default value 2000 is used and there is no data for this year. However, if we use this URL: http://localhost:8080/XmlEngine/XmlEngine.html? report=examples\example07&year=2001 It will correctly report the year 2001 report.

VII.8 Example 8: Using parameters


This example shows how to use parameters in a generated report from an URL. Output of example 8

Hours of labour - 6 parts


Print Date: 02-02-2005 Employee Year Month Day Hours

XmlEngine_tutorial - Apr 7, 2006

25/51

VII.Example reports

Johann Johann Peter Peter Peter Peter Total

2002 2002 2001 2002 2002 2002

4 5 12 4 5 5

1 15 26 14 2 15

7 8 5 7 8 3 38.0

Configuration file: Example08.xml <?xml version="1.0"?> <REPORT> <template file="example08.html" /> <PARAMETER id="param1" name = "date" default=" "/> <structure name="structure1"> <SQL> SELECT Employee, ReportYear, ReportMonth, ReportDay, Hours FROM Hours </SQL> <FIELD <FIELD <FIELD <FIELD <FIELD id="fieldEmployee">Employee</FIELD> id="fieldYear">ReportYear</FIELD> id="fieldMonth">ReportMonth</FIELD> id="fieldDay">ReportDay</FIELD> id="fieldHours">Hours</FIELD>

<FUNCTION id="funcCountParts" name="COUNT">Hours</FUNCTION> <FUNCTION id="funcSumHours" name="SUM">Hours</FUNCTION> <SECTION id="sectionDetail"/> </structure> </REPORT> It is possible to introduce a parameter on a report from an URL. It works as a parameter in the XML document and as a field in the HTML document, as in the other examples. Using <parameter> tag, the supported attributes are: id: identifies the place on the template where it will be shown

name: Identifies the parameter on the query, it will appear on the URL. default: the default value of the parameter in case is not defined in the URL.
26/51

XmlEngine_tutorial - Apr 7, 2006

VII.Example reports

As we can see in this example template: <PARAMETER id=param1 name = "date" default=" "/> The default value for this parameter is "". The destination where the parameter will be written is indicated in the HTML document. We can invoke this example with this URL: http://localhost:8080/XmlEngine/XmlEngine.html? report=examples\example08&date=06-02-2002

VII.9 Example 9: Changing attributes with parameters


This example shows how to use change the value of an attribute using parameters. Output of example 9 Hours of labour - 6 parts Print Date: 02-02-2005 Employee Johann Johann Peter Peter Peter Peter Total Year 2002 2002 2001 2002 2002 2002 Month 4 5 12 4 5 5 38.0 Day 1 15 26 14 2 15 Hours 7 8 5 7 8 3

Configuration file: Example09.xml <?xml version="1.0"?> <REPORT> <template file="example09.html" /> <PARAMETER id="param1" name = "date" default=" "/> <PARAMETER id="funcSumHours" name = "cols" attribute="colspan"/> <structure name="structure1"> <SQL> SELECT Employee, ReportYear, ReportMonth, ReportDay, Hours FROM Hours
XmlEngine_tutorial - Apr 7, 2006 27/51

VII.Example reports

</SQL> <FIELD <FIELD <FIELD <FIELD <FIELD id="fieldEmployee">Employee</FIELD> id="fieldYear">ReportYear</FIELD> id="fieldMonth">ReportMonth</FIELD> id="fieldDay">ReportDay</FIELD> id="fieldHours">Hours</FIELD>

<FUNCTION id="funcCountParts" name="COUNT">Hours</FUNCTION> <FUNCTION id="funcSumHours" name="SUM">Hours</FUNCTION> <SECTION id="sectionDetail"/> </structure> </REPORT> In previous examples we have seen how to change the tag value in the HTML document using <field>, <function> and <parameter> tags. Now, we are going to see how to change the value of an attribute. As example, we are going to use a parameter to indicate the number of columns that will be used in the total result cell. Currently, the line looks like: <td colspan="4" id="funcSumHours" align="right">x34</td> To change the colspan attribute using a parameter we add the following file in the XML configuration file: <PARAMETER id="funcSumHours" name = "cols" attribute="colspan"/> The name attribute is the name of the parameter that we have to include in the URL. To invoke the servlet, we use the following URL with the argument cols set to value 2: http://localhost:8080/XmlEngine/ XmlEngine.html? report=examples\example09&date=06-022002&cols=2 We can see that the cell where the total is now only takes 2 columns. In this example it we have used the <parameter> tag but the same functionality is available in the <field> and <function> tags.

XmlEngine_tutorial - Apr 7, 2006

28/51

VII.Example reports

VII.10 Example 10: Replacing a part of a value on an attribute


This example shows how to replace a part of a value on an attribute of an HTML tag. Output of example 10

Hours of labour - 6 parts


Personal page Personal page Personal page Personal page Personal page Personal page Total Employee Johann Johann Peter Peter Peter Peter Year 2002 2002 2001 2002 2002 2002 Month 4 5 12 4 5 5 Day Hours 1 7 15 8 26 5 14 7 2 8 15 3 38.0

Sometimes there is the need to change the values of attributes. For example, to update a hyperlink. The following example is based on example4 and introduces a hyperlink that includes the employer name. We have to add another column to the table structure in the HTML document. Configuration file: Example10.xml <?xml version="1.0"?> <REPORT> <template file="example10.html" /> <structure name="structure1"> <SQL> SELECT Employee, ReportYear, ReportMonth, ReportDay, Hours FROM Hours </SQL> <FIELD id="fieldEmployee">Employee</FIELD> <FIELD id="fieldPageEmployee" attribute="href" replace="yy">Employee</FIELD> <FIELD id="fieldYear">ReportReportYear</FIELD> <FIELD id="fieldMonth">ReportReportMonth</FIELD> <FIELD id="fieldDay">ReportReportDay</FIELD> <FIELD id="fieldHours">Hours</FIELD>

XmlEngine_tutorial - Apr 7, 2006

29/51

VII.Example reports

<FUNCTION id="funcCountParts" name="COUNT">Hours</FUNCTION> <FUNCTION id="funcSumHours" name="SUM">Hours</FUNCTION> <SECTION id="sectionDetail"/> </structure> </REPORT> We add another column in the HTML/XML template document where the employee link is displayed: <td><a id="fieldPageEmployee" href="XmlEngine?report=examples/list12sub&employe e=yy">Personal page</a></td> If nothing else is changed, the output will have as many rows as the data record has but all of them with the same hyperlink argument: code=yy. It is necessary to change the hyperlink argument, using the replace=yy attribute. Using this method, we have changed part of an attribute on the hyperlink tag, but we maintain another part. Change example10.xml near <field> to the following: <FIELD id="fieldPageEmployee" attribute="href"replace="yy">Employee</FIELD> This line indicates that on each line, yy must be remplaced by the employee value where id=fieldPageEmployee if yy is part of <href> tag attribute. We can invoke the example with the following URL: http://localhost:8080/XmlEngine/ XmlEngine.html?report=examples\example10 If we click on the link we will obtain another page with the total hours worked by the employee.

VII.11 Example 11: Using Boolean attributes


This example shows how to use boolean attributes in reports. Output of example 11

Hours of labour - 6 parts


Check Employee Johann Year 2002 Month 4 Day 1 Hours 7

XmlEngine_tutorial - Apr 7, 2006

30/51

VII.Example reports

Johann Peter Peter Peter Peter Total

2002 2001 2002 2002 2002

5 12 4 5 5

15 26 14 2 15 38.0

8 5 7 8 3

Configuration file: Example11.xml <?xml version="1.0"?> <REPORT> <template file="example11.html" /> <PARAMETER id="paramYear" name = "checkYear" /> <structure name="structure1"> <SQL> SELECT Employee, ReportYear, ReportMonth, ReportDay, Hours FROM Hours </SQL> <FIELD id="fieldCheckYear" boolean="checked" ithId="paramYear">ReportYear</FIELD> <FIELD id="fieldEmployee">Employee</FIELD> <FIELD id="fieldYear">ReportYear</FIELD> <FIELD id="fieldMonth">ReportMonth</FIELD> <FIELD id="fieldDay">ReportDay</FIELD> <FIELD id="fieldHours">Hours</FIELD> <FUNCTION id="funcCountParts" name="COUNT">Hours</FUNCTION> <FUNCTION id="funcSumHours" name="SUM">Hours</FUNCTION> <SECTION id="sectionDetail"/> </structure> </REPORT> Unlike in HTML, XML requires a value for each attribute. For example, CheckOption only has an attribute when is checked but it does not when is not checked. We can indicate when a boolean attribute is true or not on the output using <field>, <function> and <parameter> tags. These tags accept the following attributes: attribute: identifies the attribute of the template.

boolean: indicates the name of the attribute that will work as a boolean.

XmlEngine_tutorial - Apr 7, 2006

31/51

VII.Example reports

withId: The boolean attribute is only shown if both the attribute and the withId value match.

Take the example 4. Imagine that we need to include a new column with a checkbox field and we only want to check the values that match with the year indicated on the URL. We will need to add the following line: <td> <input id="fieldCheckYear" type="checkbox" name="checkbox" value="checkbox" checked></td> We now have a new column with checkOptions. We can check the values from a specific year including a <parameter> tag in HTML/XML template file like follows: <PARAMETER id="paramYear" name = "checkYear" /> This allows the servlet to receive the checkYear argument from the URL. Now, we add the <field> elements: <FIELD id="fieldCheckYear" boolean="checked" withId="paramYear"> Year </FIELD> We can invoke the example with the following URL: http://localhost:8080/XmlEngine/ XmlEngine.html? report=examples\example11&checkYear=2001 The output shows as checked all the records from 2001.

VII.12 Example 12: Creating subreports


This example shows how to create subreports in a report. Output of example 12

Hours of labour - 6 parts


Employee Johann Johann Peter Peter Year Month Day Hours Total 2002 4 2002 5 2001 12 2002 4 1 15 26 14 7 8 5 7 Total hours Total hours Total hours Total hours 15.0 15.0 23.0 23.0

XmlEngine_tutorial - Apr 7, 2006

32/51

VII.Example reports

Peter Peter Total

2002 5 2002 5

2 15

8 3 38.0

Total hours Total hours

23.0 23.0

In some XML documents we may need to insert another report in the XML document. It can be independent from the main report. Configuration file: Example12.xml <?xml version="1.0"?> <REPORT> <template file="example12.html" /> <structure name="structure1"> <SQL> SELECT Employee, ReportYear, ReportMonth, ReportDay, Hours FROM Hours </SQL> <FIELD <FIELD <FIELD <FIELD <FIELD id="fieldEmployee">Employee</FIELD> id="fieldYear">ReportYear</FIELD> id="fieldMonth">ReportMonth</FIELD> id="fieldDay">ReportDay</FIELD> id="fieldHours">Hours</FIELD>

<FUNCTION id="funcCountParts" name="COUNT">Hours</FUNCTION> <FUNCTION id="funcSumHours" name="SUM">Hours</FUNCTION> <SECTION id="sectionDetail"/> <SUBREPORT id="reportTotalEmployee" name="manual\list12sub" report="manual\list12sub"> <ARGUMENT name = "employee" withId="fieldEmployee"/> </SUBREPORT> </structure> </REPORT>

XmlEngine_tutorial - Apr 7, 2006

33/51

VII.Example reports

We are going to take the example 4 as a starting point and add an additional column that shows a subreport with the hours worked by that employee. The subreport is contained in the files list12sub.xml and list12sub.srpt. To invoke the servlet we use the following URL: http://localhost:8080/xmlEngine/XmlEngine.html?re port=examples\example2sub&employee=Peter And we obtain the following result:
Total hours 23.0

In configuration file we add the next lines at the same level than <field>, <function> or <section> tags: <SUBREPORT id="reportTotalEmployee" name="examples\example12sub" report="examples\example12sub"> <ARGUMENT name = "employee" withId="fieldEmployee"/> </SUBREPORT> As you can see, we have inserted the <subreport> tag that accepts the following attributes: id: identifies the place on the template where it will be shown

name: Identifies the parameter on the query, it will appear on the URL. report: the name of the file where is the subreport, the same way it is used in the URL call.

The <argument> tag sends arguments to the subreport. In the subreport, we receive the <parameter> or <parameter_sql> tags. The <argument> tag accepts the following attributes: name: identifies the argument in the subreport; a parameter with the same name must exist in the report.

withId: indicates where the value is obtained.

The change in the HTML template file is the same than for the tags that replace a value (<field>, <function> or <parameter>). In this case a new column in the table is inserted adding at the end of the first table row the following line: <td>Total</td> and at the end of the second table row the line: <td id="reportTotalEmployee">xTotal</td> Notice that the value of this id is the same than that the id attribute of <subreport> tag in the configuration file. The same way that we have send a value
XmlEngine_tutorial - Apr 7, 2006 34/51

VII.Example reports

for a SQL parameter, it can be sent various parameters or receive the data in parameters just to show them. Subreport configuration file: example12sub.xml <?xml version="1.0"?> <REPORT> <template file="list12sub.srpt" /> <structure name="structure1"> <SQL> SELECT Sum(Hours) as sumHours FROM Hours WHERE Employee = ? </SQL> <PARAMETER_SQL name = "employee" type = "string" default=" "/> <FIELD id="fieldHours">sumHours</FIELD> <SECTION id="sectionDetail"/> </structure> </REPORT> Subreport template file: example12sub.srpt <table width="75%" border="1" align="center"> <tr> <td>Total hours</td> <div class="sectionDetail"> <td id="fieldHours">x8</td> </div> </tr> </table>

VII.13 Example 13: More functions


This example shows how to use some additional functions. Output of example 13

Hours of labour - 6 parts


Employee Johann Johann Peter Peter Year 2002 2002 2001 2002 Month 4 5 12 4 Day 1 15 26 14 Month/Day 4.0 0.3333333333333333 0.46153846153846156 0.2857142857142857 Hours 7 8 5 7 Minutes 420.0 480.0 300.0 420.0

XmlEngine_tutorial - Apr 7, 2006

35/51

VII.Example reports

Peter Peter Total

2002 5 2002 5

2 15

2.5 8 0.3333333333333333 3 38.0

480.0 180.0

Configuration file: Example13.xml <?xml version="1.0"?> <REPORT> <template file="example13.html" /> <structure name="structure1"> <SQL> SELECT Employee, ReportYear, ReportMonth, ReportDay, Hours FROM Hours </SQL> <FIELD <FIELD <FIELD <FIELD <FIELD id="fieldEmployee">Employee</FIELD> id="fieldYear">ReportYear</FIELD> id="fieldMonth">ReportMonth</FIELD> id="fieldDay">ReportDay</FIELD> id="fieldHours">Hours</FIELD>

<FUNCTION id="funcCountParts" name="COUNT">Hours</FUNCTION> <FUNCTION id="funcSumHours" name="SUM">Hours</FUNCTION> <PARAMETER id="param60" name="Param60" default="60"/> <FUNCTION id="funcDivi" name="DIVIDE" arg1="fieldMonth" arg2="fieldDay"/> <FUNCTION id="funcMinutes" name="MULTIPLY" arg1="fieldHours" arg2="param60"/> <SECTION id="sectionDetail"/> </structure> </REPORT> In other examples we have seen the SUM, COUNT and MED functions. The only difference between those and DIVIDE, MOD, MULTIPLY, ADD is in the arguments they receive. On this example we use MULTIPLY and DIVIDE functions and we also add two new columns in the HTML template: <td>Month/Day</td> <td>Minutes</td>

XmlEngine_tutorial - Apr 7, 2006

36/51

VII.Example reports

And for each row of data: <td id="funcDivi">x480</td> <td id="funcMinutes">x480</td> In the XML document: <PARAMETER id="param60" name="Param60" default="60"/> <FUNCTION id="funcDivi" name="DIVIDE" arg1="fieldMonth" arg2="fieldDay"/> <FUNCTION id="funcMinutes" name="MULTIPLY" arg1="fieldHours" arg2="param60"/> To invoke the servlet you can use the following URL: http://localhost:8080/xmlEngine/XmlEngine.html?re port=examples\example13

VII.14 Example 14: the EQUAL function


This example shows how to use the equal function. Output of example 14

Hours of labour - 6 parts


N. 1.0 2.0 3.0 4.0 5.0 6.0 Total Employee Johann Johann Peter Peter Peter Peter Year 2002 2002 2001 2002 2002 2002 Month 4 5 12 4 5 5 Day Hours 1 7 15 8 26 5 14 7 2 8 15 3 38.0 Accumulate 7.0 15.0 20.0 27.0 35.0 38.0

Configuration file: Example14.xml <?xml version="1.0"?> <REPORT> <template file="example14.html" /> <structure name="structure1"> <SQL>

XmlEngine_tutorial - Apr 7, 2006

37/51

VII.Example reports

SELECT Employee, ReportYear, ReportMonth, ReportDay, Hours FROM Hours </SQL> <FIELD <FIELD <FIELD <FIELD <FIELD id="fieldEmployee">Employee</FIELD> id="fieldYear">ReportYear</FIELD> id="fieldMonth">ReportMonth</FIELD> id="fieldDay">ReportDay</FIELD> id="fieldHours">Hours</FIELD>

<FUNCTION id="funcCountParts" name="COUNT">Hours</FUNCTION> <FUNCTION id="funcSumHours" name="SUM">Hours</FUNCTION> <FUNCTION id="funcCounter" name="EQUAL" arg1="funcCountParts"></FUNCTION> <FUNCTION id="funcAcumulate" name="EQUAL" arg1="funcSumHours"></FUNCTION> <SECTION id="sectionDetail"/> </structure> </REPORT> The EQUAL function obtains counters and calculates accumulated sums in lists. For example, if we want to enumerate the lines of a list and have a column with the accumulated value we need to include two equal functions. <FUNCTION id="funcCounter" name="EQUAL" arg1="funcCountParts"></FUNCTION> <FUNCTION id="funcAcumulate" name="EQUAL" arg1="funcSumHours"></FUNCTION> The first function calculates the actual value in the funcCountParts function that indicates the line number. The second function represents the current value in the line of funcSumHours function.

VII.15 Example 15: Functions and fields format


This example shows how to use fields and functions. Output of example 15

Hours of labour - 6 parts

XmlEngine_tutorial - Apr 7, 2006

38/51

VII.Example reports

N. 1 2 3 4 5 6 Total

Employee Johann Johann Peter Peter Peter Peter

Year 2002 2002 2001 2002 2002 2002

Month 4 5 12 4 5 5

Day Hours 1 7 15 8 26 5 14 7 2 8 15 3 38.0

Accumulate 7.0 15.0 20.0 27.0 35.0 38.0

Configuration file: Example15.xml <?xml version="1.0"?> <REPORT> <template file="example15.html" /> <structure name="structure1"> <SQL> SELECT Employee, ReportYear, ReportMonth, ReportDay, Hours FROM Hours </SQL> <FIELD <FIELD <FIELD <FIELD <FIELD id="fieldEmployee">Employee</FIELD> id="fieldYear">ReportYear</FIELD> id="fieldMonth">ReportMonth</FIELD> id="fieldDay">ReportDay</FIELD> id="fieldHours">Hours</FIELD>

<FUNCTION id="funcCountParts" name="COUNT">Hours</FUNCTION> <FUNCTION id="funcSumHours" name="SUM">Hours</FUNCTION> <FUNCTION id="funcCounter" name="EQUAL" arg1="funcCountParts" format="integerInform"></FUNCTION> <FUNCTION id="funcAcumulate" name="EQUAL" arg1="funcSumHours"></FUNCTION> <SECTION id="sectionDetail"/> </structure> </REPORT> In the previous example the line number appears in decimal format. XmlEngine defines different formats to apply to output values. This can be

XmlEngine_tutorial - Apr 7, 2006

39/51

VII.Example reports

achieved with the format attribute of <field> and <function> tags.

XmlEngine_tutorial - Apr 7, 2006

40/51

VIII.Application examples

VIII. Application examples


VIII.1 Demo application without database
This demo application shows an XmlEngine application that does not use a database connection. This example shows every category from a log4j configuration file, updating the selected items. In the following output you can see the categories and their priorities. You can use the user interface elements to change the selection. Screen capture of the Log4J demo application
Log4j Demo

We have an HTML file (SetPriority.html): <FORM METHOD=GET ACTION="#"> <SELECT NAME="priority" SIZE="5"> <OPTION value="DEBUG" selected>DEBUG</OPTION> <OPTION value="INFO">INFO</OPTION> <OPTION value="WARN">WARN</OPTION> <OPTION value="ERROR">ERROR</OPTION> <OPTION value="FATAL">FATAL</OPTION> </SELECT> <INPUT TYPE="submit" value="Priority Update"> <p> <TABLE> <TR> <TH>Category</TH> <TH>Priority</TH> </TR> <DIV id="sectionDetail"> <TR> <TD> <INPUT id="fieldCatVal" TYPE="radio" NAME="category" value="x"> <SPAN id="fieldCategory">xCategory</SPAN></TD> <TD id="fieldPriority">xPriority</TD> </TR> </DIV> </TABLE> </FORM>

XmlEngine_tutorial - Apr 7, 2006

41/51

VIII.Application examples

We have an XML file (SetPriority.xml): <?xml version="1.0"?> <REPORT> <template file="SetPriority.html" /> <structure name="structure1"> <FIELD id="fieldCategory">category</FIELD> <FIELD id="fieldCatVal" attribute="value">category</FIELD> <FIELD id="fieldPriority">priority</FIELD> <SECTION id="sectionDetail"/> </structure> </REPORT> The XML file (SetPriority.xml) is similar to the report generation XML files. The only difference is that it not a SQL query. We have a ServletSetPriority.java file. It shows de Log4j categories and it updates. import java.io.PrintWriter; import java.io.IOException; import java.io.File; import javax.servlet.*; import javax.servlet.http.*; import org.openbravo.xmlEngine.XmlEngine; import org.openbravo.xmlEngine.XmlDocument; import org.apache.log4j.Category; import org.apache.log4j.Priority; import org.apache.log4j.PropertyConfigurator; public class ServletSetPriority extends HttpServlet { public XmlEngine xmlEngine=null; static Category log4jServletSetPriority = Category.getInstance(ServletSetPriority.class); public void init (ServletConfig config) { String prefix = config.getServletContext().getRealPath("/"); PropertyConfigurator.configure(prefix+"WEBINF/config/log4j.lcf"); xmlEngine = new XmlEngine(); xmlEngine.fileXmlEngineFormat = new File (prefix+"WEB-INF/config/Format.xml");

XmlEngine_tutorial - Apr 7, 2006

42/51

VIII.Application examples

xmlEngine.fileBaseLocation = new File(prefix+"WEB-INF/classes"); xmlEngine.initialize(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { doPost(request,response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String strCategory = request.getParameter("category"); String strPriority = request.getParameter("priority"); if (strCategory != null && strPriority != null) { Category category = Category.getInstance(strCategory); Priority priority = Priority.toPriority(strPriority); category.setPriority(priority); } printPage(response); } void printPage(HttpServletResponse response) throws IOException, ServletException { log4jServletSetPriority.debug("Output: Page"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); XmlDocument xmlDocument = xmlEngine.readXmlTemplate("SetPriority").createXm lDocument(); xmlDocument.setData("structure1",CategoryData .getCategories()); out.println(xmlDocument.print()+" v22"); out.close(); } public String getServletInfo() {

XmlEngine_tutorial - Apr 7, 2006

43/51

VIII.Application examples

return "Servlet that asign a priority to a Category"; } } We need to import several packages from the XmlEngine package: import org.openbravo.xmlEngine.XmlEngine; import org.openbravo.xmlEngine.XmlDocument; XmlDocument is a class that represents an XmlTemplate and XmlEngine is the class that represents the XmlEngine engine. The ServletSetPriority servlet extends HttpServlet. The init() function initializes an XmlEngine object. This object is later used in the printPage() function, where output string is generated: response.setContentType("text/html"); PrintWriter out = response.getWriter(); After this, the SetPriority.xml file is read using the readXmlTemplate function in the XmlEngine object. It returns an xmlTemplate object which contains the template structure. Using this template the createXmlDocument functions creates an XmlDocument object. XmlDocument xmlDocument = xmlEngine.readXmlTemplate("SetPriority").createXm lDocument(); Then we introduce the data in XmlDocument using setData.java, reciving as an argument the structure name and the data object. xmlDocument.setData("structure1",CategoryData.get Categories()); And, finally, it prints the output and passes to the PrintWriter: out.println(xmlDocument.print()); out.close(); The setData method from XmlDocument is the one who passes the data to XmlEngine, so this can later fill the template. The purpose of using a interface is to accept data from different sources. This interface is defined at FieldProvider: package org.openbravo.data; public interface FieldProvider { public String getField(String fieldName); } Every class that passes data to XmlEngine must have the getField method, which has as argument the name of the field. The class used on this demo is CategoryData: import java.util.*;

XmlEngine_tutorial - Apr 7, 2006

44/51

VIII.Application examples

import org.openbravo.data.FieldProvider; import org.apache.log4j.Category; public class CategoryData implements FieldProvider { static Category log4jCategoryData = Category.getInstance(CategoryData.class); public String category; public String priority; public String getField(String fieldName) { if (fieldName.equals("category")) return category; else if (fieldName.equals("priority")) return priority; else { log4jCategoryData.debug("No existe el campo: " + fieldName); return null; } } public static CategoryData[] getCategories() { Vector vector = new Vector(0); for (Enumeration e = Category.getCurrentCategories(); e.hasMoreElements() ;) { Category categoryItem = (Category)e.nextElement(); CategoryData categoryData = new CategoryData(); categoryData.category = categoryItem.getName(); if (categoryItem.getPriority() != null) { categoryData.priority = categoryItem.getPriority().toString(); } vector.addElement(categoryData); } CategoryData categoryData[] = new CategoryData[vector.size()]; vector.copyInto(categoryData);
XmlEngine_tutorial - Apr 7, 2006 45/51

VIII.Application examples

return(categoryData); } }

VIII.2 Demo application with a database connection


Many applications obtain their data from a database. This demo shows how an application can access XmlEngine with data obtained data from a database. This sample uses a template and configuration files similar to the ones used on the fourth example. The configuration files looks like: Configuration file: hours4.xml <?xml version="1.0"?> <REPORT> <template file="list4.html" /> <structure name="structure1"> <FIELD <FIELD <FIELD <FIELD <FIELD id="fieldEmployee">Employee</FIELD> id="fieldYear">ReportYear</FIELD> id="fieldMonth">ReportMonth</FIELD> id="fieldDay">ReportDay</FIELD> id="fieldHours">Hours</FIELD>

<FUNCTION id="funcCountParts" name="COUNT">Hours</FUNCTION> <FUNCTION id="funcSumHours" name="SUM">Hours</FUNCTION> <SECTION id="sectionDetail"/> </structure> </REPORT> The <template> tag indicates which template is used. The following servlet generates the same output that sample four but as an application. Configuration file: HoursList.java import import import import import org.openbravo.xmlEngine.XmlEngine; org.openbravo.xmlEngine.XmlDocument; java.io.*; java.sql.*; javax.servlet.*;

XmlEngine_tutorial - Apr 7, 2006

46/51

VIII.Application examples

import javax.servlet.http.*; import org.apache.log4j.Category; import org.apache.log4j.PropertyConfigurator; public class HoursList extends HttpServlet { public XmlEngine xmlEngine=null; public Connection connection; static Category log4jHoursList = Category.getInstance(HoursList.class); public void init (ServletConfig config) { String prefix = config.getServletContext().getRealPath("/"); PropertyConfigurator.configure(prefix+"WEBINF/log4j.lcf"); xmlEngine = new XmlEngine(); xmlEngine.fileXmlEngineFormat = new File (prefix+"WEB-INF/classes/Format.xml"); xmlEngine.fileBaseLocation = new File(prefix+"WEB-INF/classes"); xmlEngine.initialize(); try { Class.forName("org.apache.derby.jdbc.Embe ddedDriver"); } catch(ClassNotFoundException ex) { System.out.println("ClassNotFoundExceptio n error" + ex); } String url="jdbc:derby:../webapps/XmlEngine/WEBINF/database/tutorialDB"; try { connection=DriverManager.getConnection(ur l); } catch(SQLException ex) { System.out.println("SQLException error:" + ex); } } public void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException { doPost (request, response);
XmlEngine_tutorial - Apr 7, 2006 47/51

VIII.Application examples

} public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException { printHoursList(response); } void printHoursList(HttpServletResponse response) throws IOException, ServletException { log4jHoursList.debug("Output: HoursList"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); XmlDocument xmlDocument = xmlEngine.readXmlTemplate("hours4").createXmlDocu ment(); xmlDocument.setData("structure1", HoursData.select(connection)); out.println(xmlDocument.print() + "v17"); out.close(); } public String getServletInfo() { return "Servlet to list the hours of the employees"; } } The printHoursList() method generates the HTML page and handles the relation with XmlEngine. First we set the response content type and the PrintWriter object: response.setContentType("text/html"); PrintWriter out = response.getWriter(); The following lines of code read the hours4.xml template using the readXmlTemplate function. This function returns an XmlTemplate object that contains the template: XmlDocument xmlDocument = xmlEngine.readXmlTemplate("hours4").createXmlDocu ment(); In the following line we pass the data into the xmlDocument object using the setData method. xmlDocument.setData("structure1", HoursData.select(connection));

XmlEngine_tutorial - Apr 7, 2006

48/51

VIII.Application examples

Finally, we get the string from the print() xmlDocuments method. out.println(xmlDocument.print()); out.close(); Configuration file: HoursData.java import java.sql.*; import java.util.*; import javax.servlet.ServletException; import org.openbravo.data.FieldProvider; /**Clase HoursData */ public class HoursData implements FieldProvider { public String employee; public String year; public String month; public String day; public String hours; public String getField(String fieldName) { if (fieldName.toUpperCase().equals("EMPLOYEE") || fieldName.equals("employee")) return employee; else if (fieldName.toUpperCase().equals("REPORTYEAR") || fieldName.equals("reportyear")) return year; else if (fieldName.toUpperCase().equals("REPORTMONTH") || fieldName.equals("reportmonth")) return month; else if (fieldName.toUpperCase().equals("REPORTDAY") || fieldName.equals("reportday")) return day; else if (fieldName.toUpperCase().equals("HOURS") || fieldName.equals("hours")) return hours; else { System.out.println("No existe el campo: " + fieldName);

XmlEngine_tutorial - Apr 7, 2006

49/51

VIII.Application examples

return null; } } /**Hours list */ public static HoursData[] select(Connection connection) throws ServletException { String strSql = ""; strSql = strSql + ""; strSql = strSql + " SELECT Employee, ReportYear, ReportMonth, ReportDay, Hours FROM Hours"; strSql = strSql + " "; int iParameter = 0; Vector vector = new Vector(0); try { Statement statement=connection.createStatement(); ResultSet result=statement.executeQuery(strSql); long countRecord = 0; while(result.next()) { countRecord++; HoursData objectHoursData = new HoursData(); objectHoursData.employee = getValue(result, "Employee"); objectHoursData.year = getValue(result, "ReportYear"); objectHoursData.month = getValue(result, "ReportMonth"); objectHoursData.day = getValue(result, "ReportDay"); objectHoursData.hours = getValue(result, "Hours"); vector.addElement(objectHoursData); } result.close(); } catch(SQLException e){ System.out.println("Error of SQL in query: " + strSql + "Exception:"+ e);
XmlEngine_tutorial - Apr 7, 2006 50/51

VIII.Application examples

throw new ServletException(Integer.toString(e.getErrorCode( ))); } finally { } HoursData objectHoursData[] = new HoursData[vector.size()]; vector.copyInto(objectHoursData); return(objectHoursData); } public static String getValue(ResultSet result, String strField) throws java.sql.SQLException{ String strValueReturn = result.getString(strField); if (result.wasNull()) strValueReturn = ""; return strValueReturn; } } To invoke the application http://localhost:8080/XmlEngine/Hours you can use the URL:

Openbravo S.L. 2006 This work is licensed under the Creative Commons Attribution-ShareAlike 2.5 Spain License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/2.5/es/ or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA. The original of this work http://www.openbravo.com/docs/XmlEngine_tutorial.pdf may be obtained at

XmlEngine_tutorial - Apr 7, 2006

51/51

Das könnte Ihnen auch gefallen