2 * Copyright (c) 2013 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
8 package org.opendaylight.mdsal.binding.java.api.generator;
10 import com.google.common.base.Preconditions;
11 import java.io.BufferedWriter;
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;
30 * Generates files with JAVA source codes for every specified type.
33 public final class GeneratorJavaFile {
35 private static final Logger LOG = LoggerFactory.getLogger(GeneratorJavaFile.class);
38 * List of <code>CodeGenerator</code> instances.
40 private final List<CodeGenerator> generators = new ArrayList<>();
43 * Set of <code>Type</code> instances for which the JAVA code is generated.
45 private final Collection<? extends Type> types;
48 * BuildContext used for instantiating files
50 private final BuildContext buildContext;
53 * Creates instance of this class with the set of <code>types</code> for
54 * which the JAVA code is generated.
56 * The instances of concrete JAVA code generator are created.
59 * build context to use for accessing files
61 * set of types for which JAVA code should be generated
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());
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.
77 * The instances of concrete JAVA code generator are created.
80 * set of types for which JAVA code should be generated
82 public GeneratorJavaFile(final Collection<? extends Type> types) {
83 this(new DefaultBuildContext(), types);
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.
90 * @param generatedSourcesDirectory
91 * directory to which the output source codes should be generated
92 * @return list of output files
94 * if the error during writing to the file occurs
96 public List<File> generateToFile(final File generatedSourcesDirectory) throws IOException {
97 return generateToFile(generatedSourcesDirectory, generatedSourcesDirectory);
100 public List<File> generateToFile(final File generatedSourcesDirectory, final File persistenSourcesDirectory)
102 final List<File> result = new ArrayList<>();
103 for (Type type : types) {
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);
115 generatedJavaFile = generateTypeToJavaFile(generatedSourcesDirectory, type, generator);
117 if (generatedJavaFile != null) {
118 result.add(generatedJavaFile);
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>).
134 * directory where should be the new file generated
136 * JAVA <code>Type</code> for which should be JAVA source code
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
148 private File generateTypeToJavaFile(final File parentDir, final Type type, final CodeGenerator generator)
150 if (parentDir == null) {
151 LOG.warn("Parent Directory not specified, files will be generated "
152 + "accordingly to generated Type package path.");
155 LOG.error("Cannot generate Type into Java File because " + "Generated Type is NULL!");
156 throw new IllegalArgumentException("Generated Type Cannot be NULL!");
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!");
162 final File packageDir = packageToDirectory(parentDir, type.getPackageName());
164 if (!packageDir.exists()) {
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!");
173 final File file = new File(packageDir, generator.getUnitName(type) + ".java");
177 "Naming conflict for type '{}': file with same name already exists and will not be generated.",
178 type.getFullyQualifiedName());
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);
187 } catch (IOException e) {
188 LOG.error("Failed to write generate output into {}", file.getPath(), e);
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.
204 * @param parentDirectory
205 * <code>File</code> object with reference to parent directory
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>
211 public static File packageToDirectory(final File parentDirectory, final String packageName) {
212 if (packageName == null) {
213 throw new IllegalArgumentException("Package Name cannot be NULL!");
216 return new File(parentDirectory, packageName.replace('.', File.separatorChar));