/*
 * FichaAptidaoCreator.java
 *
 * Created on May 14, 2007, 4:50 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package siprp.ficha;

import com.evolute.utils.fop.FOPCreator;
import com.evolute.utils.fop.FOPPrinter;
import com.evolute.utils.fop.PDFCreator;
import com.evolute.utils.strings.UnicodeChecker;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.HashMap;

/**
 *
 * @author lflores
 */
public class FichaAptidaoCreator implements FichaAptidaoConstants
{
	private final FOPCreator fopCreator;
	private final PDFCreator pdfCreator;
	private final FOPPrinter fopPrinter;
	
	private static FichaAptidaoCreator creator = null;
	
	/** Creates a new instance of FichaAptidaoCreator */
	private FichaAptidaoCreator()
	{
		fopCreator = FOPCreator.getFOPCreator();
		pdfCreator = PDFCreator.getPDFCreator();
		fopPrinter = FOPPrinter.getFOPPrinter();
	}
	
	public static FichaAptidaoCreator getCreator()
	{
		if( creator == null )
		{
			creator = new FichaAptidaoCreator();
		}
		return creator;
	}
	
	public void print( byte fo[] )
		throws Exception
	{
		ByteArrayInputStream in = new ByteArrayInputStream( fo );
		fopPrinter.printFO( in, true, true, null );
//		System.out.println( "DONE" );
	}
	
	public void printSilent( byte fo[], String printerName )
		throws Exception
	{
		ByteArrayInputStream in = new ByteArrayInputStream( fo );
		fopPrinter.printFO( in, true, false, null );
	}
	
//	public byte[] createFO( HashMap<String,String> values )
//		throws Exception
//	{
//		StringBuilder buff = new StringBuilder( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<FichaAptidao>\n" );
//
//		values.put( PORTARIA, "Portaria n. 299/2007, de 16 de Mar&#xe7;o" );
//		
////		&#x2610; square
////		&#x2611; checked square
//		
//		for( String key: values.keySet() )
//		{
//			buff.append( "\t<" + key + ">" );
//			String val = values.get( key );
//			buff.append( UnicodeChecker.parseToUnicode( "&#x", ";", val, true ) );
//			buff.append( "</" + key + ">\n" );
//		}
//		buff.append( "</FichaAptidao>" );
//		String xml = buff.toString();
//		
//		System.out.println( "XML: " + xml );
//		
//		ByteArrayOutputStream out = new ByteArrayOutputStream();
//		InputStream xmlIn = new ByteArrayInputStream( xml.getBytes() );
//		
//		InputStream xslStream = getClass().getClassLoader().getResourceAsStream( "siprp/ficha/ficha_aptidao.xsl" );
//		fopCreator.createFOfromXML( xmlIn, xslStream, out );
//		//			System.out.println( "FO created" );
//		
//		return out.toByteArray();
//	}
	
	public byte[] createFO( byte []xml )
		throws Exception
	{
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		InputStream xmlIn = new ByteArrayInputStream( xml );
		
		InputStream xslStream = getClass().getClassLoader().getResourceAsStream( "siprp/ficha/ficha_aptidao.xsl" );
		fopCreator.createFOfromXML( xmlIn, xslStream, out );
		//			System.out.println( "FO created" );
		
		return out.toByteArray();
	}
	
	public byte[] createPDF( byte[] fo )
		throws Exception
	{
		return pdfCreator.createPdfFromFo( fo );
	}
}
