Search in sources :

Example 1 with SnuggleRuntimeException

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

the class WebPageBuilder method writeWebPage.

/**
 * Creates a web page representing the given (fixed) Tokens, and writes the results to the given
 * {@link OutputStream}.
 *
 * @param fixedTokens fixed Tokens from earlier stages of parsing
 * @param contentTypeSettable optional bean Object that will have its <tt>contentType</tt>
 *     property set if provided. (This is generally useful as a proxy for the
 *     <tt>HttpResponse</tt> Object in the servlet API, which I want to avoid a compile-time
 *     dependency on.)
 * @param outputStream Stream to send the resulting page to, which will be closed afterwards.
 * @param endOutputOptions specifies what to do with the outputStream once we've finished writing
 *     to it.
 * @throws SnuggleParseException
 * @throws IOException
 * @throws SnuggleRuntimeException if calling <tt>setContentType()</tt> on the contentTypeSettable
 *     Object failed, with the underlying Exception wrapped up.
 */
public final void writeWebPage(final List<FlowToken> fixedTokens, Object contentTypeSettable, final OutputStream outputStream, final EndOutputAction endOutputOptions) throws SnuggleParseException, IOException {
    /* Set content type, if requested */
    if (contentTypeSettable != null) {
        setWebPageContentType(contentTypeSettable);
    }
    /* Create resulting web page, including any client-specified XSLT */
    Document webPageDocument = createWebPage(fixedTokens);
    /* Finally serialize */
    Transformer serializer = createSerializer();
    try {
        serializer.transform(new DOMSource(webPageDocument), new StreamResult(outputStream));
    } catch (TransformerException e) {
        throw new SnuggleRuntimeException("Could not serialize web page", e);
    } finally {
        if (endOutputOptions == EndOutputAction.CLOSE) {
            outputStream.close();
        } else if (endOutputOptions == EndOutputAction.FLUSH) {
            outputStream.flush();
        } else {
        /* (Do nothing!) */
        }
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) SnuggleRuntimeException(uk.ac.ed.ph.snuggletex.SnuggleRuntimeException) Document(org.w3c.dom.Document) TransformerException(javax.xml.transform.TransformerException)

Example 2 with SnuggleRuntimeException

use of uk.ac.ed.ph.snuggletex.SnuggleRuntimeException 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)

Example 3 with SnuggleRuntimeException

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

the class StylesheetManager method compileInternalStylesheet.

private Templates compileInternalStylesheet(final String classPathUri, final boolean requireXSLT20) {
    TransformerFactory transformerFactory = getTransformerFactory(requireXSLT20);
    Source resolved;
    try {
        resolved = transformerFactory.getURIResolver().resolve(classPathUri, "");
        if (resolved == null) {
            throw new SnuggleRuntimeException("Not a ClassPath URI: " + classPathUri);
        }
        return transformerFactory.newTemplates(resolved);
    } catch (TransformerConfigurationException e) {
        throw new SnuggleRuntimeException("Could not compile internal stylesheet at " + classPathUri, e);
    } catch (TransformerException e) {
        throw new SnuggleRuntimeException("Could not resolve internal stylesheet location " + classPathUri, e);
    }
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) SnuggleRuntimeException(uk.ac.ed.ph.snuggletex.SnuggleRuntimeException) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) TransformerException(javax.xml.transform.TransformerException)

Example 4 with SnuggleRuntimeException

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

the class XMLUtilities method createNSAwareDocumentBuilder.

// ------------------------------------------------------------------
/**
 * Creates a (namespace-aware) DOM {@link DocumentBuilder}, throwing a {@link
 * SnuggleRuntimeException} if such a thing cannot be created/configured.
 */
public static DocumentBuilder createNSAwareDocumentBuilder() {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    try {
        return documentBuilderFactory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new SnuggleRuntimeException("Could not create Namespace-aware DocumentBuilder", e);
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) SnuggleRuntimeException(uk.ac.ed.ph.snuggletex.SnuggleRuntimeException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 5 with SnuggleRuntimeException

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

the class XMLUtilities method serializeNode.

/**
 * Serializes the given {@link Node} to a well-formed external parsed entity. (If the given Node
 * is an {@link Element} or a {@link Document} then the result will be a well-formed XML String.)
 *
 * <p>(This may use an XSLT 2.0 stylesheet to help with named entities, hence the requirement for
 * a {@link StylesheetManager}).
 *
 * @param stylesheetManager used to help compile and cache stylesheets used in this process.
 * @param node DOM Node to serialize.
 * @param serializationOptions XML serialization options
 */
public static String serializeNode(StylesheetManager stylesheetManager, final Node node, final SerializationSpecifier serializationOptions) {
    StringWriter resultWriter = new StringWriter();
    /* This process consists of an XSLT 1.0 transform to extract the child Nodes, plus
     * a further optional XSLT 2.0 transform to map character references to named entities.
     */
    try {
        Transformer serializer = stylesheetManager.getSerializer(null, serializationOptions);
        serializer.transform(new DOMSource(node), new StreamResult(resultWriter));
    } catch (Exception e) {
        throw new SnuggleRuntimeException("Could not serialize DOM", e);
    }
    return resultWriter.toString();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) SnuggleRuntimeException(uk.ac.ed.ph.snuggletex.SnuggleRuntimeException) SnuggleRuntimeException(uk.ac.ed.ph.snuggletex.SnuggleRuntimeException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

SnuggleRuntimeException (uk.ac.ed.ph.snuggletex.SnuggleRuntimeException)12 Transformer (javax.xml.transform.Transformer)6 DOMSource (javax.xml.transform.dom.DOMSource)5 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)4 TransformerException (javax.xml.transform.TransformerException)4 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)3 TransformerFactory (javax.xml.transform.TransformerFactory)3 StreamResult (javax.xml.transform.stream.StreamResult)3 Document (org.w3c.dom.Document)3 StringWriter (java.io.StringWriter)2 DOMResult (javax.xml.transform.dom.DOMResult)2 StreamSource (javax.xml.transform.stream.StreamSource)2 SerializationMethod (uk.ac.ed.ph.snuggletex.SerializationMethod)2 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 Method (java.lang.reflect.Method)1 Properties (java.util.Properties)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 Source (javax.xml.transform.Source)1 Templates (javax.xml.transform.Templates)1