Example: Using HTMLDocument to generate both HTML source and XSL FO source

Note: Read the Code example disclaimer for important legal information.
///////////////////////////////////////////////////////////////////////////////
// 
// Example: Using the Toolbox HTMLDocument Class
// to generate both HTML and XSL FO source data.
//
// This program uses the HTMLDocument class to 
// generate two files: one that has HTML source and 
// another than has XSL FO source.
//
// Command syntax: 
//    HTMLDocumentExample 
//
///////////////////////////////////////////////////////////////////////////////


import com.ibm.as400.util.html.*;
import java.*;
import java.io.*;
import java.lang.*;
import java.beans.PropertyVetoException;

public class HTMLDocumentExample
{
    public static void main (String[] args)
    {
        //Create the HTMLDocument that holds necessary document properties
        HTMLDocument doc = new HTMLDocument();

        //Set page and margin properties.  Numbers are in inches.
        doc.setPageWidth(8.5);
        doc.setPageHeight(11);
        doc.setMarginTop(1);
        doc.setMarginBottom(1);
        doc.setMarginLeft(1);
        doc.setMarginRight(1);

        //Create a header for the page.
        HTMLHead head = new HTMLHead();
        //Set the title for the header
        head.setTitle("This is the page header.");

        //Create several headings
        HTMLHeading h1 = new HTMLHeading(1, "Heading 1");
        HTMLHeading h2 = new HTMLHeading(2, "Heading 2");
        HTMLHeading h3 = new HTMLHeading(3, "Heading 3");
        HTMLHeading h4 = new HTMLHeading(4, "Heading 4");
        HTMLHeading h5 = new HTMLHeading(5, "Heading 5");
        HTMLHeading h6 = new HTMLHeading(6, "Heading 6");

        //Create some text that is printed from right to left.
        //Create BidiOrdering object and set the direction
        BidiOrdering bdo = new BidiOrdering();
        bdo.setDirection(HTMLConstants.RTL);

        //Create some text
        HTMLText text = new HTMLText("This is Arabic text.");
        //Add the text to the bidi-ordering object
        bdo.addItem(text);

        // Create an UnorderedList.
        UnorderedList uList = new UnorderedList(HTMLConstants.SQUARE);
        // Create and set the data for UnorderedListItems.
        UnorderedListItem listItem1 = new UnorderedListItem();
        UnorderedListItem listItem2 = new UnorderedListItem();
        listItem1.setItemData(new HTMLText("First item"));
        listItem2.setItemData(new HTMLText("Second item"));
        // Add the list items to the UnorderedList.
        uList.addListItem(listItem1);
        uList.addListItem(listItem2);

        // Create an OrderedList.
        OrderedList oList = new OrderedList(HTMLConstants.SMALL_ROMAN);
        // Create the OrderedListItems.
        OrderedListItem olistItem1 = new OrderedListItem();
        OrderedListItem olistItem2 = new OrderedListItem();
        OrderedListItem olistItem3 = new OrderedListItem();
        // Set the data in the OrderedListItems.
        olistItem1.setItemData(new HTMLText("First item"));
        olistItem2.setItemData(new HTMLText("Second item"));
        olistItem3.setItemData(new HTMLText("Third item"));
        // Add the list items to the OrderedList.
        oList.addListItem(olistItem1);
        oList.addListItem(olistItem2);
        // Add (nest) the unordered list to OrderedListItem2
        oList.addList(uList);
        // Add another OrderedListItem to the OrderedList
        // after the nested UnorderedList.
        oList.addListItem(olistItem3);

        // Create a default HTMLTable object.
        HTMLTable table = new HTMLTable();
        try
        {
            // Set the table attributes.
            table.setAlignment(HTMLTable.LEFT);
            table.setBorderWidth(1);

            // Create a default HTMLTableCaption object and set the caption text.
            HTMLTableCaption caption = new HTMLTableCaption();
            caption.setElement("Customer Account Balances - January 1, 2000");

            // Set the caption.
            table.setCaption(caption);

            // Create the table headers and add to the table.
            HTMLTableHeader account_header = new HTMLTableHeader(new HTMLText("ACCOUNT"));
            HTMLTableHeader name_header = new HTMLTableHeader(new HTMLText("NAME"));
            HTMLTableHeader balance_header = new HTMLTableHeader(new HTMLText("BALANCE"));

            table.addColumnHeader(account_header);
            table.addColumnHeader(name_header);
            table.addColumnHeader(balance_header);

            // Add rows to the table.  Each customer record represents a row in the table.
            int numCols = 3;
            for (int rowIndex=0; rowIndex< 5; rowIndex++)
            {
                HTMLTableRow row = new HTMLTableRow();
                row.setHorizontalAlignment(HTMLTableRow.CENTER);

                HTMLText account = new HTMLText("000" + rowIndex);
                HTMLText name = new HTMLText("Customer" + rowIndex);
                HTMLText balance = new HTMLText("" + (rowIndex + 1)*200);

                row.addColumn(new HTMLTableCell(account));
                row.addColumn(new HTMLTableCell(name));
                row.addColumn(new HTMLTableCell(balance));

                // Add the row to the table.
                table.addRow(row);
            }
        }
        catch(Exception e)
        {
            System.out.println("Problem creating table");
            System.exit(0);
        }
                                                         
        //Add the items to the HTMLDocument
        doc.addElement(head);
        doc.addElement(h1);
        doc.addElement(h2);
        doc.addElement(h3);
        doc.addElement(h4);
        doc.addElement(h5);
        doc.addElement(h6);
        doc.addElement(oList);
        doc.addElement(table);
        doc.addElement(bdo);

        //Print the fo tags to a file.
        try
        {
            FileOutputStream fout = new FileOutputStream("FOFILE.fo");
            PrintStream pout = new PrintStream(fout);
            pout.println(doc.getFOTag());
        }
        catch (Exception e)
        {
            System.out.println("Unable to write fo tags to FOFILE.fo");
        }

        //Print the html tags to a file
        try
        {
            FileOutputStream htmlout = new FileOutputStream("HTMLFILE.html");
            PrintStream phtmlout = new PrintStream(htmlout);
            phtmlout.println(doc.getTag());
        }
        catch (Exception e)
        {
            System.out.println("Unable to write html tags to HTMLFILE.html");
        }

    }
}