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 | |
Bharat saraswal | dedf9f8 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 27 | /** |
| 28 | * Provides the license header for the generated files. |
| 29 | */ |
| 30 | public final class CopyrightHeader { |
| 31 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 32 | private static final int EOF = -1; |
Bharat saraswal | dedf9f8 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 33 | private static ClassLoader classLoader = CopyrightHeader.class.getClassLoader(); |
| 34 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 35 | private static String copyrightHeader; |
| 36 | |
Bharat saraswal | dedf9f8 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 37 | /** |
| 38 | * Default constructor. |
| 39 | */ |
| 40 | private CopyrightHeader() { |
| 41 | } |
| 42 | |
| 43 | /** |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 44 | * Returns copyright file header. |
Bharat saraswal | dedf9f8 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 45 | * |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 46 | * @return copyright file header |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 47 | * @throws IOException when fails to parse copyright header |
Bharat saraswal | dedf9f8 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 48 | */ |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 49 | public static String getCopyrightHeader() throws IOException { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 50 | if (copyrightHeader == null) { |
| 51 | parseCopyrightHeader(); |
| 52 | } |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 53 | 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 saraswal | dedf9f8 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /** |
| 66 | * parse Copyright to the temporary file. |
| 67 | * |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 68 | * @throws IOException when fails to get the copyright header |
Bharat saraswal | dedf9f8 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 69 | */ |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 70 | public static void parseCopyrightHeader() throws IOException { |
Bharat saraswal | dedf9f8 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 71 | |
| 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 saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 78 | while ((index = stream.read()) != EOF) { |
Bharat saraswal | dedf9f8 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 79 | out.write(index); |
| 80 | } |
| 81 | out.close(); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 82 | stream.close(); |
| 83 | getStringFileContent(temp); |
| 84 | setCopyrightHeader(getStringFileContent(temp)); |
Bharat saraswal | dedf9f8 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 85 | } catch (IOException e) { |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 86 | throw new IOException("failed to parse the Copyright header"); |
Bharat saraswal | dedf9f8 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 87 | } finally { |
| 88 | temp.delete(); |
| 89 | } |
Bharat saraswal | dedf9f8 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 90 | } |
| 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 saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 99 | private static String getStringFileContent(File toAppend) throws IOException { |
| 100 | |
Bharat saraswal | dedf9f8 | 2016-02-12 20:48:30 +0530 | [diff] [blame] | 101 | 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 | } |