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;
005    
006    import graphlab.graph.graph.GraphModel;
007    import graphlab.graph.io.GraphML;
008    import graphlab.plugins.main.saveload.core.GraphIOException;
009    import graphlab.plugins.main.saveload.core.extension.GraphWriterExtension;
010    
011    import java.io.File;
012    import java.io.FileWriter;
013    import java.io.IOException;
014    
015    /**
016     * Saves a graph file to GraphML file format
017     * @author Reza Mohammadi, Azin Azadi
018     */
019    public class Save implements GraphWriterExtension {
020    
021        /**
022         * saves g in file as a GraphML
023         *
024         * @param g
025         * @param file
026         * @throws IOException
027         */
028        public static void saveGraphML(GraphModel g, File file) throws IOException {
029            FileWriter output = null;
030            output = new FileWriter(file);
031            output.write(""
032                    + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
033                    + "<!DOCTYPE graphml SYSTEM \"graphml.dtd\">\n"
034                    + "<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\"\n"
035                    + "    xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
036                    + "    xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns\n"
037                    + "     http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\">\n");
038            output.write(GraphML.graph2GraphML(g));
039            output.write("</graphml>");
040            output.close();
041        }
042    
043        public String getName() {
044            return "GraphML";
045        }
046    
047        public String getExtension() {
048            return "xml";
049        }
050    
051        public void write(File file, GraphModel graph) throws GraphIOException {
052            try {
053                saveGraphML(graph, file);
054            } catch (IOException e) {
055                throw new GraphIOException(e.getMessage());
056            }
057        }
058    
059        public String getDescription() {
060            return "GraphML Standard File Format for Graphs (Save)";
061        }
062    }