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-10-04
*
* @author Guilhelm Savin
* @author Yoann Pigné
* @author Hicham Brahimi
*/
package org.graphstream.stream.file.dgs;
import java.awt.Color;
import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.LinkedList;
import org.graphstream.graph.implementations.AbstractElement.AttributeChangeEvent;
import org.graphstream.stream.SourceBase.ElementType;
import org.graphstream.stream.file.FileSourceDGS;
import org.graphstream.util.parser.ParseException;
import org.graphstream.util.parser.Parser;
// import org.graphstream.util.time.ISODateIO;
public class DGSParser implements Parser {
static enum Token {
AN, CN, DN, AE, CE, DE, CG, ST, CL, TF, EOF
}
protected static final int BUFFER_SIZE = 4096;
public static final int ARRAY_OPEN = '{';
public static final int ARRAY_CLOSE = '}';
public static final int MAP_OPEN = '[';
public static final int MAP_CLOSE = ']';
Reader reader;
int line, column;
int bufferCapacity, bufferPosition;
char[] buffer;
int[] pushback;
int pushbackOffset;
FileSourceDGS dgs;
String sourceId;
Token lastDirective;
// ISODateIO dateIO;
public DGSParser(FileSourceDGS dgs, Reader reader) {
this.dgs = dgs;
this.reader = reader;
bufferCapacity = 0;
buffer = new char[BUFFER_SIZE];
pushback = new int[10];
pushbackOffset = -1;
this.sourceId = String.format("", System.nanoTime());
// try {
// dateIO = new ISODateIO();
// } catch (Exception e) {
// e.printStackTrace();
// }
}
/*
* (non-Javadoc)
*
* @see org.graphstream.util.parser.Parser#close()
*/
public void close() throws IOException {
reader.close();
}
/*
* (non-Javadoc)
*
* @see org.graphstream.util.parser.Parser#open()
*/
public void open() throws IOException, ParseException {
header();
}
/*
* (non-Javadoc)
*
* @see org.graphstream.util.parser.Parser#all()
*/
public void all() throws IOException, ParseException {
header();
while (next())
;
}
protected int nextChar() throws IOException {
int c;
if (pushbackOffset >= 0)
return pushback[pushbackOffset--];
if (bufferCapacity == 0 || bufferPosition >= bufferCapacity) {
bufferCapacity = reader.read(buffer, 0, BUFFER_SIZE);
bufferPosition = 0;
}
if (bufferCapacity <= 0)
return -1;
c = buffer[bufferPosition++];
//
// Handle special EOL
// - LF
// - CR
// - CR+LF
//
if (c == '\r') {
if (bufferPosition < bufferCapacity) {
if (buffer[bufferPosition] == '\n')
bufferPosition++;
} else {
c = nextChar();
if (c != '\n')
pushback(c);
}
c = '\n';
}
if (c == '\n') {
line++;
column = 0;
} else
column++;
return c;
}
protected void pushback(int c) throws IOException {
if (c < 0)
return;
if (pushbackOffset + 1 >= pushback.length)
throw new IOException("pushback buffer overflow");
pushback[++pushbackOffset] = c;
}
protected void skipLine() throws IOException {
int c;
while ((c = nextChar()) != '\n' && c >= 0)
;
}
protected void skipWhitespaces() throws IOException {
int c;
while ((c = nextChar()) == ' ' || c == '\t')
;
pushback(c);
}
protected void header() throws IOException, ParseException {
int[] dgs = new int[6];
for (int i = 0; i < 6; i++)
dgs[i] = nextChar();
if (dgs[0] != 'D' || dgs[1] != 'G' || dgs[2] != 'S')
throw parseException(
String.format("bad magic header, 'DGS' expected, got '%c%c%c'", dgs[0], dgs[1], dgs[2]));
if (dgs[3] != '0' || dgs[4] != '0' || dgs[5] < '0' || dgs[5] > '5')
throw parseException(String.format("bad version \"%c%c%c\"", dgs[0], dgs[1], dgs[2]));
if (nextChar() != '\n')
throw parseException("end-of-line is missing");
skipLine();
}
/*
* (non-Javadoc)
*
* @see org.graphstream.util.parser.Parser#next()
*/
public boolean next() throws IOException, ParseException {
int c;
String nodeId;
String edgeId, source, target;
lastDirective = directive();
switch (lastDirective) {
case AN:
nodeId = id();
dgs.sendNodeAdded(sourceId, nodeId);
attributes(ElementType.NODE, nodeId);
break;
case CN:
nodeId = id();
attributes(ElementType.NODE, nodeId);
break;
case DN:
nodeId = id();
dgs.sendNodeRemoved(sourceId, nodeId);
break;
case AE:
edgeId = id();
source = id();
skipWhitespaces();
c = nextChar();
if (c != '<' && c != '>')
pushback(c);
target = id();
switch (c) {
case '>':
dgs.sendEdgeAdded(sourceId, edgeId, source, target, true);
break;
case '<':
dgs.sendEdgeAdded(sourceId, edgeId, target, source, true);
break;
default:
dgs.sendEdgeAdded(sourceId, edgeId, source, target, false);
break;
}
attributes(ElementType.EDGE, edgeId);
break;
case CE:
edgeId = id();
attributes(ElementType.EDGE, edgeId);
break;
case DE:
edgeId = id();
dgs.sendEdgeRemoved(sourceId, edgeId);
break;
case CG:
attributes(ElementType.GRAPH, null);
break;
case ST:
// TODO release 1.2 : read timestamp
// Version for 1.2 :
// --------------------------------
// long step;
// step = timestamp();
// sendStepBegins(sourceId, ste);
double step;
step = Double.valueOf(id());
dgs.sendStepBegins(sourceId, step);
break;
case CL:
dgs.sendGraphCleared(sourceId);
break;
case TF:
// TODO for release 1.2
// String tf;
// tf = string();
// try {
// dateIO.setFormat(tf);
// } catch (Exception e) {
// throw parseException("invalid time format \"%s\"", tf);
// }
break;
case EOF:
return false;
}
skipWhitespaces();
c = nextChar();
if (c == '#') {
skipLine();
return true;
}
if (c < 0)
return false;
if (c != '\n')
throw parseException("eol expected, got '%c'", c);
return true;
}
public boolean nextStep() throws IOException, ParseException {
boolean r;
Token next;
do {
r = next();
next = directive();
if (next != Token.EOF) {
pushback(next.name().charAt(1));
pushback(next.name().charAt(0));
}
} while (next != Token.ST && next != Token.EOF);
return r;
}
protected void attributes(ElementType type, String id) throws IOException, ParseException {
int c;
skipWhitespaces();
while ((c = nextChar()) != '\n' && c != '#' && c >= 0) {
pushback(c);
attribute(type, id);
skipWhitespaces();
}
pushback(c);
}
protected void attribute(ElementType type, String elementId) throws IOException, ParseException {
String key;
Object value = null;
int c;
AttributeChangeEvent ch = AttributeChangeEvent.CHANGE;
skipWhitespaces();
c = nextChar();
if (c == '+')
ch = AttributeChangeEvent.ADD;
else if (c == '-')
ch = AttributeChangeEvent.REMOVE;
else
pushback(c);
key = id();
if (key == null)
throw parseException("attribute key expected");
if (ch != AttributeChangeEvent.REMOVE) {
skipWhitespaces();
c = nextChar();
if (c == '=' || c == ':') {
skipWhitespaces();
value = value(true);
} else {
value = Boolean.TRUE;
pushback(c);
}
}
dgs.sendAttributeChangedEvent(sourceId, elementId, type, key, ch, null, value);
}
protected Object value(boolean array) throws IOException, ParseException {
int c;
LinkedList