Skip to content

Commit

Permalink
Writer fully implemented, ready for testing/bug-fixing!
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcampanini committed Aug 3, 2010
1 parent ca035d4 commit f4a9981
Show file tree
Hide file tree
Showing 28 changed files with 561 additions and 344 deletions.
5 changes: 3 additions & 2 deletions src/main/java/com/ojn/gexf4j/core/HasNodes.java
@@ -1,11 +1,12 @@
package com.ojn.gexf4j.core;

import java.util.Map;
import java.util.List;

public interface HasNodes {

Node createNode();
Node createNode(String id);

Map<String, Node> getNodeMap();
List<Node> getNodes();
List<Edge> getAllEdges();
}
35 changes: 23 additions & 12 deletions src/main/java/com/ojn/gexf4j/core/impl/GraphImpl.java
Expand Up @@ -2,10 +2,11 @@

import static com.google.common.base.Preconditions.checkArgument;

import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.ojn.gexf4j.core.Edge;
import com.ojn.gexf4j.core.EdgeType;
import com.ojn.gexf4j.core.Graph;
import com.ojn.gexf4j.core.IDType;
Expand All @@ -24,13 +25,13 @@ public class GraphImpl extends DynamicBase<Graph> implements Graph {
private AttributeList nodeAttributeList = null;
private IDType idType = IDType.STRING;
private Mode mode = Mode.STATIC;
private Map<String, Node> nodeMap = null;
private List<Node> nodes = null;
private TimeType timeType = TimeType.DATE;

public GraphImpl() {
edgeAttributeList = new AttributeListImpl(AttributeClass.EDGE);
nodeAttributeList = new AttributeListImpl(AttributeClass.NODE);
nodeMap = new HashMap<String, Node>();
nodes = new ArrayList<Node>();
}

@Override
Expand Down Expand Up @@ -63,11 +64,6 @@ public AttributeList getNodeAttributes() {
return nodeAttributeList;
}

@Override
public Map<String, Node> getNodeMap() {
return nodeMap;
}

@Override
public TimeType getTimeType() {
return timeType;
Expand Down Expand Up @@ -106,10 +102,25 @@ public Node createNode() {
public Node createNode(String id) {
checkArgument(id != null, "ID cannot be null.");
checkArgument(!id.trim().isEmpty(), "ID cannot be empty or blank.");
checkArgument(!nodeMap.containsKey(id), "Cannot use a duplicate ID.");


Node rv = new NodeImpl(id);
nodeMap.put(id, rv);
nodes.add(rv);
return rv;
}

@Override
public List<Edge> getAllEdges() {
List<Edge> rv = new ArrayList<Edge>();

for (Node n : nodes) {
rv.addAll(n.getEdges());
}

return rv;
}

@Override
public List<Node> getNodes() {
return nodes;
}
}
24 changes: 16 additions & 8 deletions src/main/java/com/ojn/gexf4j/core/impl/NodeImpl.java
Expand Up @@ -4,9 +4,7 @@
import static com.google.common.base.Preconditions.checkState;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.ojn.gexf4j.core.Edge;
Expand All @@ -24,9 +22,9 @@ public class NodeImpl extends SliceableDatumBase<Node> implements Node {
private Position position = null;
private NodeShapeEntity shape = null;
private float size = Float.MIN_VALUE;
private List<Node> nodes = null;
private List<Edge> edges = null;
private List<Node> parentForList = null;
private Map<String, Node> nodeMap = null;

public NodeImpl() {
this(UUID.randomUUID().toString());
Expand All @@ -37,9 +35,9 @@ public NodeImpl(String id) {
checkArgument(!id.trim().isEmpty(), "ID cannot be empty or blank.");

this.id = id;
this.nodes = new ArrayList<Node>();
this.edges = new ArrayList<Edge>();
this.parentForList = new ArrayList<Node>();
this.nodeMap = new HashMap<String, Node>();
}

@Override
Expand Down Expand Up @@ -226,15 +224,25 @@ public Node createNode() {
public Node createNode(String id) {
checkArgument(id != null, "ID cannot be null.");
checkArgument(!id.trim().isEmpty(), "ID cannot be empty or blank.");
checkArgument(!nodeMap.containsKey(id), "Cannot use a duplicate ID.");

Node rv = new NodeImpl(id);
nodeMap.put(id, rv);
nodes.add(rv);
return rv;
}

@Override
public Map<String, Node> getNodeMap() {
return nodeMap;
public List<Node> getNodes() {
return nodes;
}

@Override
public List<Edge> getAllEdges() {
List<Edge> rv = new ArrayList<Edge>();

for (Node n : getNodes()) {
rv.addAll(n.getEdges());
}

return rv;
}
}
142 changes: 3 additions & 139 deletions src/main/java/com/ojn/gexf4j/core/impl/StaxGraphWriter.java
Expand Up @@ -7,22 +7,21 @@
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import com.ojn.gexf4j.core.Graph;
import com.ojn.gexf4j.core.Gexf;
import com.ojn.gexf4j.core.GexfWriter;
import com.ojn.gexf4j.core.impl.writer.GexfEntityWriter;

public class StaxGraphWriter implements GexfWriter {

@Override
public void writeToStream(Graph graph, OutputStream out) throws IOException {

public void writeToStream(Gexf gexf, OutputStream out) throws IOException {
try {
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
XMLStreamWriter writer = outputFactory.createXMLStreamWriter(out);

writer.writeStartDocument("1.0");

new GexfEntityWriter(writer, graph);
new GexfEntityWriter(writer, gexf);

writer.writeEndDocument();

Expand All @@ -33,139 +32,4 @@ public void writeToStream(Graph graph, OutputStream out) throws IOException {
throw new IOException("XML Exception: " + e.getMessage(), e);
}
}

/*
private void writeAllSlices(XMLStreamWriter writer, List<Slice> slices) throws XMLStreamException {
if (slices != null && slices.size() > 0) {
writer.writeStartElement("slices");
for (Slice s : slices) {
writeSlice(writer, s);
}
writer.writeEndElement();
}
}
private void writeSlice(XMLStreamWriter writer, Slice slice) throws XMLStreamException {
writer.writeStartElement("slice");
if (slice.getStartDate() != null) {
writer.writeAttribute("start", sdf.format(slice.getStartDate()));
}
if (slice.getEndDate() != null) {
writer.writeAttribute("end", sdf.format(slice.getEndDate()));
}
writer.writeEndElement();
}
private void writeAllAttributes(XMLStreamWriter writer, Graph graph) throws XMLStreamException {
if (graph.getAttributes().size() > 0) {
List<Attribute> attribNodes = new ArrayList<Attribute>();
List<Attribute> attribEdges = new ArrayList<Attribute>();
for (Attribute attrib : graph.getAttributes()) {
switch (attrib.getAttributeClass()) {
case EDGE:
attribEdges.add(attrib);
break;
case NODE:
attribNodes.add(attrib);
break;
}
}
// write node attributes
if (attribNodes.size() > 0) {
writer.writeStartElement("attributes");
writer.writeAttribute("class", "node");
for (Attribute attrib : attribNodes) {
writeAttribute(writer, attrib);
}
writer.writeEndElement();
}
// write edge attributes
if (attribNodes.size() > 0) {
writer.writeStartElement("attributes");
writer.writeAttribute("class", "edge");
for (Attribute attrib : attribEdges) {
writeAttribute(writer, attrib);
}
writer.writeEndElement();
}
}
}
private void writeAttribute(XMLStreamWriter writer, Attribute attrib) throws XMLStreamException {
writer.writeStartElement("attribute");
writer.writeAttribute("id", attrib.getId());
writer.writeAttribute("title", attrib.getTitle());
writer.writeAttribute("type", attrib.getAttributeType().toString().toLowerCase());
if (attrib.getDefaultValue() != null) {
writer.writeStartElement("default");
writer.writeCharacters(attrib.getDefaultValue());
writer.writeEndElement();
}
writer.writeEndElement();
}
private void writeAllEdges(XMLStreamWriter writer, Graph graph) throws XMLStreamException {
writer.writeStartElement("edges");
for (Node n : graph.getNodeMap().values()) {
for (Edge e : n.getEdges()) {
writeEdge(writer, e);
}
}
writer.writeEndElement();
}
private void writeEdge(XMLStreamWriter writer, Edge edge) throws XMLStreamException {
writer.writeStartElement("edge");
writer.writeAttribute("id", edge.getId());
writer.writeAttribute("label", edge.getLabel());
writer.writeAttribute("source", edge.getSource().getId());
writer.writeAttribute("target", edge.getTarget().getId());
writer.writeAttribute("weight", edge.getWeight() + "");
if (edge.getStartDate() != null) {
writer.writeAttribute("start", sdf.format(edge.getStartDate()));
}
if (edge.getEndDate() != null) {
writer.writeAttribute("end", sdf.format(edge.getEndDate()));
}
if (edge.getEdgeType() != EdgeType.NOTSET) {
writer.writeAttribute("type", edge.getEdgeType().toString().toLowerCase());
}
if (edge.getAttributeValues().size() > 0) {
writer.writeStartElement("attvalues");
for (AttributeValue av : edge.getAttributeValues()) {
writeAttributeValue(writer, av);
}
writer.writeEndElement();
}
writeAllSlices(writer, edge.getSlices());
writer.writeEndElement();
}
*/
}
@@ -1,14 +1,22 @@
package com.ojn.gexf4j.core.impl.writer;

import static com.google.common.base.Preconditions.*;
import static com.google.common.base.Preconditions.checkArgument;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import com.ojn.gexf4j.core.dynamic.TimeType;

public abstract class AbstractEntityWriter<T extends Object> {
protected static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
protected static TimeType writerTimeType = TimeType.DATE;

protected static String toDateString(Date d) {
return sdf.format(d);
}

protected XMLStreamWriter writer = null;
protected T entity = null;
Expand Down
Expand Up @@ -29,10 +29,7 @@ protected void writeAttributes() throws XMLStreamException {
writer.writeAttribute(
ATTRIB_VALUE,
entity.getValue());
}

@Override
protected void writeElements() throws XMLStreamException {
// do nothing

super.writeAttributes();
}
}
Expand Up @@ -13,7 +13,10 @@ public class AttValuesEntityWriter extends AbstractEntityWriter<List<AttributeVa
public AttValuesEntityWriter(XMLStreamWriter writer,
List<AttributeValue> entity) {
super(writer, entity);
write();

if (!entity.isEmpty()) {
write();
}
}

@Override
Expand Down

0 comments on commit f4a9981

Please sign in to comment.