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