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