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 * @since 1.1
036 */
037public abstract class AbstractBinarySinkFactory
038    implements SinkFactory
039{
040    /** {@inheritDoc} */
041    public Sink createSink( File outputDir, String outputName )
042        throws IOException
043    {
044        return createSink( outputDir, outputName, WriterFactory.UTF_8 );
045    }
046
047    /** {@inheritDoc} */
048    public Sink createSink( File outputDir, String outputName, String encoding )
049        throws IOException
050    {
051        if ( outputDir == null )
052        {
053            throw new IllegalArgumentException( "outputDir cannot be null." );
054        }
055
056        if ( !outputDir.exists() )
057        {
058            outputDir.mkdirs();
059        }
060        else
061        {
062            if ( !outputDir.isDirectory() )
063            {
064                throw new IllegalArgumentException( "The dir '" + outputDir + "' is not a directory." );
065            }
066        }
067
068        OutputStream out = new FileOutputStream( new File( outputDir, outputName ) );
069
070        return createSink( out, encoding );
071    }
072
073    /** {@inheritDoc} */
074    public Sink createSink( OutputStream out )
075        throws IOException
076    {
077        return createSink( out, WriterFactory.UTF_8 );
078    }
079}