blob: 98e0444e3f759f86a8128a7844a6517420dc7433 [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
Bharat saraswal97459962016-02-20 21:57:16 +053036 /**
37 * Returns the list of java files.
38 *
39 * @param root specified directory
Vinod Kumar S08710982016-03-03 19:55:30 +053040 * @return list of java files
41 * @throws NullPointerException when no files are there
Bharat saraswal97459962016-02-20 21:57:16 +053042 * @throws IOException when files get deleted while performing the
Vinod Kumar S08710982016-03-03 19:55:30 +053043 * operations
Bharat saraswal97459962016-02-20 21:57:16 +053044 */
b.janani66b749c2016-02-24 12:23:03 +053045 public static List<String> getJavaFiles(String root) throws NullPointerException, IOException {
Bharat saraswal97459962016-02-20 21:57:16 +053046 return getFiles(root, ".java");
47 }
48
49 /**
b.janani66b749c2016-02-24 12:23:03 +053050 * Returns the list of YANG files.
51 *
52 * @param root specified directory
Vinod Kumar S08710982016-03-03 19:55:30 +053053 * @return list of YANG files
54 * @throws NullPointerException when no files are there
b.janani66b749c2016-02-24 12:23:03 +053055 * @throws IOException when files get deleted while performing the
Vinod Kumar S08710982016-03-03 19:55:30 +053056 * operations
b.janani66b749c2016-02-24 12:23:03 +053057 */
58 public static List<String> getYangFiles(String root) throws NullPointerException, IOException {
59 return getFiles(root, ".yang");
60 }
61
62 /**
Bharat saraswal97459962016-02-20 21:57:16 +053063 * Returns the list of required files.
64 *
65 * @param root specified directory
Vinod Kumar S08710982016-03-03 19:55:30 +053066 * @param extension file extension
67 * @return list of required files
Bharat saraswal97459962016-02-20 21:57:16 +053068 * @throws IOException when files get deleted while performing the
Vinod Kumar S08710982016-03-03 19:55:30 +053069 * operations
70 * @throws NullPointerException null pointer access
Bharat saraswal97459962016-02-20 21:57:16 +053071 */
Vinod Kumar S08710982016-03-03 19:55:30 +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}