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 |
| 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 { |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 46 | return getFiles(root, ".java"); |
| 47 | } |
| 48 | |
| 49 | /** |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 50 | * Returns the list of YANG files. |
| 51 | * |
| 52 | * @param root specified directory |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 53 | * @return list of YANG files |
| 54 | * @throws NullPointerException when no files are there |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 55 | * @throws IOException when files get deleted while performing the |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 56 | * operations |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 57 | */ |
| 58 | public static List<String> getYangFiles(String root) throws NullPointerException, IOException { |
| 59 | return getFiles(root, ".yang"); |
| 60 | } |
| 61 | |
| 62 | /** |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 63 | * Returns the list of required files. |
| 64 | * |
| 65 | * @param root specified directory |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 66 | * @param extension file extension |
| 67 | * @return list of required files |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 68 | * @throws IOException when files get deleted while performing the |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 69 | * operations |
| 70 | * @throws NullPointerException null pointer access |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 71 | */ |
Vinod Kumar S | 0871098 | 2016-03-03 19:55:30 +0530 | [diff] [blame] | 72 | public static List<String> getFiles(String root, String extension) throws NullPointerException, IOException { |
janani b | 0863755 | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 73 | List<String> store = new LinkedList<>(); |
| 74 | Stack<String> stack = new Stack<>(); |
| 75 | stack.push(root); |
| 76 | File file; |
| 77 | File[] filelist; |
| 78 | try { |
| 79 | while (!stack.empty()) { |
| 80 | root = stack.pop(); |
| 81 | file = new File(root); |
| 82 | filelist = file.listFiles(); |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 83 | if (filelist.length == 0) { |
janani b | 0863755 | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 84 | continue; |
| 85 | } |
| 86 | for (File current : filelist) { |
| 87 | if (current.isDirectory()) { |
| 88 | stack.push(current.toString()); |
| 89 | } else { |
| 90 | String yangFile = current.getCanonicalPath(); |
Bharat saraswal | 9745996 | 2016-02-20 21:57:16 +0530 | [diff] [blame] | 91 | if (yangFile.endsWith(extension)) { |
janani b | 0863755 | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 92 | store.add(yangFile); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | return store; |
b.janani | 66b749c | 2016-02-24 12:23:03 +0530 | [diff] [blame] | 98 | } catch (NullPointerException e) { |
| 99 | throw new IOException("NullPointerException occured"); |
janani b | 0863755 | 2016-02-08 18:56:13 +0530 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | } |