blob: 8b55768389609c3c57ba42e17f35498f319f974c [file] [log] [blame]
Bharat saraswaldedf9f82016-02-12 20:48:30 +05301/*
Brian O'Connor0f7908b2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Bharat saraswaldedf9f82016-02-12 20:48:30 +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.FileOutputStream;
22import java.io.FileReader;
23import java.io.IOException;
24import java.io.InputStream;
25import java.io.OutputStream;
Bharat saraswal68fa0d12016-04-19 01:00:16 +053026import java.util.Calendar;
Bharat saraswaldedf9f82016-02-12 20:48:30 +053027
Bharat saraswal84366c52016-03-23 19:40:35 +053028import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
29
Bharat saraswaldedf9f82016-02-12 20:48:30 +053030/**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053031 * Represents the license header for the generated files.
Bharat saraswaldedf9f82016-02-12 20:48:30 +053032 */
33public final class CopyrightHeader {
34
Bharat saraswal97459962016-02-20 21:57:16 +053035 private static final int EOF = -1;
Bharat saraswal84366c52016-03-23 19:40:35 +053036 private static final String COPYRIGHT_HEADER_FILE = "CopyrightHeader.txt";
Bharat saraswal68fa0d12016-04-19 01:00:16 +053037 private static final String COPYRIGHTS_FIRST_LINE = "/*\n * Copyright " + Calendar.getInstance().get(Calendar.YEAR)
38 + "-present Open Networking Laboratory\n";
Bharat saraswal84366c52016-03-23 19:40:35 +053039 private static final String TEMP_FILE = "temp.txt";
Bharat saraswal97459962016-02-20 21:57:16 +053040
Bharat saraswaldedf9f82016-02-12 20:48:30 +053041 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053042 * Creates an instance of copyright header.
Bharat saraswaldedf9f82016-02-12 20:48:30 +053043 */
44 private CopyrightHeader() {
45 }
46
47 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053048 * parses Copyright to the temporary file.
Bharat saraswaldedf9f82016-02-12 20:48:30 +053049 *
Vidyashree Rama17992c12016-11-30 11:49:27 +053050 * @return string of file.
Vinod Kumar S08710982016-03-03 19:55:30 +053051 * @throws IOException when fails to get the copyright header
Bharat saraswaldedf9f82016-02-12 20:48:30 +053052 */
Vidyashree Rama17992c12016-11-30 11:49:27 +053053 public static String parseCopyrightHeader() throws IOException {
Bharat saraswaldedf9f82016-02-12 20:48:30 +053054
Bharat saraswal84366c52016-03-23 19:40:35 +053055 File temp = new File(TEMP_FILE);
Bharat saraswaldedf9f82016-02-12 20:48:30 +053056
57 try {
Vidyashree Rama17992c12016-11-30 11:49:27 +053058 InputStream stream = CopyrightHeader.class.getClassLoader()
59 .getResourceAsStream(COPYRIGHT_HEADER_FILE);
Bharat saraswaldedf9f82016-02-12 20:48:30 +053060 OutputStream out = new FileOutputStream(temp);
Bharat saraswal84366c52016-03-23 19:40:35 +053061
Bharat saraswaldedf9f82016-02-12 20:48:30 +053062 int index;
Bharat saraswal68fa0d12016-04-19 01:00:16 +053063 out.write(COPYRIGHTS_FIRST_LINE.getBytes());
Bharat saraswal97459962016-02-20 21:57:16 +053064 while ((index = stream.read()) != EOF) {
Bharat saraswaldedf9f82016-02-12 20:48:30 +053065 out.write(index);
66 }
67 out.close();
Bharat saraswal97459962016-02-20 21:57:16 +053068 stream.close();
Vidyashree Rama17992c12016-11-30 11:49:27 +053069 return getStringFileContent(temp);
Bharat saraswaldedf9f82016-02-12 20:48:30 +053070 } catch (IOException e) {
Bharat saraswal97459962016-02-20 21:57:16 +053071 throw new IOException("failed to parse the Copyright header");
Bharat saraswaldedf9f82016-02-12 20:48:30 +053072 } finally {
73 temp.delete();
74 }
Bharat saraswaldedf9f82016-02-12 20:48:30 +053075 }
76
77 /**
78 * Converts it to string.
79 *
80 * @param toAppend file to be converted.
81 * @return string of file.
82 * @throws IOException when fails to convert to string
83 */
Bharat saraswal97459962016-02-20 21:57:16 +053084 private static String getStringFileContent(File toAppend) throws IOException {
85
Bharat saraswal84366c52016-03-23 19:40:35 +053086 FileReader fileReader = new FileReader(toAppend);
87 BufferedReader bufferReader = new BufferedReader(fileReader);
Bharat saraswaldedf9f82016-02-12 20:48:30 +053088 try {
89 StringBuilder stringBuilder = new StringBuilder();
90 String line = bufferReader.readLine();
91
92 while (line != null) {
93 stringBuilder.append(line);
Bharat saraswal84366c52016-03-23 19:40:35 +053094 stringBuilder.append(NEW_LINE);
Bharat saraswaldedf9f82016-02-12 20:48:30 +053095 line = bufferReader.readLine();
96 }
97 return stringBuilder.toString();
98 } finally {
Bharat saraswal84366c52016-03-23 19:40:35 +053099 fileReader.close();
Bharat saraswaldedf9f82016-02-12 20:48:30 +0530100 bufferReader.close();
101 }
102 }
103}