janani b | 0863755 | 2016-02-08 18:56:13 +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.File; |
| 20 | import java.io.IOException; |
| 21 | import java.util.LinkedList; |
| 22 | import java.util.List; |
| 23 | import java.util.Stack; |
| 24 | |
| 25 | /** |
| 26 | * Provides the IO services for Yangutils-maven-Plugin. |
| 27 | */ |
| 28 | public final class YangFileScanner { |
| 29 | |
| 30 | /** |
| 31 | * Default constructor. |
| 32 | */ |
| 33 | private YangFileScanner() { |
| 34 | } |
| 35 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 36 | /** |
| 37 | * Returns the list of java files. |
| 38 | * |
| 39 | * @param root specified directory |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 40 | * @return list of java files |
Bharat saraswal | 022dae9 | 2016-03-04 20:08:09 +0530 | [diff] [blame] | 41 | * @throws NullPointerException when no files are there. |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 42 | * @throws IOException when files get deleted while performing the |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 43 | * operations |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 44 | */ |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 45 | public static List<String> getJavaFiles(String root) throws NullPointerException, IOException { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 46 | |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 47 | return getFiles(root, ".java"); |
| 48 | } |
| 49 | |
| 50 | /** |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 51 | * Returns the list of YANG files. |
| 52 | * |
| 53 | * @param root specified directory |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 54 | * @return list of YANG files |
| 55 | * @throws NullPointerException when no files are there |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 56 | * @throws IOException when files get deleted while performing the |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 57 | * operations |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 58 | */ |
| 59 | public static List<String> getYangFiles(String root) throws NullPointerException, IOException { |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 60 | |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 61 | return getFiles(root, ".yang"); |
| 62 | } |
| 63 | |
| 64 | /** |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 65 | * Returns the list of required files. |
| 66 | * |
| 67 | * @param root specified directory |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 68 | * @param extension file extension |
| 69 | * @return list of required files |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 70 | * @throws NullPointerException when no file is there |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 71 | * @throws IOException when files get deleted while performing the |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 72 | * operations |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 73 | */ |
Vinod Kumar S | 9f26ae5 | 2016-03-23 15:30:27 +0530 | [diff] [blame] | 74 | public static List<String> getFiles(String root, String extension) throws NullPointerException, IOException { |
| 75 | |
janani b | 0863755 | 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; |
| 80 | File[] filelist; |
| 81 | try { |
| 82 | while (!stack.empty()) { |
| 83 | root = stack.pop(); |
| 84 | file = new File(root); |
| 85 | filelist = file.listFiles(); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 86 | if (filelist.length == 0) { |
janani b | 0863755 | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 87 | continue; |
| 88 | } |
| 89 | for (File current : filelist) { |
| 90 | if (current.isDirectory()) { |
| 91 | stack.push(current.toString()); |
| 92 | } else { |
| 93 | String yangFile = current.getCanonicalPath(); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 94 | if (yangFile.endsWith(extension)) { |
janani b | 0863755 | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 95 | store.add(yangFile); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | return store; |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 101 | } catch (NullPointerException e) { |
| 102 | throw new IOException("NullPointerException occured"); |
janani b | 0863755 | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | } |