All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.crabshue.commons.xproc.handler.StringOutputPortHandler Maven / Gradle / Ivy

package com.crabshue.commons.xproc.handler;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.apache.commons.io.IOUtils;

import com.crabshue.commons.exceptions.ApplicationException;
import com.crabshue.commons.xproc.OutputPortHandler;
import com.crabshue.commons.xproc.exceptions.XProcErrorType;
import net.sf.saxon.s9api.Processor;
import net.sf.saxon.s9api.SaxonApiException;
import net.sf.saxon.s9api.XdmNode;
public class StringOutputPortHandler implements OutputPortHandler {
	public String handle(Processor saxon, String portName, XdmNode resultNode) {
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		try {
			saxon.newSerializer(outputStream).serializeNode(resultNode);
			outputStream.flush();
			return outputStream.toString("UTF-8");
		} catch (IOException | SaxonApiException e) {
			throw new ApplicationException(XProcErrorType.XPROC_CANNOT_RETRIEVE_OUTPUT, "Output port cannot be read.")
					.addContextValue("port.name", portName);
		} finally {
			IOUtils.closeQuietly(outputStream);
		}

	};
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy