blob: 27f1036fa0351cdbc207e323a6115bd5ac4e4350 [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
Bharat saraswaldedf9f82016-02-12 20:48:30 +053027import org.slf4j.Logger;
28
Vinod Kumar S08710982016-03-03 19:55:30 +053029import static org.slf4j.LoggerFactory.getLogger;
30
Bharat saraswaldedf9f82016-02-12 20:48:30 +053031/**
32 * Provides the license header for the generated files.
33 */
34public final class CopyrightHeader {
35
36 private static final Logger log = getLogger(CopyrightHeader.class);
Bharat saraswal97459962016-02-20 21:57:16 +053037 private static final int EOF = -1;
Bharat saraswaldedf9f82016-02-12 20:48:30 +053038 private static ClassLoader classLoader = CopyrightHeader.class.getClassLoader();
39
Bharat saraswal97459962016-02-20 21:57:16 +053040 private static String copyrightHeader;
41
Bharat saraswaldedf9f82016-02-12 20:48:30 +053042 /**
43 * Default constructor.
44 */
45 private CopyrightHeader() {
46 }
47
48 /**
Bharat saraswal97459962016-02-20 21:57:16 +053049 * Returns copyright file header.
Bharat saraswaldedf9f82016-02-12 20:48:30 +053050 *
Bharat saraswal97459962016-02-20 21:57:16 +053051 * @return copyright file header
Vinod Kumar S08710982016-03-03 19:55:30 +053052 * @throws IOException when fails to parse copyright header
Bharat saraswaldedf9f82016-02-12 20:48:30 +053053 */
Bharat saraswal97459962016-02-20 21:57:16 +053054 public static String getCopyrightHeader() throws IOException {
55 return copyrightHeader;
56 }
57
58 /**
59 * Sets the copyright header.
60 *
61 * @param header copyright header
62 */
63 private static void setCopyrightHeader(String header) {
64 copyrightHeader = header;
Bharat saraswaldedf9f82016-02-12 20:48:30 +053065 }
66
67 /**
68 * parse Copyright to the temporary file.
69 *
Vinod Kumar S08710982016-03-03 19:55:30 +053070 * @throws IOException when fails to get the copyright header
Bharat saraswaldedf9f82016-02-12 20:48:30 +053071 */
Bharat saraswal97459962016-02-20 21:57:16 +053072 public static void parseCopyrightHeader() throws IOException {
Bharat saraswaldedf9f82016-02-12 20:48:30 +053073
74 File temp = new File("temp.txt");
75
76 try {
77 InputStream stream = classLoader.getResourceAsStream("CopyrightHeader.txt");
78 OutputStream out = new FileOutputStream(temp);
79 int index;
Bharat saraswal97459962016-02-20 21:57:16 +053080 while ((index = stream.read()) != EOF) {
Bharat saraswaldedf9f82016-02-12 20:48:30 +053081 out.write(index);
82 }
83 out.close();
Bharat saraswal97459962016-02-20 21:57:16 +053084 stream.close();
85 getStringFileContent(temp);
86 setCopyrightHeader(getStringFileContent(temp));
Bharat saraswaldedf9f82016-02-12 20:48:30 +053087 } catch (IOException e) {
Bharat saraswal97459962016-02-20 21:57:16 +053088 throw new IOException("failed to parse the Copyright header");
Bharat saraswaldedf9f82016-02-12 20:48:30 +053089 } finally {
90 temp.delete();
91 }
Bharat saraswaldedf9f82016-02-12 20:48:30 +053092 }
93
94 /**
95 * Converts it to string.
96 *
97 * @param toAppend file to be converted.
98 * @return string of file.
99 * @throws IOException when fails to convert to string
100 */
Bharat saraswal97459962016-02-20 21:57:16 +0530101 private static String getStringFileContent(File toAppend) throws IOException {
102
Bharat saraswaldedf9f82016-02-12 20:48:30 +0530103 BufferedReader bufferReader = new BufferedReader(new FileReader(toAppend));
104 try {
105 StringBuilder stringBuilder = new StringBuilder();
106 String line = bufferReader.readLine();
107
108 while (line != null) {
109 stringBuilder.append(line);
110 stringBuilder.append("\n");
111 line = bufferReader.readLine();
112 }
113 return stringBuilder.toString();
114 } finally {
115 bufferReader.close();
116 }
117 }
118}