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

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

package com.crabshue.commons.xproc.handler;

import java.io.File;
import java.io.FileOutputStream;
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 FileOutputPortHandler implements OutputPortHandler {
	private final File parentFolder;

	public FileOutputPortHandler(File parentFolder) {
		super();
		this.parentFolder = parentFolder;
	}

	public File handle(Processor saxon, String portName, XdmNode resultNode) {
		try {
			File outputFile = File.createTempFile(portName, ".xml", parentFolder);
			FileOutputStream outputStream = new FileOutputStream(outputFile);
			try {
				saxon.newSerializer(outputStream).serializeNode(resultNode);
				outputStream.flush();
			} finally {
				IOUtils.closeQuietly(outputStream);
			}
			return outputFile;
		} catch (IOException | SaxonApiException e) {
			throw new ApplicationException(XProcErrorType.XPROC_CANNOT_RETRIEVE_OUTPUT,
					"Output port cannot be read or written to file.", e).addContextValue("port.name", portName);
		}

	};
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy