Update the API generation code and code generation sample
[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.HashSet;\r
17 import java.util.List;\r
18 import java.util.Set;\r
19 \r
20 import org.opendaylight.controller.sal.binding.model.api.CodeGenerator;\r
21 import org.opendaylight.controller.sal.binding.model.api.GeneratedTransferObject;\r
22 import org.opendaylight.controller.sal.binding.model.api.GeneratedType;\r
23 \r
24 public class GeneratorJavaFile {\r
25 \r
26     private final CodeGenerator interfaceGenerator;\r
27     private final CompositeKeyGenerator classGenerator;\r
28     private final Set<GeneratedType> types;\r
29     private final Set<GeneratedTransferObject> genTransferObjects;\r
30 \r
31     public GeneratorJavaFile(final CodeGenerator codeGenerator,\r
32             final Set<GeneratedType> types) {\r
33         this.interfaceGenerator = codeGenerator;\r
34         this.types = types;\r
35         this.genTransferObjects = new HashSet<GeneratedTransferObject>();\r
36         classGenerator = new CompositeKeyGenerator();\r
37     }\r
38 \r
39     public GeneratorJavaFile(final Set<GeneratedType> types,\r
40             final Set<GeneratedTransferObject> genTransferObjects) {\r
41         this.interfaceGenerator = new InterfaceGenerator();\r
42         this.classGenerator = new CompositeKeyGenerator();\r
43         this.types = types;\r
44         this.genTransferObjects = genTransferObjects;\r
45     }\r
46 \r
47     public boolean generateToFile() {\r
48         return generateToFile(null);\r
49     }\r
50 \r
51     public boolean generateToFile(String path) {\r
52         try {\r
53             for (GeneratedType type : types) {\r
54                 String parentPath = generateParentPath(path,\r
55                         type.getPackageName());\r
56 \r
57                 File file = new File(parentPath, type.getName() + ".java");\r
58                 File parent = file.getParentFile();\r
59                 if (!parent.exists()) {\r
60                     parent.mkdirs();\r
61                 }\r
62 \r
63                 if (!file.exists()) {\r
64                     FileWriter fw = null;\r
65                     BufferedWriter bw = null;\r
66 \r
67                     file.createNewFile();\r
68                     fw = new FileWriter(file);\r
69                     bw = new BufferedWriter(fw);\r
70                     Writer writer = interfaceGenerator.generate(type);\r
71                     bw.write(writer.toString());\r
72 \r
73                     if (bw != null) {\r
74                         try {\r
75                             bw.close();\r
76                         } catch (IOException e) {\r
77                             // TODO: log?\r
78                         }\r
79                     }\r
80                 }\r
81             }\r
82             for (GeneratedTransferObject transferObject : genTransferObjects) {\r
83                 String parentPath = generateParentPath(path,\r
84                         transferObject.getPackageName());\r
85 \r
86                 File file = new File(parentPath, transferObject.getName() + ".java");\r
87                 File parent = file.getParentFile();\r
88                 if (!parent.exists()) {\r
89                     parent.mkdirs();\r
90                 }\r
91 \r
92                 if (!file.exists()) {\r
93                     FileWriter fw = null;\r
94                     BufferedWriter bw = null;\r
95 \r
96                     file.createNewFile();\r
97                     fw = new FileWriter(file);\r
98                     bw = new BufferedWriter(fw);\r
99                     Writer writer = classGenerator.generate(transferObject);\r
100                     bw.write(writer.toString());\r
101 \r
102                     if (bw != null) {\r
103                         try {\r
104                             bw.close();\r
105                         } catch (IOException e) {\r
106                             // TODO: log?\r
107                         }\r
108                     }\r
109                 }\r
110             }\r
111             return true;\r
112         } catch (IOException e) {\r
113             // TODO: log?\r
114             return false;\r
115         }\r
116     }\r
117 \r
118     private String generateParentPath(String path, String pkg) {\r
119         List<String> dirs = new ArrayList<String>();\r
120         String pkgPath = "";\r
121         if (pkg != null) {\r
122             if (pkg.length() > 0) {\r
123                 if (pkg.contains(".")) {\r
124                     String[] split = pkg.split("\\.");\r
125                     for (String dir : split) {\r
126                         dirs.add(dir);\r
127                     }\r
128                 } else {\r
129                     dirs.add(pkg);\r
130                 }\r
131                 for (int i = 0; i < dirs.size(); i++) {\r
132                     if (i == 0) {\r
133                         pkgPath += dirs.get(i);\r
134                     } else {\r
135                         pkgPath += File.separator + dirs.get(i);\r
136                     }\r
137                 }\r
138             }\r
139         }\r
140         String fullPath = "";\r
141         if (path != null) {\r
142             if (path.endsWith(File.separator)) {\r
143                 fullPath = path + pkgPath;\r
144             } else {\r
145                 fullPath = path + File.separator + pkgPath;\r
146             }\r
147         } else {\r
148             fullPath = pkgPath;\r
149         }\r
150         return fullPath;\r
151     }\r
152 \r
153 }\r

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.