Search in sources :

Example 1 with SerializationMethod

use of uk.ac.ed.ph.snuggletex.SerializationMethod in project symja_android_library by axkr.

the class StylesheetManager method getSerializer.

/**
 * Obtains a serializer stylesheet based on the stylesheet at the given URI, configured as per the
 * given {@link SerializationSpecifier}. (Some options may require XSLT 2.0 support.)
 *
 * @param serializerUri URI for the required serializing stylesheet, null for the default
 *     serializer.
 * @param serializationOptions desired {@link SerializationSpecifier}, null for default options
 * @throws SnuggleRuntimeException if the serializer could not be created, or if an XSLT 2.0
 *     processor was required but could not be obtained.
 */
public Transformer getSerializer(final String serializerUri, final SerializationSpecifier serializationOptions) {
    /* Create appropriate serializer */
    Transformer serializer;
    boolean supportsXSLT20 = supportsXSLT20();
    try {
        if (serializationOptions != null && serializationOptions.isUsingNamedEntities() && supportsXSLT20) {
            /* We will perform character mapping here (which requires XSLT 2.0) */
            if (serializerUri != null) {
                /* Mix character mapper in with given serializer */
                serializer = cacheImporterStylesheet(true, serializerUri, Globals.MATHML_ENTITIES_MAP_XSL_RESOURCE_NAME).newTransformer();
            } else {
                /* Do character mapping serializer only */
                serializer = getStylesheet(Globals.SERIALIZE_WITH_NAMED_ENTITIES_XSL_RESOURCE_NAME, true).newTransformer();
            }
        } else {
            if (serializerUri != null) {
                /* Use given serializer */
                serializer = getStylesheet(serializerUri).newTransformer();
            } else {
                /* Use default serializer */
                serializer = getTransformerFactory(false).newTransformer();
            }
        }
    } catch (TransformerConfigurationException e) {
        throw new SnuggleRuntimeException("Could not create serializer", e);
    }
    /* Now configure it as per options */
    if (serializationOptions != null) {
        SerializationMethod serializationMethod = serializationOptions.getSerializationMethod();
        if (serializationMethod == SerializationMethod.XHTML && !supportsXSLT20) {
            /* Really want XHTML serialization, but we don't have an XSLT 2.0 processor
         * so downgrading to XML.
         */
            serializationMethod = SerializationMethod.XML;
        }
        serializer.setOutputProperty(OutputKeys.METHOD, serializationMethod.getName());
        serializer.setOutputProperty(OutputKeys.INDENT, StringUtilities.toYesNo(serializationOptions.isIndenting()));
        serializer.setOutputProperty(OutputKeys.ENCODING, serializationOptions.getEncoding());
        serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, StringUtilities.toYesNo(!serializationOptions.isIncludingXMLDeclaration()));
        if (serializationOptions.getDoctypePublic() != null) {
            serializer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, serializationOptions.getDoctypePublic());
        }
        if (serializationOptions.getDoctypeSystem() != null) {
            serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, serializationOptions.getDoctypeSystem());
        }
    }
    return serializer;
}
Also used : Transformer(javax.xml.transform.Transformer) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) SnuggleRuntimeException(uk.ac.ed.ph.snuggletex.SnuggleRuntimeException) SerializationMethod(uk.ac.ed.ph.snuggletex.SerializationMethod)

Aggregations

Transformer (javax.xml.transform.Transformer)1 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)1 SerializationMethod (uk.ac.ed.ph.snuggletex.SerializationMethod)1 SnuggleRuntimeException (uk.ac.ed.ph.snuggletex.SnuggleRuntimeException)1