blob: 58dfb69c96cfb6fa3e27baa3b143403000d38762 [file] [log] [blame]
Bharat saraswal97459962016-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;
27import org.onosproject.yangutils.translator.GeneratedFileType;
28import org.onosproject.yangutils.translator.tojava.CachedJavaFileHandle;
29import org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax;
30import org.onosproject.yangutils.utils.UtilConstants;
31
32/**
33 * Utility to handle file system operations.
34 */
35public final class FileSystemUtil {
36 /**
37 * Hiding constructor of a utility class.
38 */
39 private FileSystemUtil() {
40 }
41
42 /**
43 * Check if the package directory structure created.
44 *
45 * @param pkg Package to check if it is created.
46 * @return existence status of package.
47 */
b.janani66b749c2016-02-24 12:23:03 +053048 public static boolean doesPackageExist(String pkg) {
49 File pkgDir = new File(pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH));
50 File pkgWithFile = new File(pkgDir + File.separator + "package-info.java");
51 if (pkgDir.exists() && pkgWithFile.isFile()) {
Bharat saraswal97459962016-02-20 21:57:16 +053052 return true;
53 }
54 return false;
55 }
56
b.janani66b749c2016-02-24 12:23:03 +053057
Bharat saraswal97459962016-02-20 21:57:16 +053058 /**
59 * Create a package structure with package info java file if not present.
60 *
61 * @param pkg java package string
62 * @param pkgInfo description of package
63 * @throws IOException any IO exception
64 */
65 public static void createPackage(String pkg, String pkgInfo) throws IOException {
b.janani66b749c2016-02-24 12:23:03 +053066 if (!doesPackageExist(pkg)) {
Bharat saraswal97459962016-02-20 21:57:16 +053067 try {
Vinod Kumar Sc26bf192016-02-23 22:36:57 +053068 File pack = YangIoUtils
69 .createDirectories(pkg.replace(UtilConstants.PERIOD, UtilConstants.SLASH));
Bharat saraswal97459962016-02-20 21:57:16 +053070 YangIoUtils.addPackageInfo(pack, pkgInfo, pkg);
71 } catch (IOException e) {
72 throw new IOException("failed to create package-info file");
73 }
74 }
75 }
76
77 /**
78 * Create a java source file in the specified package.
79 *
80 * @param pkg java package under which the interface file needs to be created.
81 * @param yangName YANG name of the node for which java file needs to be 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 methods.
85 */
86 public static CachedFileHandle createSourceFiles(String pkg, String yangName, GeneratedFileType types)
87 throws IOException {
Bharat saraswal97459962016-02-20 21:57:16 +053088 yangName = JavaIdentifierSyntax.getCamelCase(yangName);
89 CachedFileHandle handler = new CachedJavaFileHandle(pkg, yangName, types);
90
91 return handler;
92 }
93
94 /**
95 * Read the contents from source file and append its contents to append
96 * file.
97 *
98 * @param toAppend destination file in which the contents of source file is
99 * appended.
100 * @param srcFile source file from which data is read and added to to append
101 * file.
102 * @throws IOException any IO errors.
103 */
104 public static void appendFileContents(File toAppend, File srcFile) throws IOException {
105
106 insertStringInFile(srcFile, UtilConstants.NEW_LINE + readAppendFile(toAppend.toString()));
Bharat saraswal97459962016-02-20 21:57:16 +0530107 return;
108 }
109
110 /**
111 * Reads file and convert it to string.
112 *
113 * @param toAppend file to be converted.
114 * @return string of file.
115 * @throws IOException when fails to convert to string
116 */
117 private static String readAppendFile(String toAppend) throws IOException {
118 BufferedReader bufferReader = new BufferedReader(new FileReader(toAppend));
119 try {
120 StringBuilder stringBuilder = new StringBuilder();
121 String line = bufferReader.readLine();
122
123 while (line != null) {
124 stringBuilder.append(UtilConstants.FOUR_SPACE_INDENTATION + line);
125 stringBuilder.append("\n");
126 line = bufferReader.readLine();
127 }
128 return stringBuilder.toString();
129 } finally {
130 bufferReader.close();
131 }
132 }
133
134 /**
135 * Insert content to the generated file.
136 *
137 * @param inputFile input file
138 * @param contentTobeAdded content to be appended to the file.
139 * @throws IOException when fails to append content to the file.
140 */
141 public static void insertStringInFile(File inputFile, String contentTobeAdded) throws IOException {
142 FileWriter fileWriter = new FileWriter(inputFile, true);
143 PrintWriter outputPrintWriter = new PrintWriter(fileWriter);
144 outputPrintWriter.write(contentTobeAdded);
145 outputPrintWriter.flush();
146 outputPrintWriter.close();
147
148 }
149}