Move common util methods to dedicated class
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / test / java / org / opendaylight / mdsal / binding / java / api / generator / test / FileSearchUtil.java
1 /*
2  * Copyright (c) 2020 Pantheon Technologies, s.r.o.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.mdsal.binding.java.api.generator.test;
9
10 import java.io.File;
11 import java.io.FileNotFoundException;
12 import java.util.HashMap;
13 import java.util.Map;
14 import java.util.Scanner;
15
16 final class FileSearchUtil {
17     private FileSearchUtil() {
18         // Hidden on purpose
19     }
20
21     static boolean findInFile(final File file, final String searchText) throws FileNotFoundException {
22         try (Scanner scanner = new Scanner(file)) {
23             while (scanner.hasNextLine()) {
24                 final String nextLine = scanner.nextLine();
25                 if (nextLine.contains(searchText)) {
26                     return true;
27                 }
28             }
29         }
30         return false;
31     }
32
33     static Map<String, File> getFiles(final File path) {
34         return getFiles(path, new HashMap<>());
35     }
36
37     private static Map<String, File> getFiles(final File path, final Map<String, File> files) {
38         final File [] dirFiles = path.listFiles();
39         for (File file : dirFiles) {
40             if (file.isDirectory()) {
41                 return getFiles(file, files);
42             }
43
44             files.put(file.getName(), file);
45         }
46         return files;
47     }
48 }