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.lang.reflect.Array;
033import java.util.HashMap;
034import java.util.LinkedList;
035
036import javax.xml.stream.XMLStreamException;
037
038public class GEXFAttValues implements GEXFElement {
039        GEXF root;
040        HashMap<Integer, LinkedList<GEXFAttValue>> values;
041
042        public GEXFAttValues(GEXF root) {
043                this.root = root;
044                this.values = new HashMap<Integer, LinkedList<GEXFAttValue>>();
045        }
046
047        public void attributeUpdated(GEXFAttribute decl, Object value) {
048                if (!values.containsKey(decl.id))
049                        values.put(decl.id, new LinkedList<GEXFAttValue>());
050
051                LinkedList<GEXFAttValue> attr = values.get(decl.id);
052
053                if (value != null) {
054                        if (attr.size() > 0) {
055                                if (attr.getLast().start == root.step)
056                                        attr.removeLast();
057                                else
058                                        attr.getLast().end = root.step;
059                        }
060
061                        GEXFAttValue av = new GEXFAttValue(root, Integer.toString(decl.id),
062                                        formatValue(value));
063                        attr.add(av);
064                } else {
065                        if (attr.size() > 0)
066                                attr.getLast().end = root.step;
067                }
068        }
069
070        String formatValue(Object o) {
071                if (o == null)
072                        return "<null>";
073
074                if (o.getClass().isArray()) {
075                        StringBuilder buffer = new StringBuilder();
076
077                        for (int i = 0; i < Array.getLength(o); i++) {
078                                Object ochild = Array.get(o, i);
079
080                                if (i > 0)
081                                        buffer.append("|");
082
083                                if (ochild != null)
084                                        buffer.append(ochild.toString());
085                        }
086
087                        o = buffer;
088                }
089
090                return o.toString();
091        }
092
093        /*
094         * (non-Javadoc)
095         * 
096         * @see
097         * org.graphstream.stream.file.gexf.GEXFElement#export(org.graphstream.stream
098         * .file.gexf.SmartXMLWriter)
099         */
100        public void export(SmartXMLWriter stream) throws XMLStreamException {
101                if (values.size() == 0)
102                        return;
103
104                stream.startElement("attvalues");
105
106                for (LinkedList<GEXFAttValue> attrValues : values.values()) {
107                        for (int i = 0; i < attrValues.size(); i++)
108                                attrValues.get(i).export(stream);
109                }
110
111                stream.endElement(); // ATTVALUES
112        }
113}