Project

Profile

Help

Support #3237 » ContentHandlerForwarder.java

Aniruddha Joag, 2017-06-01 16:53

 
package test.saxon.streaming.transformerhandler;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import java.util.function.Consumer;

public class ContentHandlerForwarder implements ContentHandler
{
private ContentHandler contentHandlerToForwardTo;
private final Runnable runMainTransform;
private final Consumer<ContentHandler> transformerHandler = new Consumer<ContentHandler>()
{
@Override
public void accept(ContentHandler contentHandler)
{
contentHandlerToForwardTo = contentHandler;
doMainTransform();
}
};


private void doMainTransform()
{
runMainTransform.run();
}

private final SAXSource saxSource;
private final Transformer transformer;
private final StreamResult result;
public ContentHandlerForwarder(Transformer transformer, StreamResult result, Runnable runMainTransform) throws TransformerException
{
this.result = result;
this.transformer = transformer;
this.runMainTransform = runMainTransform;
PseudoXMLReader pseudoXMLReader = new PseudoXMLReader(transformerHandler);
saxSource = new SAXSource(pseudoXMLReader, null);
}

public void initiateTransformAssignReceivingContentHandler() throws TransformerException
{
System.out.println("ContentHandlerForwarder -> transform");
transformer.transform(saxSource, result);
}

@Override
public void setDocumentLocator(Locator locator)
{
contentHandlerToForwardTo.setDocumentLocator(locator);
}

@Override
public void startDocument() throws SAXException
{
System.out.println("startDocument()");
contentHandlerToForwardTo.startDocument();
}

@Override
public void endDocument() throws SAXException
{
System.out.println("endDocument()");
contentHandlerToForwardTo.endDocument();
}

@Override
public void startPrefixMapping(String prefix, String uri) throws SAXException
{
contentHandlerToForwardTo.startPrefixMapping(prefix, uri);
}

@Override
public void endPrefixMapping(String prefix) throws SAXException
{
contentHandlerToForwardTo.endPrefixMapping(prefix);
}

@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException
{
System.out.println("startElement()");
contentHandlerToForwardTo.startElement(uri, localName, qName, atts);
}

@Override
public void endElement(String uri, String localName, String qName) throws SAXException
{
System.out.println("endElement()");
contentHandlerToForwardTo.endElement(uri, localName, qName);
}

@Override
public void characters(char[] ch, int start, int length) throws SAXException
{
contentHandlerToForwardTo.characters(ch, start, length);
}

@Override
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException
{
contentHandlerToForwardTo.ignorableWhitespace(ch, start, length);
}

@Override
public void processingInstruction(String target, String data) throws SAXException
{
contentHandlerToForwardTo.processingInstruction(target, data);
}

@Override
public void skippedEntity(String name) throws SAXException
{
contentHandlerToForwardTo.skippedEntity(name);
}
}
(7-7/15)