8f114b3536a2f54d061f7cc5e9df6a9f7e325f8e
[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.io.Writer;
15 import java.util.ArrayList;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Set;
19
20 import org.opendaylight.yangtools.sal.binding.model.api.CodeGenerator;
21 import org.opendaylight.yangtools.sal.binding.model.api.Enumeration;
22 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject;
23 import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType;
24 import org.opendaylight.yangtools.sal.binding.model.api.Type;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public final class GeneratorJavaFile {
29
30     private static final Logger log = LoggerFactory.getLogger(GeneratorJavaFile.class);
31     private final CodeGenerator interfaceGenerator;
32     private final ClassGenerator classGenerator;
33     private final EnumGenerator enumGenerator;
34     private final BuilderGenerator builderGenerator;
35
36     private final Set<GeneratedType> genTypes;
37     private final Set<GeneratedTransferObject> genTransferObjects;
38     private final Set<Enumeration> enumerations;
39
40     public GeneratorJavaFile(final CodeGenerator codeGenerator, final Set<GeneratedType> types) {
41         this.interfaceGenerator = codeGenerator;
42         this.genTypes = types;
43         this.genTransferObjects = new HashSet<>();
44         this.enumerations = new HashSet<>();
45         this.classGenerator = new ClassGenerator();
46         this.enumGenerator = new EnumGenerator();
47         this.builderGenerator = new BuilderGenerator();
48     }
49
50     public GeneratorJavaFile(final Set<GeneratedType> types, final Set<GeneratedTransferObject> genTransferObjects,
51             final Set<Enumeration> enumerations) {
52         this.interfaceGenerator = new InterfaceGenerator();
53         this.classGenerator = new ClassGenerator();
54         this.enumGenerator = new EnumGenerator();
55         this.builderGenerator = new BuilderGenerator();
56
57         this.genTypes = types;
58         this.genTransferObjects = genTransferObjects;
59         this.enumerations = enumerations;
60     }
61
62     public List<File> generateToFile(final File parentDirectory) throws IOException {
63         final List<File> result = new ArrayList<>();
64         for (GeneratedType type : genTypes) {
65             final File genFile = generateTypeToJavaFile(parentDirectory, type, interfaceGenerator, "");
66             final File genBuilderFile = generateTypeToJavaFile(parentDirectory, type, builderGenerator,
67                     BuilderGenerator.FILE_NAME_SUFFIX);
68
69             if (genFile != null) {
70                 result.add(genFile);
71             }
72             if (genBuilderFile != null) {
73                 result.add(genBuilderFile);
74             }
75         }
76         for (GeneratedTransferObject transferObject : genTransferObjects) {
77             final File genFile = generateTypeToJavaFile(parentDirectory, transferObject, classGenerator, "");
78
79             if (genFile != null) {
80                 result.add(genFile);
81             }
82         }
83
84         for (Enumeration enumeration : enumerations) {
85             final File genFile = generateTypeToJavaFile(parentDirectory, enumeration, enumGenerator, "");
86
87             if (genFile != null) {
88                 result.add(genFile);
89             }
90         }
91
92         return result;
93     }
94
95     private File generateTypeToJavaFile(final File parentDir, final Type type, final CodeGenerator generator, String fileNameSuffix)
96             throws IOException {
97         if (parentDir == null) {
98             log.warn("Parent Directory not specified, files will be generated "
99                     + "accordingly to generated Type package path.");
100         }
101         if (type == null) {
102             log.error("Cannot generate Type into Java File because " + "Generated Type is NULL!");
103             throw new IllegalArgumentException("Generated Type Cannot be NULL!");
104         }
105         if (generator == null) {
106             log.error("Cannot generate Type into Java File because " + "Code Generator instance is NULL!");
107             throw new IllegalArgumentException("Code Generator Cannot be NULL!");
108         }
109         final File packageDir = packageToDirectory(parentDir, type.getPackageName());
110
111         if (!packageDir.exists()) {
112             packageDir.mkdirs();
113         }
114         final File file = new File(packageDir, type.getName() + fileNameSuffix + ".java");
115         try (final FileWriter fw = new FileWriter(file)) {
116             file.createNewFile();
117
118             try (final BufferedWriter bw = new BufferedWriter(fw)) {
119                 Writer writer = generator.generate(type);
120                 bw.write(writer.toString());
121             }
122         } catch (IOException e) {
123             log.error(e.getMessage());
124             throw new IOException(e.getMessage());
125         }
126         return file;
127     }
128
129     private File packageToDirectory(final File parentDirectory, final String packageName) {
130         if (packageName == null) {
131             throw new IllegalArgumentException("Package Name cannot be NULL!");
132         }
133
134         final String[] subDirNames = packageName.split("\\.");
135         final StringBuilder dirPathBuilder = new StringBuilder();
136         dirPathBuilder.append(subDirNames[0]);
137         for (int i = 1; i < subDirNames.length; ++i) {
138             dirPathBuilder.append(File.separator);
139             dirPathBuilder.append(subDirNames[i]);
140         }
141         return new File(parentDirectory, dirPathBuilder.toString());
142     }
143 }