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