blob: fcc52e83bccc5d7608fbc42fa6d800aeb7bac739 [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
Bharat saraswal594bc6d2016-02-22 22:15:21 +053028import org.apache.commons.io.FileUtils;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053029import org.apache.maven.model.Resource;
30import org.apache.maven.project.MavenProject;
31import org.onosproject.yangutils.utils.UtilConstants;
32import org.slf4j.Logger;
33import org.sonatype.plexus.build.incremental.BuildContext;
Bharat saraswal870c56f2016-02-20 21:57:16 +053034
Vinod Kumar S38046502016-03-23 15:30:27 +053035import static org.slf4j.LoggerFactory.getLogger;
36
Bharat saraswal870c56f2016-02-20 21:57:16 +053037/**
38 * Provides common utility functionalities for code generation.
39 */
40public final class YangIoUtils {
41
42 private static final Logger log = getLogger(YangIoUtils.class);
Vinod Kumar S38046502016-03-23 15:30:27 +053043 private static final String TARGET_RESOURCE_PATH = UtilConstants.SLASH + UtilConstants.TEMP + UtilConstants.SLASH
44 + UtilConstants.YANG_RESOURCES + UtilConstants.SLASH;
Bharat saraswal870c56f2016-02-20 21:57:16 +053045
46 /**
47 * Default constructor.
48 */
49 private YangIoUtils() {
50 }
51
52 /**
53 * Creates the directory structure.
54 *
55 * @param path directory path
56 * @return directory structure
57 */
58 public static File createDirectories(String path) {
59
b.janani68c55e12016-02-24 12:23:03 +053060 File generatedDir = new File(path);
Bharat saraswal870c56f2016-02-20 21:57:16 +053061 generatedDir.mkdirs();
62 return generatedDir;
63 }
64
65 /**
66 * Adds package info file for the created directory.
67 *
68 * @param path directory path
69 * @param classInfo class info for the package
70 * @param pack package of the directory
Vinod Kumar Sc4216002016-03-03 19:55:30 +053071 * @throws IOException when fails to create package info file
Bharat saraswal870c56f2016-02-20 21:57:16 +053072 */
73 public static void addPackageInfo(File path, String classInfo, String pack) throws IOException {
74
Vinod Kumar S38046502016-03-23 15:30:27 +053075 if (pack.contains(UtilConstants.ORG)) {
76 String[] strArray = pack.split(UtilConstants.ORG);
77 pack = UtilConstants.ORG + strArray[1];
Bharat saraswal4bf8b152016-02-25 02:26:43 +053078 }
Bharat saraswal870c56f2016-02-20 21:57:16 +053079 try {
80
81 File packageInfo = new File(path + File.separator + "package-info.java");
82 packageInfo.createNewFile();
b.janani68c55e12016-02-24 12:23:03 +053083 FileWriter fileWriter = null;
84 BufferedWriter bufferedWriter = null;
85 fileWriter = new FileWriter(packageInfo);
86 bufferedWriter = new BufferedWriter(fileWriter);
87 bufferedWriter.write(CopyrightHeader.getCopyrightHeader());
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053088 bufferedWriter.write(JavaDocGen.getJavaDoc(JavaDocGen.JavaDocType.PACKAGE_INFO, classInfo, false));
b.janani68c55e12016-02-24 12:23:03 +053089 bufferedWriter.write(UtilConstants.PACKAGE + UtilConstants.SPACE + pack + UtilConstants.SEMI_COLAN);
90 bufferedWriter.close();
Bharat saraswal870c56f2016-02-20 21:57:16 +053091 } catch (IOException e) {
92 throw new IOException("Exception occured while creating package info file.");
93 }
94 }
95
96 /**
97 * Cleans the generated directory if already exist in source folder.
98 *
Vinod Kumar S38046502016-03-23 15:30:27 +053099 * @param dir generated directory in previous build
Bharat saraswal870c56f2016-02-20 21:57:16 +0530100 */
Vinod Kumar S38046502016-03-23 15:30:27 +0530101 public static void clean(String dir) {
102
103 File generatedDirectory = new File(dir);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530104 if (generatedDirectory.exists()) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530105 try {
Bharat saraswal594bc6d2016-02-22 22:15:21 +0530106 FileUtils.deleteDirectory(generatedDirectory);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530107 } catch (IOException e) {
108 log.info("Failed to delete the generated files in " + generatedDirectory + " directory");
109 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530110 }
111 }
112
113 /**
114 * Adds generated source directory to the compilation root.
115 *
116 * @param source directory
117 * @param project current maven project
118 * @param context current build context
119 */
120 public static void addToSource(String source, MavenProject project, BuildContext context) {
121
122 project.addCompileSourceRoot(source);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530123 context.refresh(project.getBasedir());
124 log.info("Source directory added to compilation root: " + source);
125 }
126
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530127 /**
128 * Removes extra char from the string.
129 *
130 * @param valueString string to be trimmed
131 * @param removealStirng extra chars
132 * @return new string
133 */
134 public static String trimAtLast(String valueString, String removealStirng) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530135
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530136 StringBuilder stringBuilder = new StringBuilder(valueString);
137 int index = valueString.lastIndexOf(removealStirng);
138 stringBuilder.deleteCharAt(index);
139 return stringBuilder.toString();
140 }
141
142 /**
143 * Returns new parted string.
144 *
145 * @param partString string to be parted
146 * @return parted string
147 */
148 public static String partString(String partString) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530149
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530150 String[] strArray = partString.split(UtilConstants.COMMA);
151 String newString = "";
152 for (int i = 0; i < strArray.length; i++) {
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530153 if (i % 4 != 0 || i == 0) {
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530154 newString = newString + strArray[i] + UtilConstants.COMMA;
155 } else {
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530156 newString = newString + UtilConstants.NEW_LINE + UtilConstants.TWELVE_SPACE_INDENTATION
157 + strArray[i]
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530158 + UtilConstants.COMMA;
159 }
160 }
161 return trimAtLast(newString, UtilConstants.COMMA);
162 }
163
Vinod Kumar S38046502016-03-23 15:30:27 +0530164 /**
165 * Returns backspaced string.
166 *
167 * @param charString char string
168 * @return backspace string
169 */
170 public static String deleteLastChar(String charString) {
171
172 return charString.substring(0, charString.length() - 1);
173 }
174
175 /**
176 * Get the directory path of the package in canonical form.
177 *
178 * @param baseCodeGenPath base path where the generated files needs to be
179 * put.
180 * @param pathOfJavaPkg java package of the file being generated
181 * @return absolute path of the package in canonical form
182 */
183 public static String getDirectory(String baseCodeGenPath, String pathOfJavaPkg) {
184
185 if (pathOfJavaPkg.charAt(pathOfJavaPkg.length() - 1) == File.separatorChar) {
186 pathOfJavaPkg = trimAtLast(pathOfJavaPkg, UtilConstants.SLASH);
187 }
188 String[] strArray = pathOfJavaPkg.split(UtilConstants.SLASH);
189 if (strArray[0].equals(UtilConstants.EMPTY_STRING)) {
190 return pathOfJavaPkg;
191 } else {
192 return baseCodeGenPath + File.separator + pathOfJavaPkg;
193 }
194 }
195
196 /**
197 * Get the absolute path of the package in canonical form.
198 *
199 * @param baseCodeGenPath base path where the generated files needs to be
200 * put.
201 * @param pathOfJavaPkg java package of the file being generated
202 * @return absolute path of the package in canonical form
203 */
204 public static String getAbsolutePackagePath(String baseCodeGenPath, String pathOfJavaPkg) {
205
206 return baseCodeGenPath + pathOfJavaPkg;
207 }
208
209 /**
210 * Copy YANG files to the current project's output directory.
211 *
212 * @param yangFiles list of YANG files
213 * @param outputDir project's output directory
214 * @param project maven project
215 * @throws IOException when fails to copy files to destination resource
216 * directory
217 */
218 public static void copyYangFilesToTarget(List<String> yangFiles, String outputDir, MavenProject project)
219 throws IOException {
220
221 List<File> files = getListOfFile(yangFiles);
222
223 String path = outputDir + TARGET_RESOURCE_PATH;
224 File targetDir = new File(path);
225 targetDir.mkdirs();
226
227 for (File file : files) {
228 Files.copy(file.toPath(),
229 new File(path + file.getName()).toPath(),
230 StandardCopyOption.REPLACE_EXISTING);
231 }
232 Resource rsc = new Resource();
233 rsc.setDirectory(outputDir + UtilConstants.SLASH + UtilConstants.TEMP + UtilConstants.SLASH);
234 project.addResource(rsc);
235 }
236
237 /**
238 * Provides a list of files from list of strings.
239 *
240 * @param strings list of strings
241 * @return list of files
242 */
243 private static List<File> getListOfFile(List<String> strings) {
244
245 List<File> files = new ArrayList<>();
246 for (String file : strings) {
247 files.add(new File(file));
248 }
249 return files;
250 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530251}