Bharat saraswal | 708abc0 | 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 | |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 27 | import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE; |
| 28 | |
Bharat saraswal | 708abc0 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 29 | /** |
| 30 | * Provides the license header for the generated files. |
| 31 | */ |
| 32 | public final class CopyrightHeader { |
| 33 | |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 34 | private static final int EOF = -1; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 35 | private static final String COPYRIGHT_HEADER_FILE = "CopyrightHeader.txt"; |
| 36 | private static final String TEMP_FILE = "temp.txt"; |
Bharat saraswal | 708abc0 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 37 | private static ClassLoader classLoader = CopyrightHeader.class.getClassLoader(); |
| 38 | |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 39 | private static String copyrightHeader; |
| 40 | |
Bharat saraswal | 708abc0 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 41 | /** |
| 42 | * Default constructor. |
| 43 | */ |
| 44 | private CopyrightHeader() { |
| 45 | } |
| 46 | |
| 47 | /** |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 48 | * Returns copyright file header. |
Bharat saraswal | 708abc0 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 49 | * |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 50 | * @return copyright file header |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 51 | * @throws IOException when fails to parse copyright header |
Bharat saraswal | 708abc0 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 52 | */ |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 53 | public static String getCopyrightHeader() throws IOException { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 54 | |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 55 | if (copyrightHeader == null) { |
| 56 | parseCopyrightHeader(); |
| 57 | } |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 58 | return copyrightHeader; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Sets the copyright header. |
| 63 | * |
| 64 | * @param header copyright header |
| 65 | */ |
| 66 | private static void setCopyrightHeader(String header) { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 67 | |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 68 | copyrightHeader = header; |
Bharat saraswal | 708abc0 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** |
| 72 | * parse Copyright to the temporary file. |
| 73 | * |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 74 | * @throws IOException when fails to get the copyright header |
Bharat saraswal | 708abc0 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 75 | */ |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 76 | public static void parseCopyrightHeader() throws IOException { |
Bharat saraswal | 708abc0 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 77 | |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 78 | File temp = new File(TEMP_FILE); |
Bharat saraswal | 708abc0 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 79 | |
| 80 | try { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 81 | InputStream stream = classLoader.getResourceAsStream(COPYRIGHT_HEADER_FILE); |
Bharat saraswal | 708abc0 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 82 | OutputStream out = new FileOutputStream(temp); |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 83 | |
Bharat saraswal | 708abc0 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 84 | int index; |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 85 | while ((index = stream.read()) != EOF) { |
Bharat saraswal | 708abc0 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 86 | out.write(index); |
| 87 | } |
| 88 | out.close(); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 89 | stream.close(); |
| 90 | getStringFileContent(temp); |
| 91 | setCopyrightHeader(getStringFileContent(temp)); |
Bharat saraswal | 708abc0 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 92 | } catch (IOException e) { |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 93 | throw new IOException("failed to parse the Copyright header"); |
Bharat saraswal | 708abc0 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 94 | } finally { |
| 95 | temp.delete(); |
| 96 | } |
Bharat saraswal | 708abc0 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Converts it to string. |
| 101 | * |
| 102 | * @param toAppend file to be converted. |
| 103 | * @return string of file. |
| 104 | * @throws IOException when fails to convert to string |
| 105 | */ |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 106 | private static String getStringFileContent(File toAppend) throws IOException { |
| 107 | |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 108 | FileReader fileReader = new FileReader(toAppend); |
| 109 | BufferedReader bufferReader = new BufferedReader(fileReader); |
Bharat saraswal | 708abc0 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 110 | try { |
| 111 | StringBuilder stringBuilder = new StringBuilder(); |
| 112 | String line = bufferReader.readLine(); |
| 113 | |
| 114 | while (line != null) { |
| 115 | stringBuilder.append(line); |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 116 | stringBuilder.append(NEW_LINE); |
Bharat saraswal | 708abc0 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 117 | line = bufferReader.readLine(); |
| 118 | } |
| 119 | return stringBuilder.toString(); |
| 120 | } finally { |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 121 | fileReader.close(); |
Bharat saraswal | 708abc0 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 122 | bufferReader.close(); |
| 123 | } |
| 124 | } |
| 125 | } |