Binding generator v2 - uses statement - support choice
[mdsal.git] / binding2 / mdsal-binding2-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / javav2 / java / api / generator / GeneratorJavaFile.java
1 /*
2  * Copyright (c) 2017 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.mdsal.binding.javav2.java.api.generator;
10
11 import com.google.common.annotations.Beta;
12 import com.google.common.base.Preconditions;
13 import com.google.common.base.Splitter;
14 import java.io.BufferedWriter;
15 import java.io.File;
16 import java.io.IOException;
17 import java.io.OutputStream;
18 import java.io.OutputStreamWriter;
19 import java.io.Writer;
20 import java.nio.charset.StandardCharsets;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Iterator;
24 import java.util.List;
25 import org.opendaylight.mdsal.binding.javav2.java.api.generator.util.JavaCodePrettyPrint;
26 import org.opendaylight.mdsal.binding.javav2.model.api.CodeGenerator;
27 import org.opendaylight.mdsal.binding.javav2.model.api.GeneratedTransferObject;
28 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
29 import org.opendaylight.mdsal.binding.javav2.model.api.UnitName;
30 import org.opendaylight.mdsal.binding.javav2.model.api.GeneratedTypeForBuilder;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.sonatype.plexus.build.incremental.BuildContext;
34
35 /**
36  * Generates files with JAVA source code for every specified type.
37  */
38 @Beta
39 public final class GeneratorJavaFile {
40
41     private static final Logger LOG = LoggerFactory.getLogger(GeneratorJavaFile.class);
42     private static final Splitter BSDOT_SPLITTER = Splitter.on(".");
43
44     /**
45      * List of <code>CodeGenerator</code> instances.
46      */
47     private final List<CodeGenerator> generators = new ArrayList<>();
48
49     /**
50      * Set of <code>Type</code> instances for which the JAVA code is generated.
51      */
52     private final Collection<? extends Type> types;
53
54     /**
55      * BuildContext used for instantiating files
56      */
57     private final BuildContext buildContext;
58
59     /**
60      * Creates instance of this class with the set of <code>types</code> for
61      * which the JAVA code is generated.
62      *
63      * The instances of concrete JAVA code generator are created.
64      *
65      * @param buildContext
66      *            build context to use for accessing files
67      * @param types
68      *            set of types for which JAVA code should be generated
69      */
70     public GeneratorJavaFile(final BuildContext buildContext, final Collection<? extends Type> types) {
71         this.buildContext = Preconditions.checkNotNull(buildContext);
72         this.types = Preconditions.checkNotNull(types);
73         this.generators.add(new EnumGenerator());
74         this.generators.add(new InterfaceGenerator());
75         this.generators.add(new BuilderGenerator());
76         this.generators.add(new TOGenerator());
77     }
78
79     /**
80      * Generates <code>List</code> of files for collection of types. All files are stored
81      * to sub-folders of base directory <code>persistentSourcesDirectory</code>. Subdirectories
82      * are generated according to packages to which the type belongs (e. g. if
83      * type belongs to the package <i>org.pcg</i> then in <code>persistentSourcesDirectory</code>
84      * is created directory <i>org</i> which contains <i>pcg</i>).
85      *
86      * @param generatedSourcesDirectory expected output directory for generated sources configured by
87      *            user
88      * @param persistentSourcesDirectory base directory
89      * @return list of generated files
90      * @throws IOException thrown in case of I/O error
91      */
92     public List<File> generateToFile(final File generatedSourcesDirectory, final File persistentSourcesDirectory)
93             throws IOException {
94         final List<File> result = new ArrayList<>();
95         for (final Type type : this.types) {
96             if (type != null) {
97                 for (final CodeGenerator generator : this.generators) {
98                     File generatedJavaFile = null;
99                     if (type instanceof GeneratedTransferObject
100                             && ((GeneratedTransferObject) type).isUnionTypeBuilder()) {
101                         final File packageDir = packageToDirectory(persistentSourcesDirectory, type.getPackageName());
102                         final File file = new File(packageDir, generator.getUnitName(type) + ".java");
103                         if (!file.exists()) {
104                             generatedJavaFile = generateTypeToJavaFile(persistentSourcesDirectory, type, generator);
105                         }
106                     } else {
107                         generatedJavaFile = generateTypeToJavaFile(generatedSourcesDirectory, type, generator);
108                     }
109                     if (generatedJavaFile != null) {
110                         result.add(generatedJavaFile);
111                     }
112                 }
113             }
114         }
115         return result;
116     }
117
118     /**
119      * Creates the package directory path as concatenation of
120      * <code>parentDirectory</code> and parsed <code>packageName</code>. The
121      * parsing of <code>packageName</code> is realized as replacement of the
122      * package name dots with the file system separator.
123      *
124      * @param parentDirectory
125      *            <code>File</code> object with reference to parent directory
126      * @param packageName
127      *            string with the name of the package
128      * @return <code>File</code> object which refers to the new directory for
129      *         package <code>packageName</code>
130      */
131     public static File packageToDirectory(final File parentDirectory, final String packageName) {
132         if (packageName == null) {
133             throw new IllegalArgumentException("Package Name cannot be NULL!");
134         }
135
136         final StringBuilder dirPathBuilder = new StringBuilder();
137         final Iterator<String> packageElementsItr = BSDOT_SPLITTER.split(packageName).iterator();
138         if (packageElementsItr.hasNext()) {
139             dirPathBuilder.append(packageElementsItr.next());
140         }
141
142         while (packageElementsItr.hasNext()) {
143             dirPathBuilder.append(File.separator);
144             dirPathBuilder.append(packageElementsItr.next());
145         }
146
147         return new File(parentDirectory, dirPathBuilder.toString());
148     }
149
150     /**
151      * Generates <code>File</code> for <code>type</code>. All files are stored
152      * to sub-folders of base directory <code>parentDir</code>. Subdirectories
153      * are generated according to packages to which the type belongs (e. g. if
154      * type belongs to the package <i>org.pcg</i> then in <code>parentDir</code>
155      * is created directory <i>org</i> which contains <i>pcg</i>).
156      *
157      * @param parentDir
158      *            directory where should be the new file generated
159      * @param type
160      *            JAVA <code>Type</code> for which should be JAVA source code
161      *            generated
162      * @param generator
163      *            code generator which is used for generating of the source code
164      * @return file which contains JAVA source code
165      * @throws IOException
166      *             if the error during writing to the file occurs
167      * @throws IllegalArgumentException
168      *             if <code>type</code> equals <code>null</code>
169      * @throws IllegalStateException
170      *             if string with generated code is empty
171      */
172     private File generateTypeToJavaFile(final File parentDir, final Type type, final CodeGenerator generator)
173             throws IOException {
174         if (parentDir == null) {
175             LOG.warn("Parent Directory not specified, files will be generated "
176                     + "accordingly to generated Type package path.");
177         }
178         if (type == null) {
179             LOG.error("Cannot generate Type into Java File because " + "Generated Type is NULL!");
180             throw new IllegalArgumentException("Generated Type Cannot be NULL!");
181         }
182         if (generator == null) {
183             LOG.error("Cannot generate Type into Java File because " + "Code Generator instance is NULL!");
184             throw new IllegalArgumentException("Code Generator Cannot be NULL!");
185         }
186
187         if (generator.isAcceptable(type)) {
188             File packageDir;
189             if (generator instanceof BuilderGenerator) {
190                 Preconditions.checkState(type instanceof GeneratedTypeForBuilder, type.getFullyQualifiedName());
191                 packageDir = packageToDirectory(parentDir, ((GeneratedTypeForBuilder)type).getPackageNameForBuilder());
192             } else {
193                 packageDir = packageToDirectory(parentDir, type.getPackageName());
194             }
195
196             if (!packageDir.exists()) {
197                 packageDir.mkdirs();
198             }
199
200             final String generatedCode = JavaCodePrettyPrint.perform(generator.generate(type));
201             Preconditions.checkState(!generatedCode.isEmpty(), "Generated code should not be empty!");
202             final File file = new File(packageDir, ((UnitName) generator.getUnitName(type)).getValue() + ".java");
203
204             if (file.exists()) {
205                 LOG.warn("Naming conflict for type '{}': file with same name already exists and will not be generated.",
206                     type.getFullyQualifiedName());
207                 return null;
208             }
209
210             try (final OutputStream stream = this.buildContext.newFileOutputStream(file)) {
211                 try (final Writer fw = new OutputStreamWriter(stream, StandardCharsets.UTF_8)) {
212                     try (final BufferedWriter bw = new BufferedWriter(fw)) {
213                         bw.write(generatedCode);
214                     }
215                 } catch (final IOException e) {
216                     LOG.error("Failed to write generate output into {}", file.getPath(), e);
217                     throw e;
218                 }
219             }
220             return file;
221
222         }
223         return null;
224     }
225
226 }