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.IOException; 024import java.io.OutputStream; 025import java.io.OutputStreamWriter; 026import java.io.Writer; 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 * @version $Id$ 039 * @since 1.1 040 */ 041public abstract class AbstractTextSinkFactory 042 implements SinkFactory 043{ 044 /** 045 * Create a text Sink for a given encoding. 046 * 047 * @param writer The writer for the sink output, never <code>null</code>. 048 * @param encoding The character encoding used by the writer. 049 * @return a Sink for text output in the given encoding. 050 */ 051 protected abstract Sink createSink( Writer writer, String encoding ); 052 053 /** {@inheritDoc} */ 054 public Sink createSink( File outputDir, String outputName ) 055 throws IOException 056 { 057 return createSink( outputDir, outputName, WriterFactory.UTF_8 ); 058 } 059 060 /** {@inheritDoc} */ 061 public Sink createSink( File outputDir, String outputName, String encoding ) 062 throws IOException 063 { 064 if ( outputDir == null ) 065 { 066 throw new IllegalArgumentException( "outputDir cannot be null." ); 067 } 068 069 if ( !outputDir.exists() ) 070 { 071 outputDir.mkdirs(); 072 } 073 else 074 { 075 if ( !outputDir.isDirectory() ) 076 { 077 throw new IllegalArgumentException( "The dir '" + outputDir + "' is not a directory." ); 078 } 079 } 080 081 Writer writer = WriterFactory.newWriter( new File( outputDir, outputName ), encoding ); 082 083 return createSink( writer, encoding ); 084 } 085 086 /** {@inheritDoc} */ 087 public Sink createSink( OutputStream out ) 088 throws IOException 089 { 090 return createSink( out, WriterFactory.UTF_8 ); 091 } 092 093 /** {@inheritDoc} */ 094 public Sink createSink( OutputStream out, String encoding ) 095 throws IOException 096 { 097 return createSink( new OutputStreamWriter( out, encoding ), encoding ); 098 } 099}