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