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.util.Objects;
027
028import org.apache.maven.doxia.sink.Sink;
029import org.apache.maven.doxia.sink.SinkFactory;
030import org.codehaus.plexus.util.WriterFactory;
031
032/**
033 * An abstract <code>SinkFactory</code> for Text markup syntax. <code>UTF-8</code> is used
034 * when no encoding is specified.
035 *
036 * @author Hervé Boutemy
037 * @author Benjamin Bentmann
038 * @since 1.1
039 */
040public abstract class AbstractTextSinkFactory implements SinkFactory {
041    /**
042     * Create a text Sink for a given encoding.
043     *
044     * @param writer The writer for the sink output, never <code>null</code>.
045     * @param encoding The character encoding used by the writer.
046     * @return a Sink for text output in the given encoding.
047     */
048    protected abstract Sink createSink(Writer writer, String encoding);
049
050    /** {@inheritDoc} */
051    public Sink createSink(File outputDir, String outputName) throws IOException {
052        return createSink(outputDir, outputName, WriterFactory.UTF_8);
053    }
054
055    /** {@inheritDoc} */
056    public Sink createSink(File outputDir, String outputName, String encoding) throws IOException {
057        Objects.requireNonNull(outputDir, "outputDir cannot be null");
058
059        if (!outputDir.exists()) {
060            outputDir.mkdirs();
061        } else {
062            if (!outputDir.isDirectory()) {
063                throw new IllegalArgumentException("The dir '" + outputDir + "' is not a directory.");
064            }
065        }
066
067        Writer writer = WriterFactory.newWriter(new File(outputDir, outputName), encoding);
068
069        return createSink(writer, encoding);
070    }
071
072    /** {@inheritDoc} */
073    public Sink createSink(OutputStream out) throws IOException {
074        return createSink(out, WriterFactory.UTF_8);
075    }
076
077    /** {@inheritDoc} */
078    public Sink createSink(OutputStream out, String encoding) throws IOException {
079        return createSink(new OutputStreamWriter(out, encoding), encoding);
080    }
081}