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.HashSet;
033
034import javax.xml.stream.XMLStreamException;
035
036import org.graphstream.stream.PipeBase;
037
038public class GEXF extends PipeBase implements GEXFElement {
039        public static final String XMLNS = "http://www.gexf.net/1.2draft";
040        public static final String XMLNS_XSI = "http://www.w3.org/2001/XMLSchema-instance";
041        public static final String XMLNS_SL = "http://www.gexf.net/1.2draft http://www.gexf.net/1.2draft/gexf.xsd";
042        public static final String XMLNS_VIZ = "http://www.gexf.net/1.2draft/viz";
043
044        public static final String VERSION = "1.2";
045
046        GEXFMeta meta;
047        GEXFGraph graph;
048
049        int currentAttributeIndex;
050
051        double step;
052
053        HashSet<Extension> extensions;
054
055        TimeFormat timeFormat;
056
057        public GEXF() {
058                meta = new GEXFMeta();
059                currentAttributeIndex = 0;
060                step = 0;
061                graph = new GEXFGraph(this);
062                timeFormat = TimeFormat.DOUBLE;
063
064                extensions = new HashSet<Extension>();
065                extensions.add(Extension.DATA);
066                extensions.add(Extension.DYNAMICS);
067                extensions.add(Extension.VIZ);
068        }
069
070        public TimeFormat getTimeFormat() {
071                return timeFormat;
072        }
073
074        public boolean isExtensionEnable(Extension ext) {
075                return extensions.contains(ext);
076        }
077
078        public void disable(Extension ext) {
079                extensions.remove(ext);
080        }
081
082        public void enable(Extension ext) {
083                extensions.add(ext);
084        }
085
086        /*
087         * (non-Javadoc)
088         * 
089         * @see
090         * org.graphstream.stream.file.gexf.GEXFElement#export(org.graphstream.stream
091         * .file.gexf.SmartXMLWriter)
092         */
093        public void export(SmartXMLWriter stream) throws XMLStreamException {
094                stream.startElement("gexf");
095
096                stream.stream.writeAttribute("xmlns", XMLNS);
097                stream.stream.writeAttribute("xmlns:xsi", XMLNS_XSI);
098
099                if (isExtensionEnable(Extension.VIZ))
100                        stream.stream.writeAttribute("xmlns:viz", XMLNS_VIZ);
101
102                stream.stream.writeAttribute("xsi:schemaLocation", XMLNS_SL);
103                stream.stream.writeAttribute("version", VERSION);
104
105                meta.export(stream);
106                graph.export(stream);
107
108                stream.endElement(); // GEXF
109        }
110
111        int getNewAttributeIndex() {
112                return currentAttributeIndex++;
113        }
114
115        GEXFAttribute getNodeAttribute(String key) {
116                return graph.nodesAttributes.attributes.get(key);
117        }
118
119        GEXFAttribute getEdgeAttribute(String key) {
120                return graph.edgesAttributes.attributes.get(key);
121        }
122
123        /*
124         * (non-Javadoc)
125         * 
126         * @see org.graphstream.stream.PipeBase#stepBegins(java.lang.String, long,
127         * double)
128         */
129        public void stepBegins(String sourceId, long timeId, double step) {
130                this.step = step;
131                super.stepBegins(sourceId, timeId, step);
132        }
133}