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.adapter;
17  
18  import java.io.File;
19  import java.io.IOException;
20  import java.net.URI;
21  
22  import javax.annotation.processing.Filer;
23  import javax.lang.model.element.Element;
24  import javax.tools.FileObject;
25  import javax.tools.StandardLocation;
26  
27  import de.smartics.testdoc.core.doc.FileHelper;
28  import de.smartics.testdoc.core.doc.Type;
29  
30  /**
31   * Exports the documentation fragments to a directory via the APT filer.
32   *
33   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
34   * @version $Revision:591 $
35   */
36  public class FilerExportAdapter extends AbstractFileBasedExportAdapter
37  {
38    // ********************************* Fields *********************************
39  
40    // --- constants ------------------------------------------------------------
41  
42    // --- members --------------------------------------------------------------
43  
44    /**
45     * The filer to create resources.
46     */
47    private final Filer filer;
48  
49    // ****************************** Initializer *******************************
50  
51    // ****************************** Constructors ******************************
52  
53    /**
54     * Default constructor.
55     */
56    public FilerExportAdapter(final Filer filer)
57    {
58      this.filer = filer;
59    }
60  
61    // ****************************** Inner Classes *****************************
62  
63    // ********************************* Methods ********************************
64  
65    // --- init -----------------------------------------------------------------
66  
67    // --- get&set --------------------------------------------------------------
68  
69    // --- business -------------------------------------------------------------
70  
71    @Override
72    protected File[] createFileRefs(final Type testCaseType)
73      throws IllegalStateException
74    {
75      try
76      {
77        final FileObject fileObject =
78            filer.createResource(StandardLocation.SOURCE_OUTPUT,
79                "de.smartics.testdoc.ser", testCaseType.toString() + ".ser",
80                (Element[]) null);
81        final File file = createFile(fileObject);
82        final File dir = file.getParentFile();
83        return createFileRefs(testCaseType, dir);
84      }
85      catch (IOException e)
86      {
87        throw new IllegalStateException(
88            "Cannot create file object to for test case type '" + testCaseType
89                + "'.");
90      }
91    }
92  
93    private File createFile(final FileObject fileObject)
94    {
95      final URI uri = fileObject.toUri();
96      try
97      {
98        final File file = new File(uri);
99        return file;
100     }
101     catch (final IllegalArgumentException e)
102     {
103       throw new IllegalStateException("Cannot create file with URI '" + uri
104                                       + "'.", e);
105     }
106   }
107 
108   @Override
109   protected File createFileRef(final Type testCaseType, final Type uutType)
110   {
111     try
112     {
113       final FileObject fileObject =
114           filer.createResource(StandardLocation.SOURCE_OUTPUT,
115               "de.smartics.testdoc.ser", testCaseType.toString() + '-'
116                                          + uutType.toString() + ".ser",
117               (Element[]) null);
118       final File file = new File(fileObject.toUri());
119       FileHelper.provideDir(file.getParentFile());
120       return file;
121     }
122     catch (final IOException e)
123     {
124       throw new IllegalStateException(
125           "Cannot create file object to for test case type '" + testCaseType
126               + " and UUT type '" + uutType + "'.");
127     }
128   }
129 
130   // --- object basics --------------------------------------------------------
131 
132 }