View Javadoc
1   package org.apache.maven.doxia.sink.impl;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.FileOutputStream;
24  import java.io.IOException;
25  import java.io.OutputStream;
26  
27  import org.apache.maven.doxia.sink.Sink;
28  import org.apache.maven.doxia.sink.SinkFactory;
29  import org.codehaus.plexus.util.WriterFactory;
30  
31  /**
32   * An abstract <code>SinkFactory</code> for binary output.
33   *
34   * @author <a href="mailto:hboutemy@apache.org">Hervé Boutemy</a>
35   * @since 1.1
36   */
37  public abstract class AbstractBinarySinkFactory
38      implements SinkFactory
39  {
40      /** {@inheritDoc} */
41      public Sink createSink( File outputDir, String outputName )
42          throws IOException
43      {
44          return createSink( outputDir, outputName, WriterFactory.UTF_8 );
45      }
46  
47      /** {@inheritDoc} */
48      public Sink createSink( File outputDir, String outputName, String encoding )
49          throws IOException
50      {
51          if ( outputDir == null )
52          {
53              throw new IllegalArgumentException( "outputDir cannot be null." );
54          }
55  
56          if ( !outputDir.exists() )
57          {
58              outputDir.mkdirs();
59          }
60          else
61          {
62              if ( !outputDir.isDirectory() )
63              {
64                  throw new IllegalArgumentException( "The dir '" + outputDir + "' is not a directory." );
65              }
66          }
67  
68          OutputStream out = new FileOutputStream( new File( outputDir, outputName ) );
69  
70          return createSink( out, encoding );
71      }
72  
73      /** {@inheritDoc} */
74      public Sink createSink( OutputStream out )
75          throws IOException
76      {
77          return createSink( out, WriterFactory.UTF_8 );
78      }
79  }