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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
21 package org.oran.smo.yangtools.parser.input;
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;
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.
35 * Duplicate files will not be returned.
37 * @author Mark Hollmann
39 public class FileBasedYangInputResolver implements YangInputResolver {
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";
45 private final List<String> fileExtensionsToConsider;
46 private final List<File> filesAndDirectoriesToConsider;
48 public FileBasedYangInputResolver(final List<File> filesAndDirectoriesToConsider) {
49 this.fileExtensionsToConsider = Collections.<String> emptyList();
50 this.filesAndDirectoriesToConsider = Objects.requireNonNull(filesAndDirectoriesToConsider);
53 public FileBasedYangInputResolver(final List<File> filesAndDirectoriesToConsider,
54 final List<String> fileExtensionsToConsider) {
55 this.filesAndDirectoriesToConsider = Objects.requireNonNull(filesAndDirectoriesToConsider);
56 this.fileExtensionsToConsider = Objects.requireNonNull(fileExtensionsToConsider);
60 public Set<YangInput> getResolvedYangInput() {
62 final Set<YangInput> result = new HashSet<>();
64 for (final File file : filesAndDirectoriesToConsider) {
65 resolveFile(result, file);
71 private void resolveFile(final Set<YangInput> result, final File file) {
75 filterAgainstFileExtensions(result, file);
77 for (final File subFile : file.listFiles()) {
78 resolveFile(result, subFile);
84 private void filterAgainstFileExtensions(final Set<YangInput> result, final File file) {
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) :
92 if (fileExtensionsToConsider.isEmpty() || (fileExtension != null && fileExtensionsToConsider.contains(
94 result.add(new FileBasedYangInput(file));