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.controller.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.controller.sal.binding.model.api.CodeGenerator;
21 import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;
22 import org.opendaylight.controller.sal.binding.model.api.GeneratedType;
24 public class GeneratorJavaFile {
26 private final CodeGenerator interfaceGenerator;
27 private final ClassCodeGenerator classGenerator;
28 private final Set<GeneratedType> types;
29 private final Set<GeneratedTransferObject> genTransferObjects;
31 public GeneratorJavaFile(final CodeGenerator codeGenerator,
32 final Set<GeneratedType> types) {
33 this.interfaceGenerator = codeGenerator;
35 this.genTransferObjects = new HashSet<GeneratedTransferObject>();
36 classGenerator = new ClassCodeGenerator();
39 public GeneratorJavaFile(final Set<GeneratedType> types,
40 final Set<GeneratedTransferObject> genTransferObjects) {
41 this.interfaceGenerator = new InterfaceGenerator();
42 this.classGenerator = new ClassCodeGenerator();
44 this.genTransferObjects = genTransferObjects;
47 public boolean generateToFile() {
48 return generateToFile(null);
51 public boolean generateToFile(String path) {
53 for (GeneratedType type : types) {
54 String parentPath = generateParentPath(path,
55 type.getPackageName());
57 File file = new File(parentPath, type.getName() + ".java");
58 File parent = file.getParentFile();
59 if (!parent.exists()) {
65 BufferedWriter bw = null;
68 fw = new FileWriter(file);
69 bw = new BufferedWriter(fw);
70 Writer writer = interfaceGenerator.generate(type);
71 bw.write(writer.toString());
76 } catch (IOException e) {
82 for (GeneratedTransferObject transferObject : genTransferObjects) {
83 String parentPath = generateParentPath(path,
84 transferObject.getPackageName());
86 File file = new File(parentPath, transferObject.getName() + ".java");
87 File parent = file.getParentFile();
88 if (!parent.exists()) {
94 BufferedWriter bw = null;
97 fw = new FileWriter(file);
98 bw = new BufferedWriter(fw);
99 Writer writer = classGenerator.generate(transferObject);
100 bw.write(writer.toString());
105 } catch (IOException e) {
112 } catch (IOException e) {
118 private String generateParentPath(String path, String pkg) {
119 List<String> dirs = new ArrayList<String>();
122 if (pkg.length() > 0) {
123 if (pkg.contains(".")) {
124 String[] split = pkg.split("\\.");
125 for (String dir : split) {
131 for (int i = 0; i < dirs.size(); i++) {
133 pkgPath += dirs.get(i);
135 pkgPath += File.separator + dirs.get(i);
140 String fullPath = "";
142 if (path.endsWith(File.separator)) {
143 fullPath = path + pkgPath;
145 fullPath = path + File.separator + pkgPath;