blob: 1f8a159d3ba367f3a7bfc92d51ac5466086ce1d4 [file] [log] [blame]
janani b08637552016-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
janani b08637552016-02-08 18:56:13 +053036
Bharat saraswal97459962016-02-20 21:57:16 +053037 /**
38 * Returns the list of java files.
39 *
40 * @param root specified directory
41 * @return list of java files.
b.janani66b749c2016-02-24 12:23:03 +053042 * @throws NullPointerException when no files are there.
Bharat saraswal97459962016-02-20 21:57:16 +053043 * @throws IOException when files get deleted while performing the
44 * operations.
45 */
b.janani66b749c2016-02-24 12:23:03 +053046 public static List<String> getJavaFiles(String root) throws NullPointerException, IOException {
Bharat saraswal97459962016-02-20 21:57:16 +053047 return getFiles(root, ".java");
48 }
49
50 /**
b.janani66b749c2016-02-24 12:23:03 +053051 * Returns the list of YANG files.
52 *
53 * @param root specified directory
54 * @return list of YANG files.
55 * @throws NullPointerException when no files are there.
56 * @throws IOException when files get deleted while performing the
57 * operations.
58 */
59 public static List<String> getYangFiles(String root) throws NullPointerException, IOException {
60 return getFiles(root, ".yang");
61 }
62
63 /**
Bharat saraswal97459962016-02-20 21:57:16 +053064 * Returns the list of required files.
65 *
66 * @param root specified directory
67 * @param extension file extension.
68 * @return list of required files.
69 * @throws IOException when files get deleted while performing the
70 * operations.
71 */
b.janani66b749c2016-02-24 12:23:03 +053072 public static List<String> getFiles(String root, String extension) throws NullPointerException, IOException {
janani b08637552016-02-08 18:56:13 +053073 List<String> store = new LinkedList<>();
74 Stack<String> stack = new Stack<>();
75 stack.push(root);
76 File file;
77 File[] filelist;
78 try {
79 while (!stack.empty()) {
80 root = stack.pop();
81 file = new File(root);
82 filelist = file.listFiles();
b.janani66b749c2016-02-24 12:23:03 +053083 if (filelist.length == 0) {
janani b08637552016-02-08 18:56:13 +053084 continue;
85 }
86 for (File current : filelist) {
87 if (current.isDirectory()) {
88 stack.push(current.toString());
89 } else {
90 String yangFile = current.getCanonicalPath();
Bharat saraswal97459962016-02-20 21:57:16 +053091 if (yangFile.endsWith(extension)) {
janani b08637552016-02-08 18:56:13 +053092 store.add(yangFile);
93 }
94 }
95 }
96 }
97 return store;
b.janani66b749c2016-02-24 12:23:03 +053098 } catch (NullPointerException e) {
99 throw new IOException("NullPointerException occured");
janani b08637552016-02-08 18:56:13 +0530100 }
101 }
102}