blob: 8b7e7325b0318a2bc7a631dc817ab7a485de06f1 [file] [log] [blame]
Bharat saraswal708abc02016-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);
36 private static final int NULL = -1;
37 private static ClassLoader classLoader = CopyrightHeader.class.getClassLoader();
38
39 /**
40 * Default constructor.
41 */
42 private CopyrightHeader() {
43 }
44
45 /**
46 * Returns Copyright file header.
47 *
48 * @return Copyright file header
49 */
50 public static String getCopyrightHeader() {
51 return parseOnosHeader();
52 }
53
54 /**
55 * parse Copyright to the temporary file.
56 *
57 * @param file generated file
58 * @param stream input stream
59 */
60 private static String parseOnosHeader() {
61
62 File temp = new File("temp.txt");
63
64 try {
65 InputStream stream = classLoader.getResourceAsStream("CopyrightHeader.txt");
66 OutputStream out = new FileOutputStream(temp);
67 int index;
68 while ((index = stream.read()) != NULL) {
69 out.write(index);
70 }
71 out.close();
72 return convertToString(temp.toString());
73 } catch (IOException e) {
74 log.info("Failed to insert onos header in files.");
75 } finally {
76 temp.delete();
77 }
78 return "";
79 }
80
81 /**
82 * Converts it to string.
83 *
84 * @param toAppend file to be converted.
85 * @return string of file.
86 * @throws IOException when fails to convert to string
87 */
88 private static String convertToString(String toAppend) throws IOException {
89 BufferedReader bufferReader = new BufferedReader(new FileReader(toAppend));
90 try {
91 StringBuilder stringBuilder = new StringBuilder();
92 String line = bufferReader.readLine();
93
94 while (line != null) {
95 stringBuilder.append(line);
96 stringBuilder.append("\n");
97 line = bufferReader.readLine();
98 }
99 return stringBuilder.toString();
100 } finally {
101 bufferReader.close();
102 }
103 }
104}