ClasspathCrawler.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.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.  * Gzip-compressed files are supported.
  45.  * </p>
  46.  * <p>
  47.  * Zip archives entries are supported recursively.
  48.  * </p>
  49.  * <p>
  50.  * This is a simple application of the <code>visitor</code> design pattern for
  51.  * list browsing.
  52.  * </p>
  53.  * @see DataProvidersManager
  54.  * @author Luc Maisonobe
  55.  */
  56. public class ClasspathCrawler implements DataProvider {

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

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

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

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

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

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

  83.                 final String convertedName = name.replace('\\', '/');
  84.                 final InputStream stream = classLoader.getResourceAsStream(convertedName);
  85.                 if (stream == null) {
  86.                     throw new OrekitException(OrekitMessages.UNABLE_TO_FIND_RESOURCE, name);
  87.                 }

  88.                 listElements.add(convertedName);
  89.                 try {
  90.                     stream.close();
  91.                 } catch (IOException exc) {
  92.                     // ignore this error
  93.                 }
  94.             }
  95.         }

  96.     }

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

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

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

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

  109.                         } else {

  110.                             // apply all registered filters
  111.                             NamedData data = new NamedData(name, () -> classLoader.getResourceAsStream(name));
  112.                             data = DataProvidersManager.getInstance().applyAllFilters(data);

  113.                             if (supported.matcher(data.getName()).matches()) {
  114.                                 // visit the current file
  115.                                 try (InputStream input = data.getStreamOpener().openStream()) {
  116.                                     final URI uri = classLoader.getResource(name).toURI();
  117.                                     visitor.loadData(input, uri.toString());
  118.                                     loaded = true;
  119.                                 }

  120.                             }

  121.                         }
  122.                     }

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

  132.             if (!loaded && delayedException != null) {
  133.                 throw delayedException;
  134.             }

  135.             return loaded;

  136.         } catch (IOException ioe) {
  137.             throw new OrekitException(ioe, new DummyLocalizable(ioe.getMessage()));
  138.         } catch (ParseException pe) {
  139.             throw new OrekitException(pe, new DummyLocalizable(pe.getMessage()));
  140.         }

  141.     }

  142. }