ClasspathCrawler.java

  1. /* Copyright 2002-2024 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.IOException;
  19. import java.io.InputStream;
  20. import java.net.URI;
  21. import java.net.URISyntaxException;
  22. import java.text.ParseException;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import java.util.regex.Pattern;

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


  30. /** Provider for data files stored as resources in the classpath.
  31.  * <p>
  32.  * This class handles a list of data files or zip/jar archives located in the
  33.  * classpath. Since the classpath is not a tree structure the list elements
  34.  * cannot be whole directories recursively browsed as in {@link
  35.  * DirectoryCrawler}, they must be data files or zip/jar archives.
  36.  * </p>
  37.  * <p>
  38.  * A typical use case is to put all data files in a single zip or jar archive
  39.  * and to build an instance of this class with the single name of this zip/jar
  40.  * archive. Two different instances may be used one for user or project specific
  41.  * data and another one for system-wide or general data.
  42.  * </p>
  43.  * <p>
  44.  * All {@link FiltersManager#addFilter(DataFilter) registered}
  45.  * {@link DataFilter filters} are applied.
  46.  * </p>
  47.  * <p>
  48.  * Zip archives entries are supported recursively.
  49.  * </p>
  50.  * <p>
  51.  * This is a simple application of the <code>visitor</code> design pattern for
  52.  * list browsing.
  53.  * </p>
  54.  * @see DataProvidersManager
  55.  * @author Luc Maisonobe
  56.  */
  57. public class ClasspathCrawler implements DataProvider {

  58.     /** List elements. */
  59.     private final List<String> listElements;

  60.     /** Class loader to use. */
  61.     private final ClassLoader classLoader;

  62.     /** Build a data classpath crawler.
  63.      * <p>
  64.      * Calling this constructor has the same effect as calling
  65.      * {@link #ClasspathCrawler(ClassLoader, String...)} with
  66.      * {@code ClasspathCrawler.class.getClassLoader()} as first
  67.      * argument.
  68.      * </p>
  69.      * @param list list of data file names within the classpath
  70.      */
  71.     public ClasspathCrawler(final String... list) {
  72.         this(ClasspathCrawler.class.getClassLoader(), list);
  73.     }

  74.     /** Build a data classpath crawler.
  75.      * @param classLoader class loader to use to retrieve the resources
  76.      * @param list list of data file names within the classpath
  77.      */
  78.     public ClasspathCrawler(final ClassLoader classLoader, final String... list) {

  79.         listElements = new ArrayList<>();
  80.         this.classLoader = classLoader;

  81.         // check the resources
  82.         for (final String name : list) {
  83.             if (!"".equals(name)) {

  84.                 final String convertedName = name.replace('\\', '/');
  85.                 try (InputStream stream = classLoader.getResourceAsStream(convertedName)) {
  86.                     if (stream == null) {
  87.                         throw new OrekitException(OrekitMessages.UNABLE_TO_FIND_RESOURCE, name);
  88.                     }
  89.                     listElements.add(convertedName);
  90.                 } catch (IOException exc) {
  91.                     // ignore this error
  92.                 }

  93.             }
  94.         }

  95.     }

  96.     /** {@inheritDoc} */
  97.     public boolean feed(final Pattern supported,
  98.                         final DataLoader visitor,
  99.                         final DataProvidersManager manager) {

  100.         try {
  101.             OrekitException delayedException = null;
  102.             boolean loaded = false;
  103.             for (final String name : listElements) {
  104.                 try {

  105.                     if (visitor.stillAcceptsData()) {
  106.                         if (ZIP_ARCHIVE_PATTERN.matcher(name).matches()) {

  107.                             // browse inside the zip/jar file
  108.                             final DataProvider zipProvider = new ZipJarCrawler(name);
  109.                             loaded = zipProvider.feed(supported, visitor, manager) || loaded;

  110.                         } else {

  111.                             // match supported name against file name #618
  112.                             final String fileName = name.substring(name.lastIndexOf('/') + 1);
  113.                             DataSource data = new DataSource(fileName, () -> classLoader.getResourceAsStream(name));
  114.                             // apply all registered filters
  115.                             data = manager.getFiltersManager().applyRelevantFilters(data);

  116.                             if (supported.matcher(data.getName()).matches()) {
  117.                                 // visit the current file
  118.                                 try (InputStream input = data.getOpener().openStreamOnce()) {
  119.                                     final URI uri = classLoader.getResource(name).toURI();
  120.                                     visitor.loadData(input, uri.toString());
  121.                                     loaded = true;
  122.                                 }

  123.                             }

  124.                         }
  125.                     }

  126.                 } catch (OrekitException oe) {
  127.                     // maybe the next path component will be able to provide data
  128.                     // wait until all components have been tried
  129.                     delayedException = oe;
  130.                 } catch (URISyntaxException use) {
  131.                     // this should bever happen
  132.                     throw new OrekitException(use, LocalizedCoreFormats.SIMPLE_MESSAGE, use.getMessage());
  133.                 }
  134.             }

  135.             if (!loaded && delayedException != null) {
  136.                 throw delayedException;
  137.             }

  138.             return loaded;

  139.         } catch (IOException | ParseException ioe) {
  140.             throw new OrekitException(ioe, new DummyLocalizable(ioe.getMessage()));
  141.         }

  142.     }

  143. }