001package conexp.fx.core.util;
002
003/*
004 * #%L
005 * Concept Explorer FX
006 * %%
007 * Copyright (C) 2010 - 2019 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.Arrays;
029import java.util.Iterator;
030import java.util.Optional;
031import java.util.function.Predicate;
032import java.util.stream.Collectors;
033
034import conexp.fx.core.collections.Pair;
035import javafx.stage.FileChooser.ExtensionFilter;
036
037public enum FileFormat {
038  ANY("All Files", "*"),
039  CXT("Burmeister Format (Only Context)", "cxt"),
040  CEX("ConExp Format (Only Context)", "cex"),
041  CFX("ConExpFX Format (Context & Lattice)", "cfx"),
042  CSVB("Comma Separated Values (pairs)", "csv"),
043  CSVT("Comma Separated Values (triples)", "csv"),
044  RDF("RDF Format (Graph Data)", "rdf", "rdfs", "owl", "xml"),
045  NT("N-Triples", "nt"),
046  N3("N3", "n3"),
047  TTL("Turtle", "ttl"),
048  TRIX("TriX", "xml", "trix"),
049  TRIG("TriG", "trig"),
050  BRDF("Binary RDF", "brf"),
051  NQUADS("N-Quads", "nq"),
052  JSONLD("JSON-LD", "jsonld"),
053  RDFJSON("RDF-JSON", "rj"),
054  RDFA("RDFa", "xhtml"),
055  HTML("Hypertext Markup Language (Only Context)", "html"),
056  PDF("Portable Document Format (Only Lattice)", "pdf"),
057  PNG("Portable Network Graphics (Only Lattice)", "png"),
058  SVG("Scalable Vector Graphics (Only Lattice)", "svg"),
059  TEX("Ganter's fca.sty TeX Format (Context & Lattice)", "tex");
060
061  public final String          title;
062  public final String[]        suffix;
063  public final ExtensionFilter extensionFilter;
064
065  private FileFormat(final String title, final String... suffix) {
066    this.title = title;
067    this.suffix = suffix;
068    String suffixes = "";
069    for (String s : suffix)
070      suffixes += ", *." + s;
071    this.extensionFilter = new ExtensionFilter(
072        title + suffixes,
073        Arrays.asList(suffix).parallelStream().map(s -> "*." + s).collect(Collectors.toList()));
074  }
075
076  @Override
077  public String toString() {
078    String suffixes = "";
079    final Iterator<String> it = Arrays.asList(suffix).iterator();
080    if (it.hasNext())
081      suffixes += "*." + it.next();
082    while (it.hasNext())
083      suffixes += ", *." + it.next();
084    return title + " [" + suffixes + "]";
085  }
086
087  public static final Pair<File, FileFormat> of(final File file) {
088    final String suffix = file.getName().substring(file.getName().lastIndexOf(".") + 1);
089    if (suffix.toLowerCase().equals("csv")) {
090      try {
091        final Optional<String> firstNonEmptyLine = Files
092            .lines(file.toPath())
093            .map(String::trim)
094            .filter(((Predicate<String>) String::isEmpty).negate())
095            .findAny();
096        if (firstNonEmptyLine.isPresent() && Strings.countOccurences(firstNonEmptyLine.get(), ";") < 2)
097          return Pair.of(file, CSVB);
098        else
099          return Pair.of(file, CSVT);
100      } catch (IOException e) {
101        throw new RuntimeException(e);
102      }
103    }
104    for (FileFormat ff : FileFormat.values())
105      if (ff != ANY)
106        for (String suf : ff.suffix)
107          if (suf.equals(suffix))
108            return Pair.of(file, ff);
109    return Pair.of(file, ANY);
110  }
111
112  public static final Pair<File, FileFormat> of(final File file, final FileFormat... fileFormats) {
113    final String suffix = file.getName().substring(file.getName().lastIndexOf(".") + 1);
114    for (FileFormat ff : fileFormats)
115      if (ff != ANY)
116        for (String suf : ff.suffix)
117          if (suf.equals(suffix))
118            return Pair.of(file, ff);
119    return Pair.of(file, ANY);
120  }
121}