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 | |
| 36 | /** |
| 37 | * Returns the list of yang files. |
| 38 | * |
| 39 | * @param root specified directory |
| 40 | * @return list of yang files. |
| 41 | * @throws IOException when files get deleted while performing the operations. |
| 42 | */ |
| 43 | public static List<String> getYangFiles(String root) throws IOException { |
| 44 | |
| 45 | List<String> store = new LinkedList<>(); |
| 46 | Stack<String> stack = new Stack<>(); |
| 47 | stack.push(root); |
| 48 | File file; |
| 49 | File[] filelist; |
| 50 | try { |
| 51 | while (!stack.empty()) { |
| 52 | root = stack.pop(); |
| 53 | file = new File(root); |
| 54 | filelist = file.listFiles(); |
| 55 | if (filelist == null) { |
| 56 | continue; |
| 57 | } |
| 58 | for (File current : filelist) { |
| 59 | if (current.isDirectory()) { |
| 60 | stack.push(current.toString()); |
| 61 | } else { |
| 62 | String yangFile = current.getCanonicalPath(); |
| 63 | if (yangFile.endsWith(".yang")) { |
| 64 | store.add(yangFile); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | return store; |
| 70 | } catch (IOException e) { |
| 71 | throw new IOException("IOException occured"); |
| 72 | } |
| 73 | } |
| 74 | } |