blob: 2b5c72cdf7c8442747e5b3dac21f3508e7c6c944 [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 +053027/**
28 * Provides the license header for the generated files.
29 */
30public final class CopyrightHeader {
31
Bharat saraswal97459962016-02-20 21:57:16 +053032 private static final int EOF = -1;
Bharat saraswaldedf9f82016-02-12 20:48:30 +053033 private static ClassLoader classLoader = CopyrightHeader.class.getClassLoader();
34
Bharat saraswal97459962016-02-20 21:57:16 +053035 private static String copyrightHeader;
36
Bharat saraswaldedf9f82016-02-12 20:48:30 +053037 /**
38 * Default constructor.
39 */
40 private CopyrightHeader() {
41 }
42
43 /**
Bharat saraswal97459962016-02-20 21:57:16 +053044 * Returns copyright file header.
Bharat saraswaldedf9f82016-02-12 20:48:30 +053045 *
Bharat saraswal97459962016-02-20 21:57:16 +053046 * @return copyright file header
Vinod Kumar S08710982016-03-03 19:55:30 +053047 * @throws IOException when fails to parse copyright header
Bharat saraswaldedf9f82016-02-12 20:48:30 +053048 */
Bharat saraswal97459962016-02-20 21:57:16 +053049 public static String getCopyrightHeader() throws IOException {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053050 if (copyrightHeader == null) {
51 parseCopyrightHeader();
52 }
Bharat saraswal97459962016-02-20 21:57:16 +053053 return copyrightHeader;
54 }
55
56 /**
57 * Sets the copyright header.
58 *
59 * @param header copyright header
60 */
61 private static void setCopyrightHeader(String header) {
62 copyrightHeader = header;
Bharat saraswaldedf9f82016-02-12 20:48:30 +053063 }
64
65 /**
66 * parse Copyright to the temporary file.
67 *
Vinod Kumar S08710982016-03-03 19:55:30 +053068 * @throws IOException when fails to get the copyright header
Bharat saraswaldedf9f82016-02-12 20:48:30 +053069 */
Bharat saraswal97459962016-02-20 21:57:16 +053070 public static void parseCopyrightHeader() throws IOException {
Bharat saraswaldedf9f82016-02-12 20:48:30 +053071
72 File temp = new File("temp.txt");
73
74 try {
75 InputStream stream = classLoader.getResourceAsStream("CopyrightHeader.txt");
76 OutputStream out = new FileOutputStream(temp);
77 int index;
Bharat saraswal97459962016-02-20 21:57:16 +053078 while ((index = stream.read()) != EOF) {
Bharat saraswaldedf9f82016-02-12 20:48:30 +053079 out.write(index);
80 }
81 out.close();
Bharat saraswal97459962016-02-20 21:57:16 +053082 stream.close();
83 getStringFileContent(temp);
84 setCopyrightHeader(getStringFileContent(temp));
Bharat saraswaldedf9f82016-02-12 20:48:30 +053085 } catch (IOException e) {
Bharat saraswal97459962016-02-20 21:57:16 +053086 throw new IOException("failed to parse the Copyright header");
Bharat saraswaldedf9f82016-02-12 20:48:30 +053087 } finally {
88 temp.delete();
89 }
Bharat saraswaldedf9f82016-02-12 20:48:30 +053090 }
91
92 /**
93 * Converts it to string.
94 *
95 * @param toAppend file to be converted.
96 * @return string of file.
97 * @throws IOException when fails to convert to string
98 */
Bharat saraswal97459962016-02-20 21:57:16 +053099 private static String getStringFileContent(File toAppend) throws IOException {
100
Bharat saraswaldedf9f82016-02-12 20:48:30 +0530101 BufferedReader bufferReader = new BufferedReader(new FileReader(toAppend));
102 try {
103 StringBuilder stringBuilder = new StringBuilder();
104 String line = bufferReader.readLine();
105
106 while (line != null) {
107 stringBuilder.append(line);
108 stringBuilder.append("\n");
109 line = bufferReader.readLine();
110 }
111 return stringBuilder.toString();
112 } finally {
113 bufferReader.close();
114 }
115 }
116}