spntoolsdata.pdf.util.XMLtoHtml.java Source code

Java tutorial

Introduction

Here is the source code for spntoolsdata.pdf.util.XMLtoHtml.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package spntoolsdata.pdf.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.stream.StreamSource;
import org.apache.commons.lang3.StringEscapeUtils;
import org.jsoup.Jsoup;
import spntoolsdata.map.Cliente;

/**
 *
 * @author Ivansito
 */
public class XMLtoHtml {

    public static final boolean $ORDEN_GARANTIA = true;
    public static final boolean $ORDEN_FUERA_GARANTIA = false;

    public static Boolean createHTML(boolean tipoDocumento, String direccionDestino, String direccionXML,
            Cliente cliente, boolean template)
            throws IOException, FileNotFoundException, TransformerFactoryConfigurationError, TransformerException {
        Boolean exito = false;
        try {
            TransformerFactory tFactory = TransformerFactory.newInstance();

            Source xmlDoc = new StreamSource(direccionXML);
            Source xslDoc = null;

            if (tipoDocumento == true) {
                xslDoc = new StreamSource(XMLtoHtml.class.getResourceAsStream("/spntoolsdata/pdf/res/ordenfg.xsl"));
            } else if (tipoDocumento == false) {
                xslDoc = new StreamSource(
                        XMLtoHtml.class.getResourceAsStream("/spntoolsdata/pdf/res/ordenfng.xsl"));
            }

            if (xslDoc != null) {
                StringWriter writer = new StringWriter();
                Transformer transformer = tFactory.newTransformer(xslDoc);
                transformer.setParameter("_id", cliente.get_id().toString());
                transformer.transform(xmlDoc, new javax.xml.transform.stream.StreamResult(writer));
                String result = writer.toString();
                exito = writeHTML(checkHTML(result), direccionDestino);
            }
        } catch (TransformerFactoryConfigurationError | FileNotFoundException | TransformerException ex) {
            //log.level.error(ex.getMessage());
            throw ex;
        } catch (IOException ex) {
            //log.level.error(ex.getMessage());
            throw ex;
        } catch (Exception ex) {
            //log.level.error(ex.getMessage());
        } finally {
            try {

                exito = true;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }

        return exito;
    }

    public static String checkHTML(String htmlString) throws IOException {

        String checkedhtml = null;
        try {
            String value = new String(Jsoup.parse(htmlString, "UTF-8").html().getBytes(), "UTF-8");
            checkedhtml = StringEscapeUtils.unescapeHtml4(value);
        } catch (Exception ex) {
            throw ex;
        }

        return checkedhtml;
    }

    public static void transform(File source, String srcEncoding, File target, String tgtEncoding)
            throws IOException {
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(source), srcEncoding));
            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(target), tgtEncoding));
            char[] buffer = new char[16384];
            int read;
            while ((read = br.read(buffer)) != -1) {
                bw.write(buffer, 0, read);
            }
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
            } finally {
                if (bw != null) {
                    bw.close();
                }
            }
        }
    }

    public static boolean writeHTML(String htmlString, String direccionDestino)
            throws UnsupportedEncodingException, FileNotFoundException, IOException {
        Boolean exito = false;
        FileOutputStream fos = new FileOutputStream(direccionDestino);

        Writer out = new BufferedWriter(new OutputStreamWriter(fos, "UTF-8"));
        try {
            out.write(htmlString);
            exito = true;
        } catch (Exception ex) {
        } finally {
            out.close();
            fos.close();
        }
        return exito;
    }

}