DirectoryCrawler.java

  1. /* Copyright 2002-2025 CS GROUP
  2.  * Licensed to CS GROUP (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.orekit.data;

  18. import java.io.File;
  19. import java.io.FileInputStream;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.text.ParseException;
  23. import java.util.Arrays;
  24. import java.util.regex.Pattern;

  25. import org.hipparchus.exception.DummyLocalizable;
  26. import org.orekit.errors.OrekitException;
  27. import org.orekit.errors.OrekitMessages;


  28. /**  Provider for data files stored in a directories tree on filesystem.
  29.  * <p>
  30.  * This class handles data files recursively starting from a root directories
  31.  * tree. The organization of files in the directories is free. There may be
  32.  * sub-directories to any level. All sub-directories are browsed and all terminal
  33.  * files are checked for loading.
  34.  * </p>
  35.  * <p>
  36.  * All {@link FiltersManager#addFilter(DataFilter) registered}
  37.  * {@link DataFilter filters} are applied.
  38.  * </p>
  39.  * <p>
  40.  * Zip archives entries are supported recursively.
  41.  * </p>
  42.  * <p>
  43.  * This is a simple application of the <code>visitor</code> design pattern for
  44.  * directory hierarchy crawling.
  45.  * </p>
  46.  * @see DataProvidersManager
  47.  * @author Luc Maisonobe
  48.  */
  49. public class DirectoryCrawler implements DataProvider {

  50.     /** Root directory. */
  51.     private final File root;

  52.     /** Build a data files crawler.
  53.      * @param root root of the directories tree (must be a directory)
  54.      */
  55.     public DirectoryCrawler(final File root) {
  56.         if (!root.isDirectory()) {
  57.             throw new OrekitException(OrekitMessages.NOT_A_DIRECTORY, root.getAbsolutePath());
  58.         }
  59.         this.root = root;
  60.     }

  61.     /** {@inheritDoc} */
  62.     public boolean feed(final Pattern supported,
  63.                         final DataLoader visitor,
  64.                         final DataProvidersManager manager) {
  65.         try {
  66.             return feed(supported, visitor, manager, root);
  67.         } catch (IOException | ParseException ioe) {
  68.             throw new OrekitException(ioe, new DummyLocalizable(ioe.getMessage()));
  69.         }
  70.     }

  71.     /** Feed a data file loader by browsing a directory hierarchy.
  72.      * @param supported pattern for file names supported by the visitor
  73.      * @param visitor data file visitor to feed
  74.      * @param manager with the filters to apply.
  75.      * @param directory current directory
  76.      * @return true if something has been loaded
  77.      * @exception IOException if data cannot be read
  78.      * @exception ParseException if data cannot be read
  79.      */
  80.     private boolean feed(final Pattern supported,
  81.                          final DataLoader visitor,
  82.                          final DataProvidersManager manager,
  83.                          final File directory)
  84.         throws IOException, ParseException {

  85.         // search in current directory
  86.         final File[] list = directory.listFiles();
  87.         if (list == null) {
  88.             // notify about race condition if directory is removed by another program
  89.             throw new OrekitException(OrekitMessages.NOT_A_DIRECTORY, directory.getAbsolutePath());
  90.         }
  91.         Arrays.sort(list, File::compareTo);

  92.         OrekitException delayedException = null;
  93.         boolean loaded = false;
  94.         for (final File file : list) {
  95.             try {
  96.                 if (visitor.stillAcceptsData()) {
  97.                     if (file.isDirectory()) {

  98.                         // recurse in the sub-directory
  99.                         loaded = feed(supported, visitor, manager, file) || loaded;

  100.                     } else if (ZIP_ARCHIVE_PATTERN.matcher(file.getName()).matches()) {

  101.                         // browse inside the zip/jar file
  102.                         final DataProvider zipProvider = new ZipJarCrawler(file);
  103.                         loaded = zipProvider.feed(supported, visitor, manager) || loaded;

  104.                     } else {

  105.                         // apply all registered filters
  106.                         DataSource data = new DataSource(file.getName(), () -> new FileInputStream(file));
  107.                         data = manager.getFiltersManager().applyRelevantFilters(data);

  108.                         if (supported.matcher(data.getName()).matches()) {
  109.                             // visit the current file
  110.                             try (InputStream input = data.getOpener().openStreamOnce()) {
  111.                                 visitor.loadData(input, file.getPath());
  112.                                 loaded = true;
  113.                             }
  114.                         }

  115.                     }
  116.                 }
  117.             } catch (OrekitException oe) {
  118.                 delayedException = oe;
  119.             }

  120.         }

  121.         if (!loaded && delayedException != null) {
  122.             throw delayedException;
  123.         }

  124.         return loaded;

  125.     }

  126. }