001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.doxia.sink.impl;
020
021import java.io.File;
022import java.io.IOException;
023import java.io.OutputStream;
024import java.io.OutputStreamWriter;
025import java.io.Writer;
026import java.nio.charset.Charset;
027import java.nio.file.Files;
028import java.util.Objects;
029
030import org.apache.maven.doxia.sink.Sink;
031import org.apache.maven.doxia.sink.SinkFactory;
032
033/**
034 * An abstract <code>SinkFactory</code> for Text markup syntax. <code>UTF-8</code> is used
035 * when no encoding is specified.
036 *
037 * @author Hervé Boutemy
038 * @author Benjamin Bentmann
039 * @since 1.1
040 */
041public abstract class AbstractTextSinkFactory implements SinkFactory {
042    /**
043     * Create a text Sink for a given encoding.
044     *
045     * @param writer The writer for the sink output, never <code>null</code>.
046     * @param encoding The character encoding used by the writer.
047     * @return a Sink for text output in the given encoding.
048     */
049    protected abstract Sink createSink(Writer writer, String encoding);
050
051    public Sink createSink(File outputDir, String outputName) throws IOException {
052        return createSink(outputDir, outputName, "UTF-8");
053    }
054
055    public Sink createSink(File outputDir, String outputName, String encoding) throws IOException {
056        Objects.requireNonNull(outputDir, "outputDir cannot be null");
057
058        if (!outputDir.exists()) {
059            outputDir.mkdirs();
060        } else {
061            if (!outputDir.isDirectory()) {
062                throw new IllegalArgumentException("The dir '" + outputDir + "' is not a directory.");
063            }
064        }
065
066        Writer writer = Files.newBufferedWriter(new File(outputDir, outputName).toPath(), Charset.forName(encoding));
067        return createSink(writer, encoding);
068    }
069
070    public Sink createSink(OutputStream out) throws IOException {
071        return createSink(out, "UTF-8");
072    }
073
074    public Sink createSink(OutputStream out, String encoding) throws IOException {
075        return createSink(new OutputStreamWriter(out, encoding), encoding);
076    }
077}