001package conexp.fx.core.util;
002
003/*
004 * #%L
005 * Concept Explorer FX
006 * %%
007 * Copyright (C) 2010 - 2023 Francesco Kriegel
008 * %%
009 * This program is free software: you can redistribute it and/or modify
010 * it under the terms of the GNU General Public License as
011 * published by the Free Software Foundation, either version 3 of the
012 * License, or (at your option) any later version.
013 * 
014 * This program is distributed in the hope that it will be useful,
015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017 * GNU General Public License for more details.
018 * 
019 * You should have received a copy of the GNU General Public
020 * License along with this program.  If not, see
021 * <http://www.gnu.org/licenses/gpl-3.0.html>.
022 * #L%
023 */
024
025import java.io.File;
026import java.io.IOException;
027import java.nio.file.Files;
028import java.util.*;
029import java.util.function.Predicate;
030import java.util.stream.Collectors;
031
032import conexp.fx.core.collections.Pair;
033import javafx.stage.FileChooser.ExtensionFilter;
034
035public enum FileFormat {
036  ANY("All Files", "*"),
037  CXT("Burmeister Format (Only Context)", "cxt"),
038  CEX("ConExp Format (Only Context)", "cex"),
039  CFX("ConExpFX Format (Context & Lattice)", "cfx"),
040  CSVB("Comma Separated Values (pairs)", "csv"),
041  CSVT("Comma Separated Values (triples)", "csv"),
042  RDF("RDF Format (Graph Data)", "rdf", "rdfs", "owl", "xml"),
043  NT("N-Triples", "nt"),
044  N3("N3", "n3"),
045  TTL("Turtle", "ttl"),
046  TRIX("TriX", "xml", "trix"),
047  TRIG("TriG", "trig"),
048  BRDF("Binary RDF", "brf"),
049  NQUADS("N-Quads", "nq"),
050  JSONLD("JSON-LD", "jsonld"),
051  RDFJSON("RDF-JSON", "rj"),
052  RDFA("RDFa", "xhtml"),
053  HTML("Hypertext Markup Language (Only Context)", "html"),
054  PDF("Portable Document Format (Only Lattice)", "pdf"),
055  PNG("Portable Network Graphics (Only Lattice)", "png"),
056  SVG("Scalable Vector Graphics (Only Lattice)", "svg"),
057  TEX("Ganter's fca.sty TeX Format (Context & Lattice)", "tex");
058
059  public final String          title;
060  public final String[]        suffix;
061  public final ExtensionFilter extensionFilter;
062
063  private FileFormat(final String title, final String... suffix) {
064    this.title = title;
065    this.suffix = suffix;
066    String suffixes = "";
067    final List<String> extensions = new LinkedList<String>();
068    for (String s : suffix) {
069      suffixes += ", *." + s;
070      extensions.add("*." + s);
071    }
072//    this.extensionFilter = new ExtensionFilter(
073//        title + suffixes,
074//        Arrays.asList(suffix).parallelStream().map(s -> "*." + s).collect(Collectors.toList()));
075    this.extensionFilter = new ExtensionFilter(title + suffixes, extensions);
076  }
077
078  @Override
079  public String toString() {
080    String suffixes = "";
081    final Iterator<String> it = Arrays.asList(suffix).iterator();
082    if (it.hasNext())
083      suffixes += "*." + it.next();
084    while (it.hasNext())
085      suffixes += ", *." + it.next();
086    return title + " [" + suffixes + "]";
087  }
088
089  public static final Pair<File, FileFormat> of(final File file) {
090    final String suffix = file.getName().substring(file.getName().lastIndexOf(".") + 1);
091    if (suffix.toLowerCase().equals("csv")) {
092      try {
093        final Optional<String> firstNonEmptyLine = Files
094            .lines(file.toPath())
095            .map(String::trim)
096            .filter(((Predicate<String>) String::isEmpty).negate())
097            .findAny();
098        if (firstNonEmptyLine.isPresent() && Strings.countOccurences(firstNonEmptyLine.get(), ";") < 2)
099          return Pair.of(file, CSVB);
100        else
101          return Pair.of(file, CSVT);
102      } catch (IOException e) {
103        throw new RuntimeException(e);
104      }
105    }
106    for (FileFormat ff : FileFormat.values())
107      if (ff != ANY)
108        for (String suf : ff.suffix)
109          if (suf.equals(suffix))
110            return Pair.of(file, ff);
111    return Pair.of(file, ANY);
112  }
113
114  public static final Pair<File, FileFormat> of(final File file, final FileFormat... fileFormats) {
115    final String suffix = file.getName().substring(file.getName().lastIndexOf(".") + 1);
116    for (FileFormat ff : fileFormats)
117      if (ff != ANY)
118        for (String suf : ff.suffix)
119          if (suf.equals(suffix))
120            return Pair.of(file, ff);
121    return Pair.of(file, ANY);
122  }
123}