blob: 3ba2fc6ec153027d13d06185e5d0d065863020e8 [file] [log] [blame]
Bharat saraswaldedf9f82016-02-12 20:48:30 +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.FileOutputStream;
22import java.io.FileReader;
23import java.io.IOException;
24import java.io.InputStream;
25import java.io.OutputStream;
26
27import static org.slf4j.LoggerFactory.getLogger;
28import org.slf4j.Logger;
29
30/**
31 * Provides the license header for the generated files.
32 */
33public final class CopyrightHeader {
34
35 private static final Logger log = getLogger(CopyrightHeader.class);
Bharat saraswal97459962016-02-20 21:57:16 +053036 private static final int EOF = -1;
Bharat saraswaldedf9f82016-02-12 20:48:30 +053037 private static ClassLoader classLoader = CopyrightHeader.class.getClassLoader();
38
Bharat saraswal97459962016-02-20 21:57:16 +053039 private static String copyrightHeader;
40
Bharat saraswaldedf9f82016-02-12 20:48:30 +053041 /**
42 * Default constructor.
43 */
44 private CopyrightHeader() {
45 }
46
47 /**
Bharat saraswal97459962016-02-20 21:57:16 +053048 * Returns copyright file header.
Bharat saraswaldedf9f82016-02-12 20:48:30 +053049 *
Bharat saraswal97459962016-02-20 21:57:16 +053050 * @return copyright file header
51 * @throws IOException when fails to parse copyright header.
Bharat saraswaldedf9f82016-02-12 20:48:30 +053052 */
Bharat saraswal97459962016-02-20 21:57:16 +053053 public static String getCopyrightHeader() throws IOException {
54 return copyrightHeader;
55 }
56
57 /**
58 * Sets the copyright header.
59 *
60 * @param header copyright header
61 */
62 private static void setCopyrightHeader(String header) {
63 copyrightHeader = header;
Bharat saraswaldedf9f82016-02-12 20:48:30 +053064 }
65
66 /**
67 * parse Copyright to the temporary file.
68 *
Bharat saraswal97459962016-02-20 21:57:16 +053069 * @throws IOException when fails to get the copyright header.
Bharat saraswaldedf9f82016-02-12 20:48:30 +053070 */
Bharat saraswal97459962016-02-20 21:57:16 +053071 public static void parseCopyrightHeader() throws IOException {
Bharat saraswaldedf9f82016-02-12 20:48:30 +053072
73 File temp = new File("temp.txt");
74
75 try {
76 InputStream stream = classLoader.getResourceAsStream("CopyrightHeader.txt");
77 OutputStream out = new FileOutputStream(temp);
78 int index;
Bharat saraswal97459962016-02-20 21:57:16 +053079 while ((index = stream.read()) != EOF) {
Bharat saraswaldedf9f82016-02-12 20:48:30 +053080 out.write(index);
81 }
82 out.close();
Bharat saraswal97459962016-02-20 21:57:16 +053083 stream.close();
84 getStringFileContent(temp);
85 setCopyrightHeader(getStringFileContent(temp));
Bharat saraswaldedf9f82016-02-12 20:48:30 +053086 } catch (IOException e) {
Bharat saraswal97459962016-02-20 21:57:16 +053087 throw new IOException("failed to parse the Copyright header");
Bharat saraswaldedf9f82016-02-12 20:48:30 +053088 } finally {
89 temp.delete();
90 }
Bharat saraswaldedf9f82016-02-12 20:48:30 +053091 }
92
93 /**
94 * Converts it to string.
95 *
96 * @param toAppend file to be converted.
97 * @return string of file.
98 * @throws IOException when fails to convert to string
99 */
Bharat saraswal97459962016-02-20 21:57:16 +0530100 private static String getStringFileContent(File toAppend) throws IOException {
101
Bharat saraswaldedf9f82016-02-12 20:48:30 +0530102 BufferedReader bufferReader = new BufferedReader(new FileReader(toAppend));
103 try {
104 StringBuilder stringBuilder = new StringBuilder();
105 String line = bufferReader.readLine();
106
107 while (line != null) {
108 stringBuilder.append(line);
109 stringBuilder.append("\n");
110 line = bufferReader.readLine();
111 }
112 return stringBuilder.toString();
113 } finally {
114 bufferReader.close();
115 }
116 }
117}