Java Swing HTML getHTMLFromXML(String xml, URL xsl)

Here you can find the source of getHTMLFromXML(String xml, URL xsl)

Description

Converts an XML file to a formatted HTML output via an XSLT definition.

License

Open Source License

Parameter

Parameter Description
xml String containing an XML file
xsl URL containing an XSLT definition

Return

Formatted HTML output

Declaration

public static String getHTMLFromXML(String xml, URL xsl) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2016 Pablo Pavon-Marino.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * Contributors:/*from  w w w.ja  va2  s.c  o  m*/
 *     Pablo Pavon-Marino - Jose-Luis Izquierdo-Zaragoza, up to version 0.3.1
 *     Pablo Pavon-Marino - from version 0.4.0 onwards
 ******************************************************************************/

import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.parser.ParserDelegator;
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import java.io.*;

import java.net.URL;

import java.nio.charset.StandardCharsets;
import java.util.Set;
import java.util.TreeSet;

public class Main {
    /**
     * Converts an XML file to a formatted HTML output via an XSLT definition.
     * 
     * @param xml String containing an XML file
     * @param xsl File containing an XSLT definition
     * @return Formatted HTML output
     */
    public static String getHTMLFromXML(String xml, File xsl) {
        try {
            URL url = xsl.toURI().toURL();
            return getHTMLFromXML(xml, url);
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Converts an XML file to a formatted HTML output via an XSLT definition.
     * 
     * @param xml String containing an XML file
     * @param xsl URL containing an XSLT definition
     * @return Formatted HTML output
     */
    public static String getHTMLFromXML(String xml, URL xsl) {
        try {
            Source xmlDoc = new StreamSource(new StringReader(xml));
            Source xslDoc = new StreamSource(xsl.openStream());
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            TransformerFactory tFactory = TransformerFactory.newInstance();
            Transformer transformer = tFactory.newTransformer(xslDoc);
            transformer.transform(xmlDoc, new StreamResult(baos));
            String html = baos.toString(StandardCharsets.UTF_8.name());
            html = prepareImagePath(html, xsl);
            return html;
        } catch (IOException | TransformerFactoryConfigurationError | TransformerException e) {
            throw new RuntimeException(e);
        }
    }

    private static String prepareImagePath(String html, URL url) {
        final Set<String> list = new TreeSet<String>();
        final ParserDelegator parserDelegator = new ParserDelegator();
        final HTMLEditorKit.ParserCallback parserCallback = new HTMLEditorKit.ParserCallback() {
            @Override
            public void handleText(final char[] data, final int pos) {
            }

            @Override
            public void handleStartTag(HTML.Tag tag, MutableAttributeSet attribute, int pos) {
                if (tag == HTML.Tag.IMG) {
                    String address = (String) attribute.getAttribute(HTML.Attribute.SRC);
                    list.add(address);
                }
            }

            @Override
            public void handleEndTag(HTML.Tag t, final int pos) {
            }

            @Override
            public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, final int pos) {
                if (t == HTML.Tag.IMG) {
                    String address = (String) a.getAttribute(HTML.Attribute.SRC);
                    list.add(address);
                }
            }

            @Override
            public void handleComment(final char[] data, final int pos) {
            }

            @Override
            public void handleError(final String errMsg, final int pos) {
            }
        };

        final Reader reader = new StringReader(html);
        try {
            parserDelegator.parse(reader, parserCallback, true);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        for (String item : list) {
            try {
                URL newURL = new URL(url, item);
                html = html.replace(item, newURL.toExternalForm());
            } catch (Throwable e) {
                throw new RuntimeException(e);
            }
        }

        return html;
    }
}

Related

  1. containsAttribute(Element element, Object name, Object value)
  2. findElementDown(final String name, final Element parent)
  3. findElementUp(final String name1, final String name2, final Element start)
  4. findLinkElementUp(Element elem)
  5. findLinkUp(Element elem)
  6. getImgs(final String html)
  7. getRowIndex(final Element cell)
  8. hasClass(AttributeSet attr, String className)
  9. htmlToPlain(String html)