package com.test;

import javax.ejb.*;
import javax.rmi.*;
import java.rmi.RemoteException;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.sql.*;
import java.util.Vector;
import java.util.*;
import java.util.TreeMap;
import java.util.Hashtable;
import java.util.HashMap;
import java.util.Properties;
import java.math.BigDecimal;


import java.io.*;
import java.util.Date;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import org.apache.fop.apps.Driver;
import org.apache.fop.apps.Version;
import org.apache.fop.apps.XSLTInputHandler;

import org.apache.log.*;


public class Translate  {

	static final boolean    VERBOSE              = true;


public static void main(String arg[]) {

		log("Converting XML to PDF");
		Translate xml2pdf = new Translate();
		log(xml2pdf.xml);
		xml2pdf.convert();

	} // End Main


	public String convert() {

		String xslFileName = "FOGenerator.xsl";
		String pdfFileLink = "THIS IS PDF FILE LINK";

		try {

			Date dt = new Date();
			long timeStamp = dt.getTime();

			String xmlFileName = "tempXML"+timeStamp+".xml";

			File xmlTempFile = new File(xmlFileName);
			FileOutputStream xmlFileOutputStream =
					new FileOutputStream(xmlTempFile);
			xmlFileOutputStream.write(xml.getBytes());
			xmlFileOutputStream.close();


			ByteArrayOutputStream out = new ByteArrayOutputStream();

			File pdfFile = new File("Report1.pdf");
			FileOutputStream fileOutputStream =
							new FileOutputStream(pdfFile);


			Logger log = null;
			Hierarchy hierarchy = Hierarchy.getDefaultHierarchy();

				log = hierarchy.getLoggerFor("fop");
            	log.setPriority(Priority.DEBUG);

			Driver  driver = new Driver();

			driver.setErrorDump(true);
			driver.setLogger(log);
			driver.setRenderer(Driver.RENDER_PDF);



			File xmlFile = new File(xmlFileName);
			File xslFile = new File(xslFileName);

			if (xmlFile != null ) log("XML FILE NOT NULL");
			else log("XML FILE NULL");

			log("SIZE OF XML FILE :"+xmlFile.length());

			if (xslFile != null ) log("XSL FILE NOT NULL");
			else log("XSL FILE NULL");

			log("SIZE OF XSL FILE :"+xslFile.length());

			XSLTInputHandler input =
					new XSLTInputHandler(xmlFile,xslFile);

			if (input != null ) log("XSLT Input Handler is not null: "+input.toString());
			else log("XSLT INPUT HANDLER IS NULL");

			XMLReader parser = input.getParser();

			if (parser != null ) log("PARSER IS NOT NULL");
			else log("PARSER IS NULL");

			InputSource inputSource = input.getInputSource();

			if (inputSource != null ) log("Input Source is not null");
			else log("Input Source is null");

			driver.setOutputStream(out);

			log("OutputStream set");

			driver.buildFOTree(parser, inputSource);
			driver.format();
			driver.render();
			//driver.render(parser, inputSource);

			log("Rendering Done");

			byte[] pdfContent = out.toByteArray();
			log("SIZE OF  BYTE ARRAY OUTPUT STREAM :"+out.size());

			fileOutputStream.write(pdfContent);
			fileOutputStream.flush();
			fileOutputStream.close();

			//log("Deleting the temp XML file:"+xmlTempFile.delete()); // Delete the temp XML file


			pdfFileLink = pdfFile.getName();

			log("NAME OF PDF FILE :"+pdfFileLink);

		} catch (Exception e) {
			log("Unable to Translate XML to PDF...");
			e.printStackTrace();
		}

		return pdfFileLink;

	} // end convert

	public static void log(String message) {
		System.out.println(message);
	} // End log

