blob: 54adce91b2e821a9d25e9efb2c83d5a8925df96f [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
Bharat saraswal870c56f2016-02-20 21:57:16 +053036 /**
37 * Returns the list of java files.
38 *
39 * @param root specified directory
Vinod Kumar Sc4216002016-03-03 19:55:30 +053040 * @return list of java files
Bharat saraswal2f00b4b2016-03-04 20:08:09 +053041 * @throws NullPointerException when no files are there.
Bharat saraswal870c56f2016-02-20 21:57:16 +053042 * @throws IOException when files get deleted while performing the
Vinod Kumar Sc4216002016-03-03 19:55:30 +053043 * operations
Bharat saraswal870c56f2016-02-20 21:57:16 +053044 */
b.janani68c55e12016-02-24 12:23:03 +053045 public static List<String> getJavaFiles(String root) throws NullPointerException, IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +053046
Bharat saraswal870c56f2016-02-20 21:57:16 +053047 return getFiles(root, ".java");
48 }
49
50 /**
b.janani68c55e12016-02-24 12:23:03 +053051 * Returns the list of YANG files.
52 *
53 * @param root specified directory
Vinod Kumar Sc4216002016-03-03 19:55:30 +053054 * @return list of YANG files
55 * @throws NullPointerException when no files are there
b.janani68c55e12016-02-24 12:23:03 +053056 * @throws IOException when files get deleted while performing the
Vinod Kumar Sc4216002016-03-03 19:55:30 +053057 * operations
b.janani68c55e12016-02-24 12:23:03 +053058 */
59 public static List<String> getYangFiles(String root) throws NullPointerException, IOException {
Vinod Kumar S38046502016-03-23 15:30:27 +053060
b.janani68c55e12016-02-24 12:23:03 +053061 return getFiles(root, ".yang");
62 }
63
64 /**
Bharat saraswal870c56f2016-02-20 21:57:16 +053065 * Returns the list of required files.
66 *
67 * @param root specified directory
Vinod Kumar Sc4216002016-03-03 19:55:30 +053068 * @param extension file extension
69 * @return list of required files
Vinod Kumar S38046502016-03-23 15:30:27 +053070 * @throws NullPointerException when no file is there
Bharat saraswal870c56f2016-02-20 21:57:16 +053071 * @throws IOException when files get deleted while performing the
Vinod Kumar Sc4216002016-03-03 19:55:30 +053072 * operations
Bharat saraswal870c56f2016-02-20 21:57:16 +053073 */
Vinod Kumar S38046502016-03-23 15:30:27 +053074 public static List<String> getFiles(String root, String extension) throws NullPointerException, IOException {
75
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;
80 File[] filelist;
81 try {
82 while (!stack.empty()) {
83 root = stack.pop();
84 file = new File(root);
85 filelist = file.listFiles();
b.janani68c55e12016-02-24 12:23:03 +053086 if (filelist.length == 0) {
janani b5c12efc2016-02-08 18:56:13 +053087 continue;
88 }
89 for (File current : filelist) {
90 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;
b.janani68c55e12016-02-24 12:23:03 +0530101 } catch (NullPointerException e) {
102 throw new IOException("NullPointerException occured");
janani b5c12efc2016-02-08 18:56:13 +0530103 }
104 }
105}