blob: b5f5fca164c270325c34622daa9a6daaf087706a [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 saraswal2d90b0c2016-08-04 02:00:03 +053030 private static final String JAVA_FILE_EXTENSION = ".java";
31 private static final String YANG_FILE_EXTENSION = ".yang";
Bharat saraswale2d51d62016-03-23 19:40:35 +053032
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.
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053045 * @throws IOException when files get deleted while performing the
46 * operations
Bharat saraswal870c56f2016-02-20 21:57:16 +053047 */
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053048 static List<String> getJavaFiles(String root) throws IOException {
49 return getFiles(root, JAVA_FILE_EXTENSION);
Bharat saraswal870c56f2016-02-20 21:57:16 +053050 }
51
52 /**
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053053 * Returns the list of YANG file.
b.janani68c55e12016-02-24 12:23:03 +053054 *
55 * @param root specified directory
Vidyashree Rama1db15562016-05-17 16:16:15 +053056 * @return list of YANG file information
Vinod Kumar Sc4216002016-03-03 19:55:30 +053057 * @throws NullPointerException when no files are there
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053058 * @throws IOException when files get deleted while performing the
59 * operations
b.janani68c55e12016-02-24 12:23:03 +053060 */
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053061 public static List<String> getYangFiles(String root) throws IOException {
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053062 return getFiles(root, YANG_FILE_EXTENSION);
b.janani68c55e12016-02-24 12:23:03 +053063 }
64
65 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +053066 * Returns the list of required files.
67 *
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053068 * @param root specified directory
Vinod Kumar Sc4216002016-03-03 19:55:30 +053069 * @param extension file extension
70 * @return list of required files
Vinod Kumar S38046502016-03-23 15:30:27 +053071 * @throws NullPointerException when no file is there
Gaurav Agrawal0d43bb52016-05-17 18:06:38 +053072 * @throws IOException when files get deleted while performing the operations
Bharat saraswal870c56f2016-02-20 21:57:16 +053073 */
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053074 private static List<String> getFiles(String root, String extension) throws IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +053075
janani b5c12efc2016-02-08 18:56:13 +053076 List<String> store = new LinkedList<>();
77 Stack<String> stack = new Stack<>();
78 stack.push(root);
79 File file;
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053080 File[] fileList;
janani b5c12efc2016-02-08 18:56:13 +053081 try {
82 while (!stack.empty()) {
83 root = stack.pop();
84 file = new File(root);
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053085 fileList = file.listFiles();
86 if ((fileList == null) || (fileList.length == 0)) {
janani b5c12efc2016-02-08 18:56:13 +053087 continue;
88 }
Bharat saraswal2d90b0c2016-08-04 02:00:03 +053089 for (File current : fileList) {
janani b5c12efc2016-02-08 18:56:13 +053090 if (current.isDirectory()) {
91 stack.push(current.toString());
92 } else {
93 String yangFile = current.getCanonicalPath();
Bharat saraswal870c56f2016-02-20 21:57:16 +053094 if (yangFile.endsWith(extension)) {
janani b5c12efc2016-02-08 18:56:13 +053095 store.add(yangFile);
96 }
97 }
98 }
99 }
100 return store;
Bharat saraswal2f11f652016-03-25 18:19:46 +0530101 } catch (IOException e) {
102 throw new IOException("No File found of " + extension + " extension in " + root + " directory.");
janani b5c12efc2016-02-08 18:56:13 +0530103 }
104 }
105}