ClasspathCrawler.java

  1. /* Copyright 2002-2018 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.      * @exception OrekitException if a list elements is not an existing resource
  70.      */
  71.     public ClasspathCrawler(final String... list) throws OrekitException {
  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.      * @exception OrekitException if a list elements is not an existing resource
  78.      */
  79.     public ClasspathCrawler(final ClassLoader classLoader, final String... list)
  80.         throws OrekitException {

  81.         listElements = new ArrayList<String>();
  82.         this.classLoader = classLoader;

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

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

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

  99.     }

  100.     /** {@inheritDoc} */
  101.     public boolean feed(final Pattern supported, final DataLoader visitor)
  102.         throws OrekitException {

  103.         try {
  104.             OrekitException delayedException = null;
  105.             boolean loaded = false;
  106.             for (final String name : listElements) {
  107.                 try {

  108.                     if (visitor.stillAcceptsData()) {
  109.                         if (ZIP_ARCHIVE_PATTERN.matcher(name).matches()) {

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

  113.                         } else {

  114.                             // apply all registered filters
  115.                             NamedData data = new NamedData(name, () -> classLoader.getResourceAsStream(name));
  116.                             data = DataProvidersManager.getInstance().applyAllFilters(data);

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

  124.                             }

  125.                         }
  126.                     }

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

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

  139.             return loaded;

  140.         } catch (IOException ioe) {
  141.             throw new OrekitException(ioe, new DummyLocalizable(ioe.getMessage()));
  142.         } catch (ParseException pe) {
  143.             throw new OrekitException(pe, new DummyLocalizable(pe.getMessage()));
  144.         }

  145.     }

  146. }