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) {
71 for (CodeGenerator generator : generators) {
72 File generatedJavaFile = generateTypeToJavaFile(parentDirectory, type, generator);
73 if (generatedJavaFile != null) {
74 result.add(generatedJavaFile);
82 * Generates <code>File</code> for <code>type</code>. All files are stored
83 * to subfolders of base directory <code>parentDir</code>. Subdirectories
84 * are generated according to packages to which the type belongs (e. g. if
85 * type belongs to the package <i>org.pcg</i> then in <code>parentDir</code>
86 * is created directory <i>org</i> which contains <i>pcg</i>).
89 * directory where should be the new file generated
91 * JAVA <code>Type</code> for which should be JAVA source code
94 * code generator which is used for generating of the source code
95 * @return file which contains JAVA source code
97 * if the error during writting to the file occures
98 * @throws IllegalArgumentException
99 * if <code>type</code> equals <code>null</code>
100 * @throws IllegalStateException
101 * if string with generated code is empty
103 private File generateTypeToJavaFile(final File parentDir, final Type type, final CodeGenerator generator)
105 if (parentDir == null) {
106 log.warn("Parent Directory not specified, files will be generated "
107 + "accordingly to generated Type package path.");
110 log.error("Cannot generate Type into Java File because " + "Generated Type is NULL!");
111 throw new IllegalArgumentException("Generated Type Cannot be NULL!");
113 if (generator == null) {
114 log.error("Cannot generate Type into Java File because " + "Code Generator instance is NULL!");
115 throw new IllegalArgumentException("Code Generator Cannot be NULL!");
117 final File packageDir = packageToDirectory(parentDir, type.getPackageName());
119 if (!packageDir.exists()) {
123 if (generator.isAcceptable(type)) {
124 String generatedCode = generator.generate(type);
125 if (generatedCode.isEmpty()) {
126 throw new IllegalStateException("Generated code should not be empty!");
128 final File file = new File(packageDir, generator.getUnitName(type) + ".java");
129 try (final FileWriter fw = new FileWriter(file)) {
130 file.createNewFile();
131 try (final BufferedWriter bw = new BufferedWriter(fw)) {
132 bw.write(generatedCode);
134 } catch (IOException e) {
135 log.error(e.getMessage());
136 throw new IOException(e.getMessage());
144 * Creates the package directory path as concatenation of
145 * <code>parentDirectory</code> and parsed <code>packageName</code>. The
146 * parsing of <code>packageName</code> is realized as replacement of the
147 * package name dots with the file system separator.
149 * @param parentDirectory
150 * <code>File</code> object with reference to parent directory
152 * string with the name of the package
153 * @return <code>File</code> object which refers to the new directory for
154 * package <code>packageName</code>
156 private File packageToDirectory(final File parentDirectory, final String packageName) {
157 if (packageName == null) {
158 throw new IllegalArgumentException("Package Name cannot be NULL!");
161 final String[] subDirNames = packageName.split("\\.");
162 final StringBuilder dirPathBuilder = new StringBuilder();
163 dirPathBuilder.append(subDirNames[0]);
164 for (int i = 1; i < subDirNames.length; ++i) {
165 dirPathBuilder.append(File.separator);
166 dirPathBuilder.append(subDirNames[i]);
168 return new File(parentDirectory, dirPathBuilder.toString());