blob: a9006d292d032cc5690509335ac3a8c9537f34dc [file] [log] [blame]
janani b5c12efc2016-02-08 18:56:13 +05301/*
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
17package org.onosproject.yangutils.utils.io.impl;
18
19import java.io.File;
20import java.io.IOException;
21import java.util.LinkedList;
22import java.util.List;
23import java.util.Stack;
24
25/**
26 * Provides the IO services for Yangutils-maven-Plugin.
27 */
28public final class YangFileScanner {
29
30 /**
31 * Default constructor.
32 */
33 private YangFileScanner() {
34 }
35
36 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +053037 * Returns the list of YANG files.
janani b5c12efc2016-02-08 18:56:13 +053038 *
39 * @param root specified directory
Bharat saraswal870c56f2016-02-20 21:57:16 +053040 * @return list of YANG files.
41 * @throws IOException when files get deleted while performing the
42 * operations.
janani b5c12efc2016-02-08 18:56:13 +053043 */
44 public static List<String> getYangFiles(String root) throws IOException {
Bharat saraswal870c56f2016-02-20 21:57:16 +053045 return getFiles(root, ".yang");
46 }
janani b5c12efc2016-02-08 18:56:13 +053047
Bharat saraswal870c56f2016-02-20 21:57:16 +053048 /**
49 * Returns the list of java files.
50 *
51 * @param root specified directory
52 * @return list of java files.
53 * @throws IOException when files get deleted while performing the
54 * operations.
55 */
56 public static List<String> getJavaFiles(String root) throws IOException {
57 return getFiles(root, ".java");
58 }
59
60 /**
61 * Returns the list of required files.
62 *
63 * @param root specified directory
64 * @param extension file extension.
65 * @return list of required files.
66 * @throws IOException when files get deleted while performing the
67 * operations.
68 */
69 public static List<String> getFiles(String root, String extension) throws IOException {
janani b5c12efc2016-02-08 18:56:13 +053070 List<String> store = new LinkedList<>();
71 Stack<String> stack = new Stack<>();
72 stack.push(root);
73 File file;
74 File[] filelist;
75 try {
76 while (!stack.empty()) {
77 root = stack.pop();
78 file = new File(root);
79 filelist = file.listFiles();
80 if (filelist == null) {
81 continue;
82 }
83 for (File current : filelist) {
84 if (current.isDirectory()) {
85 stack.push(current.toString());
86 } else {
87 String yangFile = current.getCanonicalPath();
Bharat saraswal870c56f2016-02-20 21:57:16 +053088 if (yangFile.endsWith(extension)) {
janani b5c12efc2016-02-08 18:56:13 +053089 store.add(yangFile);
90 }
91 }
92 }
93 }
94 return store;
95 } catch (IOException e) {
96 throw new IOException("IOException occured");
97 }
98 }
99}