Project

Profile

Help

Support #5254 » SvgNamespace.java

Test Program - Dansker Dave, 2022-01-30 14:00

 
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.*;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import net.sf.saxon.jaxp.SaxonTransformerFactory;

public class SvgNamespace {

private static final String DATA_NODE_NAME = "DataNodeName";

private static final String DATA_XML = """
<?xml version="1.0" encoding="UTF-8"?><""" + DATA_NODE_NAME + "/>";

private static final String STYLESHEET = """
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"

version="3.0">

<xsl:template match=""" + quote(DATA_NODE_NAME) + """
>
<fo:root>
<fo:layout-master-set>
<fo:simple-page-master master-name="singlePage" page-width="297mm" page-height="210mm">
<fo:region-body margin-left="2.19mm" margin-top="6.75mm"/>
</fo:simple-page-master>
</fo:layout-master-set>

<fo:page-sequence master-reference="singlePage">
<fo:flow flow-name="xsl-region-body">
<fo:block>
<fo:instream-foreign-object content-width="272.6mm" content-type="image/svg+xml">
<svg xmlns="http://www.w3.org/2000/svg" width="1184" height="775" viewBox="0 0 1184 775">
<g style="fill-opacity:0;stroke-width:7;stroke:red">
<rect width="254" height="99"/>
</g>
</svg>
</fo:instream-foreign-object>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
</xsl:stylesheet>
""";

private static final String quote(final String value) {
return '"' + value + '"';
}

private static Document parse(final byte[] byteArray) throws ParserConfigurationException, SAXException, IOException {

final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
final DocumentBuilder dbd = dbf.newDocumentBuilder();
final Document doc = dbd.parse(new ByteArrayInputStream(byteArray));

System.out.println("DocumentBuilderFactory.: " + dbf.getClass());
System.out.println("DocumentBuilder........: " + dbd.getClass());
System.out.println("Document...............: " + doc.getClass());

return doc;
}

public static void main(final String[] args) throws Exception {

System.getProperties().forEach((key, value) -> {
System.out.println("System Property........: " + key + '=' + value);
});

System .out.println("Data XML...............:" + '\n' + DATA_XML + '\n');
System .out.println("Stylesheet.............:" + '\n' + STYLESHEET);

try(final ByteArrayInputStream ist = new ByteArrayInputStream (DATA_XML.getBytes());
final ByteArrayOutputStream ost = new ByteArrayOutputStream();)
{
final Document stylesheetDoc = parse(STYLESHEET.getBytes());
final TransformerFactory transformerFactory = SaxonTransformerFactory.newInstance();

final Templates templates = transformerFactory.newTemplates(new DOMSource(stylesheetDoc));
final Transformer transformer = templates.newTransformer();

transformer.transform(new StreamSource(ist), new StreamResult(ost));

final String foString = new String(ost.toByteArray());

System.out.println("TransformerFactory.....: " + transformerFactory.getClass());
System.out.println("FO Bytes...............:" + '\n' + foString);
System.out.println( foString.replace(">", ">" + '\n'));
}
}
}
(1-1/3)