use of uk.ac.ed.ph.snuggletex.SnuggleRuntimeException in project symja_android_library by axkr.
the class XMLUtilities method createJAXPTransformerFactory.
/**
* Creates an instance of the currently specified JAXP {@link TransformerFactory}, ensuring that
* the result supports the {@link DOMSource#FEATURE} and {@link DOMResult#FEATURE} features.
*/
public static TransformerFactory createJAXPTransformerFactory() {
TransformerFactory transformerFactory = null;
try {
transformerFactory = TransformerFactory.newInstance();
} catch (TransformerFactoryConfigurationError e) {
throw new SnuggleRuntimeException(e);
}
/* Make sure we have DOM-based features */
requireFeature(transformerFactory, DOMSource.FEATURE);
requireFeature(transformerFactory, DOMResult.FEATURE);
/* Must have been OK! */
return transformerFactory;
}
use of uk.ac.ed.ph.snuggletex.SnuggleRuntimeException in project symja_android_library by axkr.
the class MathMLDownConverter method downConvertDOM.
public Document downConvertDOM(Document document) {
/* If inlining CSS, create a document to hold the name/value pairs as described in
* buildCSSPropertiesDocument(). Otherwise, we'll create an empty one to indicate
* that nothing should be inlined */
Document cssPropertiesDocument = XMLUtilities.createNSAwareDocumentBuilder().newDocument();
if (cssProperties != null) {
buildCSSPropertiesDocument(cssPropertiesDocument, cssProperties);
}
/* Create URI Resolver to let the XSLT get at this document */
CSSPropertiesURIResolver uriResolver = new CSSPropertiesURIResolver(cssPropertiesDocument);
/* Run the conversion XSLT */
Templates templates = stylesheetManager.getStylesheet(Globals.MATHML_TO_XHTML_XSL_RESOURCE_NAME);
Document result = XMLUtilities.createNSAwareDocumentBuilder().newDocument();
try {
Transformer transformer = templates.newTransformer();
transformer.setURIResolver(uriResolver);
transformer.transform(new DOMSource(document), new DOMResult(result));
} catch (Exception e) {
throw new SnuggleRuntimeException("Unexpected Exception down-converting DOM", e);
}
return result;
}
use of uk.ac.ed.ph.snuggletex.SnuggleRuntimeException in project symja_android_library by axkr.
the class StylesheetManager method compileImporterStylesheet.
/**
* Helper to create "driver" XSLT stylesheets that import the stylesheets at the given URIs, using
* the given {@link TransformerFactory}.
*
* @param transformerFactory
* @param importUris
*/
private Templates compileImporterStylesheet(final TransformerFactory transformerFactory, boolean requireXSLT20, final String... importUris) {
/* Build up driver XSLT that simply imports the required stylesheets */
StringBuilder xsltBuilder = new StringBuilder("<stylesheet version='").append(requireXSLT20 ? "2.0" : "1.0").append("' xmlns='http://www.w3.org/1999/XSL/Transform'>\n");
for (String importUri : importUris) {
xsltBuilder.append("<import href='").append(importUri).append("'/>\n");
}
xsltBuilder.append("</stylesheet>");
String xslt = xsltBuilder.toString();
/* Now compile and return result */
try {
return transformerFactory.newTemplates(new StreamSource(new StringReader(xslt)));
} catch (TransformerConfigurationException e) {
throw new SnuggleRuntimeException("Could not compile stylesheet driver " + xslt, e);
}
}
use of uk.ac.ed.ph.snuggletex.SnuggleRuntimeException in project symja_android_library by axkr.
the class WebPageBuilder method setWebPageContentType.
/**
* Calls the <tt>setContentType</tt> method for the given Object (by reflection) to something
* appropriate for serving the types of web pages generated by this builder over HTTP.
*
* <p>(The main example for this would be passing a <tt>javax.servlet.http.HttpResponse</tt>
* Object, which I want to avoid a compile-time dependency on.)
*
* @param contentTypeSettable Object that will have its <tt>contentType</tt> property set if
* provided.
*/
public final void setWebPageContentType(Object contentTypeSettable) {
try {
Method setterMethod = contentTypeSettable.getClass().getMethod("setContentType", new Class<?>[] { String.class });
setterMethod.invoke(contentTypeSettable, computeContentTypeHeader());
} catch (Exception e) {
throw new SnuggleRuntimeException("Could not find and call setContentType() on Object " + contentTypeSettable, e);
}
}
use of uk.ac.ed.ph.snuggletex.SnuggleRuntimeException in project symja_android_library by axkr.
the class WebPageBuilder method createWebPage.
public final Document createWebPage(final List<FlowToken> fixedTokens) throws SnuggleParseException {
Document document = XMLUtilities.createNSAwareDocumentBuilder().newDocument();
/* Add in any client-side XSLT */
String[] clientXSLTURLs = options.getClientSideXSLTStylesheetURLs();
if (clientXSLTURLs != null) {
for (String url : clientXSLTURLs) {
/* (These go in at the top of the Document) */
document.appendChild(document.createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"" + url + "\""));
}
}
/* Create <body/> and maybe add title header */
Element body = document.createElementNS(W3CConstants.XHTML_NAMESPACE, "body");
String title = options.getTitle();
if (title != null && options.isAddingTitleHeading()) {
Element titleHeader = document.createElementNS(W3CConstants.XHTML_NAMESPACE, "h1");
titleHeader.appendChild(document.createTextNode(title));
body.appendChild(titleHeader);
}
/* Build <body/> */
DOMBuildingController domBuildingController = new DOMBuildingController(sessionContext, options);
domBuildingController.buildDOMSubtree(body, fixedTokens);
/* Build <head/> */
Element head = document.createElementNS(W3CConstants.XHTML_NAMESPACE, "head");
/* Do template-y stuff */
WebPageType pageType = options.getWebPageType();
if (pageType == WebPageType.MATHPLAYER_HTML) {
/* To trigger MathPlayer, we must declare the appropriate MathML prefix on the
* <html/> element. Then add an <object/> followed by the appropriate PI to
* the <head/> element. Getting any of this in the wrong order will fail
* to make MathPlayer work.
*/
Element object = document.createElementNS(W3CConstants.XHTML_NAMESPACE, "object");
object.setAttribute("id", "MathPlayer");
object.setAttribute("classid", "clsid:32F66A20-7614-11D4-BD11-00104BD3F987");
head.appendChild(object);
/* NOTE: We need to add the final '?' as we're outputting an HTML PI which normally
* doesn't have one, even though the docs for MathPlayer seem to expect one!
*/
head.appendChild(document.createProcessingInstruction("import", "namespace=\"" + options.getMathMLPrefix() + "\" implementation=\"#MathPlayer\" ?"));
}
/* Add content type <meta/> element. (The serializer might add another of these but let's
* be safe as we don't know what's going to happen at this point.) */
Element meta = document.createElementNS(W3CConstants.XHTML_NAMESPACE, "meta");
meta.setAttribute("http-equiv", "Content-Type");
meta.setAttribute("content", computeMetaContentType());
head.appendChild(meta);
/* Add common relevant metadata */
meta = document.createElementNS(W3CConstants.XHTML_NAMESPACE, "meta");
meta.setAttribute("name", "Generator");
meta.setAttribute("content", "SnuggleTeX");
head.appendChild(meta);
/* Add <title/>, if specified */
if (title != null) {
Element titleElement = document.createElementNS(W3CConstants.XHTML_NAMESPACE, "title");
titleElement.appendChild(document.createTextNode(options.getTitle()));
head.appendChild(titleElement);
}
/* Add any external CSS links */
String[] cssStylesheetURLs = options.getCSSStylesheetURLs();
if (cssStylesheetURLs != null) {
Element link;
for (String url : cssStylesheetURLs) {
link = document.createElementNS(W3CConstants.XHTML_NAMESPACE, "link");
link.setAttribute("rel", "stylesheet");
link.setAttribute("href", url);
head.appendChild(link);
}
}
/* Maybe add <style>...</style> section. */
if (options.isIncludingStyleElement()) {
Element style = document.createElementNS(W3CConstants.XHTML_NAMESPACE, "style");
style.setAttribute("type", "text/css");
Properties cssProperties = CSSUtilities.readInlineCSSProperties(options);
style.appendChild(document.createTextNode(CSSUtilities.writeStylesheet(cssProperties)));
head.appendChild(style);
}
/* Create finished document */
Element html = document.createElementNS(W3CConstants.XHTML_NAMESPACE, "html");
/* Add pref:renderer attribute if doing USS */
if (pageType == WebPageType.UNIVERSAL_STYLESHEET) {
html.setAttributeNS(W3CConstants.MATHML_PREF_NAMESPACE, "pref:renderer", "mathplayer-dl");
}
String lang = options.getLang();
if (lang != null) {
/* Set language either as 'xml:lang' or plain old 'lang', or maybe both */
if (pageType == WebPageType.MATHPLAYER_HTML || pageType == WebPageType.PROCESSED_HTML) {
html.setAttribute("lang", lang);
if (options.getSerializationMethod() != SerializationMethod.HTML) {
html.setAttributeNS(XMLConstants.XML_NS_URI, "xml:lang", lang);
}
} else {
html.setAttributeNS(XMLConstants.XML_NS_URI, "xml:lang", lang);
}
}
if (options.isPrefixingMathML()) {
/* We'll explicitly set the MathML prefix on the root element.
* (MathPlayer needs it to be declared here too.)
*/
html.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, "xmlns:" + options.getMathMLPrefix(), W3CConstants.MATHML_NAMESPACE);
}
html.appendChild(head);
html.appendChild(body);
document.appendChild(html);
/* Apply any extra XSLT specified in the options */
Transformer[] stylesheets = options.getStylesheets();
if (!ObjectUtilities.isNullOrEmpty(stylesheets)) {
for (Transformer stylesheet : stylesheets) {
DOMSource input = new DOMSource(document);
document = XMLUtilities.createNSAwareDocumentBuilder().newDocument();
try {
stylesheet.transform(input, new DOMResult(document));
} catch (TransformerException e) {
throw new SnuggleRuntimeException("Could not apply stylesheet " + stylesheet);
}
}
}
return document;
}
Aggregations