1 package org.apache.maven.doxia.sink;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.io.OutputStream;
25 import java.io.OutputStreamWriter;
26 import java.io.Writer;
27
28 import org.codehaus.plexus.util.WriterFactory;
29
30
31
32
33
34
35
36
37
38
39 public abstract class AbstractTextSinkFactory
40 implements SinkFactory
41 {
42
43
44
45
46
47
48
49 protected abstract Sink createSink( Writer writer, String encoding );
50
51
52 public Sink createSink( File outputDir, String outputName )
53 throws IOException
54 {
55 return createSink( outputDir, outputName, WriterFactory.UTF_8 );
56 }
57
58
59 public Sink createSink( File outputDir, String outputName, String encoding )
60 throws IOException
61 {
62 if ( outputDir == null )
63 {
64 throw new IllegalArgumentException( "outputDir cannot be null." );
65 }
66
67 if ( !outputDir.exists() )
68 {
69 outputDir.mkdirs();
70 }
71 else
72 {
73 if ( !outputDir.isDirectory() )
74 {
75 throw new IllegalArgumentException( "The dir '" + outputDir + "' is not a directory." );
76 }
77 }
78
79 Writer writer = WriterFactory.newWriter( new File( outputDir, outputName ), encoding );
80
81 return createSink( writer, encoding );
82 }
83
84
85 public Sink createSink( OutputStream out )
86 throws IOException
87 {
88 return createSink( out, WriterFactory.UTF_8 );
89 }
90
91
92 public Sink createSink( OutputStream out, String encoding )
93 throws IOException
94 {
95 return createSink( new OutputStreamWriter( out, encoding ), encoding );
96 }
97 }