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