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.graph; 033 034/** 035 * An interface aimed at dynamically creating graph objects based on a class 036 * name. 037 * 038 * @since september 2007 039 */ 040public class GraphFactory { 041 /** 042 * Create a new instance of graph. 043 */ 044 public GraphFactory() { 045 } 046 047 /** 048 * Instantiate a new graph from the given class name. 049 * 050 * @return A graph instance or null if the graph class was not found. 051 */ 052 public Graph newInstance(String id, String graphClass) { 053 try { 054 String completeGraphClass; 055 if (graphClass.split("[.]").length < 2) { 056 completeGraphClass = "org.graphstream.graph.implementations." 057 + graphClass; 058 } else { 059 completeGraphClass = graphClass; 060 } 061 // Graph res = (Graph) Class.forName( completeGraphClass 062 // ).newInstance(); 063 // res.setId( id ); 064 Class<?> clazz = Class.forName(completeGraphClass); 065 Graph res = (Graph) clazz.getConstructor(String.class).newInstance( 066 id); 067 return res; 068 } catch (InstantiationException e) { 069 System.out 070 .println("GraphFactory newInstance InstantiationException : " 071 + e.getMessage()); 072 } catch (ClassNotFoundException e) { 073 System.out 074 .println("GraphFactory newInstance ClassNotFoundException : " 075 + e.getMessage()); 076 } catch (IllegalAccessException e) { 077 System.out 078 .println("GraphFactory newInstance IllegalAccessException : " 079 + e.getMessage()); 080 } catch (NoSuchMethodException e) { 081 System.out 082 .println("GraphFactory newInstance NoSuchMethodException : " 083 + e.getMessage()); 084 } catch (java.lang.reflect.InvocationTargetException e) { 085 System.out 086 .println("GraphFactory newInstance InvocationTargetException : " 087 + e.getMessage()); 088 } 089 090 return null; 091 } 092}