	public String xml= ""+
"<?xml version=\"1.0\"?>"+"\n"+
"<Document name=\"M-Vest  Sample Report\">"+"\n"+
"	<Header>"+"\n"+
"		<HeaderLine color=\"black\" font-family=\"Helvetica\" font-size=\"12\" align=\"center\">"+"\n"+
"			M-VEST ASSET ALLOCATION SYSTEM"+"\n"+
"		</HeaderLine>"+"\n"+
"		<HeaderLine color=\"blue\" font-family=\"Helvetica\" font-size=\"12\" align=\"center\">"+"\n"+
"			Sample Report"+"\n"+
"		</HeaderLine>"+"\n"+
"	</Header>"+"\n"+
"	"+"\n"+
"	<Footer>"+"\n"+
"		<FooterLine color=\"black\" font-family=\"Helvetica\" font-size=\"8\" align=\"right\">"+"\n"+
"			Copyright @ Ascent Computing Group Inc."+"\n"+
"		</FooterLine>"+"\n"+
"	</Footer>"+"\n"+
"	"+"\n"+
"	<Page pageNumber=\"1\">"+"\n"+
"	"+"\n"+
"		<PageTitle>"+"\n"+
"			<PageTitleLine color=\"blue\" font-family=\"Helvetica\" font-size=\"12\" align=\"center\">"+"\n"+
"				EFFICIENT ASSET MIX VERSUS TOTAL RETURN"+"\n"+
"			</PageTitleLine>"+"\n"+
"		</PageTitle>"+"\n"+
"		"+"\n"+
"		<SubTitle>"+"\n"+
"			<SubTitleLine color=\"blue\" font-family=\"Helvetica\" font-size=\"8\" align=\"left\">"+"\n"+
"				Optimization Objective: Total Risk."+"\n"+
"			</SubTitleLine>"+"\n"+
"			<SubTitleLine color=\"blue\" font-family=\"Helvetica\" font-size=\"8\" align=\"left\">"+"\n"+
"				Optimization Method:"+"\n"+
"			</SubTitleLine>"+"\n"+
"		</SubTitle>"+"\n"+
"		"+"\n"+
"		<Bookmark marker=\"EFFICIENT ASSET MIX VERSUS TOTAL RETURN\">EFFICIENT ASSET MIX VERSUS TOTAL RETURN</Bookmark>"+"\n"+
"		"+"\n"+
"		<PageBody>"+"\n"+
"			<Image id=\"1\" height=\"450\" width=\"450\" external=\"false\">chart1.gif</Image>"+"\n"+
"			<Image id=\"2\" height=\"450\" width=\"450\" external=\"false\">chart2.gif</Image>"+"\n"+
"		</PageBody>"+"\n"+
"		"+"\n"+
"	</Page>"+"\n"+
"	"+"\n"+
"	<Page pageNumber=\"2\">"+"\n"+
"		"+"\n"+
"		<PageTitle>"+"\n"+
"			<PageTitleLine color=\"blue\" font-family=\"Helvetica\" font-size=\"12\" align=\"center\">"+"\n"+
"				EFFICIENT ASSET MIX VERSUS TOTAL RISK"+"\n"+
"			</PageTitleLine>"+"\n"+
"		</PageTitle>"+"\n"+
"		"+"\n"+
"		<SubTitle>"+"\n"+
"			<SubTitleLine color=\"blue\" font-family=\"Helvetica\" font-size=\"8\" align=\"left\">"+"\n"+
"				2 Optimization Objective: Total Risk."+"\n"+
"			</SubTitleLine>"+"\n"+
"			<SubTitleLine color=\"blue\" font-family=\"Helvetica\" font-size=\"8\" align=\"left\">"+"\n"+
"				2 Optimization Method:"+"\n"+
"			</SubTitleLine>"+"\n"+
"		</SubTitle>"+"\n"+
"		"+"\n"+
"		<Bookmark marker=\"EFFICIENT ASSET MIX VERSUS TOTAL RISK\">EFFICIENT ASSET MIX VERSUS TOTAL RISK</Bookmark>"+"\n"+
"		"+"\n"+
"		<PageBody>"+"\n"+
"			<Image id=\"1\" height=\"450\" width=\"450\" external=\"false\">chart1.gif</Image>"+"\n"+
"			<Image id=\"2\" height=\"450\" width=\"450\" external=\"false\">chart2.gif</Image>"+"\n"+
"		</PageBody>"+"\n"+
"	</Page>"+"\n"+
"	"+"\n"+
"	<Page pageNumber=\"3\">"+"\n"+
"	"+"\n"+
"		<PageTitle>"+"\n"+
"			<PageTitleLine color=\"blue\" font-family=\"Helvetica\" font-size=\"12\" align=\"center\">"+"\n"+
"				FRONTIER REPORT"+"\n"+
"			</PageTitleLine>"+"\n"+
"		</PageTitle>"+"\n"+
"		"+"\n"+
"		<Bookmark marker=\"FRONTIER REPORT\">FRONTIER REPORT</Bookmark>"+"\n"+
"		"+"\n"+
"		<PageBody>"+"\n"+
"			"+"\n"+
"			<Table font-size=\"8pt\" font-family=\"Helvetica\" NumberOfColumns=\"6\" NumberOfRows=\"6\" width=\"90%\">"+"\n"+
"				<TableHeading color=\"blue\" font-size=\"12pt\" font-family=\"Helvetica\" font-weight=\"bold\">"+"\n"+
"					Data"+"\n"+
"				</TableHeading>"+"\n"+
"				"+"\n"+
"				<tr line-height=\"8pt\" font-weight=\"bold\">"+"\n"+
"					<td color=\"blue\">Portfolio Number</td>"+"\n"+
"					<td color=\"blue\">(1)</td>"+"\n"+
"					<td color=\"blue\">(2)</td>"+"\n"+
"					<td color=\"blue\">(3)</td>"+"\n"+
"					<td color=\"blue\">(4)</td>"+"\n"+
"					<td color=\"blue\">(5)</td>"+"\n"+
"					<td color=\"blue\">(5)</td>"+"\n"+
"				</tr>"+"\n"+
"				"+"\n"+
"				<tr line-height=\"8pt\" font-weight=\"bold\">"+"\n"+
"					<td color=\"blue\">Total Risk Premium</td>"+"\n"+
"					<td color=\"blue\">9.00</td>"+"\n"+
"					<td color=\"blue\">8.00</td>"+"\n"+
"					<td color=\"blue\">7.00</td>"+"\n"+
"					<td color=\"blue\">6.00</td>"+"\n"+
"					<td color=\"blue\">5.00</td>"+"\n"+
"					<td color=\"blue\">(5)</td>"+"\n"+
"				</tr>"+"\n"+
"				"+"\n"+
"				<tr line-height=\"8pt\" font-weight=\"bold\">"+"\n"+
"					<td color=\"blue\">Residual Risk Premium</td>"+"\n"+
"					<td color=\"blue\">0.00</td>"+"\n"+
"					<td color=\"blue\">0.00</td>"+"\n"+
"					<td color=\"blue\">0.00</td>"+"\n"+
"					<td color=\"blue\">0.00</td>"+"\n"+
"					<td color=\"blue\">0.00</td>"+"\n"+
"					<td color=\"blue\">(5)</td>"+"\n"+
"				</tr>"+"\n"+
"				"+"\n"+
"				<tr line-height=\"8pt\" font-weight=\"bold\">"+"\n"+
"					<td>Russell 1000 Growth</td>"+"\n"+
"					<td>13.23</td>"+"\n"+
"					<td>14.95</td>"+"\n"+
"					<td>17.17</td>"+"\n"+
"					<td>20.12</td>"+"\n"+
"					<td>24.26</td>"+"\n"+
"					<td color=\"blue\">(5)</td>"+"\n"+
"				</tr>"+"\n"+
"				"+"\n"+
"				<tr line-height=\"8pt\" font-weight=\"bold\">"+"\n"+
"					<td>Russell 1000 Value</td>"+"\n"+
"					<td>16.79</td>"+"\n"+
"					<td>18.41</td>"+"\n"+
"					<td>20.51</td>"+"\n"+
"					<td>23.29</td>"+"\n"+
"					<td>27.20</td>"+"\n"+
"					<td color=\"blue\">(5)</td>"+"\n"+
"				</tr>"+"\n"+
"				"+"\n"+
"				<tr line-height=\"8pt\" font-weight=\"bold\">"+"\n"+
"					<td>Salomon Brothers Int BIG</td>"+"\n"+
"					<td>69.98</td>"+"\n"+
"					<td>66.64</td>"+"\n"+
"					<td>62.33</td>"+"\n"+
"					<td>56.59</td>"+"\n"+
"					<td>48.55</td>"+"\n"+
"					<td color=\"blue\">(5)</td>"+"\n"+
"				</tr>"+"\n"+
"				"+"\n"+
"			</Table>"+"\n"+
"		</PageBody>"+"\n"+
"	</Page>"+"\n"+
"	"+"\n"+
"</Document>"+"\n"+
"";



}  // End class

