Example usage for javax.xml.transform.sax SAXTransformerFactory getAssociatedStylesheet

List of usage examples for javax.xml.transform.sax SAXTransformerFactory getAssociatedStylesheet

Introduction

In this page you can find the example usage for javax.xml.transform.sax SAXTransformerFactory getAssociatedStylesheet.

Prototype

public abstract Source getAssociatedStylesheet(Source source, String media, String title, String charset)
        throws TransformerConfigurationException;

Source Link

Document

Get the stylesheet specification(s) associated with the XML Source document via the <a href="http://www.w3.org/TR/xml-stylesheet/"> xml-stylesheet processing instruction</a> that match the given criteria.

Usage

From source file:Examples.java

/**
 * Show how to get stylesheets that are associated with a given
 * xml document via the xml-stylesheet PI (see http://www.w3.org/TR/xml-stylesheet/).
 *///from   w w w  .  j  a  v a2 s. c om
public static void exampleUseAssociated(String sourceID)
        throws TransformerException, TransformerConfigurationException {
    TransformerFactory tfactory = TransformerFactory.newInstance();

    // The DOM tfactory will have it's own way, based on DOM2, 
    // of getting associated stylesheets.
    if (tfactory instanceof SAXTransformerFactory) {
        SAXTransformerFactory stf = ((SAXTransformerFactory) tfactory);
        Source sources = stf.getAssociatedStylesheet(new StreamSource(sourceID), null, null, null);

        if (null != sources) {
            Transformer transformer = tfactory.newTransformer(sources);

            transformer.transform(new StreamSource(sourceID),
                    new StreamResult(new OutputStreamWriter(System.out)));
        } else {
            System.out.println("Can't find the associated stylesheet!");
        }
    }
}