Skip to content

Commit

Permalink
Added unit tests. Add Stream writing for convenience. Changed stax im…
Browse files Browse the repository at this point in the history
…plementation to woodstox because JDK had some nasty bugs regarding UTF-8 and FileWriter.
  • Loading branch information
tadas-subonis committed Jul 27, 2012
1 parent 45ffad4 commit 0f6951e
Show file tree
Hide file tree
Showing 13 changed files with 988 additions and 111 deletions.
40 changes: 35 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
<?xml version="1.0"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>it.uniroma1.dis.wiserver.gexf4j</groupId>
<artifactId>gexf4j</artifactId>
<version>0.3.2-ALPHA</version>
<version>0.4.0-ALPHA</version>

<name>gexf4j</name>

<properties>
<jdkLevel>1.6</jdkLevel>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<plugins>
<plugin>
Expand All @@ -14,10 +22,12 @@
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
Expand All @@ -26,9 +36,29 @@
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.codehaus.woodstox</groupId>
<artifactId>wstx-asl</artifactId>
<version>4.0.6</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>xmlunit</groupId>
<artifactId>xmlunit</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<jdkLevel>1.6</jdkLevel>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
28 changes: 21 additions & 7 deletions src/main/java/it/uniroma1/dis/wiserver/gexf4j/core/GexfWriter.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package it.uniroma1.dis.wiserver.gexf4j.core;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

