blob: 271d6984d5b438f62165aef046f0663631785f9c [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.BufferedReader;
20import java.io.File;
21import java.io.FileReader;
22import java.io.FileWriter;
23import java.io.IOException;
24import java.io.PrintWriter;
25
26import org.onosproject.yangutils.translator.CachedFileHandle;
Bharat saraswal870c56f2016-02-20 21:57:16 +053027import org.onosproject.yangutils.translator.tojava.CachedJavaFileHandle;
28import org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax;
29import org.onosproject.yangutils.utils.UtilConstants;
30
31/**
32 * Utility to handle file system operations.
33 */
34public final class FileSystemUtil {
35 /**
36 * Hiding constructor of a utility class.
37 */
38 private FileSystemUtil() {
39 }
40
41 /**
42 * Check if the package directory structure created.
43 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +053044 * @param pkg Package to check if it is created
45 * @return existence status of package
Bharat saraswal870c56f2016-02-20 21:57:16 +053046 */
b.janani68c55e12016-02-24 12:23:03 +053047 public static boolean doesPackageExist(String pkg) {
48 File pkgDir = new File(pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH));
49 File pkgWithFile = new File(pkgDir + File.separator + "package-info.java");
50 if (pkgDir.exists() && pkgWithFile.isFile()) {
Bharat saraswal870c56f2016-02-20 21:57:16 +053051 return true;
52 }
53 return false;
54 }
55
56 /**
57 * Create a package structure with package info java file if not present.
58 *
59 * @param pkg java package string
60 * @param pkgInfo description of package
61 * @throws IOException any IO exception
62 */
63 public static void createPackage(String pkg, String pkgInfo) throws IOException {
b.janani68c55e12016-02-24 12:23:03 +053064 if (!doesPackageExist(pkg)) {
Bharat saraswal870c56f2016-02-20 21:57:16 +053065 try {
Vinod Kumar S0c330cd2016-02-23 22:36:57 +053066 File pack = YangIoUtils
67 .createDirectories(pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH));
Bharat saraswal870c56f2016-02-20 21:57:16 +053068 YangIoUtils.addPackageInfo(pack, pkgInfo, pkg);
69 } catch (IOException e) {
70 throw new IOException("failed to create package-info file");
71 }
72 }
73 }
74
75 /**
76 * Create a java source file in the specified package.
77 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +053078 * @param pkg java package under which the interface file needs to be
79 * created
80 * @param yangName YANG name of the node for which java file needs to be
81 * created
82 * @param types types of files to be created
83 * @throws IOException when fails to create interface file
84 * @return the cached java file handle, which can be used to further add
85 * methods
Bharat saraswal870c56f2016-02-20 21:57:16 +053086 */
Vinod Kumar Sc4216002016-03-03 19:55:30 +053087 public static CachedFileHandle createSourceFiles(String pkg, String yangName, int types)
Bharat saraswal870c56f2016-02-20 21:57:16 +053088 throws IOException {
Bharat saraswal870c56f2016-02-20 21:57:16 +053089 yangName = JavaIdentifierSyntax.getCamelCase(yangName);
90 CachedFileHandle handler = new CachedJavaFileHandle(pkg, yangName, types);
91
92 return handler;
93 }
94
95 /**
96 * Read the contents from source file and append its contents to append
97 * file.
98 *
99 * @param toAppend destination file in which the contents of source file is
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530100 * appended
Bharat saraswal870c56f2016-02-20 21:57:16 +0530101 * @param srcFile source file from which data is read and added to to append
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530102 * file
103 * @throws IOException any IO errors
Bharat saraswal870c56f2016-02-20 21:57:16 +0530104 */
105 public static void appendFileContents(File toAppend, File srcFile) throws IOException {
106
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530107 updateFileHandle(srcFile, UtilConstants.NEW_LINE + readAppendFile(toAppend.toString()), false);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530108 return;
109 }
110
111 /**
112 * Reads file and convert it to string.
113 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530114 * @param toAppend file to be converted
115 * @return string of file
Bharat saraswal870c56f2016-02-20 21:57:16 +0530116 * @throws IOException when fails to convert to string
117 */
118 private static String readAppendFile(String toAppend) throws IOException {
119 BufferedReader bufferReader = new BufferedReader(new FileReader(toAppend));
120 try {
121 StringBuilder stringBuilder = new StringBuilder();
122 String line = bufferReader.readLine();
123
124 while (line != null) {
125 stringBuilder.append(UtilConstants.FOUR_SPACE_INDENTATION + line);
126 stringBuilder.append("\n");
127 line = bufferReader.readLine();
128 }
129 return stringBuilder.toString();
130 } finally {
131 bufferReader.close();
132 }
133 }
134
135 /**
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530136 * Update the generated file handle.
Bharat saraswal870c56f2016-02-20 21:57:16 +0530137 *
138 * @param inputFile input file
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530139 * @param contentTobeAdded content to be appended to the file
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530140 * @param isClose when close of file is called.
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530141 * @throws IOException when fails to append content to the file
Bharat saraswal870c56f2016-02-20 21:57:16 +0530142 */
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530143 public static void updateFileHandle(File inputFile, String contentTobeAdded, boolean isClose) throws IOException {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530144 FileWriter fileWriter = new FileWriter(inputFile, true);
145 PrintWriter outputPrintWriter = new PrintWriter(fileWriter);
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530146 if (!isClose) {
147 outputPrintWriter.write(contentTobeAdded);
148 outputPrintWriter.flush();
149 outputPrintWriter.close();
150 } else {
151 fileWriter.flush();
152 fileWriter.close();
153 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530154 }
155}