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   * @version $Id: AbstractBinarySinkFactory.html 979316 2016-02-02 21:51:43Z hboutemy $
36   * @since 1.1
37   */
38  public abstract class AbstractBinarySinkFactory
39      implements SinkFactory
40  {
41      /** {@inheritDoc} */
42      public Sink createSink( File outputDir, String outputName )
43          throws IOException
44      {
45          return createSink( outputDir, outputName, WriterFactory.UTF_8 );
46      }
47  
48      /** {@inheritDoc} */
49      public Sink createSink( File outputDir, String outputName, String encoding )
50          throws IOException
51      {
52          if ( outputDir == null )
53          {
54              throw new IllegalArgumentException( "outputDir cannot be null." );
55          }
56  
57          if ( !outputDir.exists() )
58          {
59              outputDir.mkdirs();
60          }
61          else
62          {
63              if ( !outputDir.isDirectory() )
64              {
65                  throw new IllegalArgumentException( "The dir '" + outputDir + "' is not a directory." );
66              }
67          }
68  
69          OutputStream out = new FileOutputStream( new File( outputDir, outputName ) );
70  
71          return createSink( out, encoding );
72      }
73  
74      /** {@inheritDoc} */
75      public Sink createSink( OutputStream out )
76          throws IOException
77      {
78          return createSink( out, WriterFactory.UTF_8 );
79      }
80  }