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    package graphlab.plugins.main.saveload.matrix;
005    
006    import graphlab.graph.graph.GraphModel;
007    import graphlab.plugins.main.saveload.SaveLoadPluginMethods;
008    import graphlab.plugins.main.saveload.core.GraphIOException;
009    import graphlab.plugins.main.saveload.core.extension.GraphReaderExtension;
010    
011    import java.io.BufferedReader;
012    import java.io.File;
013    import java.io.FileReader;
014    import java.io.IOException;
015    
016    /**
017     * @author Azin Azadi
018     */
019    public class LoadMatrix implements GraphReaderExtension {
020    
021        public boolean accepts(File file) {
022            return SaveLoadPluginMethods.getExtension(file).equals(getExtension());
023        }
024    
025        public String getName() {
026            return "Matrix";
027        }
028    
029        public String getExtension() {
030            return "mat";
031        }
032    
033        public GraphModel read(File file) throws GraphIOException {
034            try {
035                return loadMatrix(file);
036            } catch (IOException e) {
037                throw new GraphIOException(e.getMessage());
038            }
039        }
040    
041        public String getDescription() {
042            return "0-1 Matrix File Format";
043        }
044    
045        public static GraphModel loadMatrix(File selectedFile) throws IOException {
046            GraphModel g = new GraphModel();
047            FileReader in = new FileReader(selectedFile);
048            BufferedReader br = new BufferedReader(in);
049            String _, s = "";
050            while ((_ = br.readLine()) != null) s += _ + "\n";
051            Matrix.Matrix2Graph(Matrix.String2Matrix(s), g);
052            return g;
053        }
054    }
055    
056