janani b | 5c12efc | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
janani b | 5c12efc | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 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.File; |
| 20 | import java.io.IOException; |
| 21 | import java.util.LinkedList; |
| 22 | import java.util.List; |
| 23 | import java.util.Stack; |
| 24 | |
| 25 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 26 | * Represents utility for searching the files in a directory. |
janani b | 5c12efc | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 27 | */ |
| 28 | public final class YangFileScanner { |
| 29 | |
Bharat saraswal | 2d90b0c | 2016-08-04 02:00:03 +0530 | [diff] [blame^] | 30 | private static final String JAVA_FILE_EXTENSION = ".java"; |
| 31 | private static final String YANG_FILE_EXTENSION = ".yang"; |
Bharat saraswal | e2d51d6 | 2016-03-23 19:40:35 +0530 | [diff] [blame] | 32 | |
janani b | 5c12efc | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 33 | /** |
Bharat saraswal | d9822e9 | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 34 | * Creates an instance of YANG file scanner. |
janani b | 5c12efc | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 35 | */ |
| 36 | private YangFileScanner() { |
| 37 | } |
| 38 | |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 39 | /** |
| 40 | * Returns the list of java files. |
| 41 | * |
| 42 | * @param root specified directory |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 43 | * @return list of java files |
Bharat saraswal | 2f00b4b | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 44 | * @throws NullPointerException when no files are there. |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 45 | * @throws IOException when files get deleted while performing the |
| 46 | * operations |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 47 | */ |
Bharat saraswal | 2d90b0c | 2016-08-04 02:00:03 +0530 | [diff] [blame^] | 48 | static List<String> getJavaFiles(String root) throws IOException { |
| 49 | return getFiles(root, JAVA_FILE_EXTENSION); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | /** |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 53 | * Returns the list of YANG file. |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 54 | * |
| 55 | * @param root specified directory |
Vidyashree Rama | 1db1556 | 2016-05-17 16:16:15 +0530 | [diff] [blame] | 56 | * @return list of YANG file information |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 57 | * @throws NullPointerException when no files are there |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 58 | * @throws IOException when files get deleted while performing the |
| 59 | * operations |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 60 | */ |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 61 | public static List<String> getYangFiles(String root) throws IOException { |
Bharat saraswal | 2d90b0c | 2016-08-04 02:00:03 +0530 | [diff] [blame^] | 62 | return getFiles(root, YANG_FILE_EXTENSION); |
b.janani | 68c55e1 | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /** |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 66 | * Returns the list of required files. |
| 67 | * |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 68 | * @param root specified directory |
Vinod Kumar S | c421600 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 69 | * @param extension file extension |
| 70 | * @return list of required files |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 71 | * @throws NullPointerException when no file is there |
Gaurav Agrawal | 0d43bb5 | 2016-05-17 18:06:38 +0530 | [diff] [blame] | 72 | * @throws IOException when files get deleted while performing the operations |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 73 | */ |
Bharat saraswal | 2d90b0c | 2016-08-04 02:00:03 +0530 | [diff] [blame^] | 74 | private static List<String> getFiles(String root, String extension) throws IOException { |
Vinod Kumar S | 3804650 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 75 | |
janani b | 5c12efc | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 76 | List<String> store = new LinkedList<>(); |
| 77 | Stack<String> stack = new Stack<>(); |
| 78 | stack.push(root); |
| 79 | File file; |
Bharat saraswal | 2d90b0c | 2016-08-04 02:00:03 +0530 | [diff] [blame^] | 80 | File[] fileList; |
janani b | 5c12efc | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 81 | try { |
| 82 | while (!stack.empty()) { |
| 83 | root = stack.pop(); |
| 84 | file = new File(root); |
Bharat saraswal | 2d90b0c | 2016-08-04 02:00:03 +0530 | [diff] [blame^] | 85 | fileList = file.listFiles(); |
| 86 | if ((fileList == null) || (fileList.length == 0)) { |
janani b | 5c12efc | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 87 | continue; |
| 88 | } |
Bharat saraswal | 2d90b0c | 2016-08-04 02:00:03 +0530 | [diff] [blame^] | 89 | for (File current : fileList) { |
janani b | 5c12efc | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 90 | if (current.isDirectory()) { |
| 91 | stack.push(current.toString()); |
| 92 | } else { |
| 93 | String yangFile = current.getCanonicalPath(); |
Bharat saraswal | 870c56f | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 94 | if (yangFile.endsWith(extension)) { |
janani b | 5c12efc | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 95 | store.add(yangFile); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | return store; |
Bharat saraswal | 2f11f65 | 2016-03-25 18:19:46 +0530 | [diff] [blame] | 101 | } catch (IOException e) { |
| 102 | throw new IOException("No File found of " + extension + " extension in " + root + " directory."); |
janani b | 5c12efc | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | } |