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.yangtools.sal.java.api.generator;
10 import java.io.BufferedWriter;
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.List;
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;
28 import com.google.common.base.Preconditions;
31 * Generates files with JAVA source codes for every specified type.
34 public final class GeneratorJavaFile {
36 private static final Logger LOG = LoggerFactory.getLogger(GeneratorJavaFile.class);
39 * List of <code>CodeGenerator</code> instances.
41 private final List<CodeGenerator> generators = new ArrayList<>();
44 * Set of <code>Type</code> instances for which the JAVA code is generated.
46 private final Set<? extends Type> types;
49 * BuildContext used for instantiating files
51 private final BuildContext buildContext;
54 * Creates instance of this class with the set of <code>types</code> for
55 * which the JAVA code is generated.
57 * The instances of concrete JAVA code generator are created.
60 * build context to use for accessing files
62 * set of types for which JAVA code should be generated
64 public GeneratorJavaFile(final BuildContext buildContext, final Set<? 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());
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.
78 * The instances of concrete JAVA code generator are created.
81 * set of types for which JAVA code should be generated
83 public GeneratorJavaFile(final Set<? extends Type> types) {
84 this(new DefaultBuildContext(), types);
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.
91 * @param generatedSourcesDirectory
92 * directory to which the output source codes should be generated
93 * @return list of output files
95 * if the error during writing to the file occurs
97 public List<File> generateToFile(final File generatedSourcesDirectory) throws IOException {
98 return generateToFile(generatedSourcesDirectory, generatedSourcesDirectory);
101 public List<File> generateToFile(final File generatedSourcesDirectory, final File persistenSourcesDirectory)
103 final List<File> result = new ArrayList<>();
104 for (Type type : types) {
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);
116 generatedJavaFile = generateTypeToJavaFile(generatedSourcesDirectory, type, generator);
118 if (generatedJavaFile != null) {
119 result.add(generatedJavaFile);
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>).
135 * directory where should be the new file generated
137 * JAVA <code>Type</code> for which should be JAVA source code
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
149 private File generateTypeToJavaFile(final File parentDir, final Type type, final CodeGenerator generator)
151 if (parentDir == null) {
152 LOG.warn("Parent Directory not specified, files will be generated "
153 + "accordingly to generated Type package path.");
156 LOG.error("Cannot generate Type into Java File because " + "Generated Type is NULL!");
157 throw new IllegalArgumentException("Generated Type Cannot be NULL!");
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!");
163 final File packageDir = packageToDirectory(parentDir, type.getPackageName());
165 if (!packageDir.exists()) {
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!");
174 final File file = new File(packageDir, generator.getUnitName(type) + ".java");
175 try (final OutputStream stream = buildContext.newFileOutputStream(file)) {
176 try (final Writer fw = new OutputStreamWriter(stream)) {
177 try (final BufferedWriter bw = new BufferedWriter(fw)) {
178 bw.write(generatedCode);
180 } catch (IOException e) {
181 LOG.error("Failed to write generate output into {}", file.getPath(), e);
191 * Creates the package directory path as concatenation of
192 * <code>parentDirectory</code> and parsed <code>packageName</code>. The
193 * parsing of <code>packageName</code> is realized as replacement of the
194 * package name dots with the file system separator.
196 * @param parentDirectory
197 * <code>File</code> object with reference to parent directory
199 * string with the name of the package
200 * @return <code>File</code> object which refers to the new directory for
201 * package <code>packageName</code>
203 private File packageToDirectory(final File parentDirectory, final String packageName) {
204 if (packageName == null) {
205 throw new IllegalArgumentException("Package Name cannot be NULL!");
208 final String[] subDirNames = packageName.split("\\.");
209 final StringBuilder dirPathBuilder = new StringBuilder();
210 dirPathBuilder.append(subDirNames[0]);
211 for (int i = 1; i < subDirNames.length; ++i) {
212 dirPathBuilder.append(File.separator);
213 dirPathBuilder.append(subDirNames[i]);
215 return new File(parentDirectory, dirPathBuilder.toString());