Bharat saraswal | dedf9f8 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.yangutils.utils.io.impl; |
| 18 | |
| 19 | import java.io.BufferedReader; |
| 20 | import java.io.File; |
| 21 | import java.io.FileOutputStream; |
| 22 | import java.io.FileReader; |
| 23 | import java.io.IOException; |
| 24 | import java.io.InputStream; |
| 25 | import java.io.OutputStream; |
| 26 | |
| 27 | import static org.slf4j.LoggerFactory.getLogger; |
| 28 | import org.slf4j.Logger; |
| 29 | |
| 30 | /** |
| 31 | * Provides the license header for the generated files. |
| 32 | */ |
| 33 | public final class CopyrightHeader { |
| 34 | |
| 35 | private static final Logger log = getLogger(CopyrightHeader.class); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame^] | 36 | private static final int EOF = -1; |
Bharat saraswal | dedf9f8 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 37 | private static ClassLoader classLoader = CopyrightHeader.class.getClassLoader(); |
| 38 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame^] | 39 | private static String copyrightHeader; |
| 40 | |
Bharat saraswal | dedf9f8 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 41 | /** |
| 42 | * Default constructor. |
| 43 | */ |
| 44 | private CopyrightHeader() { |
| 45 | } |
| 46 | |
| 47 | /** |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame^] | 48 | * Returns copyright file header. |
Bharat saraswal | dedf9f8 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 49 | * |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame^] | 50 | * @return copyright file header |
| 51 | * @throws IOException when fails to parse copyright header. |
Bharat saraswal | dedf9f8 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 52 | */ |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame^] | 53 | 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 saraswal | dedf9f8 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /** |
| 67 | * parse Copyright to the temporary file. |
| 68 | * |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame^] | 69 | * @throws IOException when fails to get the copyright header. |
Bharat saraswal | dedf9f8 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 70 | */ |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame^] | 71 | public static void parseCopyrightHeader() throws IOException { |
Bharat saraswal | dedf9f8 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 72 | |
| 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 saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame^] | 79 | while ((index = stream.read()) != EOF) { |
Bharat saraswal | dedf9f8 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 80 | out.write(index); |
| 81 | } |
| 82 | out.close(); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame^] | 83 | stream.close(); |
| 84 | getStringFileContent(temp); |
| 85 | setCopyrightHeader(getStringFileContent(temp)); |
Bharat saraswal | dedf9f8 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 86 | } catch (IOException e) { |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame^] | 87 | throw new IOException("failed to parse the Copyright header"); |
Bharat saraswal | dedf9f8 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 88 | } finally { |
| 89 | temp.delete(); |
| 90 | } |
Bharat saraswal | dedf9f8 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 91 | } |
| 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 saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame^] | 100 | private static String getStringFileContent(File toAppend) throws IOException { |
| 101 | |
Bharat saraswal | dedf9f8 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 102 | 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 | } |