All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.fudaa.dodico.telemac.TelemacDicoManagerLoader Maven / Gradle / Ivy

There is a newer version: 2.7
Show newest version
package org.fudaa.dodico.telemac;

import com.memoire.fu.FuLog;
import org.apache.commons.lang.StringUtils;
import org.fudaa.ctulu.Pair;
import org.fudaa.dodico.dico.DicoCasFileFormat;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.*;
import java.util.*;
import java.util.stream.Stream;

public class TelemacDicoManagerLoader {
  static URI getDicoZipUri() {
    try {
      return TelemacDicoManager.class.getResource("dicos/").toURI();
    } catch (URISyntaxException e) {
      FuLog.error(e);
    }
    return null;
  }

  public Map> load() {

    Map>> versionsByName = new HashMap<>();
    final URI dicoZipPath = getDicoZipUri();
    if (dicoZipPath == null) {
      return Collections.emptyMap();
    }
    if (dicoZipPath.toString().indexOf('!') >= 0) {
      final String[] array = dicoZipPath.toString().split("!");
      try (final FileSystem fs = FileSystems.newFileSystem(URI.create(array[0]), new HashMap<>())) {
        Path path = fs.getPath(array[1]);
        collectDicoPaths(versionsByName, path);
      } catch (IOException ioe) {
        FuLog.error(ioe);
      }
    } else {
      collectDicoPaths(versionsByName, Paths.get(dicoZipPath));
    }

    Map> versions = new HashMap<>();
    versionsByName.values().forEach(p -> p.secondValue.sort(null));
    versionsByName.values().forEach(p -> versions.put(p.firstValue, p.secondValue));
    return versions;
  }

  private void collectDicoPaths(Map>> versionsByName, Path dicoPath) {
    try (Stream paths = Files.find(dicoPath, 2, (p, b) -> b.isRegularFile(), FileVisitOption.FOLLOW_LINKS)) {
      paths.forEach(path -> {
        addDicoFile(path, versionsByName);
      });
    } catch (IOException ioe) {
      FuLog.error(ioe);
    }
  }

  private void addDicoFile(Path path, Map>> versionsByName) {
    Path fileName = path.getFileName();
    if (fileName.toString().endsWith(".dico")) {
      String version = path.getParent().getFileName().toString();
      String dico = StringUtils.substringBefore(fileName.toString(), ".");
      versionsByName.putIfAbsent(dico, new Pair<>(getDicoFileFormat(dico), new ArrayList<>()));
      versionsByName.get(dico).secondValue.add(version);
    }
  }

  private TelemacDicoFileFormat getDicoFileFormat(String codeId) {
    if (Telemac2dFileFormat.getInstance().getName().equals(codeId)) {
      return Telemac2dFileFormat.getInstance();
    }
    return new TelemacDicoFileFormat(codeId);
  }

  public static void main(String[] args) throws IOException {
    new TelemacDicoManagerLoader().load();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy