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.impl;
017
018import java.io.OutputStream;
019import java.util.Map;
020import java.util.Properties;
021
022import javax.xml.transform.OutputKeys;
023import javax.xml.transform.sax.TransformerHandler;
024
025import org.ametys.plugins.extraction.execution.pipeline.PipelineSerializerModel;
026
027/**
028 * Model for text pipeline serializers
029 */
030public class TextPipelineSerializerModel implements PipelineSerializerModel
031{
032    @Override
033    public String getDefaultFileExtension()
034    {
035        return "txt";
036    }
037    
038    @Override
039    public PipelineSerializer newSerializer(TransformerHandler handler, OutputStream out, Map<String, String> outputParameters)
040    {
041        return new TextPipelineSerializer(handler, out, outputParameters);
042    }
043    
044    private static final class TextPipelineSerializer extends AbstractSerializerImpl
045    {
046        TextPipelineSerializer(TransformerHandler handler, OutputStream out, Map<String, String> outputParameters)
047        {
048            super(handler, out);
049            _setOutputProps(outputParameters);
050        }
051        
052        private void _setOutputProps(Map<String, String> outputParameters)
053        {
054            Properties properties = new Properties();
055            properties.put(OutputKeys.METHOD, outputParameters.getOrDefault("method", "text"));
056            properties.put(OutputKeys.ENCODING, outputParameters.getOrDefault("encoding", "UTF-8"));
057            _handler.getTransformer().setOutputProperties(properties);
058        }
059        
060        @Override
061        public void serialize() throws Exception
062        {
063            // Nothing
064        }
065    }
066}