blob: 866ff971eb2f9e0d19cdf0392a92f22b145ebdb9 [file] [log] [blame]
Bharat saraswal870c56f2016-02-20 21:57:16 +05301/*
2 * Copyright 2016 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.yangutils.utils.io.impl;
18
19import java.io.BufferedWriter;
20import java.io.File;
21import java.io.FileWriter;
22import java.io.IOException;
Vinod Kumar S38046502016-03-23 15:30:27 +053023import java.nio.file.Files;
24import java.nio.file.StandardCopyOption;
25import java.util.ArrayList;
Bharat saraswal870c56f2016-02-20 21:57:16 +053026import java.util.List;
27
Vinod Kumar Sc4216002016-03-03 19:55:30 +053028import org.apache.maven.model.Resource;
29import org.apache.maven.project.MavenProject;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053030import org.slf4j.Logger;
31import org.sonatype.plexus.build.incremental.BuildContext;
Bharat saraswal870c56f2016-02-20 21:57:16 +053032
Bharat saraswale2d51d62016-03-23 19:40:35 +053033import static org.apache.commons.io.FileUtils.deleteDirectory;
34import static org.onosproject.yangutils.utils.UtilConstants.COMMA;
35import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
36import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
37import static org.onosproject.yangutils.utils.UtilConstants.ORG;
38import static org.onosproject.yangutils.utils.UtilConstants.PACKAGE;
39import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
40import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
41import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
42import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
43import static org.onosproject.yangutils.utils.UtilConstants.TEMP;
44import static org.onosproject.yangutils.utils.UtilConstants.TWELVE_SPACE_INDENTATION;
45import static org.onosproject.yangutils.utils.UtilConstants.YANG_RESOURCES;
46import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.appendFileContents;
47import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.updateFileHandle;
48import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.PACKAGE_INFO;
Vinod Kumar S38046502016-03-23 15:30:27 +053049import static org.slf4j.LoggerFactory.getLogger;
50
Bharat saraswal870c56f2016-02-20 21:57:16 +053051/**
52 * Provides common utility functionalities for code generation.
53 */
54public final class YangIoUtils {
55
56 private static final Logger log = getLogger(YangIoUtils.class);
Bharat saraswale2d51d62016-03-23 19:40:35 +053057 private static final String TARGET_RESOURCE_PATH = SLASH + TEMP + SLASH + YANG_RESOURCES + SLASH;
Bharat saraswal870c56f2016-02-20 21:57:16 +053058
59 /**
60 * Default constructor.
61 */
62 private YangIoUtils() {
63 }
64
65 /**
66 * Creates the directory structure.
67 *
68 * @param path directory path
69 * @return directory structure
70 */
71 public static File createDirectories(String path) {
72
b.janani68c55e12016-02-24 12:23:03 +053073 File generatedDir = new File(path);
Bharat saraswal870c56f2016-02-20 21:57:16 +053074 generatedDir.mkdirs();
75 return generatedDir;
76 }
77
78 /**
79 * Adds package info file for the created directory.
80 *
81 * @param path directory path
82 * @param classInfo class info for the package
83 * @param pack package of the directory
Vinod Kumar Sc4216002016-03-03 19:55:30 +053084 * @throws IOException when fails to create package info file
Bharat saraswal870c56f2016-02-20 21:57:16 +053085 */
86 public static void addPackageInfo(File path, String classInfo, String pack) throws IOException {
87
Bharat saraswale2d51d62016-03-23 19:40:35 +053088 if (pack.contains(ORG)) {
89 String[] strArray = pack.split(ORG);
90 pack = ORG + strArray[1];
Bharat saraswal4bf8b152016-02-25 02:26:43 +053091 }
Bharat saraswal870c56f2016-02-20 21:57:16 +053092 try {
93
Bharat saraswale2d51d62016-03-23 19:40:35 +053094 File packageInfo = new File(path + SLASH + "package-info.java");
Bharat saraswal870c56f2016-02-20 21:57:16 +053095 packageInfo.createNewFile();
Bharat saraswale2d51d62016-03-23 19:40:35 +053096
97 FileWriter fileWriter = new FileWriter(packageInfo);
98 BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
99
b.janani68c55e12016-02-24 12:23:03 +0530100 bufferedWriter.write(CopyrightHeader.getCopyrightHeader());
Bharat saraswale2d51d62016-03-23 19:40:35 +0530101 bufferedWriter.write(JavaDocGen.getJavaDoc(PACKAGE_INFO, classInfo, false));
102 bufferedWriter.write(PACKAGE + SPACE + pack + SEMI_COLAN);
103
b.janani68c55e12016-02-24 12:23:03 +0530104 bufferedWriter.close();
Bharat saraswale2d51d62016-03-23 19:40:35 +0530105 fileWriter.close();
Bharat saraswal870c56f2016-02-20 21:57:16 +0530106 } catch (IOException e) {
107 throw new IOException("Exception occured while creating package info file.");
108 }
109 }
110
111 /**
112 * Cleans the generated directory if already exist in source folder.
113 *
Vinod Kumar S38046502016-03-23 15:30:27 +0530114 * @param dir generated directory in previous build
Bharat saraswale2d51d62016-03-23 19:40:35 +0530115 * @throws IOException when failed to delete directory
Bharat saraswal870c56f2016-02-20 21:57:16 +0530116 */
Bharat saraswale2d51d62016-03-23 19:40:35 +0530117 public static void clean(String dir) throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530118
119 File generatedDirectory = new File(dir);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530120 if (generatedDirectory.exists()) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530121 try {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530122 deleteDirectory(generatedDirectory);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530123 } catch (IOException e) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530124 throw new IOException("Failed to delete the generated files in " + generatedDirectory + " directory");
Bharat saraswal870c56f2016-02-20 21:57:16 +0530125 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530126 }
127 }
128
129 /**
130 * Adds generated source directory to the compilation root.
131 *
132 * @param source directory
133 * @param project current maven project
134 * @param context current build context
135 */
136 public static void addToSource(String source, MavenProject project, BuildContext context) {
137
138 project.addCompileSourceRoot(source);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530139 context.refresh(project.getBasedir());
140 log.info("Source directory added to compilation root: " + source);
141 }
142
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530143 /**
144 * Removes extra char from the string.
145 *
146 * @param valueString string to be trimmed
147 * @param removealStirng extra chars
148 * @return new string
149 */
150 public static String trimAtLast(String valueString, String removealStirng) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530151
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530152 StringBuilder stringBuilder = new StringBuilder(valueString);
153 int index = valueString.lastIndexOf(removealStirng);
154 stringBuilder.deleteCharAt(index);
155 return stringBuilder.toString();
156 }
157
158 /**
159 * Returns new parted string.
160 *
161 * @param partString string to be parted
162 * @return parted string
163 */
164 public static String partString(String partString) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530165
Bharat saraswale2d51d62016-03-23 19:40:35 +0530166 String[] strArray = partString.split(COMMA);
167 String newString = EMPTY_STRING;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530168 for (int i = 0; i < strArray.length; i++) {
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530169 if (i % 4 != 0 || i == 0) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530170 newString = newString + strArray[i] + COMMA;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530171 } else {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530172 newString = newString + NEW_LINE + TWELVE_SPACE_INDENTATION
173 + strArray[i] + COMMA;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530174 }
175 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530176 return trimAtLast(newString, COMMA);
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530177 }
178
Vinod Kumar S38046502016-03-23 15:30:27 +0530179 /**
180 * Returns backspaced string.
181 *
182 * @param charString char string
183 * @return backspace string
184 */
185 public static String deleteLastChar(String charString) {
186
187 return charString.substring(0, charString.length() - 1);
188 }
189
190 /**
191 * Get the directory path of the package in canonical form.
192 *
193 * @param baseCodeGenPath base path where the generated files needs to be
194 * put.
195 * @param pathOfJavaPkg java package of the file being generated
196 * @return absolute path of the package in canonical form
197 */
198 public static String getDirectory(String baseCodeGenPath, String pathOfJavaPkg) {
199
200 if (pathOfJavaPkg.charAt(pathOfJavaPkg.length() - 1) == File.separatorChar) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530201 pathOfJavaPkg = trimAtLast(pathOfJavaPkg, SLASH);
Vinod Kumar S38046502016-03-23 15:30:27 +0530202 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530203 String[] strArray = pathOfJavaPkg.split(SLASH);
204 if (strArray[0].equals(EMPTY_STRING)) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530205 return pathOfJavaPkg;
206 } else {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530207 return baseCodeGenPath + SLASH + pathOfJavaPkg;
Vinod Kumar S38046502016-03-23 15:30:27 +0530208 }
209 }
210
211 /**
212 * Get the absolute path of the package in canonical form.
213 *
214 * @param baseCodeGenPath base path where the generated files needs to be
215 * put.
216 * @param pathOfJavaPkg java package of the file being generated
217 * @return absolute path of the package in canonical form
218 */
219 public static String getAbsolutePackagePath(String baseCodeGenPath, String pathOfJavaPkg) {
220
221 return baseCodeGenPath + pathOfJavaPkg;
222 }
223
224 /**
225 * Copy YANG files to the current project's output directory.
226 *
227 * @param yangFiles list of YANG files
228 * @param outputDir project's output directory
229 * @param project maven project
Bharat saraswale2d51d62016-03-23 19:40:35 +0530230 * @throws IOException when fails to copy files to destination resource directory
Vinod Kumar S38046502016-03-23 15:30:27 +0530231 */
232 public static void copyYangFilesToTarget(List<String> yangFiles, String outputDir, MavenProject project)
233 throws IOException {
234
235 List<File> files = getListOfFile(yangFiles);
236
237 String path = outputDir + TARGET_RESOURCE_PATH;
238 File targetDir = new File(path);
239 targetDir.mkdirs();
240
241 for (File file : files) {
242 Files.copy(file.toPath(),
Bharat saraswale2d51d62016-03-23 19:40:35 +0530243 (new File(path + file.getName())).toPath(),
Vinod Kumar S38046502016-03-23 15:30:27 +0530244 StandardCopyOption.REPLACE_EXISTING);
245 }
246 Resource rsc = new Resource();
Bharat saraswale2d51d62016-03-23 19:40:35 +0530247 rsc.setDirectory(outputDir + SLASH + TEMP + SLASH);
Vinod Kumar S38046502016-03-23 15:30:27 +0530248 project.addResource(rsc);
249 }
250
251 /**
252 * Provides a list of files from list of strings.
253 *
254 * @param strings list of strings
255 * @return list of files
256 */
257 private static List<File> getListOfFile(List<String> strings) {
258
259 List<File> files = new ArrayList<>();
260 for (String file : strings) {
261 files.add(new File(file));
262 }
263 return files;
264 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530265
266 /**
267 * Merge the temp java files to main java files.
268 *
269 * @param appendFile temp file
270 * @param srcFile main file
271 * @throws IOException when fails to append contents
272 */
273 public static void mergeJavaFiles(File appendFile, File srcFile) throws IOException {
274
275 try {
276 appendFileContents(appendFile, srcFile);
277 } catch (IOException e) {
278 throw new IOException("Failed to append " + appendFile + " in " + srcFile);
279 }
280 }
281
282 /**
283 * Insert data in the generated file.
284 *
285 * @param file file in which need to be inserted
286 * @param data data which need to be inserted
287 * @throws IOException when fails to insert into file
288 */
289 public static void insertDataIntoJavaFile(File file, String data) throws IOException {
290
291 try {
292 updateFileHandle(file, data, false);
293 } catch (IOException e) {
294 throw new IOException("Failed to insert in " + file + "file");
295 }
296 }
297
298 /**
299 * Convert directory path in java package format.
300 *
301 * @param path directory path
302 * @return java package
303 */
304 public static String convertPathToPkg(String path) {
305
306 return path.replace(SLASH, PERIOD);
307 }
308
309 /**
310 * Convert java package in directory path format.
311 *
312 * @param pkg java package
313 * @return directory path
314 */
315 public static String convertPkgToPath(String pkg) {
316
317 return pkg.replace(PERIOD, SLASH);
318 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530319}