001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    
005    package graphlab.plugins.main.saveload.core.extension;
006    
007    import graphlab.platform.core.AbstractAction;
008    import graphlab.platform.core.BlackBoard;
009    import graphlab.platform.extension.ExtensionHandler;
010    import graphlab.platform.preferences.lastsettings.StorableOnExit;
011    import graphlab.platform.preferences.lastsettings.UserModifiableProperty;
012    
013    /**
014     * the plug in handler for graph Input and Output, this class loads classes that are implementing
015     * GraphReaderExtension interface,...
016     *
017     * @author azin azadi
018    
019     */
020    public class GraphIOExtensionHandler implements ExtensionHandler, StorableOnExit {
021        private boolean handlingReaders;
022        private AbstractAction a = null;
023    
024        {
025            SETTINGS.registerSetting(this, "Only Storable");
026        }
027    
028        /**
029         * @param isHandlingReaders indicates that if this Object should handle GraphReaders or GraphWriters
030         */
031        public GraphIOExtensionHandler(boolean isHandlingReaders) {
032            handlingReaders = isHandlingReaders;
033        }
034    
035        @UserModifiableProperty(displayName = "default Directory Path")
036        public static String defaultFile = ".";
037    
038        /**
039         * @param b
040         * @param ext
041         * @return null if ext doesn't implements GraphReaderExtension
042         */
043        public AbstractAction handle(BlackBoard b, Object ext) {
044            a = null;
045            if (!handlingReaders) {
046                if (ext instanceof GraphWriterExtension) {
047                    try {
048                        GraphWriterExtension gg = (GraphWriterExtension) ext;
049                        a = new GraphWriterExtensionAction(b, gg);
050                    } catch (Exception e) {
051                        e.printStackTrace();
052                    }
053                }
054            } else {
055                if (ext instanceof GraphReaderExtension) {
056                    try {
057                        GraphReaderExtension gg = (GraphReaderExtension) ext;
058                        a = new GraphReaderExtensionAction(b, gg);
059                    } catch (Exception e) {
060                        e.printStackTrace();
061                    }
062                }
063            }
064            return a;
065        }
066    }