blob: 6da8bcd25cd2357304346ad80035bbced6d2e3d7 [file] [log] [blame]
janani b5c12efc2016-02-08 18:56:13 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
janani b5c12efc2016-02-08 18:56:13 +05303 *
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/**
Bharat saraswald9822e92016-04-05 15:13:44 +053026 * Represents utility for searching the files in a directory.
janani b5c12efc2016-02-08 18:56:13 +053027 */
28public final class YangFileScanner {
29
Bharat saraswale2d51d62016-03-23 19:40:35 +053030 private static final String JAVA_FILE_EXTENTION = ".java";
31 private static final String YANG_FILE_EXTENTION = ".yang";
32
janani b5c12efc2016-02-08 18:56:13 +053033 /**
Bharat saraswald9822e92016-04-05 15:13:44 +053034 * Creates an instance of YANG file scanner.
janani b5c12efc2016-02-08 18:56:13 +053035 */
36 private YangFileScanner() {
37 }
38
Bharat saraswal870c56f2016-02-20 21:57:16 +053039 /**
40 * Returns the list of java files.
41 *
42 * @param root specified directory
Vinod Kumar Sc4216002016-03-03 19:55:30 +053043 * @return list of java files
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053044 * @throws NullPointerException when no files are there.
Bharat saraswal870c56f2016-02-20 21:57:16 +053045 * @throws IOException when files get deleted while performing the
Vinod Kumar Sc4216002016-03-03 19:55:30 +053046 * operations
Bharat saraswal870c56f2016-02-20 21:57:16 +053047 */
Bharat saraswal2f11f652016-03-25 18:19:46 +053048 public static List<String> getJavaFiles(String root) throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +053049
Bharat saraswale2d51d62016-03-23 19:40:35 +053050 return getFiles(root, JAVA_FILE_EXTENTION);
Bharat saraswal870c56f2016-02-20 21:57:16 +053051 }
52
53 /**
b.janani68c55e12016-02-24 12:23:03 +053054 * Returns the list of YANG files.
55 *
56 * @param root specified directory
Vinod Kumar Sc4216002016-03-03 19:55:30 +053057 * @return list of YANG files
58 * @throws NullPointerException when no files are there
b.janani68c55e12016-02-24 12:23:03 +053059 * @throws IOException when files get deleted while performing the
Vinod Kumar Sc4216002016-03-03 19:55:30 +053060 * operations
b.janani68c55e12016-02-24 12:23:03 +053061 */
Bharat saraswal2f11f652016-03-25 18:19:46 +053062 public static List<String> getYangFiles(String root) throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +053063
Bharat saraswale2d51d62016-03-23 19:40:35 +053064 return getFiles(root, YANG_FILE_EXTENTION);
b.janani68c55e12016-02-24 12:23:03 +053065 }
66
67 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +053068 * Returns the list of required files.
69 *
70 * @param root specified directory
Vinod Kumar Sc4216002016-03-03 19:55:30 +053071 * @param extension file extension
72 * @return list of required files
Vinod Kumar S38046502016-03-23 15:30:27 +053073 * @throws NullPointerException when no file is there
Bharat saraswale2d51d62016-03-23 19:40:35 +053074 * @throws IOException when files get deleted while performing the operations
Bharat saraswal870c56f2016-02-20 21:57:16 +053075 */
Bharat saraswal2f11f652016-03-25 18:19:46 +053076 public static List<String> getFiles(String root, String extension) throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +053077
janani b5c12efc2016-02-08 18:56:13 +053078 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.janani68c55e12016-02-24 12:23:03 +053088 if (filelist.length == 0) {
janani b5c12efc2016-02-08 18:56:13 +053089 continue;
90 }
91 for (File current : filelist) {
92 if (current.isDirectory()) {
93 stack.push(current.toString());
94 } else {
95 String yangFile = current.getCanonicalPath();
Bharat saraswal870c56f2016-02-20 21:57:16 +053096 if (yangFile.endsWith(extension)) {
janani b5c12efc2016-02-08 18:56:13 +053097 store.add(yangFile);
98 }
99 }
100 }
101 }
102 return store;
Bharat saraswal2f11f652016-03-25 18:19:46 +0530103 } catch (IOException e) {
104 throw new IOException("No File found of " + extension + " extension in " + root + " directory.");
janani b5c12efc2016-02-08 18:56:13 +0530105 }
106 }
107}