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.FileWriter;
13 import java.io.IOException;
14 import java.util.ArrayList;
15 import java.util.List;
18 import org.opendaylight.yangtools.sal.binding.model.api.CodeGenerator;
19 import org.opendaylight.yangtools.sal.binding.model.api.Type;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
24 * Generates files with JAVA source codes for every specified type.
27 public final class GeneratorJavaFile {
29 private static final Logger LOG = LoggerFactory.getLogger(GeneratorJavaFile.class);
32 * List of <code>CodeGenerator</code> instances.
34 private final List<CodeGenerator> generators = new ArrayList<>();
37 * Set of <code>Type</code> instances for which the JAVA code is generated.
39 private final Set<? extends Type> types;
42 * Creates instance of this class with the set of <code>types</code> for
43 * which the JAVA code is generated.
45 * The instances of concrete JAVA code generator are created.
48 * set of types for which JAVA code should be generated
50 public GeneratorJavaFile(final Set<? extends Type> types) {
52 generators.add(new InterfaceGenerator());
53 generators.add(new TOGenerator());
54 generators.add(new EnumGenerator());
55 generators.add(new BuilderGenerator());
59 * Generates list of files with JAVA source code. Only the suitable code
60 * generator is used to generate the source code for the concrete type.
62 * @param parentDirectory
63 * directory to which the output source codes should be generated
64 * @return list of output files
66 * if the error during writting to the file occures
68 public List<File> generateToFile(final File parentDirectory) throws IOException {
69 final List<File> result = new ArrayList<>();
70 for (Type type : types) {
72 for (CodeGenerator generator : generators) {
73 File generatedJavaFile = generateTypeToJavaFile(parentDirectory, type, generator);
74 if (generatedJavaFile != null) {
75 result.add(generatedJavaFile);
84 * Generates <code>File</code> for <code>type</code>. All files are stored
85 * to subfolders of base directory <code>parentDir</code>. Subdirectories
86 * are generated according to packages to which the type belongs (e. g. if
87 * type belongs to the package <i>org.pcg</i> then in <code>parentDir</code>
88 * is created directory <i>org</i> which contains <i>pcg</i>).
91 * directory where should be the new file generated
93 * JAVA <code>Type</code> for which should be JAVA source code
96 * code generator which is used for generating of the source code
97 * @return file which contains JAVA source code
99 * if the error during writting to the file occures
100 * @throws IllegalArgumentException
101 * if <code>type</code> equals <code>null</code>
102 * @throws IllegalStateException
103 * if string with generated code is empty
105 private File generateTypeToJavaFile(final File parentDir, final Type type, final CodeGenerator generator)
107 if (parentDir == null) {
108 LOG.warn("Parent Directory not specified, files will be generated "
109 + "accordingly to generated Type package path.");
112 LOG.error("Cannot generate Type into Java File because " + "Generated Type is NULL!");
113 throw new IllegalArgumentException("Generated Type Cannot be NULL!");
115 if (generator == null) {
116 LOG.error("Cannot generate Type into Java File because " + "Code Generator instance is NULL!");
117 throw new IllegalArgumentException("Code Generator Cannot be NULL!");
119 final File packageDir = packageToDirectory(parentDir, type.getPackageName());
121 if (!packageDir.exists()) {
125 if (generator.isAcceptable(type)) {
126 String generatedCode = generator.generate(type);
127 if (generatedCode.isEmpty()) {
128 throw new IllegalStateException("Generated code should not be empty!");
130 final File file = new File(packageDir, generator.getUnitName(type) + ".java");
131 try (final FileWriter fw = new FileWriter(file)) {
132 file.createNewFile();
133 try (final BufferedWriter bw = new BufferedWriter(fw)) {
134 bw.write(generatedCode);
136 } catch (IOException e) {
137 LOG.error(e.getMessage());
138 throw new IOException(e);
146 * Creates the package directory path as concatenation of
147 * <code>parentDirectory</code> and parsed <code>packageName</code>. The
148 * parsing of <code>packageName</code> is realized as replacement of the
149 * package name dots with the file system separator.
151 * @param parentDirectory
152 * <code>File</code> object with reference to parent directory
154 * string with the name of the package
155 * @return <code>File</code> object which refers to the new directory for
156 * package <code>packageName</code>
158 private File packageToDirectory(final File parentDirectory, final String packageName) {
159 if (packageName == null) {
160 throw new IllegalArgumentException("Package Name cannot be NULL!");
163 final String[] subDirNames = packageName.split("\\.");
164 final StringBuilder dirPathBuilder = new StringBuilder();
165 dirPathBuilder.append(subDirNames[0]);
166 for (int i = 1; i < subDirNames.length; ++i) {
167 dirPathBuilder.append(File.separator);
168 dirPathBuilder.append(subDirNames[i]);
170 return new File(parentDirectory, dirPathBuilder.toString());