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!) */
}
}
}
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;
}
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);
}
}
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);
}
}
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();
}
Aggregations