Project

Profile

Help

Support #3237 » Program.java

Aniruddha Joag, 2017-06-29 11:35

 
package test.saxon.streaming.transformerhandler;

import com.hsbc.dsl.dob.tdcf.strm.utils.StrmUtils;
import com.saxonica.config.EnterpriseConfiguration;
import com.saxonica.config.StreamingTransformerFactory;

import javax.xml.transform.*;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.StringReader;

public class Program
{

private static final String strXslt =
"<xsl:stylesheet version=\"3.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\" >\n" +
" <xsl:output method=\"xml\" version=\"1.0\" encoding=\"UTF-8\" indent=\"yes\"/>\n" +
" <xsl:mode streamable=\"yes\" on-no-match=\"shallow-skip\"/>\n" +
" <xsl:mode name=\"row\" streamable=\"no\"/>\n" +
"\n" +
" <xsl:template match=\"/\">\n" +
" <rows>\n" +
" <xsl:apply-templates select=\"copy-of(rows/row)\" mode=\"row\"/>\n" +
" </rows>\n" +
" </xsl:template>\n" +
"\n" +
" <xsl:template match=\"row\" mode=\"row\">\n" +
" <row>\n" +
" <x-name><xsl:value-of select=\"name\"/></x-name>\n" +
" <x-id><xsl:value-of select=\"id\"/></x-id>\n" +
" </row>\n" +
" </xsl:template>\n" +
"\n" +
"</xsl:stylesheet>";

//private static final int NUM_ROWS = 1;
private static final String RESULT_FILE1 = "C://swdtools/delete_later/fork1.xml";
private static final String RESULT_FILE2 = "C://swdtools/delete_later/fork2.xml";
private static final String RESULT_FILE3 = "C://swdtools/delete_later/fork3.xml";

private static final int NUM_ROWS = 10;
public static void main(String[] args) throws TransformerException, FileNotFoundException
{
//straightTransform();
forwardingHandlerTransform();
}

private static void straightTransform() throws TransformerException
{
SAXSource saxSource = new SAXSource(new RowXMLReader(NUM_ROWS), null);
Result result = new StreamResult(System.out);
getTransformer(strXslt).transform(saxSource, result);
}

private static void forwardingHandlerTransform() throws TransformerException, FileNotFoundException
{
final String newXslt1 = strXslt.replace("x-", "y-").replace("\"name\"", "\"x-name\"").replace("\"id\"", "\"x-id\"");
final String newXslt3 = strXslt.replace("x-", "z-").replace("\"name\"", "\"x-name\"").replace("\"id\"", "\"x-id\"");
final Transformer forkingTransformer1 = getTransformer(newXslt1);
final Transformer forkingTransformer2 = StrmUtils.getIdentityTransformer();
final Transformer forkingTransformer3 = getTransformer(newXslt3);

Transformer mainTransformer = getTransformer(strXslt);
SAXSource mainSource = new SAXSource(new RowXMLReader(NUM_ROWS), null);
Transformation forkingTransformation1 = new Transformation(forkingTransformer1, new StreamResult(new File(RESULT_FILE1)));
Transformation forkingTransformation2 = new Transformation(forkingTransformer2, new StreamResult(new File(RESULT_FILE2)));
Transformation forkingTransformation3 = new Transformation(forkingTransformer3, new StreamResult(new File(RESULT_FILE3)));

ContentHandlerForwarder.forkResult(mainTransformer, mainSource, forkingTransformation1, forkingTransformation2, forkingTransformation3);
}


private static Transformer getTransformer(String stringXslt) throws TransformerConfigurationException
{
final StreamingTransformerFactory transformerFactory = new StreamingTransformerFactory(new EnterpriseConfiguration());
return transformerFactory.newTransformer(new StreamSource(new StringReader(stringXslt)));
}
}
(13-13/15)