The document summarizes a presentation about the Apache software foundation and various Apache projects. It discusses Apache Commons, which provides reusable Java components. It also discusses Apache POI, which provides APIs for working with Microsoft document formats like Excel. Finally, it discusses Apache FOP, which is a formatter driven by XSL formatting objects that can convert XML files into PDF documents.
Apache POI: Generate Excel Spreadsheets from Domino Data
1. UKLUG 2012 – Cardiff, Wales September 2012
Presenter: Paul T. Calhoun
Company: NetNotes Solutions
ICON UK 2013
Apache Software: The FREE Java
toolbox you didn't know you had !!
2. UKLUG 2012 – Cardiff, Wales
Obligatory Introduction Slide
•Have used Notes/Domino since R2
•Certified in Administration and Development or every release: R3-R8.5
•Independent trainer/developer/mentor for the past 22 years running
NetNotes Solutions Unlimited (www.nnsu.com)
•Specializing in
• Java
• Web Services
• XPages
• J2EE (WebSphere)
• Eclipse
• Rational Developer
• And….
3. UKLUG 2012 – Cardiff, Wales
Obligatory Introduction Slide
•Raising Grand Kids
4. UKLUG 2012 – Cardiff, Wales
Agenda
●What is APACHE ?
●Commons
●POI
●FOP
●Summary
5. UKLUG 2012 – Cardiff, Wales
What is APACHE ?
•The Apache Software Foundation
(www.apache.org) is
• An Open Source (Yes that means FREE)
consortium of companies and developers
• Donate time and resources to developing
tools (primarily Java based) that simplify
many tasks that developers are faced
with everyday
• It’s a lot like OpenNTF, but for Java
developers
6. UKLUG 2012 – Cardiff, Wales
What is APACHE ?
•IBM is a major contributor/supporter
•If you develop using Java code (and you should be) than you
owe it to yourself to spend some time reviewing the projects on
this site
•Some I use all the time
• Xerces
• Xalan
• XML Graphics (FOP)
• POI
• Commons
8. UKLUG 2012 – Cardiff, Wales
Agenda
●What is APACHE ?
●Commons
●POI
●FOP
●Summary
9. UKLUG 2012 – Cardiff, Wales
Apache Commons
•http://commons.apache.org
•Focused on all aspects of reusable Java
components
10. UKLUG 2012 – Cardiff, Wales
Commons Proper
•Commons Proper
• Goal of creating and maintaining
reusable java Components
•Individual downloadable components
• Not one GIANT utility library
• Just get what you need
11. UKLUG 2012 – Cardiff, Wales
Commons Proper
Component
Attributes Runtime API to metadata attributes such as doclet tags.
BCEL Byte Code Engineering Library - analyze, create, and manipulate Java class files
BeanUtils Easy-to-use wrappers around the Java reflection and introspection APIs.
Betwixt Services for mapping JavaBeans to XML documents, and vice versa.
BSF Bean Scripting Framework - interface to scripting languages, including JSR-223
Chain Chain of Responsibility pattern implemention.
CLI Command Line arguments parser.
Codec General encoding/decoding algorithms (for example phonetic, base64, URL).
Collections Extends or augments the Java Collections Framework.
Compress Defines an API for working with tar, zip and bzip2 files.
Configuration Reading of configuration/preferences files in various formats.
CSV Component for reading and writing comma separated value files.
Daemon Alternative invocation mechanism for unix-daemon-like java code.
DBCP Database connection pooling services.
DbUtils JDBC helper library.
Digester XML-to-Java-object mapping utility.
12. UKLUG 2012 – Cardiff, Wales
Commons Proper
Component
Discovery Tools for locating resources by mapping service/reference names to
resource names.
EL Interpreter for the Expression Language defined by the JSP 2.0
specification.
Email Library for sending e-mail from Java.
Exec API for dealing with external process execution and environment
management in Java.
FileUpload File upload capability for your servlets and web applications.
Functor A functor is a function that can be manipulated as an object, or an object
representing a single, generic function.
Imaging (previously called
Sanselan)
A pure-Java image library.
IO Collection of I/O utilities.
JCI Java Compiler Interface
JCS Java Caching System
Jelly XML based scripting and processing engine.
Jexl Expression language which extends the Expression Language of the JSTL.
JXPath Utilities for manipulating Java Beans using the XPath syntax.
Lang Provides extra functionality for classes in java.lang.
13. UKLUG 2012 – Cardiff, Wales
Commons Proper
Component
Launcher Cross platform Java application launcher.
Logging Wrapper around a variety of logging API implementations.
Math Lightweight, self-contained mathematics and statistics components.
Modeler Mechanisms to create Model MBeans compatible with JMX specification.
Net Collection of network utilities and protocol implementations.
OGNL An Object-Graph Navigation Language
Pool Generic object pooling component.
Primitives Smaller, faster and easier to work with types supporting Java primitive types.
Proxy Library for creating dynamic proxies.
SCXML An implementation of the State Chart XML specification aimed at creating and
maintaining a Java SCXML engine. It is capable of executing a state machine
defined using a SCXML document, and abstracts out the environment interfaces.
Transaction Implementations for multi level locks, transactional collections and transactional
file access.
Validator Framework to define validators and validation rules in an xml file.
VFS Virtual File System component for treating files, FTP, SMB, ZIP and such like as a
single logical file system.
14. UKLUG 2012 – Cardiff, Wales
Commons Lang
•Provides a host of helper utilities for the
java.lang API
• String manipulation methods
• Basic numerical methods
• Object reflection
• Additionally it contains basic
enhancements to java.util.Date
15. UKLUG 2012 – Cardiff, Wales
Commons Lang - Versions
•Latest Version is 3.3.1
• For Java 1.5 and higher
• For Notes/Domino 8.x and 9.x
•Version 2.6 is still available
• For Java 1.2 and higher
• For Notes Domino 7.x and below
http://commons.apache.org/proper/commons-lang/download_lang.cgi
16. UKLUG 2012 – Cardiff, Wales
Commons Lang API
Packages
org.apache.commons.lang3 Provides highly reusable static utility methods, chiefly concerned with adding value to
the java.lang classes.
org.apache.commons.lang3.builder Assists in creating consistent equals(Object), toString(), hashCode(),
and compareTo(Object) methods.
org.apache.commons.lang3.concurrent Provides support classes for multi-threaded programming.
org.apache.commons.lang3.event Provides some useful event-based utilities.
org.apache.commons.lang3.exception Provides functionality for Exceptions.
org.apache.commons.lang3.math Extends java.math for business mathematical classes.
org.apache.commons.lang3.mutable Provides typed mutable wrappers to primitive values and Object.
org.apache.commons.lang3.reflect Accumulates common high-level uses of the java.lang.reflect APIs.
org.apache.commons.lang3.text Provides classes for handling and manipulating text, partly as an extension to java.text.
org.apache.commons.lang3.text.translate An API for creating text translation routines from a set of smaller building blocks.
org.apache.commons.lang3.time Provides classes and methods to work with dates and durations.
org.apache.commons.lang3.tuple Tuple classes, starting with a Pair class in version 3.0.
17. UKLUG 2012 – Cardiff, Wales
org.apache.commons.lang3.StringUtils
•IsEmpty/IsBlank - checks if a String contains text
•Trim/Strip - removes leading and trailing whitespace
•Equals - compares two strings null-safe
•startsWith - check if a String starts with a prefix null-safe
•endsWith - check if a String ends with a suffix null-safe
•IndexOf/LastIndexOf/Contains - null-safe index-of checks
•IndexOfAny/LastIndexOfAny/IndexOfAnyBut/LastIndexOfAnyBut - index-
of any of a set of Strings
18. UKLUG 2012 – Cardiff, Wales
org.apache.commons.lang3.StringUtils
•ContainsOnly/ContainsNone/ContainsAny - does String contains
only/none/any of these characters
•Substring/Left/Right/Mid - null-safe substring extractions
•SubstringBefore/SubstringAfter/SubstringBetween - substring extraction
relative to other strings
•Split/Join - splits a String into an array of substrings and vice versa
•Remove/Delete - removes part of a String
•Replace/Overlay - Searches a String and replaces one String with
another
19. UKLUG 2012 – Cardiff, Wales
org.apache.commons.lang3.StringUtils
•Chomp/Chop - removes the last part of a String
•LeftPad/RightPad/Center/Repeat - pads a String
•UpperCase/LowerCase/SwapCase/Capitalize/Uncapitalize - changes the
case of a String
•CountMatches - counts the number of occurrences of one String in
another
20. UKLUG 2012 – Cardiff, Wales
org.apache.commons.lang3.StringUtils
•IsAlpha/IsNumeric/IsWhitespace/IsAsciiPrintable - checks the characters
in a String
•DefaultString - protects against a null input String
•Reverse/ReverseDelimited - reverses a String
•Abbreviate - abbreviates a string using ellipsis
•Difference - compares Strings and reports on their differences
•LevenshteinDistance - the number of changes needed to change one
String into another
22. UKLUG 2012 – Cardiff, Wales
Agenda
●What is APACHE ?
●Commons
●POI
●FOP
●Summary
23. UKLUG 2012 – Cardiff, Wales
So what is POI?
•POI is a Java API for Microsoft Documents
• Not just spreadsheets, but what it’s used
most often for
• Which is misleading because it contains
libraries that specifically allow API access
to file formats based upon
• OOXML – Open Office XML
Standards
• OLE2 – Microsoft’s Compound
Document Format
• You do NOT have to have MS Office
installed in order to use POI !!
24. UKLUG 2012 – Cardiff, Wales
So what is POI?
•POI can “Read” and “Write” to MS and Open
source versions of
• Spreadsheets
• Word Processing documents
• Presentation (Powerpoint)
• MS Publisher
25. UKLUG 2012 – Cardiff, Wales
So what is POI?
•Useless Trivia
• POI is an acronym for “Poor
Obfuscation Implementation” in
reference to reverse engineering the
original MS Office document formats
26. UKLUG 2012 – Cardiff, Wales
Use Cases
•There are three primary use cases for
creating spreadsheets from Domino Data
• Export all the documents in a view
• Export selected documents from a
view
• Export documents that match a query
(Ad Hoc Reporting)
27. UKLUG 2012 – Cardiff, Wales
Export All Documents From a View
•This is the primary use case
• Create an XPage that displays the
View
• This isn’t really required, but is
nice for context purposes
28. UKLUG 2012 – Cardiff, Wales
Export All Documents From a View
•Create a clickable component that will
trigger running an XAgent that will output
the data to the spreadsheet
29. UKLUG 2012 – Cardiff, Wales
The Button Code
•Create a session scoped variable to hold
the view name
•Create an array of the fields that will be
exported
• This should generally match up to the
columns in the view but the View is
only used to aggregate the
documents. Field values are read
from the document collection
30. UKLUG 2012 – Cardiff, Wales
The Button Code
•Call the “XAgent” that will stream the
spreadsheet to the browser
31. UKLUG 2012 – Cardiff, Wales
The XAgent Code
•An XAgent is an XPage that has had it’s
“rendered” property turned off
•All of the code is in either the
“beforeRenderResponse” or
“afterRenderResponse” event
•We want to hi-jack the output stream so our
code will go in the “beforeRenderResponse”
event
32. UKLUG 2012 – Cardiff, Wales
The XAgent Code
•In an Xagent, the code is responsible for
producing all of the output
•It uses the underlying JSF API’s to
accomplish this task
33. UKLUG 2012 – Cardiff, Wales
The XAgent Code
•This is required because we need the
output stream of the page but CAN NOT
use the one from the executing XPage
(There can only be one!)
34. UKLUG 2012 – Cardiff, Wales
The XAgent Code
•The XAgent code reads the session scope
variables from the initiating XPage to
process the view and field list
35. UKLUG 2012 – Cardiff, Wales
The XAgent Code
•The Apache POI API is used to create a
new spreadsheet file and workbook
36. UKLUG 2012 – Cardiff, Wales
The XAgent Code
•The field list array is used to read the fields
from the notes documents
•Every document in the view is processed
37. UKLUG 2012 – Cardiff, Wales
The XAgent Code
•For every field in the field list array we read
the ITEMS from the Notes documents
•We have have to account for field “type” as
Java is a strongly “typed” language
38. UKLUG 2012 – Cardiff, Wales
The XAgent Code
•We use the “create” and “set” CellValue
methods to, you guessed it, create and set
the cell values
40. UKLUG 2012 – Cardiff, Wales
Agenda
●What is APACHE ?
●Commons
●POI
●FOP
●Summary
41. UKLUG 2012 – Cardiff, Wales
So what is FOP?
•FOP is a sub-project of the XML Graphics
project at APACHE
•FOP stands for Formatting Objects
Processor
• This is a print formatter driven by XSL
FO (Formatted Objects)
42. UKLUG 2012 – Cardiff, Wales
So what is FOP?
•An API that
• Reads an FO Tree
• Renders the resulting pages to a specified output
• PDF (Primary)
• PS
• PCL
• AFP
• XML
• Print
• AWT
• PNG
43. UKLUG 2012 – Cardiff, Wales
Um.. How about that again in English !!
•It’s a java based toolset that lets YOU (the
developer) create solutions that allow your End
Users (the Bain of all existence) to create PDF
documents from Notes content for FREE !!!
•Yes FREE!!!
• Ok, Free is relative.
• You are going to have to invest some time,
but I’m going to give you a working framework
that you can implement out of the box.
• For Free!!!
44. UKLUG 2012 – Cardiff, Wales
The Process
•The great thing about FOP is it is a highly
duplicable design pattern
XML
Source
XSLT
StyleSheet
FO Source
FO
Renderi
ng
Engine
Rendered Output
45. UKLUG 2012 – Cardiff, Wales
The Process
•It all starts with an XML Document (can be
from disk or in memory)
XML
Source
XSLT
StyleSheet
FO Source
FO
Renderi
ng
Engine
Rendered Output
46. UKLUG 2012 – Cardiff, Wales
The XML
•The XML can be
• A Static Document
• The output from ?ReadViewEntries
• Appended to the end of a Domino View
URL
• The output from generateXML
• Method of the Notes Document class
• The results of running an XAgent
• The results of running an Agent
• The results of running a Web Service
47. UKLUG 2012 – Cardiff, Wales
The XML
•The source of the XML is not as important as
the FORMAT and CONSISTANCY of the XML
• The XML must be well formed and
optionally valid
• Make sure that if you do not control the
source that you have an SLA with the
source provider that includes them
providing the XML Schema AND changes
to the XML Schema in enough advance
that you have time to test it.
48. UKLUG 2012 – Cardiff, Wales
The XML
•Using the ?ReadViewEntries option is
HIGHLY duplicable
• Once you have the stylesheet (and I’m
providing that to you) that transforms
the source XML to the FO XML then
ANY view source can be passed to the
code to produce a PDF of the view
49. UKLUG 2012 – Cardiff, Wales
The XML
•The other options require an XSLT stylesheet that
is specific to transforming the specified XML to FO
XML.
• This is not as flexible, but once the base
stylesheet is created it can be stored in a
notes document that is editable by a non-
developer
• This option allows changes to colors,
fonts etc without developer intervention
and re-compiling/re-deploying the code
50. UKLUG 2012 – Cardiff, Wales
The Stylesheet
•The stylesheet that is used is an XSLT
document (written in XML) that uses the
FOP tags from the tag library
XML
Source
XSLT
StyleSheet
FO Source
FO
Render
ing
Engine
Rendered Output
51. UKLUG 2012 – Cardiff, Wales
The XSL FO Stylesheet
•This is by far the most challenging part of this solution
•Stylesheet creators must be able to create and edit XSLT stylesheets (Doh!)
•The Tags used in the style sheet are not documented at the FOP site
• The good news is they ARE documented at the w3 schools site
• http://www.w3schools.com/xslfo/default.asp
• The other good news is they are documented in the form of a tutorial !!
• You can follow the tutorial to examine how to create a base XSL FO
style sheet
• You can also copy/paste this example code directly to your
XSL FO style sheet to get started !!
52. UKLUG 2012 – Cardiff, Wales
Using Eclipse or an XSLT editor is the
best choice
•Download the version that has the Web Tools Plugin (WTP)
•I usually download the one that supports J2EE development
•The benefit of this option is there is an XSLT editor included
not just an XML editor
•Also you can train “power-users” to use eclipse to build, edit,
maintain XSLT stylesheets for the purpose of maintaining their
own output without the need for them to have designer
• This might sound difficult, but it is significantly easier to
train both developers and power users on XSLT than it
is Java !!
53. UKLUG 2012 – Cardiff, Wales
The XSLT
•The Stylesheet is made up of a
combination of XSLT and XSL:FO tags
54. UKLUG 2012 – Cardiff, Wales
Structure of the FO Tags
•The XSL:FO tags are all about the layout of
the “printed” page
•XSL:FO tags always start with “root”
• Followed by a “layout master”
• Followed by a “page master”
• The a series of page sequences
that contain
• Flows
• Blocks
55. UKLUG 2012 – Cardiff, Wales
Structure of the FO Tags
56. UKLUG 2012 – Cardiff, Wales
The Layout and Page Master
•These tags define the output page
• Height
• Width
• Margins
• etc
57. UKLUG 2012 – Cardiff, Wales
The Page Sequence tag
•References the page master set tag to get
its output constraints
58. UKLUG 2012 – Cardiff, Wales
The Page Content
•The Page content is the output using a
series of “flows” and “blocks”
•A “flow” contains a series of “blocks”
•A “block” is roughly equivalent to a
paragraph on the page
59. UKLUG 2012 – Cardiff, Wales
The Page Content
•Blocks can contain other constraining tags
like the “table” tag
60. UKLUG 2012 – Cardiff, Wales
Where are the stylesheets Stored
•After the stylesheet is created there are two
options
• Save the stylesheet as a design
resource (A stylesheet)
• Save the stylesheet in a document that
is accessible from the notes client
• This allows editing/maintainence
of the stylesheets without the
need of a designer client
61. UKLUG 2012 – Cardiff, Wales
Storing Stylesheets in Designer
•The XSLT Sheets can be
stored in DDE in the Style
Sheets folder contained in
the resources folder
62. UKLUG 2012 – Cardiff, Wales
Alternately Store in documents
•You can create a
• Form
• View
•to store Notes documents that contain the
XSLT stylesheets
•Your code will “lookup” the stylesheet when
applying it to the XML source
63. UKLUG 2012 – Cardiff, Wales
The Code
•Now that you have the XML Source and the
XSLT stylesheet you are ready to write
some code !!! (Yea !!)
•The APACHE FOP is a JAVA Library
• Yes this means you have to code the
solution using Java !!
64. UKLUG 2012 – Cardiff, Wales
The Code
•Can be coded as
• Java Agents
• Java Code elements
• Coded in SSJS (like an XAgent)
65. UKLUG 2012 – Cardiff, Wales
Creating the FO Source
•The code will take the XSLT stylesheet, apply it to the XML source and
produce the XSL:FO that is used by the rendering engine to produce the
PDF
XML
Source
XSLT
StyleSheet
FO Source
FO
Renderi
ng
Engine
Rendered Output
66. UKLUG 2012 – Cardiff, Wales
The Code
•The “XAgent” that calls the Java Code
67. UKLUG 2012 – Cardiff, Wales
The XPage that calls the XAgent
•In the onClick event of a button the XAgent
is “executed” to produce the PDF output
68. UKLUG 2012 – Cardiff, Wales
The Complete Process
•That finishes the design pattern
XML Source
XSLT
StyleSheet
FO Source
FO
Renderin
g Engine Rendered Output
70. UKLUG 2012 – Cardiff, Wales
Agenda
●What is APACHE ?
●Commons
●POI
●FOP
●Summary
71. UKLUG 2012 – Cardiff, Wales
Summary / Q and A
•Thank YOU !!!
•Thank THE SPONSORS !!!!
•Don’t forget to fill out those evals !!!
•This is how to get ahold of me
•Email: pcalhoun@nnsu.com
•Twitter: ptcalhoun