001/*
002 *  Copyright 2018 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.extraction.execution.pipeline;
017
018import java.io.IOException;
019import java.io.OutputStream;
020import java.nio.file.Files;
021import java.nio.file.Path;
022import java.util.Optional;
023
024import javax.xml.transform.TransformerFactory;
025import javax.xml.transform.sax.SAXTransformerFactory;
026import javax.xml.transform.sax.TransformerHandler;
027import javax.xml.transform.stream.StreamResult;
028
029import org.ametys.core.util.LambdaUtils;
030
031/**
032 * Convenient method for {@link Pipeline} API implementations
033 */
034public final class Pipelines
035{
036    private static SAXTransformerFactory __transformerFactory;
037    
038    private Pipelines()
039    {
040        // Nothing
041    }
042    
043    /**
044     * Gets a {@link SAXTransformerFactory}
045     * @return a {@link SAXTransformerFactory}
046     */
047    public static SAXTransformerFactory getSaxTransformerFactory()
048    {
049        if (__transformerFactory == null)
050        {
051            __transformerFactory = (SAXTransformerFactory) TransformerFactory.newInstance();
052        }
053        return __transformerFactory;
054    }
055    
056    /**
057     * Gets the output stream of the given file (which will be created if it does not exist).
058     * @param outputFile The output file. Will be created if needed.
059     * @return the output stream
060     * @throws IOException if an I/O error occured
061     */
062    public static OutputStream getOutputStream(Path outputFile) throws IOException
063    {
064        outputFile.getParent().toFile().mkdirs();
065        OutputStream resultOs = Files.newOutputStream(outputFile);
066        return resultOs;
067    }
068    
069    /**
070     * Sets the result of a transformer handler into an output stream.
071     * @param transformerHandler The transformer handler
072     * @param outputStream The output stream
073     * @return the stream result
074     * @throws IOException if an I/O error occured
075     */
076    public static StreamResult setResult(TransformerHandler transformerHandler, OutputStream outputStream) throws IOException
077    {
078        StreamResult streamResult = new StreamResult(outputStream);
079        transformerHandler.setResult(streamResult);
080        return streamResult;
081    }
082    
083    /**
084     * Close the given stream result
085     * @param result the stream result
086     */
087    public static void close(StreamResult result)
088    {
089        Optional.ofNullable(result)
090                .map(StreamResult::getOutputStream)
091                .ifPresent(LambdaUtils.wrapConsumer(OutputStream::close));
092    }
093}