/**
Expand All @@ -9,11 +11,23 @@
*/
public interface GexfWriter {

/**
* Method to physically write the GEXF object in a file
* @param gexf the current object of the GEXF file
* @param out the OutputStream of the output file
* @throws IOException
*/
void writeToStream(Gexf gexf, Writer out, String encoding) throws IOException;
/**
* Method to physically write the GEXF object in a file
*
* @param gexf the current object of the GEXF file
* @param out the OutputStream of the output file
* @throws IOException
*/
void writeToStream(Gexf gexf, Writer out, String encoding) throws IOException;

/**
* Method to physically write the GEXF object in a file.
* Stream version. Currently implementation wrap OutputStream
* with OutputStreamWriter
*
* @param gexf the current object of the GEXF file
* @param out the OutputStream of the output file
* @throws IOException
*/
void writeToStream(Gexf gexf, OutputStream out, String encoding) throws IOException;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package it.uniroma1.dis.wiserver.gexf4j.core.impl;

import com.ctc.wstx.stax.WstxOutputFactory;
import it.uniroma1.dis.wiserver.gexf4j.core.Gexf;
import it.uniroma1.dis.wiserver.gexf4j.core.GexfWriter;
import it.uniroma1.dis.wiserver.gexf4j.core.impl.writer.GexfEntityWriter;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

import javax.xml.stream.XMLOutputFactory;
Expand All @@ -17,23 +20,27 @@
*/
public class StaxGraphWriter implements GexfWriter {

@Override
public void writeToStream(Gexf gexf, Writer out, String encoding) throws IOException {
try {
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
XMLStreamWriter writer = outputFactory.createXMLStreamWriter(out);

writer.writeStartDocument(encoding, "1.0");

new GexfEntityWriter(writer, gexf);

writer.writeEndDocument();

writer.flush();
writer.close();

} catch (XMLStreamException e) {
throw new IOException("XML Exception: " + e.getMessage(), e);
}
}
@Override
public void writeToStream(Gexf gexf, Writer out, String encoding) throws IOException {
try {
XMLOutputFactory outputFactory1 = WstxOutputFactory.newInstance();
XMLStreamWriter streamWriter = outputFactory1.createXMLStreamWriter(out);
streamWriter.writeStartDocument(encoding, "1.0");

new GexfEntityWriter(streamWriter, gexf);

streamWriter.writeEndDocument();

streamWriter.flush();
streamWriter.close();

} catch (XMLStreamException e) {
throw new IOException("XML Exception: " + e.getMessage(), e);
}
}

@Override
public void writeToStream(Gexf gexf, OutputStream out, String encoding) throws IOException {
writeToStream(gexf, new OutputStreamWriter(out, encoding), encoding);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,78 +10,87 @@
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;


public abstract class AbstractEntityWriter<T extends Object> {
private static final SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");
private static final SimpleDateFormat sdfDateTime = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
protected static String writerTimeType = TimeFormat.DOUBLE;

protected static String toDouble(Object number) {
String returnValue = null;
if(number instanceof Double) {
Double n = (Double) number;
returnValue = String.valueOf(n);
}
return returnValue;
}

protected static String toInteger(Object number) {
String returnValue = null;
if(number instanceof Integer) {
Integer n = (Integer) number;
returnValue = String.valueOf(n);
}
return returnValue;
}

protected static String toDateString(Object date) {
String returnValue = null;
if(date instanceof Date) {
Date d = (Date) date;
returnValue = sdfDate.format(d);
}
return returnValue;
}

protected static String toDateTimeString(Object date) {
String returnValue = null;
if(date instanceof Date) {
Date d = (Date) date;
returnValue = sdfDateTime.format(d);
}
return returnValue;
}

protected XMLStreamWriter writer = null;
protected T entity = null;

protected abstract String getElementName();
protected abstract void writeAttributes() throws XMLStreamException;
protected abstract void writeElements() throws XMLStreamException;

public AbstractEntityWriter(XMLStreamWriter writer, T entity) {
checkArgument(writer != null, "XML Writer cannot be null.");
checkArgument(entity != null, "Entity cannot be null.");

this.writer = writer;
this.entity = entity;
}

protected void write() {
try {
writeStartElement();

writeAttributes();
writeElements();

writer.writeEndElement();

} catch (XMLStreamException e) {
e.printStackTrace();
}
}

protected void writeStartElement() throws XMLStreamException {
writer.writeStartElement(getElementName());
}

/*
* These (SimpleDateFormat) should not be static as they are not thread safe
*
* Date formats are not synchronized. It is recommended to create separate
* format instances for each thread. If multiple threads access a format concurrently,
* it must be synchronized externally.
* http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html
*/
private final SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd");
private final SimpleDateFormat sdfDateTime = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
protected static String writerTimeType = TimeFormat.DOUBLE;

protected static String toDouble(Object number) {
String returnValue = null;
if (number instanceof Double) {
Double n = (Double) number;
returnValue = String.valueOf(n);
}
return returnValue;
}

protected static String toInteger(Object number) {
String returnValue = null;
if (number instanceof Integer) {
Integer n = (Integer) number;
returnValue = String.valueOf(n);
}
return returnValue;
}

protected String toDateString(Object date) {
String returnValue = null;
if (date instanceof Date) {
Date d = (Date) date;
returnValue = sdfDate.format(d);
}
return returnValue;
}

protected String toDateTimeString(Object date) {
String returnValue = null;
if (date instanceof Date) {
Date d = (Date) date;
returnValue = sdfDateTime.format(d);
}
return returnValue;
}
protected XMLStreamWriter writer = null;
protected T entity = null;

protected abstract String getElementName();

protected abstract void writeAttributes() throws XMLStreamException;

protected abstract void writeElements() throws XMLStreamException;

public AbstractEntityWriter(XMLStreamWriter writer, T entity) {
checkArgument(writer != null, "XML Writer cannot be null.");
checkArgument(entity != null, "Entity cannot be null.");

this.writer = writer;
this.entity = entity;
}

protected void write() {
try {
writeStartElement();

writeAttributes();
writeElements();

writer.writeEndElement();

} catch (XMLStreamException e) {
e.printStackTrace();
}
}

protected void writeStartElement() throws XMLStreamException {
writer.writeStartElement(getElementName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ protected void writeAttributes() throws XMLStreamException {
/** integer timeformat */
if(AbstractEntityWriter.writerTimeType.equals(TimeFormat.INTEGER)) {
if (entity.hasStartDate()) {
String startValue = AbstractEntityWriter.toInteger(entity.getStartValue());
String startValue = toInteger(entity.getStartValue());
checkArgument(startValue != null,
entity.getClass().getName() + ": " +
"For timeformat \"integer\" please enter a Integer object.");
writer.writeAttribute(attribStart, startValue);
}

if (entity.hasEndDate()) {
String endValue = AbstractEntityWriter.toInteger(entity.getEndValue());
String endValue = toInteger(entity.getEndValue());
checkArgument(endValue != null,
entity.getClass().getName() + ": " +
"For timeformat \"integer\" please enter a Integer object.");
Expand All @@ -81,15 +81,15 @@ protected void writeAttributes() throws XMLStreamException {
/** date timeformat */
if(AbstractEntityWriter.writerTimeType.equals(TimeFormat.DATE)) {
if (entity.hasStartDate()) {
String startValue = AbstractEntityWriter.toDateString(entity.getStartValue());
String startValue = toDateString(entity.getStartValue());
checkArgument(startValue != null,
entity.getClass().getName() + ": " +
"For timeformat \"date\" please enter a Date object.");
writer.writeAttribute(attribStart, startValue);
}

if (entity.hasEndDate()) {
String endValue = AbstractEntityWriter.toDateString(entity.getEndValue());
String endValue = toDateString(entity.getEndValue());
checkArgument(endValue != null,
entity.getClass().getName() + ": " +
"For timeformat \"date\" please enter a Date object.");
Expand All @@ -99,15 +99,15 @@ protected void writeAttributes() throws XMLStreamException {
/** dateTime timeformat */
if(AbstractEntityWriter.writerTimeType.equals(TimeFormat.XSDDATETIME)) {
if (entity.hasStartDate()) {
String startValue = AbstractEntityWriter.toDateTimeString(entity.getStartValue());
String startValue = toDateTimeString(entity.getStartValue());
checkArgument(startValue != null,
entity.getClass().getName() + ": " +
"For timeformat \"date\" please enter a Date object.");
writer.writeAttribute(attribStart, startValue);
}

if (entity.hasEndDate()) {
String endValue = AbstractEntityWriter.toDateTimeString(entity.getEndValue());
String endValue = toDateTimeString(entity.getEndValue());
checkArgument(endValue != null,
entity.getClass().getName() + ": " +
"For timeformat \"date\" please enter a Date object.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected void writeAttributes() throws XMLStreamException {
if (entity.hasLastModified()) {
writer.writeAttribute(
ATTRIB_LASTMODIFIED,
AbstractEntityWriter.toDateString(entity.getLastModified()));
toDateString(entity.getLastModified()));
}
}

Expand Down
Loading

0 comments on commit 0f6951e

Please sign in to comment.