blob: 7d8fd84845442065928d148cf7ef6ebc69030009 [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.BufferedReader;
20import java.io.File;
21import java.io.FileReader;
22import java.io.FileWriter;
23import java.io.IOException;
24import java.io.PrintWriter;
25
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053026import org.onosproject.yangutils.datamodel.YangNode;
Bharat saraswalc0e04842016-05-12 13:16:57 +053027import org.onosproject.yangutils.translator.exception.TranslatorException;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053028import org.onosproject.yangutils.translator.tojava.JavaFileInfo;
Bharat saraswalc0e04842016-05-12 13:16:57 +053029import org.onosproject.yangutils.translator.tojava.JavaFileInfoContainer;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053030
Bharat saraswal2f11f652016-03-25 18:19:46 +053031import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getJavaPackageFromPackagePath;
32import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getPackageDirPathFromJavaJPackage;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053033import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getParentNodeInGenCode;
Bharat saraswale2d51d62016-03-23 19:40:35 +053034import static org.onosproject.yangutils.utils.UtilConstants.EIGHT_SPACE_INDENTATION;
35import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
36import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
37import static org.onosproject.yangutils.utils.UtilConstants.MULTIPLE_NEW_LINE;
38import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
39import static org.onosproject.yangutils.utils.UtilConstants.SLASH;
40import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
41import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.addPackageInfo;
Bharat saraswale2d51d62016-03-23 19:40:35 +053042import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.createDirectories;
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053043import static org.onosproject.yangutils.utils.io.impl.YangIoUtils.getAbsolutePackagePath;
Bharat saraswal870c56f2016-02-20 21:57:16 +053044
45/**
Bharat saraswald9822e92016-04-05 15:13:44 +053046 * Represents utility to handle file system operations.
Bharat saraswal870c56f2016-02-20 21:57:16 +053047 */
48public final class FileSystemUtil {
Vinod Kumar S38046502016-03-23 15:30:27 +053049
Bharat saraswal870c56f2016-02-20 21:57:16 +053050 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053051 * Creates an instance of file system util.
Bharat saraswal870c56f2016-02-20 21:57:16 +053052 */
53 private FileSystemUtil() {
54 }
55
56 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053057 * Checks if the package directory structure created.
Bharat saraswal870c56f2016-02-20 21:57:16 +053058 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +053059 * @param pkg Package to check if it is created
60 * @return existence status of package
Bharat saraswal870c56f2016-02-20 21:57:16 +053061 */
b.janani68c55e12016-02-24 12:23:03 +053062 public static boolean doesPackageExist(String pkg) {
Bharat saraswal2f11f652016-03-25 18:19:46 +053063 File pkgDir = new File(getPackageDirPathFromJavaJPackage(pkg));
Bharat saraswale2d51d62016-03-23 19:40:35 +053064 File pkgWithFile = new File(pkgDir + SLASH + "package-info.java");
Bharat saraswalc0e04842016-05-12 13:16:57 +053065 return pkgDir.exists() && pkgWithFile.isFile();
Bharat saraswal870c56f2016-02-20 21:57:16 +053066 }
67
68 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053069 * Creates a package structure with package info java file if not present.
Bharat saraswal870c56f2016-02-20 21:57:16 +053070 *
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053071 * @param yangNode YANG node for which code is being generated
Bharat saraswal870c56f2016-02-20 21:57:16 +053072 * @throws IOException any IO exception
73 */
Bharat saraswalc0e04842016-05-12 13:16:57 +053074 public static void createPackage(YangNode yangNode) throws IOException {
75 if (!(yangNode instanceof JavaFileInfoContainer)) {
76 throw new TranslatorException("current node must have java file info");
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +053077 }
Bharat saraswalc0e04842016-05-12 13:16:57 +053078 String pkgInfo;
79 JavaFileInfo javaFileInfo = ((JavaFileInfoContainer) yangNode).getJavaFileInfo();
80 String pkg = getAbsolutePackagePath(javaFileInfo.getBaseCodeGenPath(), javaFileInfo.getPackageFilePath());
81 if (!doesPackageExist(pkg)) {
Bharat saraswal870c56f2016-02-20 21:57:16 +053082 try {
Bharat saraswalc0e04842016-05-12 13:16:57 +053083 File pack = createDirectories(pkg);
84 YangNode parent = getParentNodeInGenCode(yangNode);
85 if (parent != null) {
86 pkgInfo = ((JavaFileInfoContainer) parent).getJavaFileInfo().getJavaName();
87 addPackageInfo(pack, pkgInfo, getJavaPackageFromPackagePath(pkg), true);
88 } else {
89 pkgInfo = ((JavaFileInfoContainer) yangNode).getJavaFileInfo().getJavaName();
90 addPackageInfo(pack, pkgInfo, getJavaPackageFromPackagePath(pkg), false);
91 }
Bharat saraswal870c56f2016-02-20 21:57:16 +053092 } catch (IOException e) {
93 throw new IOException("failed to create package-info file");
94 }
95 }
96 }
97
98 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053099 * Reads the contents from source file and append its contents to append
Bharat saraswal870c56f2016-02-20 21:57:16 +0530100 * file.
101 *
102 * @param toAppend destination file in which the contents of source file is
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530103 * appended
Bharat saraswal870c56f2016-02-20 21:57:16 +0530104 * @param srcFile source file from which data is read and added to to append
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530105 * file
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530106 * @throws IOException any IO errors
Bharat saraswal870c56f2016-02-20 21:57:16 +0530107 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530108 public static void appendFileContents(File toAppend, File srcFile)
109 throws IOException {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530110 updateFileHandle(srcFile, NEW_LINE + readAppendFile(toAppend.toString(), FOUR_SPACE_INDENTATION), false);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530111 }
112
113 /**
114 * Reads file and convert it to string.
115 *
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530116 * @param toAppend file to be converted
Vinod Kumar S38046502016-03-23 15:30:27 +0530117 * @param spaces spaces to be appended
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530118 * @return string of file
Bharat saraswal870c56f2016-02-20 21:57:16 +0530119 * @throws IOException when fails to convert to string
120 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530121 public static String readAppendFile(String toAppend, String spaces)
122 throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +0530123 FileReader fileReader = new FileReader(toAppend);
124 BufferedReader bufferReader = new BufferedReader(fileReader);
Bharat saraswal870c56f2016-02-20 21:57:16 +0530125 try {
126 StringBuilder stringBuilder = new StringBuilder();
127 String line = bufferReader.readLine();
128
129 while (line != null) {
Vidyashree Rama74453712016-04-18 12:29:39 +0530130 if (line.equals(SPACE) || line.equals(EMPTY_STRING) || line.equals(EIGHT_SPACE_INDENTATION)
131 || line.equals(MULTIPLE_NEW_LINE)) {
Bharat saraswale2d51d62016-03-23 19:40:35 +0530132 stringBuilder.append(NEW_LINE);
133 } else if (line.equals(FOUR_SPACE_INDENTATION)) {
134 stringBuilder.append(EMPTY_STRING);
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530135 } else {
Vinod Kumar S38046502016-03-23 15:30:27 +0530136 stringBuilder.append(spaces + line);
Bharat saraswale2d51d62016-03-23 19:40:35 +0530137 stringBuilder.append(NEW_LINE);
Bharat saraswal8f2a6c52016-03-09 18:34:56 +0530138 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530139 line = bufferReader.readLine();
140 }
141 return stringBuilder.toString();
142 } finally {
Vinod Kumar S38046502016-03-23 15:30:27 +0530143 fileReader.close();
Bharat saraswal870c56f2016-02-20 21:57:16 +0530144 bufferReader.close();
145 }
146 }
147
148 /**
Bharat saraswald9822e92016-04-05 15:13:44 +0530149 * Updates the generated file handle.
Bharat saraswal870c56f2016-02-20 21:57:16 +0530150 *
151 * @param inputFile input file
Vinod Kumar Sc4216002016-03-03 19:55:30 +0530152 * @param contentTobeAdded content to be appended to the file
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530153 * @param isClose when close of file is called.
Bharat saraswale2d51d62016-03-23 19:40:35 +0530154 * @throws IOException if the named file exists but is a directory rather than a regular file,
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530155 * does not exist but cannot be created, or cannot be opened for any other reason
Bharat saraswal870c56f2016-02-20 21:57:16 +0530156 */
VinodKumarS-Huaweicb3a1f52016-05-10 17:58:57 +0530157 public static void updateFileHandle(File inputFile, String contentTobeAdded, boolean isClose)
158 throws IOException {
Bharat saraswal870c56f2016-02-20 21:57:16 +0530159 FileWriter fileWriter = new FileWriter(inputFile, true);
Vinod Kumar S38046502016-03-23 15:30:27 +0530160 PrintWriter outputPrintWriter = new PrintWriter(fileWriter, true);
Bharat saraswal2f00b4b2016-03-04 20:08:09 +0530161 if (!isClose) {
162 outputPrintWriter.write(contentTobeAdded);
163 outputPrintWriter.flush();
164 outputPrintWriter.close();
165 } else {
166 fileWriter.flush();
167 fileWriter.close();
168 }
Bharat saraswal870c56f2016-02-20 21:57:16 +0530169 }
170}