blob: 51ca81c1501f1770d1551d1ab3685f4b21bbf700 [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;
Bharat saraswald72411a2016-04-19 01:00:16 +053047import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
Bharat saraswale2d51d62016-03-23 19:40:35 +053048import 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/**
Bharat saraswald9822e92016-04-05 15:13:44 +053052 * Represents common utility functionalities for code generation.
Bharat saraswal870c56f2016-02-20 21:57:16 +053053 */
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 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053060 * Creates an instance of YANG io utils.
Bharat saraswal870c56f2016-02-20 21:57:16 +053061 */
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) {
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 saraswald72411a2016-04-19 01:00:16 +0530100 bufferedWriter.write(getJavaDoc(PACKAGE_INFO, classInfo, false));
Bharat saraswale2d51d62016-03-23 19:40:35 +0530101 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 File generatedDirectory = new File(dir);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530118 if (generatedDirectory.exists()) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530119 try {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530120 deleteDirectory(generatedDirectory);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530121 } catch (IOException e) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530122 throw new IOException("Failed to delete the generated files in " + generatedDirectory + " directory");
Bharat saraswal870c56f2016-02-20 21:57:16 +0530123 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530124 }
125 }
126
127 /**
128 * Adds generated source directory to the compilation root.
129 *
130 * @param source directory
131 * @param project current maven project
132 * @param context current build context
133 */
134 public static void addToSource(String source, MavenProject project, BuildContext context) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530135 project.addCompileSourceRoot(source);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530136 context.refresh(project.getBasedir());
137 log.info("Source directory added to compilation root: " + source);
138 }
139
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530140 /**
141 * Removes extra char from the string.
142 *
143 * @param valueString string to be trimmed
144 * @param removealStirng extra chars
145 * @return new string
146 */
147 public static String trimAtLast(String valueString, String removealStirng) {
148 StringBuilder stringBuilder = new StringBuilder(valueString);
149 int index = valueString.lastIndexOf(removealStirng);
150 stringBuilder.deleteCharAt(index);
151 return stringBuilder.toString();
152 }
153
154 /**
155 * Returns new parted string.
156 *
157 * @param partString string to be parted
158 * @return parted string
159 */
160 public static String partString(String partString) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530161 String[] strArray = partString.split(COMMA);
162 String newString = EMPTY_STRING;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530163 for (int i = 0; i < strArray.length; i++) {
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530164 if (i % 4 != 0 || i == 0) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530165 newString = newString + strArray[i] + COMMA;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530166 } else {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530167 newString = newString + NEW_LINE + TWELVE_SPACE_INDENTATION
168 + strArray[i] + COMMA;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530169 }
170 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530171 return trimAtLast(newString, COMMA);
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530172 }
173
Vinod Kumar S38046502016-03-23 15:30:27 +0530174 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530175 * Returns the directory path of the package in canonical form.
Vinod Kumar S38046502016-03-23 15:30:27 +0530176 *
177 * @param baseCodeGenPath base path where the generated files needs to be
Bharat saraswald6f12412016-03-28 15:50:13 +0530178 * put
Vinod Kumar S38046502016-03-23 15:30:27 +0530179 * @param pathOfJavaPkg java package of the file being generated
180 * @return absolute path of the package in canonical form
181 */
182 public static String getDirectory(String baseCodeGenPath, String pathOfJavaPkg) {
183
184 if (pathOfJavaPkg.charAt(pathOfJavaPkg.length() - 1) == File.separatorChar) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530185 pathOfJavaPkg = trimAtLast(pathOfJavaPkg, SLASH);
Vinod Kumar S38046502016-03-23 15:30:27 +0530186 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530187 String[] strArray = pathOfJavaPkg.split(SLASH);
188 if (strArray[0].equals(EMPTY_STRING)) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530189 return pathOfJavaPkg;
190 } else {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530191 return baseCodeGenPath + SLASH + pathOfJavaPkg;
Vinod Kumar S38046502016-03-23 15:30:27 +0530192 }
193 }
194
195 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530196 * Returns the absolute path of the package in canonical form.
Vinod Kumar S38046502016-03-23 15:30:27 +0530197 *
198 * @param baseCodeGenPath base path where the generated files needs to be
Bharat saraswald6f12412016-03-28 15:50:13 +0530199 * put
Vinod Kumar S38046502016-03-23 15:30:27 +0530200 * @param pathOfJavaPkg java package of the file being generated
201 * @return absolute path of the package in canonical form
202 */
203 public static String getAbsolutePackagePath(String baseCodeGenPath, String pathOfJavaPkg) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530204 return baseCodeGenPath + pathOfJavaPkg;
205 }
206
207 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530208 * Copies YANG files to the current project's output directory.
Vinod Kumar S38046502016-03-23 15:30:27 +0530209 *
210 * @param yangFiles list of YANG files
211 * @param outputDir project's output directory
212 * @param project maven project
Bharat saraswale2d51d62016-03-23 19:40:35 +0530213 * @throws IOException when fails to copy files to destination resource directory
Vinod Kumar S38046502016-03-23 15:30:27 +0530214 */
215 public static void copyYangFilesToTarget(List<String> yangFiles, String outputDir, MavenProject project)
216 throws IOException {
217
218 List<File> files = getListOfFile(yangFiles);
219
220 String path = outputDir + TARGET_RESOURCE_PATH;
221 File targetDir = new File(path);
222 targetDir.mkdirs();
223
224 for (File file : files) {
225 Files.copy(file.toPath(),
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530226 new File(path + file.getName()).toPath(),
Vinod Kumar S38046502016-03-23 15:30:27 +0530227 StandardCopyOption.REPLACE_EXISTING);
228 }
229 Resource rsc = new Resource();
Bharat saraswale2d51d62016-03-23 19:40:35 +0530230 rsc.setDirectory(outputDir + SLASH + TEMP + SLASH);
Vinod Kumar S38046502016-03-23 15:30:27 +0530231 project.addResource(rsc);
232 }
233
234 /**
235 * Provides a list of files from list of strings.
236 *
237 * @param strings list of strings
238 * @return list of files
239 */
240 private static List<File> getListOfFile(List<String> strings) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530241 List<File> files = new ArrayList<>();
242 for (String file : strings) {
243 files.add(new File(file));
244 }
245 return files;
246 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530247
248 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530249 * Merges the temp java files to main java files.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530250 *
251 * @param appendFile temp file
252 * @param srcFile main file
253 * @throws IOException when fails to append contents
254 */
255 public static void mergeJavaFiles(File appendFile, File srcFile) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530256 try {
257 appendFileContents(appendFile, srcFile);
258 } catch (IOException e) {
259 throw new IOException("Failed to append " + appendFile + " in " + srcFile);
260 }
261 }
262
263 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530264 * Inserts data in the generated file.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530265 *
266 * @param file file in which need to be inserted
267 * @param data data which need to be inserted
268 * @throws IOException when fails to insert into file
269 */
270 public static void insertDataIntoJavaFile(File file, String data) throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530271 try {
272 updateFileHandle(file, data, false);
273 } catch (IOException e) {
274 throw new IOException("Failed to insert in " + file + "file");
275 }
276 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530277}