245 lines
9.4 KiB
Java
245 lines
9.4 KiB
Java
package com.tarisan.util;
|
|
|
|
import java.io.File;
|
|
import java.io.OutputStream;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.Date;
|
|
|
|
import javax.xml.transform.Result;
|
|
import javax.xml.transform.Source;
|
|
import javax.xml.transform.Transformer;
|
|
import javax.xml.transform.TransformerFactory;
|
|
import javax.xml.transform.sax.SAXResult;
|
|
import javax.xml.transform.stream.StreamSource;
|
|
|
|
//import org.apache.fop.apps.FOUserAgent;
|
|
import org.apache.fop.apps.Fop;
|
|
import org.apache.fop.apps.FopFactory;
|
|
|
|
import com.tarisan.log.LogTarisan;
|
|
import com.tarisan.log.NivelLog;
|
|
|
|
public class XML2PDF {
|
|
|
|
public static void transformar(String ruta_xml, String ruta_xsl, String ruta_pdf) {
|
|
try {
|
|
/*El xml lo dejaremos en una ruta temporal para borrarlo al terminar
|
|
* El xsl está dentro de la carpeta css
|
|
* El jpg está dentro de la carpeta img
|
|
* El pdf lo dejaremos en la carpeta resultados
|
|
*/
|
|
LogTarisan.logger.log(NivelLog.INFO, "FOP XML2PDF");
|
|
//System.out.println("Preparando...");
|
|
LogTarisan.logger.log(NivelLog.INFO, "Preparando...");
|
|
|
|
//ServletContext sc = null;
|
|
//System.out.println("La ruta al xls es: " + sc.getRealPath("/"));
|
|
|
|
String ruta = WorkingDirectory.get().getAbsolutePath();
|
|
|
|
String sisop = System.getProperty("os.name");
|
|
|
|
String slash = "\\";
|
|
if(sisop.contains("indows"))
|
|
{
|
|
slash = "\\";
|
|
}
|
|
else{
|
|
slash = "/";
|
|
}
|
|
|
|
File baseDir = new File(ruta + slash);
|
|
File xslDir = new File(ruta + slash + ".." + slash + "css" + slash);
|
|
|
|
//File outDir = baseDir;
|
|
//outDir.mkdirs();
|
|
|
|
|
|
File xmlfile = new File(baseDir + ".." + slash + "resultados" + slash, ruta_xml);
|
|
File xsltfile = new File(xslDir, ruta_xsl);
|
|
File pdffile = new File(baseDir + ".." + slash + "resultados" + slash, ruta_pdf);
|
|
|
|
|
|
//System.out.println("XML de entrada: XML (" + xmlfile + ")");
|
|
LogTarisan.logger.log(NivelLog.INFO, "XML de entrada: XML (" + xmlfile + ")");
|
|
//System.out.println("Hoja de estilos xslt: " + xsltfile);
|
|
LogTarisan.logger.log(NivelLog.INFO, "Hoja de estilos xslt: " + xsltfile);
|
|
//System.out.println("PDF salida: (" + pdffile + ")");
|
|
LogTarisan.logger.log(NivelLog.INFO, "PDF salida: (" + pdffile + ")");
|
|
//System.out.println("Ejecutando...");
|
|
LogTarisan.logger.log(NivelLog.INFO, "Ejecutando...");
|
|
|
|
|
|
|
|
FopFactory fopFactory = FopFactory.newInstance();
|
|
//LogTarisan.logger.log(NivelLog.INFO, "Creamos la instancia FopFactory");
|
|
|
|
//FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
|
|
//LogTarisan.logger.log(NivelLog.INFO, "Creamos FOUserAgent");
|
|
|
|
|
|
|
|
OutputStream out = new java.io.FileOutputStream(pdffile);
|
|
//LogTarisan.logger.log(NivelLog.INFO, "Creando outputstream");
|
|
out = new java.io.BufferedOutputStream(out);
|
|
|
|
try {
|
|
|
|
Fop fop = fopFactory.newFop("text");
|
|
// .newFop("application/pdf", foUserAgent, out);
|
|
//LogTarisan.logger.log(NivelLog.INFO, "Creando Fop");
|
|
|
|
|
|
LogTarisan.setNivel("INFO");
|
|
TransformerFactory factory = TransformerFactory.newInstance();
|
|
//LogTarisan.logger.log(NivelLog.INFO, "Creando TransformerFactory");
|
|
Transformer transformer = factory.newTransformer(new StreamSource(xsltfile));
|
|
//LogTarisan.logger.log(NivelLog.INFO, "Le pasamos el xsl al transformer");
|
|
|
|
|
|
transformer.setParameter("versionParam", "1.0");
|
|
Date fecha = new Date();
|
|
SimpleDateFormat formato = new SimpleDateFormat("yyyy-MM-dd");
|
|
String cadenaFecha = formato.format(fecha);
|
|
transformer.setParameter("fecha", cadenaFecha);
|
|
//LogTarisan.logger.log(NivelLog.INFO, "Establecemos un parametro para el xsl");
|
|
|
|
|
|
Source src = new StreamSource(xmlfile);
|
|
//LogTarisan.logger.log(NivelLog.INFO, "Establecemos un nuevo origen con el xml");
|
|
|
|
|
|
Result res = new SAXResult(fop.getDefaultHandler());
|
|
//LogTarisan.logger.log(NivelLog.INFO, "Creamos el Result");
|
|
|
|
transformer.transform(src, res);
|
|
//LogTarisan.logger.log(NivelLog.INFO, "Ejecutamos el transformer.transform");
|
|
} finally {
|
|
out.close();
|
|
}
|
|
LogTarisan.logger.log(NivelLog.INFO, "Conversion realizada con éxito");
|
|
//System.out.println("Bien!");
|
|
LogTarisan.setNivel("ALL");
|
|
if(xmlfile.delete())
|
|
{
|
|
LogTarisan.logger.log(NivelLog.INFO,"Fichero xml eliminado correctamente");
|
|
}
|
|
else
|
|
{
|
|
LogTarisan.logger.log(NivelLog.WARN, "No se ha podido borrar el xml de la carpeta resultados.");
|
|
}
|
|
} catch (Exception e) {
|
|
LogTarisan.logger.log(NivelLog.ERROR, "Error al convertir xml a pdf: " + e.toString());
|
|
}
|
|
}
|
|
|
|
|
|
public static void transformar(String ruta_xml, String ruta_xsl, String ruta_pdf, String ruta) {
|
|
try {
|
|
/*El xml lo dejaremos en una ruta temporal para borrarlo al terminar
|
|
* El xsl está dentro de la carpeta css
|
|
* El jpg está dentro de la carpeta img
|
|
* El pdf lo dejaremos en la carpeta resultados
|
|
*/
|
|
System.out.println("Preparando...");
|
|
|
|
//ServletContext sc = null;
|
|
//System.out.println("La ruta al xls es: " + sc.getRealPath("/"));
|
|
|
|
String sisop = System.getProperty("os.name");
|
|
String slash = "\\";
|
|
if(sisop.contains("indows"))
|
|
{
|
|
slash = "\\";
|
|
}
|
|
else{
|
|
slash = "/";
|
|
}
|
|
|
|
File xslDir = new File(ruta + slash + ".." + slash + "css" + slash);
|
|
File resDir = new File(ruta + slash + ".." + slash + "resultados" + slash);
|
|
|
|
//File outDir = baseDir;
|
|
//outDir.mkdirs();
|
|
|
|
|
|
File xmlfile = new File(resDir, ruta_xml);
|
|
File xsltfile = new File(xslDir, ruta_xsl);
|
|
File pdffile = new File(resDir, ruta_pdf);
|
|
|
|
|
|
System.out.println("XML de entrada: XML (" + xmlfile + ")");
|
|
//LogTarisan.logger.log(NivelLog.INFO, "XML de entrada: XML (" + xmlfile + ")");
|
|
System.out.println("Hoja de estilos xslt: " + xsltfile);
|
|
//LogTarisan.logger.log(NivelLog.INFO, "Hoja de estilos xslt: " + xsltfile);
|
|
System.out.println("PDF salida: (" + pdffile + ")");
|
|
//LogTarisan.logger.log(NivelLog.INFO, "PDF salida: (" + pdffile + ")");
|
|
System.out.println("Ejecutando...");
|
|
//LogTarisan.logger.log(NivelLog.INFO, "Ejecutando...");
|
|
|
|
|
|
|
|
FopFactory fopFactory = FopFactory.newInstance();
|
|
////LogTarisan.logger.log(NivelLog.INFO, "Creamos la instancia FopFactory");
|
|
|
|
//FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
|
|
//LogTarisan.logger.log(NivelLog.INFO, "Creamos FOUserAgent");
|
|
|
|
|
|
|
|
OutputStream out = new java.io.FileOutputStream(pdffile);
|
|
//LogTarisan.logger.log(NivelLog.INFO, "Creando outputstream");
|
|
out = new java.io.BufferedOutputStream(out);
|
|
|
|
try {
|
|
|
|
Fop fop = fopFactory.newFop("text");
|
|
//LogTarisan.logger.log(NivelLog.INFO, "Creando Fop");
|
|
|
|
|
|
//LogTarisan.setNivel("INFO");
|
|
TransformerFactory factory = TransformerFactory.newInstance();
|
|
//LogTarisan.logger.log(NivelLog.INFO, "Creando TransformerFactory");
|
|
Transformer transformer = factory.newTransformer(new StreamSource(xsltfile));
|
|
//LogTarisan.logger.log(NivelLog.INFO, "Le pasamos el xsl al transformer");
|
|
|
|
|
|
transformer.setParameter("versionParam", "1.0");
|
|
Date fecha = new Date();
|
|
SimpleDateFormat formato = new SimpleDateFormat("yyyy-MM-dd");
|
|
String cadenaFecha = formato.format(fecha);
|
|
transformer.setParameter("fecha", cadenaFecha);
|
|
//LogTarisan.logger.log(NivelLog.INFO, "Establecemos un parametro para el xsl");
|
|
|
|
|
|
Source src = new StreamSource(xmlfile);
|
|
//LogTarisan.logger.log(NivelLog.INFO, "Establecemos un nuevo origen con el xml");
|
|
|
|
|
|
Result res = new SAXResult(fop.getDefaultHandler());
|
|
//LogTarisan.logger.log(NivelLog.INFO, "Creamos el Result");
|
|
|
|
transformer.transform(src, res);
|
|
//LogTarisan.logger.log(NivelLog.INFO, "Ejecutamos el transformer.transform");
|
|
} finally {
|
|
out.close();
|
|
}
|
|
//LogTarisan.logger.log(NivelLog.INFO, "Conversion realizada con éxito");
|
|
System.out.println("Bien!");
|
|
//LogTarisan.setNivel("ALL");
|
|
if(xmlfile.delete())
|
|
{
|
|
//LogTarisan.logger.log(NivelLog.INFO,"Fichero xml eliminado correctamente");
|
|
System.out.println("Fichero xml eliminado correctamente");
|
|
}
|
|
else
|
|
{
|
|
//LogTarisan.logger.log(NivelLog.WARN, "No se ha podido borrar el xml de la carpeta resultados.");
|
|
System.out.println("No se ha podido borrar el xml de la carpeta resultados.");
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
//LogTarisan.logger.log(NivelLog.ERROR, "Error al convertir xml a pdf: " + e.toString());
|
|
}
|
|
}
|
|
} |