All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.graphstream.stream.file.gexf.GEXFAttributes Maven / Gradle / Ivy

/*
 * This file is part of GraphStream .
 * 
 * GraphStream is a library whose purpose is to handle static or dynamic
 * graph, create them from scratch, file or any source and display them.
 * 
 * This program is free software distributed under the terms of two licenses, the
 * CeCILL-C license that fits European law, and the GNU Lesser General Public
 * License. You can  use, modify and/ or redistribute the software under the terms
 * of the CeCILL-C license as circulated by CEA, CNRS and INRIA at the following
 * URL  or under the terms of the GNU LGPL as published by
 * the Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see .
 * 
 * The fact that you are presently reading this means that you have had
 * knowledge of the CeCILL-C and LGPL licenses and that you accept their terms.
 */

/**
 * @since 2013-09-18
 * 
 * @author Guilhelm Savin 
 * @author Hicham Brahimi 
 */
package org.graphstream.stream.file.gexf;

import java.net.URI;
import java.net.URL;
import java.util.Collection;
import java.util.HashMap;

import javax.xml.stream.XMLStreamException;

import org.graphstream.stream.AttributeSink;

public class GEXFAttributes implements GEXFElement, AttributeSink {
	GEXF root;

	ClassType type;
	Mode mode;

	HashMap attributes;

	public GEXFAttributes(GEXF root, ClassType type) {
		this.root = root;

		this.type = type;
		this.mode = Mode.STATIC;
		this.attributes = new HashMap();

		root.addAttributeSink(this);
	}

	protected void checkAttribute(String key, Object value) {
		AttrType type = detectType(value);

		if (!attributes.containsKey(key))
			attributes.put(key, new GEXFAttribute(root, key, type));
		else {
			GEXFAttribute a = attributes.get(key);

			if (a.type != type && value != null)
				a.type = AttrType.STRING;
		}
	}

	protected AttrType detectType(Object value) {
		if (value == null)
			return AttrType.STRING;

		if (value instanceof Integer || value instanceof Short)
			return AttrType.INTEGER;
		else if (value instanceof Long)
			return AttrType.LONG;
		else if (value instanceof Float)
			return AttrType.FLOAT;
		else if (value instanceof Double)
			return AttrType.DOUBLE;
		else if (value instanceof Boolean)
			return AttrType.BOOLEAN;
		else if (value instanceof URL || value instanceof URI)
			return AttrType.ANYURI;
		else if (value.getClass().isArray() || value instanceof Collection)
			return AttrType.LISTSTRING;

		return AttrType.STRING;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.graphstream.stream.file.gexf.GEXFElement#export(org.graphstream.stream
	 * .file.gexf.SmartXMLWriter)
	 */
	public void export(SmartXMLWriter stream) throws XMLStreamException {
		if (attributes.size() == 0)
			return;

		stream.startElement("attributes");
		stream.stream.writeAttribute("class", type.qname);

		for (GEXFAttribute attribute : attributes.values())
			attribute.export(stream);

		stream.endElement(); // ATTRIBUTES
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.graphstream.stream.AttributeSink#nodeAttributeAdded(java.lang.String,
	 * long, java.lang.String, java.lang.String, java.lang.Object)
	 */
	public void nodeAttributeAdded(String sourceId, long timeId, String nodeId, String attribute, Object value) {
		if (type == ClassType.NODE)
			checkAttribute(attribute, value);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.graphstream.stream.AttributeSink#nodeAttributeChanged(java.lang.String ,
	 * long, java.lang.String, java.lang.String, java.lang.Object, java.lang.Object)
	 */
	public void nodeAttributeChanged(String sourceId, long timeId, String nodeId, String attribute, Object oldValue,
			Object newValue) {
		if (type == ClassType.NODE)
			checkAttribute(attribute, newValue);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.graphstream.stream.AttributeSink#edgeAttributeAdded(java.lang.String,
	 * long, java.lang.String, java.lang.String, java.lang.Object)
	 */
	public void edgeAttributeAdded(String sourceId, long timeId, String edgeId, String attribute, Object value) {
		if (type == ClassType.EDGE)
			checkAttribute(attribute, value);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.graphstream.stream.AttributeSink#edgeAttributeChanged(java.lang.String ,
	 * long, java.lang.String, java.lang.String, java.lang.Object, java.lang.Object)
	 */
	public void edgeAttributeChanged(String sourceId, long timeId, String edgeId, String attribute, Object oldValue,
			Object newValue) {
		if (type == ClassType.EDGE)
			checkAttribute(attribute, newValue);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.graphstream.stream.AttributeSink#nodeAttributeRemoved(java.lang.String ,
	 * long, java.lang.String, java.lang.String)
	 */
	public void nodeAttributeRemoved(String sourceId, long timeId, String nodeId, String attribute) {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.graphstream.stream.AttributeSink#graphAttributeAdded(java.lang.String ,
	 * long, java.lang.String, java.lang.Object)
	 */
	public void graphAttributeAdded(String sourceId, long timeId, String attribute, Object value) {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.graphstream.stream.AttributeSink#graphAttributeChanged(java.lang.
	 * String, long, java.lang.String, java.lang.Object, java.lang.Object)
	 */
	public void graphAttributeChanged(String sourceId, long timeId, String attribute, Object oldValue,
			Object newValue) {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.graphstream.stream.AttributeSink#graphAttributeRemoved(java.lang.
	 * String, long, java.lang.String)
	 */
	public void graphAttributeRemoved(String sourceId, long timeId, String attribute) {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.graphstream.stream.AttributeSink#edgeAttributeRemoved(java.lang.String ,
	 * long, java.lang.String, java.lang.String)
	 */
	public void edgeAttributeRemoved(String sourceId, long timeId, String edgeId, String attribute) {
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy