DirectoryCrawler.java

  1. /* Copyright 2002-2019 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (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.Comparator;
  25. import java.util.regex.Pattern;

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


  29. /**  Provider for data files stored in a directories tree on filesystem.

  30.  * <p>
  31.  * This class handles data files recursively starting from a root directories
  32.  * tree. The organization of files in the directories is free. There may be
  33.  * sub-directories to any level. All sub-directories are browsed and all terminal
  34.  * files are checked for loading.
  35.  * </p>
  36.  * <p>
  37.  * Gzip-compressed files are supported.
  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, final DataLoader visitor) {
  63.         try {
  64.             return feed(supported, visitor, root);
  65.         } catch (IOException ioe) {
  66.             throw new OrekitException(ioe, new DummyLocalizable(ioe.getMessage()));
  67.         } catch (ParseException pe) {
  68.             throw new OrekitException(pe, new DummyLocalizable(pe.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 directory current directory
  75.      * @return true if something has been loaded
  76.      * @exception IOException if data cannot be read
  77.      * @exception ParseException if data cannot be read
  78.      */
  79.     private boolean feed(final Pattern supported, final DataLoader visitor, final File directory)
  80.         throws IOException, ParseException {

  81.         // search in current directory
  82.         final File[] list = directory.listFiles();
  83.         Arrays.sort(list, new Comparator<File>() {
  84.             @Override
  85.             public int compare(final File o1, final File o2) {
  86.                 return o1.compareTo(o2);
  87.             }
  88.         });

  89.         OrekitException delayedException = null;
  90.         boolean loaded = false;
  91.         for (int i = 0; i < list.length; ++i) {
  92.             try {
  93.                 if (visitor.stillAcceptsData()) {
  94.                     final File file = list[i];
  95.                     if (file.isDirectory()) {

  96.                         // recurse in the sub-directory
  97.                         loaded = feed(supported, visitor, file) || loaded;

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

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

  102.                     } else {

  103.                         // apply all registered filters
  104.                         NamedData data = new NamedData(file.getName(), () -> new FileInputStream(file));
  105.                         data = DataProvidersManager.getInstance().applyAllFilters(data);

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

  113.                     }
  114.                 }
  115.             } catch (OrekitException oe) {
  116.                 delayedException = oe;
  117.             }

  118.         }

  119.         if (!loaded && delayedException != null) {
  120.             throw delayedException;
  121.         }

  122.         return loaded;

  123.     }

  124. }