Implemented use of ChildOf interface in generated classes.
[yangtools.git] / code-generator / binding-java-api-generator / src / main / java / org / opendaylight / yangtools / sal / java / api / generator / GeneratorJavaFile.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.sal.java.api.generator;
9
10 import java.io.BufferedWriter;
11 import java.io.File;
12 import java.io.FileWriter;
13 import java.io.IOException;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.Set;
17
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;
22
23 /**
24  * Generates files with JAVA source codes for every specified type.
25  *
26  */
27 public final class GeneratorJavaFile {
28
29     private static final Logger LOG = LoggerFactory.getLogger(GeneratorJavaFile.class);
30
31     /**
32      * List of <code>CodeGenerator</code> instances.
33      */
34     private final List<CodeGenerator> generators = new ArrayList<>();
35
36     /**
37      * Set of <code>Type</code> instances for which the JAVA code is generated.
38      */
39     private final Set<? extends Type> types;
40
41     /**
42      * Creates instance of this class with the set of <code>types</code> for
43      * which the JAVA code is generated.
44      *
45      * The instances of concrete JAVA code generator are created.
46      *
47      * @param types
48      *            set of types for which JAVA code should be generated
49      */
50     public GeneratorJavaFile(final Set<? extends Type> types) {
51         this.types = types;
52         generators.add(new InterfaceGenerator());
53         generators.add(new TOGenerator());
54         generators.add(new EnumGenerator());
55         generators.add(new BuilderGenerator());
56     }
57
58     /**
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.
61      *
62      * @param parentDirectory
63      *            directory to which the output source codes should be generated
64      * @return list of output files
65      * @throws IOException
66      *             if the error during writting to the file occures
67      */
68     public List<File> generateToFile(final File parentDirectory) throws IOException {
69         final List<File> result = new ArrayList<>();
70         for (Type type : types) {
71             if (type != null) {
72                 for (CodeGenerator generator : generators) {
73                     File generatedJavaFile = generateTypeToJavaFile(parentDirectory, type, generator);
74                     if (generatedJavaFile != null) {
75                         result.add(generatedJavaFile);
76                     }
77                 }
78             }
79         }
80         return result;
81     }
82
83     /**
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>).
89      *
90      * @param parentDir
91      *            directory where should be the new file generated
92      * @param type
93      *            JAVA <code>Type</code> for which should be JAVA source code
94      *            generated
95      * @param generator
96      *            code generator which is used for generating of the source code
97      * @return file which contains JAVA source code
98      * @throws IOException
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
104      */
105     private File generateTypeToJavaFile(final File parentDir, final Type type, final CodeGenerator generator)
106             throws IOException {
107         if (parentDir == null) {
108             LOG.warn("Parent Directory not specified, files will be generated "
109                     + "accordingly to generated Type package path.");
110         }
111         if (type == null) {
112             LOG.error("Cannot generate Type into Java File because " + "Generated Type is NULL!");
113             throw new IllegalArgumentException("Generated Type Cannot be NULL!");
114         }
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!");
118         }
119         final File packageDir = packageToDirectory(parentDir, type.getPackageName());
120
121         if (!packageDir.exists()) {
122             packageDir.mkdirs();
123         }
124
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!");
129             }
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);
135                 }
136             } catch (IOException e) {
137                 LOG.error(e.getMessage());
138                 throw new IOException(e);
139             }
140             return file;
141         }
142         return null;
143     }
144
145     /**
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.
150      *
151      * @param parentDirectory
152      *            <code>File</code> object with reference to parent directory
153      * @param packageName
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>
157      */
158     private File packageToDirectory(final File parentDirectory, final String packageName) {
159         if (packageName == null) {
160             throw new IllegalArgumentException("Package Name cannot be NULL!");
161         }
162
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]);
169         }
170         return new File(parentDirectory, dirPathBuilder.toString());
171     }
172 }