public SVGDocument domToSvgDom(final Document dom)
throws GraphicException {
/* Do some basic validation of the DOM document. */
final Element rootElement = dom.getDocumentElement();
if (! MathMLUtil.MATHML_NAMESPACE.equals(rootElement.getNamespaceURI())) {
throw new GraphicException("Document is not in the MathML "
+ "namespace");
}
final String rootElementName = rootElement.getLocalName();
if (! MathMLUtil.MATHML_ROOT_ELEMENT_NAME.equals(rootElementName)) {
throw new GraphicException("Expected root element <math> for "
+ "MathML document, but was: " + rootElementName);
}
final MathBase mathBase = new MathBase(MathBase.getDefaultParameters());
new DOMBuilder(dom, mathBase);
final SVGGraphics2D svgGenerator = this.createSVGGenerator(mathBase);
mathBase.paint(svgGenerator);
/* The following line is what /should/ work. However, there appears to
* be some disconnect in Batik between the Document and the root
* Element. The root Element recognizes the Document as its Document,
* but the Document does not see the root Element as its root Element
* ... */
// final Document svgDocument = svgGenerator.getDOMFactory();
final Element svgRoot = svgGenerator.getRoot();
/* The variable svgDocument below is the same object as the one
* commented out above ... */
final Document svgDocument = svgRoot.getOwnerDocument();
if (svgDocument == null) {
throw new GraphicException("Error converting MathML to SVG: "
+ "Document is null.");
}
/* ... however, the root element below is /NOT/ the same as the svgRoot
* variable above ... */
final Element svgRoot2 = svgDocument.getDocumentElement();
/* ... so we need to get the Document and the root element hooked up. */
svgDocument.removeChild(svgRoot2);
svgDocument.appendChild(svgRoot);
if (! (svgDocument instanceof SVGDocument)) {
throw new GraphicException("Error converting MathML to SVG: "
+ "Converted document is not SVG.");
}
return (SVGDocument) svgDocument;
}