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.io.IOException;
035import java.util.HashMap;
036
037import org.graphstream.graph.Edge;
038import org.graphstream.graph.Graph;
039import org.graphstream.graph.Node;
040
041public class FileSinkGraphML extends FileSinkBase {
042
043        protected void outputEndOfFile() throws IOException {
044                print("</graphml>\n");
045        }
046
047        protected void outputHeader() throws IOException {
048                print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
049                print("<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\"\n");
050                print("\t xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
051                print("\t xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns\n");
052                print("\t   http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\">\n");
053        }
054
055        private void print(String format, Object... args) throws IOException {
056                output.write(String.format(format, args));
057        }
058
059        @Override
060        protected void exportGraph(Graph g) {
061                try {
062                        int attribute = 0;
063                        HashMap<String, String> nodeAttributes = new HashMap<String, String>();
064                        HashMap<String, String> edgeAttributes = new HashMap<String, String>();
065
066                        for (Node n : g.getEachNode()) {
067                                for (String k : n.getAttributeKeySet()) {
068                                        if (!nodeAttributes.containsKey(k)) {
069                                                Object value = n.getAttribute(k);
070                                                String type;
071
072                                                if (value == null)
073                                                        continue;
074
075                                                String id = String.format("attr%04X", attribute++);
076
077                                                if (value instanceof Boolean)
078                                                        type = "boolean";
079                                                else if (value instanceof Long)
080                                                        type = "long";
081                                                else if (value instanceof Integer)
082                                                        type = "int";
083                                                else if (value instanceof Double)
084                                                        type = "double";
085                                                else if (value instanceof Float)
086                                                        type = "float";
087                                                else
088                                                        type = "string";
089
090                                                nodeAttributes.put(k, id);
091
092                                                print(
093                                                                "\t<key id=\"%s\" for=\"node\" attr.name=\"%s\" attr.type=\"%s\"/>\n",
094                                                                id, k, type);
095                                        }
096                                }
097                        }
098
099                        for (Edge n : g.getEachEdge()) {
100                                for (String k : n.getAttributeKeySet()) {
101                                        if (!edgeAttributes.containsKey(k)) {
102                                                Object value = n.getAttribute(k);
103                                                String type;
104
105                                                if (value == null)
106                                                        continue;
107
108                                                String id = String.format("attr%04X", attribute++);
109
110                                                if (value instanceof Boolean)
111                                                        type = "boolean";
112                                                else if (value instanceof Long)
113                                                        type = "long";
114                                                else if (value instanceof Integer)
115                                                        type = "int";
116                                                else if (value instanceof Double)
117                                                        type = "double";
118                                                else if (value instanceof Float)
119                                                        type = "float";
120                                                else
121                                                        type = "string";
122
123                                                edgeAttributes.put(k, id);
124                                                print(
125                                                                "\t<key id=\"%s\" for=\"edge\" attr.name=\"%s\" attr.type=\"%s\"/>\n",
126                                                                id, k, type);
127                                        }
128                                }
129                        }
130
131                        print("\t<graph id=\"%s\" edgedefault=\"undirected\">\n", g.getId());
132
133                        for (Node n : g.getEachNode()) {
134                                print("\t\t<node id=\"%s\">\n", n.getId());
135                                for (String k : n.getAttributeKeySet()) {
136                                        print("\t\t\t<data key=\"%s\">%s</data>\n", nodeAttributes
137                                                        .get(k), n.getAttribute(k).toString());
138                                }
139                                print("\t\t</node>\n");
140                        }
141                        for (Edge e : g.getEachEdge()) {
142                                print(
143                                                "\t\t<edge id=\"%s\" source=\"%s\" target=\"%s\" directed=\"%s\">\n",
144                                                e.getId(), e.getSourceNode().getId(), e.getTargetNode()
145                                                                .getId(), e.isDirected());
146                                for (String k : e.getAttributeKeySet()) {
147                                        print("\t\t\t<data key=\"%s\">%s</data>\n", edgeAttributes
148                                                        .get(k), e.getAttribute(k).toString());
149                                }
150                                print("\t\t</edge>\n");
151                        }
152                        print("\t</graph>\n");
153                } catch (IOException e) {
154                        e.printStackTrace();
155                }
156        }
157
158        public void edgeAttributeAdded(String sourceId, long timeId, String edgeId,
159                        String attribute, Object value) {
160                throw new UnsupportedOperationException();
161        }
162
163        public void edgeAttributeChanged(String sourceId, long timeId,
164                        String edgeId, String attribute, Object oldValue, Object newValue) {
165                throw new UnsupportedOperationException();
166        }
167
168        public void edgeAttributeRemoved(String sourceId, long timeId,
169                        String edgeId, String attribute) {
170                throw new UnsupportedOperationException();
171        }
172
173        public void graphAttributeAdded(String sourceId, long timeId,
174                        String attribute, Object value) {
175                throw new UnsupportedOperationException();
176        }
177
178        public void graphAttributeChanged(String sourceId, long timeId,
179                        String attribute, Object oldValue, Object newValue) {
180                throw new UnsupportedOperationException();
181        }
182
183        public void graphAttributeRemoved(String sourceId, long timeId,
184                        String attribute) {
185                throw new UnsupportedOperationException();
186        }
187
188        public void nodeAttributeAdded(String sourceId, long timeId, String nodeId,
189                        String attribute, Object value) {
190                throw new UnsupportedOperationException();
191        }
192
193        public void nodeAttributeChanged(String sourceId, long timeId,
194                        String nodeId, String attribute, Object oldValue, Object newValue) {
195                throw new UnsupportedOperationException();
196        }
197
198        public void nodeAttributeRemoved(String sourceId, long timeId,
199                        String nodeId, String attribute) {
200                throw new UnsupportedOperationException();
201        }
202
203        public void edgeAdded(String sourceId, long timeId, String edgeId,
204                        String fromNodeId, String toNodeId, boolean directed) {
205                throw new UnsupportedOperationException();
206        }
207
208        public void edgeRemoved(String sourceId, long timeId, String edgeId) {
209                throw new UnsupportedOperationException();
210        }
211
212        public void graphCleared(String sourceId, long timeId) {
213                throw new UnsupportedOperationException();
214        }
215
216        public void nodeAdded(String sourceId, long timeId, String nodeId) {
217                throw new UnsupportedOperationException();
218        }
219
220        public void nodeRemoved(String sourceId, long timeId, String nodeId) {
221                throw new UnsupportedOperationException();
222        }
223
224        public void stepBegins(String sourceId, long timeId, double step) {
225                throw new UnsupportedOperationException();
226        }
227
228}