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.io.Writer;
15 import java.util.ArrayList;
16 import java.util.HashSet;
17 import java.util.List;
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;
28 public final class GeneratorJavaFile {
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;
36 private final Set<GeneratedType> genTypes;
37 private final Set<GeneratedTransferObject> genTransferObjects;
38 private final Set<Enumeration> enumerations;
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();
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();
57 this.genTypes = types;
58 this.genTransferObjects = genTransferObjects;
59 this.enumerations = enumerations;
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);
69 if (genFile != null) {
72 if (genBuilderFile != null) {
73 result.add(genBuilderFile);
76 for (GeneratedTransferObject transferObject : genTransferObjects) {
77 final File genFile = generateTypeToJavaFile(parentDirectory, transferObject, classGenerator, "");
79 if (genFile != null) {
84 for (Enumeration enumeration : enumerations) {
85 final File genFile = generateTypeToJavaFile(parentDirectory, enumeration, enumGenerator, "");
87 if (genFile != null) {
95 private File generateTypeToJavaFile(final File parentDir, final Type type, final CodeGenerator generator, String fileNameSuffix)
97 if (parentDir == null) {
98 log.warn("Parent Directory not specified, files will be generated "
99 + "accordingly to generated Type package path.");
102 log.error("Cannot generate Type into Java File because " + "Generated Type is NULL!");
103 throw new IllegalArgumentException("Generated Type Cannot be NULL!");
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!");
109 final File packageDir = packageToDirectory(parentDir, type.getPackageName());
111 if (!packageDir.exists()) {
114 final File file = new File(packageDir, type.getName() + fileNameSuffix + ".java");
115 try (final FileWriter fw = new FileWriter(file)) {
116 file.createNewFile();
118 try (final BufferedWriter bw = new BufferedWriter(fw)) {
119 Writer writer = generator.generate(type);
120 bw.write(writer.toString());
122 } catch (IOException e) {
123 log.error(e.getMessage());
124 throw new IOException(e.getMessage());
129 private File packageToDirectory(final File parentDirectory, final String packageName) {
130 if (packageName == null) {
131 throw new IllegalArgumentException("Package Name cannot be NULL!");
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]);
141 return new File(parentDirectory, dirPathBuilder.toString());