001/*
002 * Copyright 2006 - 2013
003 *     Stefan Balev     <stefan.balev@graphstream-project.org>
004 *     Julien Baudry    <julien.baudry@graphstream-project.org>
005 *     Antoine Dutot    <antoine.dutot@graphstream-project.org>
006 *     Yoann Pigné      <yoann.pigne@graphstream-project.org>
007 *     Guilhelm Savin   <guilhelm.savin@graphstream-project.org>
008 * 
009 * This file is part of GraphStream <http://graphstream-project.org>.
010 * 
011 * GraphStream is a library whose purpose is to handle static or dynamic
012 * graph, create them from scratch, file or any source and display them.
013 * 
014 * This program is free software distributed under the terms of two licenses, the
015 * CeCILL-C license that fits European law, and the GNU Lesser General Public
016 * License. You can  use, modify and/ or redistribute the software under the terms
017 * of the CeCILL-C license as circulated by CEA, CNRS and INRIA at the following
018 * URL <http://www.cecill.info> or under the terms of the GNU LGPL as published by
019 * the Free Software Foundation, either version 3 of the License, or (at your
020 * option) any later version.
021 * 
022 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
023 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
024 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
025 * 
026 * You should have received a copy of the GNU Lesser General Public License
027 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
028 * 
029 * The fact that you are presently reading this means that you have had
030 * knowledge of the CeCILL-C and LGPL licenses and that you accept their terms.
031 */
032package org.graphstream.stream.file;
033
034import java.util.concurrent.ConcurrentHashMap;
035
036/**
037 * Try to instantiate the correct writer given a graph filename.
038 * 
039 * <p>
040 * This class tries to instantiate a writer given a filename. Actually it purely
041 * tries to analyze the extension and propose the writer according to this
042 * extension.
043 * </p>
044 */
045public class FileSinkFactory {
046        private static final ConcurrentHashMap<String, Class<? extends FileSink>> ext2sink;
047
048        static {
049                ext2sink = new ConcurrentHashMap<String, Class<? extends FileSink>>();
050
051                ext2sink.put("dgs", FileSinkDGS.class);
052                ext2sink.put("dgsz", FileSinkDGS.class);
053                ext2sink.put("dgml", FileSinkDynamicGML.class);
054                ext2sink.put("gml", FileSinkGML.class);
055                ext2sink.put("dot", FileSinkDOT.class);
056                ext2sink.put("svg", FileSinkSVG2.class);
057                ext2sink.put("pgf", FileSinkTikZ.class);
058                ext2sink.put("tikz", FileSinkTikZ.class);
059                ext2sink.put("tex", FileSinkTikZ.class);
060                ext2sink.put("gexf", FileSinkGEXF.class);
061                ext2sink.put("xml", FileSinkGEXF.class);
062                ext2sink.put("png", FileSinkImages.class);
063                ext2sink.put("jpg", FileSinkImages.class);
064        }
065
066        /**
067         * Looks at the file name given and its extension and propose a file output
068         * for the format that match this extension.
069         * 
070         * @param filename
071         *            The file name where the graph will be written.
072         * @return A file sink or null.
073         */
074        public static FileSink sinkFor(String filename) {
075                if (filename.indexOf('.') > 0) {
076                        String ext = filename.substring(filename.lastIndexOf('.') + 1);
077                        ext = ext.toLowerCase();
078
079                        if (ext2sink.containsKey(ext)) {
080                                Class<? extends FileSink> fsink = ext2sink.get(ext);
081
082                                try {
083                                        return fsink.newInstance();
084                                } catch (InstantiationException e) {
085                                        e.printStackTrace();
086                                } catch (IllegalAccessException e) {
087                                        e.printStackTrace();
088                                }
089                        }
090                }
091
092                return null;
093        }
094}