Java XML String Transform getOutputMimeType(String xslName)

Here you can find the source of getOutputMimeType(String xslName)

Description

Returns the MIME type of the XSLT transformation output taken from the "media-type" attribute of the xsl:output element.

License

Apache License

Parameter

Parameter Description
xslName a parameter

Declaration

public static String getOutputMimeType(String xslName) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Main {
    private static Document xsl = null;

    /**/* w w  w.  j a va  2 s .  com*/
     * Returns the MIME type of the XSLT transformation output taken from the "media-type" attribute of the xsl:output element.
     * If the "media-type" attribute is not used it takes the "method" attribute of the same element. 
     * The following mappings between the "method" attribute values and MIME types are used
     * text -> text/plain
     * xml  -> application/xml
     * html -> text/html
     * @param xslName
     * @return
     */
    public static String getOutputMimeType(String xslName) {
        String mimeType = "";
        String mediaType = "";
        String method = "";
        getDocument(xslName);
        NodeList nList = xsl.getElementsByTagName("xsl:output");
        for (int i = 0; i < nList.getLength(); i++) {
            Node outputNode = nList.item(i);
            if (outputNode.getNodeType() == Node.ELEMENT_NODE) {
                Element outputElement = (Element) outputNode;
                mediaType = outputElement.getAttribute("media-type");
                method = outputElement.getAttribute("method");
            }
        }

        if ("".equals(mediaType)) {

            mimeType = ("text".equals(method)) ? "text/plain"
                    : ("xml".equals(method)) ? "application/xml" : ("html".equals(method)) ? "text/html" : "";

        } else
            mimeType = mediaType;

        return mimeType;
    }

    private static void getDocument(String xslName) {
        try {
            URL xslUri = new URL(xslName);
            InputStream xslIs = xslUri.openStream();
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            xsl = dBuilder.parse(xslIs);
            xsl.getDocumentElement().normalize();
        } catch (ParserConfigurationException ex) {
            throw new RuntimeException(ex);
        } catch (SAXException ex) {
            throw new RuntimeException(ex);
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }

    }
}

Related

  1. getConfigurationAsXML(Map properties)
  2. getHtmlAsString(Node node)
  3. getIntegerAttribute(XMLStreamReader in, String attribute)
  4. getNode(String text)
  5. getNotFoundXmlForObjectId(final String pid, final String detailCode, final String description)
  6. getOutputSQLPageXML(String inStatement, Map map)
  7. getPropertyAsString(final Properties prop, final String comment)
  8. getSchema(String schemaString)
  9. getSchemaFromResource(String... files)