blob: 05cd471b078824c0ee25b5716c6df115dcf6c413 [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
Bharat saraswal33dfa012016-05-17 19:59:16 +053019import java.io.BufferedReader;
Bharat saraswal870c56f2016-02-20 21:57:16 +053020import java.io.BufferedWriter;
21import java.io.File;
Bharat saraswal96dfef02016-06-16 00:29:12 +053022import java.io.FileInputStream;
23import java.io.FileOutputStream;
Bharat saraswal33dfa012016-05-17 19:59:16 +053024import java.io.FileReader;
Bharat saraswal870c56f2016-02-20 21:57:16 +053025import java.io.FileWriter;
26import java.io.IOException;
Bharat saraswal96dfef02016-06-16 00:29:12 +053027import java.io.InputStream;
28import java.io.ObjectInputStream;
29import java.io.ObjectOutputStream;
Vinod Kumar S38046502016-03-23 15:30:27 +053030import java.nio.file.Files;
31import java.nio.file.StandardCopyOption;
32import java.util.ArrayList;
Bharat saraswal96dfef02016-06-16 00:29:12 +053033import java.util.Enumeration;
Vidyashree Rama1db15562016-05-17 16:16:15 +053034import java.util.Iterator;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053035import java.util.LinkedList;
Bharat saraswal33dfa012016-05-17 19:59:16 +053036import java.util.List;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053037import java.util.Set;
Bharat saraswal33dfa012016-05-17 19:59:16 +053038import java.util.Stack;
Bharat saraswal96dfef02016-06-16 00:29:12 +053039import java.util.jar.JarEntry;
40import java.util.jar.JarFile;
Bharat saraswal33dfa012016-05-17 19:59:16 +053041import java.util.regex.Pattern;
Bharat saraswal870c56f2016-02-20 21:57:16 +053042
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053043import org.apache.commons.io.FileUtils;
Bharat saraswal96dfef02016-06-16 00:29:12 +053044import org.apache.maven.artifact.repository.ArtifactRepository;
45import org.apache.maven.model.Dependency;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053046import org.apache.maven.model.Resource;
47import org.apache.maven.project.MavenProject;
Bharat saraswal96dfef02016-06-16 00:29:12 +053048import org.onosproject.yangutils.datamodel.YangNode;
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053049import org.onosproject.yangutils.plugin.manager.YangFileInfo;
Bharat saraswal33dfa012016-05-17 19:59:16 +053050import org.onosproject.yangutils.translator.tojava.utils.YangPluginConfig;
Vinod Kumar Sc4216002016-03-03 19:55:30 +053051import org.slf4j.Logger;
52import org.sonatype.plexus.build.incremental.BuildContext;
Bharat saraswal870c56f2016-02-20 21:57:16 +053053
Bharat saraswal96dfef02016-06-16 00:29:12 +053054import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCamelCase;
55import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
Bharat saraswale2d51d62016-03-23 19:40:35 +053056import static org.onosproject.yangutils.utils.UtilConstants.COMMA;
57import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
Bharat saraswal33dfa012016-05-17 19:59:16 +053058import static org.onosproject.yangutils.utils.UtilConstants.HASH;
Bharat saraswal96dfef02016-06-16 00:29:12 +053059import static org.onosproject.yangutils.utils.UtilConstants.HYPHEN;
60import static org.onosproject.yangutils.utils.UtilConstants.JAR;
Bharat saraswale2d51d62016-03-23 19:40:35 +053061import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Bharat saraswal33dfa012016-05-17 19:59:16 +053062import static org.onosproject.yangutils.utils.UtilConstants.OPEN_PARENTHESIS;
Bharat saraswale2d51d62016-03-23 19:40:35 +053063import static org.onosproject.yangutils.utils.UtilConstants.ORG;
64import static org.onosproject.yangutils.utils.UtilConstants.PACKAGE;
Bharat saraswal33dfa012016-05-17 19:59:16 +053065import static org.onosproject.yangutils.utils.UtilConstants.PERIOD;
Bharat saraswale2d51d62016-03-23 19:40:35 +053066import static org.onosproject.yangutils.utils.UtilConstants.SEMI_COLAN;
67import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
68import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
69import static org.onosproject.yangutils.utils.UtilConstants.TEMP;
70import static org.onosproject.yangutils.utils.UtilConstants.TWELVE_SPACE_INDENTATION;
71import static org.onosproject.yangutils.utils.UtilConstants.YANG_RESOURCES;
72import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.appendFileContents;
73import static org.onosproject.yangutils.utils.io.impl.FileSystemUtil.updateFileHandle;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053074import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.getJavaDoc;
Bharat saraswal96dfef02016-06-16 00:29:12 +053075import static org.onosproject.yangutils.utils.io.impl.JavaDocGen.JavaDocType.PACKAGE_INFO;
Vinod Kumar S38046502016-03-23 15:30:27 +053076import static org.slf4j.LoggerFactory.getLogger;
77
Bharat saraswal870c56f2016-02-20 21:57:16 +053078/**
Bharat saraswald9822e92016-04-05 15:13:44 +053079 * Represents common utility functionalities for code generation.
Bharat saraswal870c56f2016-02-20 21:57:16 +053080 */
81public final class YangIoUtils {
82
83 private static final Logger log = getLogger(YangIoUtils.class);
Bharat saraswale2d51d62016-03-23 19:40:35 +053084 private static final String TARGET_RESOURCE_PATH = SLASH + TEMP + SLASH + YANG_RESOURCES + SLASH;
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +053085 private static final int LINE_SIZE = 118;
Bharat saraswal33dfa012016-05-17 19:59:16 +053086 private static final int SUB_LINE_SIZE = 112;
87 private static final int ZERO = 0;
Bharat saraswal96dfef02016-06-16 00:29:12 +053088 private static final String SERIALIZED_FILE_EXTENSION = ".ser";
Bharat saraswal870c56f2016-02-20 21:57:16 +053089
90 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053091 * Creates an instance of YANG io utils.
Bharat saraswal870c56f2016-02-20 21:57:16 +053092 */
93 private YangIoUtils() {
94 }
95
96 /**
97 * Creates the directory structure.
98 *
99 * @param path directory path
100 * @return directory structure
101 */
102 public static File createDirectories(String path) {
b.janani68c55e12016-02-24 12:23:03 +0530103 File generatedDir = new File(path);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530104 generatedDir.mkdirs();
105 return generatedDir;
106 }
107
108 /**
109 * Adds package info file for the created directory.
110 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530111 * @param path directory path
112 * @param classInfo class info for the package
113 * @param pack package of the directory
114 * @param isChildNode is it a child node
Bharat saraswal33dfa012016-05-17 19:59:16 +0530115 * @param pluginConfig plugin configurations
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530116 * @throws IOException when fails to create package info file
Bharat saraswal870c56f2016-02-20 21:57:16 +0530117 */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530118 public static void addPackageInfo(File path, String classInfo, String pack, boolean isChildNode,
119 YangPluginConfig pluginConfig)
Bharat saraswalc0e04842016-05-12 13:16:57 +0530120 throws IOException {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530121
Bharat saraswal33dfa012016-05-17 19:59:16 +0530122 pack = parsePkg(pack);
123
Bharat saraswal870c56f2016-02-20 21:57:16 +0530124 try {
125
Bharat saraswale2d51d62016-03-23 19:40:35 +0530126 File packageInfo = new File(path + SLASH + "package-info.java");
Bharat saraswal870c56f2016-02-20 21:57:16 +0530127 packageInfo.createNewFile();
Bharat saraswale2d51d62016-03-23 19:40:35 +0530128
129 FileWriter fileWriter = new FileWriter(packageInfo);
130 BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
131
b.janani68c55e12016-02-24 12:23:03 +0530132 bufferedWriter.write(CopyrightHeader.getCopyrightHeader());
Bharat saraswal33dfa012016-05-17 19:59:16 +0530133 bufferedWriter.write(getJavaDoc(PACKAGE_INFO, classInfo, isChildNode, pluginConfig));
134 String pkg = PACKAGE + SPACE + pack + SEMI_COLAN;
135 if (pkg.length() > LINE_SIZE) {
136 pkg = whenDelimiterIsPersent(pkg, LINE_SIZE);
137 }
138 bufferedWriter.write(pkg);
b.janani68c55e12016-02-24 12:23:03 +0530139 bufferedWriter.close();
Bharat saraswale2d51d62016-03-23 19:40:35 +0530140 fileWriter.close();
Bharat saraswal870c56f2016-02-20 21:57:16 +0530141 } catch (IOException e) {
142 throw new IOException("Exception occured while creating package info file.");
143 }
144 }
145
146 /**
Bharat saraswal33dfa012016-05-17 19:59:16 +0530147 * Parses package and returns updated package.
148 *
149 * @param pack package needs to be updated
150 * @return updated package
151 */
152 public static String parsePkg(String pack) {
153
154 if (pack.contains(ORG)) {
155 String[] strArray = pack.split(ORG);
156 if (strArray.length >= 3) {
157 for (int i = 1; i < strArray.length; i++) {
158 if (i == 1) {
159 pack = ORG + strArray[1];
160 } else {
161 pack = pack + ORG + strArray[i];
162 }
163 }
164 } else {
165 pack = ORG + strArray[1];
166 }
167 }
168
169 return pack;
170 }
171
172 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +0530173 * Cleans the generated directory if already exist in source folder.
174 *
Vinod Kumar S38046502016-03-23 15:30:27 +0530175 * @param dir generated directory in previous build
Bharat saraswale2d51d62016-03-23 19:40:35 +0530176 * @throws IOException when failed to delete directory
Bharat saraswal870c56f2016-02-20 21:57:16 +0530177 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530178 public static void deleteDirectory(String dir)
179 throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530180 File generatedDirectory = new File(dir);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530181 if (generatedDirectory.exists()) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530182 try {
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530183 FileUtils.deleteDirectory(generatedDirectory);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530184 } catch (IOException e) {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530185 throw new IOException(
186 "Failed to delete the generated files in " + generatedDirectory + " directory");
Bharat saraswal870c56f2016-02-20 21:57:16 +0530187 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530188 }
189 }
190
191 /**
Bharat saraswalc0e04842016-05-12 13:16:57 +0530192 * Searches and deletes generated temporary directories.
193 *
194 * @param root root directory
195 * @throws IOException when fails to do IO operations.
196 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530197 public static void searchAndDeleteTempDir(String root)
198 throws IOException {
Bharat saraswalc0e04842016-05-12 13:16:57 +0530199 List<File> store = new LinkedList<>();
200 Stack<String> stack = new Stack<>();
201 stack.push(root);
202
203 while (!stack.empty()) {
204 root = stack.pop();
205 File file = new File(root);
206 File[] filelist = file.listFiles();
207 if (filelist == null || filelist.length == 0) {
208 continue;
209 }
210 for (File current : filelist) {
211 if (current.isDirectory()) {
212 stack.push(current.toString());
213 if (current.getName().endsWith("-Temp")) {
214 store.add(current);
215 }
216 }
217 }
218 }
219
220 for (File dir : store) {
Bharat saraswalab4c6ba2016-05-17 14:19:38 +0530221 FileUtils.deleteDirectory(dir);
Bharat saraswalc0e04842016-05-12 13:16:57 +0530222 }
223 }
224
225 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +0530226 * Adds generated source directory to the compilation root.
227 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530228 * @param source directory
Bharat saraswal870c56f2016-02-20 21:57:16 +0530229 * @param project current maven project
230 * @param context current build context
231 */
Bharat saraswal96dfef02016-06-16 00:29:12 +0530232 public static void addToCompilationRoot(String source, MavenProject project, BuildContext context) {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530233 project.addCompileSourceRoot(source);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530234 context.refresh(project.getBasedir());
235 log.info("Source directory added to compilation root: " + source);
236 }
237
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530238 /**
239 * Removes extra char from the string.
240 *
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530241 * @param valueString string to be trimmed
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530242 * @param removealStirng extra chars
243 * @return new string
244 */
245 public static String trimAtLast(String valueString, String removealStirng) {
246 StringBuilder stringBuilder = new StringBuilder(valueString);
247 int index = valueString.lastIndexOf(removealStirng);
248 stringBuilder.deleteCharAt(index);
249 return stringBuilder.toString();
250 }
251
252 /**
253 * Returns new parted string.
254 *
255 * @param partString string to be parted
256 * @return parted string
257 */
258 public static String partString(String partString) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530259 String[] strArray = partString.split(COMMA);
260 String newString = EMPTY_STRING;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530261 for (int i = 0; i < strArray.length; i++) {
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530262 if (i % 4 != 0 || i == 0) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530263 newString = newString + strArray[i] + COMMA;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530264 } else {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530265 newString = newString + NEW_LINE + TWELVE_SPACE_INDENTATION
266 + strArray[i] + COMMA;
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530267 }
268 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530269 return trimAtLast(newString, COMMA);
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530270 }
271
Vinod Kumar S38046502016-03-23 15:30:27 +0530272 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530273 * Returns the directory path of the package in canonical form.
Vinod Kumar S38046502016-03-23 15:30:27 +0530274 *
275 * @param baseCodeGenPath base path where the generated files needs to be
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530276 * put
277 * @param pathOfJavaPkg java package of the file being generated
Vinod Kumar S38046502016-03-23 15:30:27 +0530278 * @return absolute path of the package in canonical form
279 */
280 public static String getDirectory(String baseCodeGenPath, String pathOfJavaPkg) {
281
282 if (pathOfJavaPkg.charAt(pathOfJavaPkg.length() - 1) == File.separatorChar) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530283 pathOfJavaPkg = trimAtLast(pathOfJavaPkg, SLASH);
Vinod Kumar S38046502016-03-23 15:30:27 +0530284 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530285 String[] strArray = pathOfJavaPkg.split(SLASH);
286 if (strArray[0].equals(EMPTY_STRING)) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530287 return pathOfJavaPkg;
288 } else {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530289 return baseCodeGenPath + SLASH + pathOfJavaPkg;
Vinod Kumar S38046502016-03-23 15:30:27 +0530290 }
291 }
292
293 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530294 * Returns the absolute path of the package in canonical form.
Vinod Kumar S38046502016-03-23 15:30:27 +0530295 *
296 * @param baseCodeGenPath base path where the generated files needs to be
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530297 * put
298 * @param pathOfJavaPkg java package of the file being generated
Vinod Kumar S38046502016-03-23 15:30:27 +0530299 * @return absolute path of the package in canonical form
300 */
301 public static String getAbsolutePackagePath(String baseCodeGenPath, String pathOfJavaPkg) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530302 return baseCodeGenPath + pathOfJavaPkg;
303 }
304
305 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530306 * Copies YANG files to the current project's output directory.
Vinod Kumar S38046502016-03-23 15:30:27 +0530307 *
Bharat saraswal33dfa012016-05-17 19:59:16 +0530308 * @param yangFileInfo list of YANG files
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530309 * @param outputDir project's output directory
310 * @param project maven project
Bharat saraswale2d51d62016-03-23 19:40:35 +0530311 * @throws IOException when fails to copy files to destination resource directory
Vinod Kumar S38046502016-03-23 15:30:27 +0530312 */
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530313 public static void copyYangFilesToTarget(Set<YangFileInfo> yangFileInfo, String outputDir, MavenProject project)
Vinod Kumar S38046502016-03-23 15:30:27 +0530314 throws IOException {
315
Vidyashree Rama1db15562016-05-17 16:16:15 +0530316 List<File> files = getListOfFile(yangFileInfo);
Vinod Kumar S38046502016-03-23 15:30:27 +0530317
318 String path = outputDir + TARGET_RESOURCE_PATH;
319 File targetDir = new File(path);
320 targetDir.mkdirs();
321
322 for (File file : files) {
323 Files.copy(file.toPath(),
Vidyashree Rama7142d9c2016-04-26 15:06:06 +0530324 new File(path + file.getName()).toPath(),
Vinod Kumar S38046502016-03-23 15:30:27 +0530325 StandardCopyOption.REPLACE_EXISTING);
326 }
Bharat saraswal96dfef02016-06-16 00:29:12 +0530327 addToProjectResource(outputDir + SLASH + TEMP + SLASH, project);
Vinod Kumar S38046502016-03-23 15:30:27 +0530328 }
329
330 /**
331 * Provides a list of files from list of strings.
332 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530333 * @param yangFileInfo set of yang file information
Vinod Kumar S38046502016-03-23 15:30:27 +0530334 * @return list of files
335 */
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530336 private static List<File> getListOfFile(Set<YangFileInfo> yangFileInfo) {
Vinod Kumar S38046502016-03-23 15:30:27 +0530337 List<File> files = new ArrayList<>();
Vidyashree Rama1db15562016-05-17 16:16:15 +0530338 Iterator<YangFileInfo> yangFileIterator = yangFileInfo.iterator();
339 while (yangFileIterator.hasNext()) {
340 YangFileInfo yangFile = yangFileIterator.next();
Bharat saraswal96dfef02016-06-16 00:29:12 +0530341 if (yangFile.isForTranslator()) {
342 files.add(new File(yangFile.getYangFileName()));
343 }
Vinod Kumar S38046502016-03-23 15:30:27 +0530344 }
345 return files;
346 }
Bharat saraswale2d51d62016-03-23 19:40:35 +0530347
348 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530349 * Merges the temp java files to main java files.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530350 *
351 * @param appendFile temp file
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +0530352 * @param srcFile main file
Bharat saraswale2d51d62016-03-23 19:40:35 +0530353 * @throws IOException when fails to append contents
354 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530355 public static void mergeJavaFiles(File appendFile, File srcFile)
356 throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530357 try {
358 appendFileContents(appendFile, srcFile);
359 } catch (IOException e) {
360 throw new IOException("Failed to append " + appendFile + " in " + srcFile);
361 }
362 }
363
364 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530365 * Inserts data in the generated file.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530366 *
367 * @param file file in which need to be inserted
368 * @param data data which need to be inserted
369 * @throws IOException when fails to insert into file
370 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530371 public static void insertDataIntoJavaFile(File file, String data)
372 throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530373 try {
374 updateFileHandle(file, data, false);
375 } catch (IOException e) {
376 throw new IOException("Failed to insert in " + file + "file");
377 }
378 }
Bharat saraswal33dfa012016-05-17 19:59:16 +0530379
380 /**
381 * Validates a line size in given file whether it is having more then 120 characters.
382 * If yes it will update and give a new file.
383 *
384 * @param dataFile file in which need to verify all lines.
385 * @return updated file
386 * @throws IOException when fails to do IO operations.
387 */
VinodKumarS-Huawei2ee9e7e2016-06-01 14:30:22 +0530388 public static File validateLineLength(File dataFile)
389 throws IOException {
Bharat saraswal33dfa012016-05-17 19:59:16 +0530390 File tempFile = dataFile;
391 FileReader fileReader = new FileReader(dataFile);
392 BufferedReader bufferReader = new BufferedReader(fileReader);
393 try {
394 StringBuilder stringBuilder = new StringBuilder();
395 String line = bufferReader.readLine();
396
397 while (line != null) {
398 if (line.length() > LINE_SIZE) {
399 if (line.contains(PERIOD)) {
400 line = whenDelimiterIsPersent(line, LINE_SIZE);
401 } else if (line.contains(SPACE)) {
402 line = whenSpaceIsPresent(line, LINE_SIZE);
403 }
404 stringBuilder.append(line);
405 } else {
406 stringBuilder.append(line + NEW_LINE);
407 }
408 line = bufferReader.readLine();
409 }
410 FileWriter writer = new FileWriter(tempFile);
411 writer.write(stringBuilder.toString());
412 writer.close();
413 return tempFile;
414 } finally {
415 fileReader.close();
416 bufferReader.close();
417 }
418 }
419
Bharat saraswal96dfef02016-06-16 00:29:12 +0530420 /* When delimiters are present in the given line. */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530421 private static String whenDelimiterIsPersent(String line, int lineSize) {
422 StringBuilder stringBuilder = new StringBuilder();
423
424 if (line.length() > lineSize) {
425 String[] strArray = line.split(Pattern.quote(PERIOD));
426 stringBuilder = updateString(strArray, stringBuilder, PERIOD, lineSize);
427 } else {
428 stringBuilder.append(line + NEW_LINE);
429 }
430 String[] strArray = stringBuilder.toString().split(NEW_LINE);
431 StringBuilder tempBuilder = new StringBuilder();
432 for (String str : strArray) {
433 if (str.length() > SUB_LINE_SIZE) {
434 if (line.contains(PERIOD) && !line.contains(PERIOD + HASH + OPEN_PARENTHESIS)) {
435 String[] strArr = str.split(Pattern.quote(PERIOD));
436 tempBuilder = updateString(strArr, tempBuilder, PERIOD, SUB_LINE_SIZE);
437 } else if (str.contains(SPACE)) {
438 tempBuilder.append(whenSpaceIsPresent(str, SUB_LINE_SIZE));
439 }
440 } else {
441 tempBuilder.append(str + NEW_LINE);
442 }
443 }
444 return tempBuilder.toString();
445
446 }
447
Bharat saraswal96dfef02016-06-16 00:29:12 +0530448 /* When spaces are present in the given line. */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530449 private static String whenSpaceIsPresent(String line, int lineSize) {
450 StringBuilder stringBuilder = new StringBuilder();
451 if (line.length() > lineSize) {
452 String[] strArray = line.split(SPACE);
453 stringBuilder = updateString(strArray, stringBuilder, SPACE, lineSize);
454 } else {
455 stringBuilder.append(line + NEW_LINE);
456 }
Bharat saraswalcad0e652016-05-26 23:48:38 +0530457
458 String[] strArray = stringBuilder.toString().split(NEW_LINE);
459 StringBuilder tempBuilder = new StringBuilder();
460 for (String str : strArray) {
461 if (str.length() > SUB_LINE_SIZE) {
462 if (str.contains(SPACE)) {
463 String[] strArr = str.split(SPACE);
464 tempBuilder = updateString(strArr, tempBuilder, SPACE, SUB_LINE_SIZE);
465 }
466 } else {
467 tempBuilder.append(str + NEW_LINE);
468 }
469 }
470 return tempBuilder.toString();
Bharat saraswal33dfa012016-05-17 19:59:16 +0530471 }
472
Bharat saraswal96dfef02016-06-16 00:29:12 +0530473 /* Updates the given line with the given size conditions. */
Bharat saraswal33dfa012016-05-17 19:59:16 +0530474 private static StringBuilder updateString(String[] strArray, StringBuilder stringBuilder, String string,
475 int lineSize) {
476
477 StringBuilder tempBuilder = new StringBuilder();
478 for (String str : strArray) {
479 tempBuilder.append(str + string);
480 if (tempBuilder.length() > lineSize) {
481 String tempString = stringBuilder.toString();
482 stringBuilder.delete(ZERO, stringBuilder.length());
483 tempString = trimAtLast(tempString, string);
484 stringBuilder.append(tempString);
485 if (string.equals(PERIOD)) {
486 stringBuilder.append(NEW_LINE + TWELVE_SPACE_INDENTATION + PERIOD + str + string);
487 } else {
488 stringBuilder.append(NEW_LINE + TWELVE_SPACE_INDENTATION + str + string);
489 }
490 tempBuilder.delete(ZERO, tempBuilder.length());
491 tempBuilder.append(TWELVE_SPACE_INDENTATION);
492 } else {
493 stringBuilder.append(str + string);
494 }
495 }
496 String tempString = stringBuilder.toString();
497 tempString = trimAtLast(tempString, string);
498 stringBuilder.delete(ZERO, stringBuilder.length());
499 stringBuilder.append(tempString + NEW_LINE);
500 return stringBuilder;
501 }
Bharat saraswal96dfef02016-06-16 00:29:12 +0530502
503 /**
504 * Serializes data-model.
505 *
506 * @param directory base directory for serialized files
507 * @param fileInfoSet YANG file info set
508 * @param project maven project
509 * @param operation true if need to add to resource
510 * @throws IOException when fails to do IO operations
511 */
512 public static void serializeDataModel(String directory, Set<YangFileInfo> fileInfoSet,
513 MavenProject project, boolean operation) throws IOException {
514
515 String serFileDirPath = directory + TARGET_RESOURCE_PATH;
516 File dir = new File(serFileDirPath);
517 dir.mkdirs();
518
519 if (operation) {
520 addToProjectResource(directory + SLASH + TEMP + SLASH, project);
521 }
522
523 for (YangFileInfo fileInfo : fileInfoSet) {
524
525 String serFileName = serFileDirPath + getCamelCase(fileInfo.getRootNode().getName(), null)
526 + SERIALIZED_FILE_EXTENSION;
527 fileInfo.setSerializedFile(serFileName);
528 FileOutputStream fileOutputStream = new FileOutputStream(serFileName);
529 ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
530 objectOutputStream.writeObject(fileInfo.getRootNode());
531 objectOutputStream.close();
532 fileOutputStream.close();
533 }
534 }
535
536 /* Adds directory to resources of project */
537 private static void addToProjectResource(String dir, MavenProject project) {
538 Resource rsc = new Resource();
539 rsc.setDirectory(dir);
540 project.addResource(rsc);
541 }
542
543 /**
544 * Returns de-serializes YANG data-model nodes.
545 *
546 * @param serailizedfileInfoSet YANG file info set
547 * @return de-serializes YANG data-model nodes
548 * @throws IOException when fails do IO operations
549 */
550 public static List<YangNode> deSerializeDataModel(List<String> serailizedfileInfoSet) throws IOException {
551
552 List<YangNode> nodes = new ArrayList<>();
553 for (String fileInfo : serailizedfileInfoSet) {
554 YangNode node = null;
555 try {
556 FileInputStream fileInputStream = new FileInputStream(fileInfo);
557 ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
558 node = (YangNode) objectInputStream.readObject();
559 nodes.add(node);
560 objectInputStream.close();
561 fileInputStream.close();
562 } catch (IOException | ClassNotFoundException e) {
563 throw new IOException(fileInfo + " not found.");
564 }
565 }
566 return nodes;
567 }
568
569 /**
570 * Resolves inter jar dependencies.
571 *
572 * @param project current maven project
573 * @param localRepository local maven repository
574 * @param remoteRepos list of remote repository
575 * @param directory directory for serialized files
576 * @return list of resolved datamodel nodes
577 * @throws IOException when fails to do IO operations
578 */
579 public static List<YangNode> resolveInterJarDependencies(MavenProject project, ArtifactRepository localRepository,
580 List<ArtifactRepository> remoteRepos, String directory) throws IOException {
581
582 List<String> dependeciesJarPaths = resolveDependecyJarPath(project, localRepository, remoteRepos);
583 List<YangNode> resolvedDataModelNodes = new ArrayList<>();
584 for (String dependecy : dependeciesJarPaths) {
585 resolvedDataModelNodes.addAll(deSerializeDataModel(parseJarFile(dependecy, directory)));
586 }
587 return resolvedDataModelNodes;
588 }
589
590 /**
591 * Returns list of jar path.
592 *
593 * @return list of jar paths
594 */
595 private static List<String> resolveDependecyJarPath(MavenProject project, ArtifactRepository localRepository,
596 List<ArtifactRepository> remoteRepos) {
597
598 StringBuilder path = new StringBuilder();
599 List<String> jarPaths = new ArrayList<>();
600 for (Dependency dependency : project.getDependencies()) {
601
602 path.append(localRepository.getBasedir());
603 path.append(SLASH);
604 path.append(getPackageDirPathFromJavaJPackage(dependency.getGroupId()));
605 path.append(SLASH);
606 path.append(dependency.getArtifactId());
607 path.append(SLASH);
608 path.append(dependency.getVersion());
609 path.append(SLASH);
610 path.append(dependency.getArtifactId() + HYPHEN + dependency.getVersion() + PERIOD + JAR);
611 File jarFile = new File(path.toString());
612 if (jarFile.exists()) {
613 jarPaths.add(path.toString());
614 }
615 path.delete(0, path.length());
616 }
617
618 for (ArtifactRepository repo : remoteRepos) {
619 // TODO: add resolver for remote repo.
620 }
621 return jarPaths;
622 }
623
624 /**
625 * Parses jar file and returns list of serialized file names.
626 *
627 * @param jarFile jar file to be parsed
628 * @param directory directory for keeping the searized files
629 * @return list of serialized files
630 * @throws IOException when fails to do IO operations
631 */
632 public static List<String> parseJarFile(String jarFile, String directory)
633 throws IOException {
634
635 List<String> serailizedFiles = new ArrayList<>();
636 JarFile jar = new JarFile(jarFile);
637 Enumeration<?> enumEntries = jar.entries();
638
639 File serializedFileDir = new File(directory);
640 serializedFileDir.mkdirs();
641 while (enumEntries.hasMoreElements()) {
642 JarEntry file = (JarEntry) enumEntries.nextElement();
643 if (file.getName().endsWith(SERIALIZED_FILE_EXTENSION)) {
644 if (file.getName().contains(SLASH)) {
645 String[] strArray = file.getName().split(SLASH);
646 String tempPath = "";
647 for (int i = 0; i < strArray.length - 1; i++) {
648 tempPath = SLASH + tempPath + SLASH + strArray[i];
649 }
650 File dir = new File(directory + tempPath);
651 dir.mkdirs();
652 }
653 File serailizedFile = new File(directory + SLASH + file.getName());
654 if (file.isDirectory()) {
655 serailizedFile.mkdirs();
656 continue;
657 }
658 InputStream inputStream = jar.getInputStream(file);
659
660 FileOutputStream fileOutputStream = new FileOutputStream(serailizedFile);
661 while (inputStream.available() > 0) {
662 fileOutputStream.write(inputStream.read());
663 }
664 fileOutputStream.close();
665 inputStream.close();
666 serailizedFiles.add(serailizedFile.toString());
667 }
668 }
669 jar.close();
670 return serailizedFiles;
671 }
672
Bharat saraswal870c56f2016-02-20 21:57:16 +0530673}