2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
\r
4 * This program and the accompanying materials are made available under the
\r
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
\r
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
\r
8 package org.opendaylight.controller.sal.java.api.generator;
\r
10 import java.io.BufferedWriter;
\r
11 import java.io.File;
\r
12 import java.io.FileWriter;
\r
13 import java.io.IOException;
\r
14 import java.io.Writer;
\r
15 import java.util.ArrayList;
\r
16 import java.util.List;
\r
17 import java.util.Set;
\r
19 import org.opendaylight.controller.sal.binding.model.api.CodeGenerator;
\r
20 import org.opendaylight.controller.sal.binding.model.api.GeneratedType;
\r
22 public class GeneratorJavaFile {
\r
24 private final CodeGenerator codeGenerator;
\r
25 private final Set<GeneratedType> types;
\r
27 public GeneratorJavaFile(CodeGenerator codeGenerator,
\r
28 Set<GeneratedType> types) {
\r
29 this.codeGenerator = codeGenerator;
\r
33 public boolean generateToFile() {
\r
34 return generateToFile(null);
\r
37 public boolean generateToFile(String path) {
\r
39 for (GeneratedType type : types) {
\r
40 String parentPath = generateParentPath(path,
\r
41 type.getPackageName());
\r
43 File file = new File(parentPath, type.getName() + ".java");
\r
44 File parent = file.getParentFile();
\r
45 if (!parent.exists()) {
\r
49 if (!file.exists()) {
\r
50 FileWriter fw = null;
\r
51 BufferedWriter bw = null;
\r
53 file.createNewFile();
\r
54 fw = new FileWriter(file);
\r
55 bw = new BufferedWriter(fw);
\r
56 Writer writer = codeGenerator.generate(type);
\r
57 bw.write(writer.toString());
\r
62 } catch (IOException e) {
\r
69 } catch (IOException e) {
\r
75 private String generateParentPath(String path, String pkg) {
\r
76 List<String> dirs = new ArrayList<String>();
\r
77 String pkgPath = "";
\r
79 if (pkg.length() > 0) {
\r
80 if (pkg.contains(".")) {
\r
81 String[] split = pkg.split("\\.");
\r
82 for (String dir : split) {
\r
88 for (int i = 0; i < dirs.size(); i++) {
\r
90 pkgPath += dirs.get(i);
\r
92 pkgPath += File.separator + dirs.get(i);
\r
97 String fullPath = "";
\r
99 if (path.endsWith(File.separator)) {
\r
100 fullPath = path + pkgPath;
\r
102 fullPath = path + File.separator + pkgPath;
\r
105 fullPath = pkgPath;
\r