bbc77c63443a352ad52aa6e6fd2a5ae4832c56d4
[smo/teiv.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2024 Ericsson
4  *  Modifications Copyright (C) 2024 OpenInfra Foundation Europe
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21 package org.oran.smo.yangtools.parser.input;
22
23 import java.io.File;
24 import java.util.Collections;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Locale;
28 import java.util.Objects;
29 import java.util.Set;
30
31 /**
32  * A resolver that recursively searches through a list of files and directory paths and
33  * identifies all files having any of the specified file extensions.
34  * <p/>
35  * Duplicate files will not be returned.
36  *
37  * @author Mark Hollmann
38  */
39 public class FileBasedYangInputResolver implements YangInputResolver {
40
41     public static final String FILE_EXTENSION_YANG = "yang";
42     public static final String FILE_EXTENSION_XML = "xml";
43     public static final String FILE_EXTENSION_JSON = "json";
44
45     private final List<String> fileExtensionsToConsider;
46     private final List<File> filesAndDirectoriesToConsider;
47
48     public FileBasedYangInputResolver(final List<File> filesAndDirectoriesToConsider) {
49         this.fileExtensionsToConsider = Collections.<String> emptyList();
50         this.filesAndDirectoriesToConsider = Objects.requireNonNull(filesAndDirectoriesToConsider);
51     }
52
53     public FileBasedYangInputResolver(final List<File> filesAndDirectoriesToConsider,
54             final List<String> fileExtensionsToConsider) {
55         this.filesAndDirectoriesToConsider = Objects.requireNonNull(filesAndDirectoriesToConsider);
56         this.fileExtensionsToConsider = Objects.requireNonNull(fileExtensionsToConsider);
57     }
58
59     @Override
60     public Set<YangInput> getResolvedYangInput() {
61
62         final Set<YangInput> result = new HashSet<>();
63
64         for (final File file : filesAndDirectoriesToConsider) {
65             resolveFile(result, file);
66         }
67
68         return result;
69     }
70
71     private void resolveFile(final Set<YangInput> result, final File file) {
72
73         if (file.exists()) {
74             if (file.isFile()) {
75                 filterAgainstFileExtensions(result, file);
76             } else {
77                 for (final File subFile : file.listFiles()) {
78                     resolveFile(result, subFile);
79                 }
80             }
81         }
82     }
83
84     private void filterAgainstFileExtensions(final Set<YangInput> result, final File file) {
85
86         final String fileName = file.getName();
87         final int lastIndexOf = fileName.lastIndexOf('.');
88         final String fileExtension = lastIndexOf > -1 ?
89                 fileName.substring(lastIndexOf + 1).toLowerCase(Locale.ENGLISH) :
90                 null;
91
92         if (fileExtensionsToConsider.isEmpty() || (fileExtension != null && fileExtensionsToConsider.contains(
93                 fileExtension))) {
94             result.add(new FileBasedYangInput(file));
95         }
96     }
97
98 }