blob: 78a3aef6ded3f8fbe6cd0d4be031de3231cfd600 [file] [log] [blame]
Bharat saraswal870c56f2016-02-20 21:57:16 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Bharat saraswal870c56f2016-02-20 21:57:16 +05303 *
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;
Bharat saraswale2d51d62016-03-23 19:40:35 +053039import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
40import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
41import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
42import static org.onosproject.yangutils.utils.UtilConstants.TEMP;
43import static org.onosproject.yangutils.utils.UtilConstants.TWELVE_SPACE_INDENTATION;
44import static org.onosproject.yangutils.utils.UtilConstants.YANG_RESOURCES;
45import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.appendFileContents;
46import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.updateFileHandle;
47import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.PACKAGE_INFO;
Vinod Kumar S38046502016-03-23 15:30:27 +053048import static org.slf4j.LoggerFactory.getLogger;
49
Bharat saraswal870c56f2016-02-20 21:57:16 +053050/**
51 * Provides common utility functionalities for code generation.
52 */
53public final class YangIoUtils {
54
55 private static final Logger log = getLogger(YangIoUtils.class);
Bharat saraswale2d51d62016-03-23 19:40:35 +053056 private static final String TARGET_RESOURCE_PATH = SLASH + TEMP + SLASH + YANG_RESOURCES + SLASH;
Bharat saraswal870c56f2016-02-20 21:57:16 +053057
58 /**
59 * Default constructor.
60 */
61 private YangIoUtils() {
62 }
63
64 /**
65 * Creates the directory structure.
66 *
67 * @param path directory path
68 * @return directory structure
69 */
70 public static File createDirectories(String path) {
71
b.janani68c55e12016-02-24 12:23:03 +053072 File generatedDir = new File(path);
Bharat saraswal870c56f2016-02-20 21:57:16 +053073 generatedDir.mkdirs();
74 return generatedDir;
75 }
76
77 /**
78 * Adds package info file for the created directory.
79 *
80 * @param path directory path
81 * @param classInfo class info for the package
82 * @param pack package of the directory
Vinod Kumar Sc4216002016-03-03 19:55:30 +053083 * @throws IOException when fails to create package info file
Bharat saraswal870c56f2016-02-20 21:57:16 +053084 */
85 public static void addPackageInfo(File path, String classInfo, String pack) throws IOException {
86
Bharat saraswale2d51d62016-03-23 19:40:35 +053087 if (pack.contains(ORG)) {
88 String[] strArray = pack.split(ORG);
89 pack = ORG + strArray[1];
Bharat saraswal4bf8b152016-02-25 02:26:43 +053090 }
Bharat saraswal870c56f2016-02-20 21:57:16 +053091 try {
92
Bharat saraswale2d51d62016-03-23 19:40:35 +053093 File packageInfo = new File(path + SLASH + "package-info.java");
Bharat saraswal870c56f2016-02-20 21:57:16 +053094 packageInfo.createNewFile();
Bharat saraswale2d51d62016-03-23 19:40:35 +053095
96 FileWriter fileWriter = new FileWriter(packageInfo);
97 BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
98
b.janani68c55e12016-02-24 12:23:03 +053099 bufferedWriter.write(CopyrightHeader.getCopyrightHeader());
Bharat saraswale2d51d62016-03-23 19:40:35 +0530100 bufferedWriter.write(JavaDocGen.getJavaDoc(PACKAGE_INFO, classInfo, false));
101 bufferedWriter.write(PACKAGE + SPACE + pack + SEMI_COLAN);
102
b.janani68c55e12016-02-24 12:23:03 +0530103 bufferedWriter.close();
Bharat saraswale2d51d62016-03-23 19:40:35 +0530104 fileWriter.close();
Bharat saraswal870c56f2016-02-20 21:57:16 +0530105 } catch (IOException e) {
106 throw new IOException("Exception occured while creating package info file.");
107 }
108 }
109
110 /**
111 * Cleans the generated directory if already exist in source folder.
112 *
Vinod Kumar S38046502016-03-23 15:30:27 +0530113 * @param dir generated directory in previous build
Bharat saraswale2d51d62016-03-23 19:40:35 +0530114 * @throws IOException when failed to delete directory
Bharat saraswal870c56f2016-02-20 21:57:16 +0530115 */
Bharat saraswale2d51d62016-03-23 19:40:35 +0530116 public static void clean(String dir) throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530117
118 File generatedDirectory = new File(dir);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530119 if (generatedDirectory.exists()) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530120 try {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530121 deleteDirectory(generatedDirectory);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530122 } catch (IOException e) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530123 throw new IOException("Failed to delete the generated files in " + generatedDirectory + " directory");
Bharat saraswal870c56f2016-02-20 21:57:16 +0530124 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530125 }
126 }
127
128 /**
129 * Adds generated source directory to the compilation root.
130 *
131 * @param source directory
132 * @param project current maven project
133 * @param context current build context
134 */
135 public static void addToSource(String source, MavenProject project, BuildContext context) {
136
137 project.addCompileSourceRoot(source);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530138 context.refresh(project.getBasedir());
139 log.info("Source directory added to compilation root: " + source);
140 }
141
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530142 /**
143 * Removes extra char from the string.
144 *
145 * @param valueString string to be trimmed
146 * @param removealStirng extra chars
147 * @return new string
148 */
149 public static String trimAtLast(String valueString, String removealStirng) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530150
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530151 StringBuilder stringBuilder = new StringBuilder(valueString);
152 int index = valueString.lastIndexOf(removealStirng);
153 stringBuilder.deleteCharAt(index);
154 return stringBuilder.toString();
155 }
156
157 /**
158 * Returns new parted string.
159 *
160 * @param partString string to be parted
161 * @return parted string
162 */
163 public static String partString(String partString) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530164
Bharat saraswale2d51d62016-03-23 19:40:35 +0530165 String[] strArray = partString.split(COMMA);
166 String newString = EMPTY_STRING;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530167 for (int i = 0; i < strArray.length; i++) {
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530168 if (i % 4 != 0 || i == 0) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530169 newString = newString + strArray[i] + COMMA;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530170 } else {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530171 newString = newString + NEW_LINE + TWELVE_SPACE_INDENTATION
172 + strArray[i] + COMMA;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530173 }
174 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530175 return trimAtLast(newString, COMMA);
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530176 }
177
Vinod Kumar S38046502016-03-23 15:30:27 +0530178 /**
Vinod Kumar S38046502016-03-23 15:30:27 +0530179 * Get the directory path of the package in canonical form.
180 *
181 * @param baseCodeGenPath base path where the generated files needs to be
Bharat saraswald6f12412016-03-28 15:50:13 +0530182 * put
Vinod Kumar S38046502016-03-23 15:30:27 +0530183 * @param pathOfJavaPkg java package of the file being generated
184 * @return absolute path of the package in canonical form
185 */
186 public static String getDirectory(String baseCodeGenPath, String pathOfJavaPkg) {
187
188 if (pathOfJavaPkg.charAt(pathOfJavaPkg.length() - 1) == File.separatorChar) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530189 pathOfJavaPkg = trimAtLast(pathOfJavaPkg, SLASH);
Vinod Kumar S38046502016-03-23 15:30:27 +0530190 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530191 String[] strArray = pathOfJavaPkg.split(SLASH);
192 if (strArray[0].equals(EMPTY_STRING)) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530193 return pathOfJavaPkg;
194 } else {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530195 return baseCodeGenPath + SLASH + pathOfJavaPkg;
Vinod Kumar S38046502016-03-23 15:30:27 +0530196 }
197 }
198
199 /**
200 * Get the absolute path of the package in canonical form.
201 *
202 * @param baseCodeGenPath base path where the generated files needs to be
Bharat saraswald6f12412016-03-28 15:50:13 +0530203 * put
Vinod Kumar S38046502016-03-23 15:30:27 +0530204 * @param pathOfJavaPkg java package of the file being generated
205 * @return absolute path of the package in canonical form
206 */
207 public static String getAbsolutePackagePath(String baseCodeGenPath, String pathOfJavaPkg) {
208
209 return baseCodeGenPath + pathOfJavaPkg;
210 }
211
212 /**
213 * Copy YANG files to the current project's output directory.
214 *
215 * @param yangFiles list of YANG files
216 * @param outputDir project's output directory
217 * @param project maven project
Bharat saraswale2d51d62016-03-23 19:40:35 +0530218 * @throws IOException when fails to copy files to destination resource directory
Vinod Kumar S38046502016-03-23 15:30:27 +0530219 */
220 public static void copyYangFilesToTarget(List<String> yangFiles, String outputDir, MavenProject project)
221 throws IOException {
222
223 List<File> files = getListOfFile(yangFiles);
224
225 String path = outputDir + TARGET_RESOURCE_PATH;
226 File targetDir = new File(path);
227 targetDir.mkdirs();
228
229 for (File file : files) {
230 Files.copy(file.toPath(),
Bharat saraswale2d51d62016-03-23 19:40:35 +0530231 (new File(path + file.getName())).toPath(),
Vinod Kumar S38046502016-03-23 15:30:27 +0530232 StandardCopyOption.REPLACE_EXISTING);
233 }
234 Resource rsc = new Resource();
Bharat saraswale2d51d62016-03-23 19:40:35 +0530235 rsc.setDirectory(outputDir + SLASH + TEMP + SLASH);
Vinod Kumar S38046502016-03-23 15:30:27 +0530236 project.addResource(rsc);
237 }
238
239 /**
240 * Provides a list of files from list of strings.
241 *
242 * @param strings list of strings
243 * @return list of files
244 */
245 private static List<File> getListOfFile(List<String> strings) {
246
247 List<File> files = new ArrayList<>();
248 for (String file : strings) {
249 files.add(new File(file));
250 }
251 return files;
252 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530253
254 /**
255 * Merge the temp java files to main java files.
256 *
257 * @param appendFile temp file
258 * @param srcFile main file
259 * @throws IOException when fails to append contents
260 */
261 public static void mergeJavaFiles(File appendFile, File srcFile) throws IOException {
262
263 try {
264 appendFileContents(appendFile, srcFile);
265 } catch (IOException e) {
266 throw new IOException("Failed to append " + appendFile + " in " + srcFile);
267 }
268 }
269
270 /**
271 * Insert data in the generated file.
272 *
273 * @param file file in which need to be inserted
274 * @param data data which need to be inserted
275 * @throws IOException when fails to insert into file
276 */
277 public static void insertDataIntoJavaFile(File file, String data) throws IOException {
278
279 try {
280 updateFileHandle(file, data, false);
281 } catch (IOException e) {
282 throw new IOException("Failed to insert in " + file + "file");
283 }
284 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530285}