Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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 2011-07-22
*
* @author Guilhelm Savin
* @author Hans Schulz
* @author Hicham Brahimi
*/
package org.graphstream.stream.file;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Stack;
import java.util.logging.Logger;
import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.Location;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.XMLEvent;
import org.graphstream.stream.SourceBase;
/**
* GraphML is a comprehensive and easy-to-use file format for graphs. It
* consists of a language core to describe the structural properties of a graph
* and a flexible extension mechanism to add application-specific data. Its main
* features include support of
*
*
directed, undirected, and mixed graphs,
*
hypergraphs,
*
hierarchical graphs,
*
graphical representations,
*
references to external data,
*
application-specific attribute data, and
*
light-weight parsers.
*
*
* Unlike many other file formats for graphs, GraphML does not use a custom
* syntax. Instead, it is based on XML and hence ideally suited as a common
* denominator for all kinds of services generating, archiving, or processing
* graphs.
*
* Source
*/
public class FileSourceGraphML extends FileSourceXML {
private static final Logger LOGGER = Logger.getLogger(FileSourceGraphML.class.getName());
public interface GraphMLConstants {
enum Balise {
GRAPHML, GRAPH, NODE, EDGE, HYPEREDGE, DESC, DATA, LOCATOR, PORT, KEY, DEFAULT
}
enum GraphAttribute {
ID, EDGEDEFAULT
}
enum LocatorAttribute {
XMLNS_XLINK, XLINK_HREF, XLINK_TYPE
}
enum NodeAttribute {
ID
}
enum EdgeAttribute {
ID, SOURCE, SOURCEPORT, TARGET, TARGETPORT, DIRECTED
}
enum DataAttribute {
KEY, ID
}
enum PortAttribute {
NAME
}
enum EndPointAttribute {
ID, NODE, PORT, TYPE
}
enum EndPointType {
IN, OUT, UNDIR
}
enum HyperEdgeAttribute {
ID
}
enum KeyAttribute {
ID, FOR, ATTR_NAME, ATTR_TYPE
}
enum KeyDomain {
GRAPHML, GRAPH, NODE, EDGE, HYPEREDGE, PORT, ENDPOINT, ALL
}
enum KeyAttrType {
BOOLEAN, INT, LONG, FLOAT, DOUBLE, STRING
}
class Key {
KeyDomain domain;
String name;
KeyAttrType type;
String def = null;
Key() {
domain = KeyDomain.ALL;
name = null;
type = KeyAttrType.STRING;
}
Object getKeyValue(String value) {
if (value == null)
return null;
switch (type) {
case STRING:
return value;
case INT:
return Integer.valueOf(value);
case LONG:
return Long.valueOf(value);
case FLOAT:
return Float.valueOf(value);
case DOUBLE:
return Double.valueOf(value);
case BOOLEAN:
return Boolean.valueOf(value);
}
return value;
}
Object getDefaultValue() {
return getKeyValue(def);
}
}
class Data {
Key key;
String id;
String value;
}
class Locator {
String href;
String xlink;
String type;
Locator() {
xlink = "http://www.w3.org/TR/2000/PR-xlink-20001220/";
type = "simple";
href = null;
}
}
class Port {
String name;
String desc;
LinkedList datas;
LinkedList ports;
Port() {
name = null;
desc = null;
datas = new LinkedList();
ports = new LinkedList();
}
}
class EndPoint {
String id;
String node;
String port;
String desc;
EndPointType type;
EndPoint() {
id = null;
node = null;
port = null;
desc = null;
type = EndPointType.UNDIR;
}
}
}
protected GraphMLParser parser;
/**
* Build a new source to parse an xml stream in GraphML format.
*/
public FileSourceGraphML() {
}
@Override
protected void afterStartDocument() throws IOException, XMLStreamException {
parser = new GraphMLParser();
parser.__graphml();
}
@Override
protected void beforeEndDocument() throws IOException, XMLStreamException {
parser = null;
}
/*
* (non-Javadoc)
*
* @see org.graphstream.stream.file.FileSource#nextEvents()
*/
public boolean nextEvents() throws IOException {
return false;
}
protected class GraphMLParser extends Parser implements GraphMLConstants {
protected HashMap keys;
protected Stack graphId;
protected int graphCounter;
public GraphMLParser() {
keys = new HashMap();
graphId = new Stack();
graphCounter = 0;
}
private Object getValue(Data data) {
return getValue(data.key, data.value);
}
private Object getValue(Key key, String value) {
switch (key.type) {
case BOOLEAN:
return Boolean.parseBoolean(value);
case INT:
return Integer.parseInt(value);
case LONG:
return Long.parseLong(value);
case FLOAT:
return Float.parseFloat(value);
case DOUBLE:
return Double.parseDouble(value);
case STRING:
return value;
}
return value;
}
private Object getDefaultValue(Key key) {
switch (key.type) {
case BOOLEAN:
return Boolean.TRUE;
case INT:
if (key.def != null)
return Integer.valueOf(key.def);
return Integer.valueOf(0);
case LONG:
if (key.def != null)
return Long.valueOf(key.def);
return Long.valueOf(0);
case FLOAT:
if (key.def != null)
return Float.valueOf(key.def);
return Float.valueOf(0.0f);
case DOUBLE:
if (key.def != null)
return Double.valueOf(key.def);
return Double.valueOf(0.0);
case STRING:
if (key.def != null)
return key.def;
return "";
}
return key.def != null ? key.def : Boolean.TRUE;
}
/**
*
*
*
*
* @throws IOException
* @throws XMLStreamException
*/
private void __graphml() throws IOException, XMLStreamException {
XMLEvent e;
e = getNextEvent();
checkValid(e, XMLEvent.START_ELEMENT, "graphml");
e = getNextEvent();
if (isEvent(e, XMLEvent.START_ELEMENT, "desc")) {
pushback(e);
__desc();
e = getNextEvent();
}
while (isEvent(e, XMLEvent.START_ELEMENT, "key")) {
pushback(e);
__key();
e = getNextEvent();
}
while (isEvent(e, XMLEvent.START_ELEMENT, "data") || isEvent(e, XMLEvent.START_ELEMENT, "graph")) {
pushback(e);
if (isEvent(e, XMLEvent.START_ELEMENT, "data")) {
__data();
} else {
__graph();
}
e = getNextEvent();
}
checkValid(e, XMLEvent.END_ELEMENT, "graphml");
}
/**
*