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 * GraphStream is a library whose purpose is to handle static or dynamic
010 * graph, create them from scratch, file or any source and display them.
011 * 
012 * This program is free software distributed under the terms of two licenses, the
013 * CeCILL-C license that fits European law, and the GNU Lesser General Public
014 * License. You can  use, modify and/ or redistribute the software under the terms
015 * of the CeCILL-C license as circulated by CEA, CNRS and INRIA at the following
016 * URL <http://www.cecill.info> or under the terms of the GNU LGPL as published by
017 * the Free Software Foundation, either version 3 of the License, or (at your
018 * option) any later version.
019 * 
020 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
021 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
022 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
023 * 
024 * You should have received a copy of the GNU Lesser General Public License
025 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
026 * 
027 * The fact that you are presently reading this means that you have had
028 * knowledge of the CeCILL-C and LGPL licenses and that you accept their terms.
029 */
030package org.graphstream.stream.file.gexf;
031
032import javax.xml.stream.XMLStreamException;
033
034public class GEXFEdge implements GEXFElement {
035        GEXF root;
036
037        String id;
038        String label;
039        String source;
040        String target;
041        boolean directed;
042
043        double weight = Double.NaN;
044
045        GEXFAttValues attvalues;
046        GEXFSpells spells;
047
048        public GEXFEdge(GEXF root, String id, String source, String target,
049                        boolean directed) {
050                this.root = root;
051
052                this.id = id;
053                this.label = id;
054                this.source = source;
055                this.target = target;
056                this.directed = directed;
057
058                spells = new GEXFSpells(root);
059                attvalues = new GEXFAttValues(root);
060        }
061
062        /*
063         * (non-Javadoc)
064         * 
065         * @see
066         * org.graphstream.stream.file.gexf.GEXFElement#export(org.graphstream.stream
067         * .file.gexf.SmartXMLWriter)
068         */
069        public void export(SmartXMLWriter stream) throws XMLStreamException {
070                stream.startElement("edge");
071
072                stream.stream.writeAttribute("id", id);
073                stream.stream.writeAttribute("label", label);
074                stream.stream.writeAttribute("source", source);
075                stream.stream.writeAttribute("target", target);
076                stream.stream.writeAttribute("type", directed ? "directed"
077                                : "undirected");
078
079                if (!Double.isNaN(weight))
080                        stream.stream.writeAttribute("weight", Double.toString(weight));
081
082                if (root.isExtensionEnable(Extension.DYNAMICS))
083                        spells.export(stream);
084
085                attvalues.export(stream);
086
087                stream.endElement(); // EDGE
088        }
089}