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 java.util.HashMap;
033
034import javax.xml.stream.XMLStreamException;
035
036import org.graphstream.stream.SinkAdapter;
037
038public class GEXFEdges extends SinkAdapter implements GEXFElement {
039        GEXF root;
040        HashMap<String, GEXFEdge> edges;
041
042        public GEXFEdges(GEXF root) {
043                this.root = root;
044                this.edges = new HashMap<String, GEXFEdge>();
045
046                root.addSink(this);
047        }
048
049        /*
050         * (non-Javadoc)
051         * 
052         * @see
053         * org.graphstream.stream.file.gexf.GEXFElement#export(org.graphstream.stream
054         * .file.gexf.SmartXMLWriter)
055         */
056        public void export(SmartXMLWriter stream) throws XMLStreamException {
057                stream.startElement("edges");
058
059                for (GEXFEdge edge : edges.values())
060                        edge.export(stream);
061
062                stream.endElement(); // EDGES
063        }
064
065        /*
066         * (non-Javadoc)
067         * 
068         * @see org.graphstream.stream.SinkAdapter#edgeAdded(java.lang.String, long,
069         * java.lang.String, java.lang.String, java.lang.String, boolean)
070         */
071        public void edgeAdded(String sourceId, long timeId, String edgeId,
072                        String fromNodeId, String toNodeId, boolean directed) {
073                GEXFEdge edge = edges.get(edgeId);
074
075                if (edge == null) {
076                        edge = new GEXFEdge(root, edgeId, fromNodeId, toNodeId, directed);
077                        edges.put(edgeId, edge);
078                }
079
080                edge.spells.start();
081        }
082
083        /*
084         * (non-Javadoc)
085         * 
086         * @see org.graphstream.stream.SinkAdapter#edgeRemoved(java.lang.String,
087         * long, java.lang.String)
088         */
089        public void edgeRemoved(String sourceId, long timeId, String edgeId) {
090                GEXFEdge edge = edges.get(edgeId);
091
092                if (edge == null) {
093                        System.err.printf("edge removed but not added\n");
094                        return;
095                }
096
097                edge.spells.end();
098        }
099
100        /*
101         * (non-Javadoc)
102         * 
103         * @see org.graphstream.stream.SinkAdapter#graphCleared(java.lang.String,
104         * long)
105         */
106        public void graphCleared(String sourceId, long timeId) {
107                for (GEXFEdge edge : edges.values())
108                        edge.spells.end();
109        }
110
111        /*
112         * (non-Javadoc)
113         * 
114         * @see
115         * org.graphstream.stream.SinkAdapter#edgeAttributeAdded(java.lang.String,
116         * long, java.lang.String, java.lang.String, java.lang.Object)
117         */
118        public void edgeAttributeAdded(String sourceId, long timeId, String edgeId,
119                        String attribute, Object value) {
120                GEXFEdge edge = edges.get(edgeId);
121
122                if (("ui.label".equals(attribute) || "label".equals(attribute))
123                                && value != null)
124                        edge.label = value.toString();
125
126                if ("weight".equals("attribute") && value != null
127                                && value instanceof Number)
128                        edge.weight = ((Number) value).doubleValue();
129
130                edge.attvalues
131                                .attributeUpdated(root.getEdgeAttribute(attribute), value);
132        }
133
134        /*
135         * (non-Javadoc)
136         * 
137         * @see
138         * org.graphstream.stream.SinkAdapter#edgeAttributeChanged(java.lang.String,
139         * long, java.lang.String, java.lang.String, java.lang.Object,
140         * java.lang.Object)
141         */
142        public void edgeAttributeChanged(String sourceId, long timeId,
143                        String edgeId, String attribute, Object oldValue, Object newValue) {
144                edgeAttributeAdded(sourceId, timeId, edgeId, attribute, newValue);
145        }
146
147        /*
148         * (non-Javadoc)
149         * 
150         * @see
151         * org.graphstream.stream.SinkAdapter#edgeAttributeRemoved(java.lang.String,
152         * long, java.lang.String, java.lang.String)
153         */
154        public void edgeAttributeRemoved(String sourceId, long timeId,
155                        String edgeId, String attribute) {
156                GEXFEdge edge = edges.get(edgeId);
157                edge.attvalues.attributeUpdated(root.getNodeAttribute(attribute), null);
158        }
159}