ea33be2e9ffc4af319e1828aefd9d1a5cff9fb7f
[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             for (CodeGenerator generator : generators) {
72                 File generatedJavaFile = generateTypeToJavaFile(parentDirectory, type, generator);
73                 if (generatedJavaFile != null) {
74                     result.add(generatedJavaFile);
75                 }
76             }
77         }
78         return result;
79     }
80
81     /**
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>).
87      * 
88      * @param parentDir
89      *            directory where should be the new file generated
90      * @param type
91      *            JAVA <code>Type</code> for which should be JAVA source code
92      *            generated
93      * @param generator
94      *            code generator which is used for generating of the source code
95      * @return file which contains JAVA source code
96      * @throws IOException
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
102      */
103     private File generateTypeToJavaFile(final File parentDir, final Type type, final CodeGenerator generator)
104             throws IOException {
105         if (parentDir == null) {
106             log.warn("Parent Directory not specified, files will be generated "
107                     + "accordingly to generated Type package path.");
108         }
109         if (type == null) {
110             log.error("Cannot generate Type into Java File because " + "Generated Type is NULL!");
111             throw new IllegalArgumentException("Generated Type Cannot be NULL!");
112         }
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!");
116         }
117         final File packageDir = packageToDirectory(parentDir, type.getPackageName());
118
119         if (!packageDir.exists()) {
120             packageDir.mkdirs();
121         }
122
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!");
127             }
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);
133                 }
134             } catch (IOException e) {
135                 log.error(e.getMessage());
136                 throw new IOException(e.getMessage());
137             }
138             return file;
139         }
140         return null;
141     }
142
143     /**
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.
148      * 
149      * @param parentDirectory
150      *            <code>File</code> object with reference to parent directory
151      * @param packageName
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>
155      */
156     private File packageToDirectory(final File parentDirectory, final String packageName) {
157         if (packageName == null) {
158             throw new IllegalArgumentException("Package Name cannot be NULL!");
159         }
160
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]);
167         }
168         return new File(parentDirectory, dirPathBuilder.toString());
169     }
170 }