Initial opendaylight infrastructure commit!!
[controller.git] / opendaylight / sal / yang-prototype / code-generator / binding-java-api-generator / src / main / java / org / opendaylight / controller / sal / java / api / generator / GeneratorJavaFile.java
1 /*\r
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.\r
3  *\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
7  */\r
8 package org.opendaylight.controller.sal.java.api.generator;\r
9 \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
18 \r
19 import org.opendaylight.controller.sal.binding.model.api.CodeGenerator;\r
20 import org.opendaylight.controller.sal.binding.model.api.GeneratedType;\r
21 \r
22 public class GeneratorJavaFile {\r
23 \r
24     private final CodeGenerator codeGenerator;\r
25     private final Set<GeneratedType> types;\r
26 \r
27     public GeneratorJavaFile(CodeGenerator codeGenerator,\r
28             Set<GeneratedType> types) {\r
29         this.codeGenerator = codeGenerator;\r
30         this.types = types;\r
31     }\r
32 \r
33     public boolean generateToFile() {\r
34         return generateToFile(null);\r
35     }\r
36 \r
37     public boolean generateToFile(String path) {\r
38         try {\r
39             for (GeneratedType type : types) {\r
40                 String parentPath = generateParentPath(path,\r
41                         type.getPackageName());\r
42 \r
43                 File file = new File(parentPath, type.getName() + ".java");\r
44                 File parent = file.getParentFile();\r
45                 if (!parent.exists()) {\r
46                     parent.mkdirs();\r
47                 }\r
48 \r
49                 if (!file.exists()) {\r
50                     FileWriter fw = null;\r
51                     BufferedWriter bw = null;\r
52 \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
58 \r
59                     if (bw != null) {\r
60                         try {\r
61                             bw.close();\r
62                         } catch (IOException e) {\r
63                             // TODO: log?\r
64                         }\r
65                     }\r
66                 }\r
67             }\r
68             return true;\r
69         } catch (IOException e) {\r
70             // TODO: log?\r
71             return false;\r
72         }\r
73     }\r
74 \r
75     private String generateParentPath(String path, String pkg) {\r
76         List<String> dirs = new ArrayList<String>();\r
77         String pkgPath = "";\r
78         if (pkg != null) {\r
79             if (pkg.length() > 0) {\r
80                 if (pkg.contains(".")) {\r
81                     String[] split = pkg.split("\\.");\r
82                     for (String dir : split) {\r
83                         dirs.add(dir);\r
84                     }\r
85                 } else {\r
86                     dirs.add(pkg);\r
87                 }\r
88                 for (int i = 0; i < dirs.size(); i++) {\r
89                     if (i == 0) {\r
90                         pkgPath += dirs.get(i);\r
91                     } else {\r
92                         pkgPath += File.separator + dirs.get(i);\r
93                     }\r
94                 }\r
95             }\r
96         }\r
97         String fullPath = "";\r
98         if (path != null) {\r
99             if (path.endsWith(File.separator)) {\r
100                 fullPath = path + pkgPath;\r
101             } else {\r
102                 fullPath = path + File.separator + pkgPath;\r
103             }\r
104         } else {\r
105             fullPath = pkgPath;\r
106         }\r
107         return fullPath;\r
108     }\r
109 \r
110 }\r