Java XML Transform Usage writeXhtml(InputStream inputStream, Writer writer, Templates... templates)

Here you can find the source of writeXhtml(InputStream inputStream, Writer writer, Templates... templates)

Description

write Xhtml

License

Open Source License

Declaration

public static void writeXhtml(InputStream inputStream, Writer writer, Templates... templates)
            throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.stream.StreamResult;

import org.xml.sax.InputSource;
import org.xml.sax.XMLFilter;

public class Main {
    static SAXTransformerFactory saxTf = (SAXTransformerFactory) TransformerFactory.newInstance();

    public static void writeXhtml(InputStream inputStream, Writer writer, Templates... templates)
            throws IOException {
        try {/* w w w.  j a  v  a2  s  .c  o m*/
            XMLFilter filter = null;
            for (Templates tplt : templates) {
                XMLFilter nextFilter = saxTf.newXMLFilter(tplt);
                if (filter != null)
                    nextFilter.setParent(filter);
                filter = nextFilter;
            }
            Transformer htmlTransformer = saxTf.newTransformer();
            SAXSource transformSource = new SAXSource(filter, new InputSource(inputStream));
            htmlTransformer.setOutputProperty(OutputKeys.METHOD, "xml");
            htmlTransformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            htmlTransformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "-//W3C//DTD XHTML 1.0 Strict//EN");
            htmlTransformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
                    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd");
            htmlTransformer.transform(transformSource, new StreamResult(writer));
        } catch (TransformerConfigurationException e) {
            throw new RuntimeException(e);
        } catch (TransformerException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. toNonValidatingSAXSource(InputStream in)
  2. toXMLInputSource(StreamSource in)
  3. unwrapException(Throwable t)
  4. verifySignature(Element element, PublicKey validatingKey)
  5. writeElementContent(XMLInputFactory inputFactory, XMLStreamWriter writer, Element element)
  6. xsl(File inFile, File outFile, InputStream xslStream)