View Javadoc

1   /*
2    * Copyright 2010-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.testdoc.core.export;
17  
18  import java.io.IOException;
19  import java.io.OutputStream;
20  
21  import org.apache.commons.io.IOUtils;
22  
23  import de.smartics.testdoc.core.doc.OutputManager;
24  import de.smartics.testdoc.core.doc.UnitTestDoc;
25  
26  /**
27   * Base implementation of the {@link OutputManager} interface.
28   *
29   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
30   * @version $Revision:591 $
31   */
32  public abstract class AbstractOutputManager implements OutputManager
33  {
34    // ********************************* Fields *********************************
35  
36    // --- constants ------------------------------------------------------------
37  
38    // --- members --------------------------------------------------------------
39  
40    /**
41     * The exporter that knows the format of the file to be written.
42     */
43    protected final UnitTestDocExporter exporter;
44  
45    // ****************************** Initializer *******************************
46  
47    // ****************************** Constructors ******************************
48  
49    /**
50     * Default constructor.
51     *
52     * @param exporter the exporter that knows the format of the file to be
53     *          written.
54     */
55    protected AbstractOutputManager(final UnitTestDocExporter exporter)
56    {
57      this.exporter = exporter;
58    }
59  
60    // ****************************** Inner Classes *****************************
61  
62    /**
63     * {@inheritDoc}
64     *
65     * @see de.smartics.testdoc.core.doc.OutputManager#init()
66     */
67    @Override
68    public void init()
69    {
70    }
71  
72    /**
73     * {@inheritDoc}
74     *
75     * @see de.smartics.testdoc.core.doc.OutputManager#export(de.smartics.testdoc.core.doc.UnitTestDoc)
76     */
77    @Override
78    public void export(final UnitTestDoc testDoc) throws IOException
79    {
80      final OutputStream output = openStreamFor(testDoc);
81      try
82      {
83        exporter.export(testDoc, output);
84      }
85      finally
86      {
87        IOUtils.closeQuietly(output);
88      }
89    }
90  
91    /**
92     * {@inheritDoc}
93     *
94     * @see de.smartics.testdoc.core.doc.OutputManager#finish()
95     */
96    @Override
97    public void finish()
98    {
99    }
100 
101   /**
102    * Creates a stream to write the given document to.
103    *
104    * @param testDoc the documentation to open a stream to write it to.
105    * @return the opened stream. The client is responsible to close it.
106    * @throws IOException if the stream cannot be opened.
107    * @throws NullPointerException if <code>testDoc</code> is <code>null</code>.
108    */
109   protected abstract OutputStream openStreamFor(final UnitTestDoc testDoc)
110     throws IOException, NullPointerException;
111 
112   // --- object basics --------------------------------------------------------
113 
114 }