Project

Profile

Help

Support #5254 » SvgNamespaceStreamSource.java

StreamSource Version - Dansker Dave, 2022-01-30 16:41

 
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import net.sf.saxon.jaxp.SaxonTransformerFactory;

public final class SvgNamespaceStreamSource {

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:XXX="http://www.exclude.all.unused.namespace.prefixes.org/2022/XSL/Exclude"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"

exclude-result-prefixes="#all"

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 + '"';
}

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

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

try(final InputStream xml = new ByteArrayInputStream (DATA_XML .getBytes());
final InputStream xsl = new ByteArrayInputStream (STYLESHEET.getBytes());
final ByteArrayOutputStream ost = new ByteArrayOutputStream();)
{
final TransformerFactory transformerFactory = SaxonTransformerFactory.newInstance();

final Templates templates = transformerFactory.newTemplates(new StreamSource(xsl));
final Transformer transformer = templates.newTransformer();

transformer.transform(new StreamSource(xml), 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'));
}
}
}
(3-3/3)