2 * Copyright (c) 2017 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.mdsal.binding.javav2.java.api.generator;
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;
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;
36 * Generates files with JAVA source code for every specified type.
39 public final class GeneratorJavaFile {
41 private static final Logger LOG = LoggerFactory.getLogger(GeneratorJavaFile.class);
42 private static final Splitter BSDOT_SPLITTER = Splitter.on(".");
45 * List of <code>CodeGenerator</code> instances.
47 private final List<CodeGenerator> generators = new ArrayList<>();
50 * Set of <code>Type</code> instances for which the JAVA code is generated.
52 private final Collection<? extends Type> types;
55 * BuildContext used for instantiating files
57 private final BuildContext buildContext;
60 * Creates instance of this class with the set of <code>types</code> for
61 * which the JAVA code is generated.
63 * The instances of concrete JAVA code generator are created.
66 * build context to use for accessing files
68 * set of types for which JAVA code should be generated
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());
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>).
86 * @param generatedSourcesDirectory expected output directory for generated sources configured by
88 * @param persistentSourcesDirectory base directory
89 * @return list of generated files
90 * @throws IOException thrown in case of I/O error
92 public List<File> generateToFile(final File generatedSourcesDirectory, final File persistentSourcesDirectory)
94 final List<File> result = new ArrayList<>();
95 for (final Type type : this.types) {
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);
107 generatedJavaFile = generateTypeToJavaFile(generatedSourcesDirectory, type, generator);
109 if (generatedJavaFile != null) {
110 result.add(generatedJavaFile);
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.
124 * @param parentDirectory
125 * <code>File</code> object with reference to parent directory
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>
131 public static File packageToDirectory(final File parentDirectory, final String packageName) {
132 if (packageName == null) {
133 throw new IllegalArgumentException("Package Name cannot be NULL!");
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());
142 while (packageElementsItr.hasNext()) {
143 dirPathBuilder.append(File.separator);
144 dirPathBuilder.append(packageElementsItr.next());
147 return new File(parentDirectory, dirPathBuilder.toString());
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>).
158 * directory where should be the new file generated
160 * JAVA <code>Type</code> for which should be JAVA source code
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
172 private File generateTypeToJavaFile(final File parentDir, final Type type, final CodeGenerator generator)
174 if (parentDir == null) {
175 LOG.warn("Parent Directory not specified, files will be generated "
176 + "accordingly to generated Type package path.");
179 LOG.error("Cannot generate Type into Java File because " + "Generated Type is NULL!");
180 throw new IllegalArgumentException("Generated Type Cannot be NULL!");
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!");
187 if (generator.isAcceptable(type)) {
189 if (generator instanceof BuilderGenerator) {
190 Preconditions.checkState(type instanceof GeneratedTypeForBuilder);
191 packageDir = packageToDirectory(parentDir, ((GeneratedTypeForBuilder)type).getPackageNameForBuilder());
193 packageDir = packageToDirectory(parentDir, type.getPackageName());
196 if (!packageDir.exists()) {
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");
205 LOG.warn("Naming conflict for type '{}': file with same name already exists and will not be generated.",
206 type.getFullyQualifiedName());
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);
215 } catch (final IOException e) {
216 LOG.error("Failed to write generate output into {}", file.getPath(), e);