-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added dot, GraphML and TEI message body writers.
- Loading branch information
Showing
4 changed files
with
161 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...x-servlet/src/main/java/eu/interedition/collatex/io/VariantGraphDotMessageBodyWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package eu.interedition.collatex.io; | ||
|
||
import eu.interedition.collatex.VariantGraph; | ||
import eu.interedition.collatex.simple.SimpleVariantGraphSerializer; | ||
|
||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.WebApplicationException; | ||
import javax.ws.rs.core.HttpHeaders; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.MultivaluedMap; | ||
import javax.ws.rs.ext.MessageBodyWriter; | ||
import javax.ws.rs.ext.Provider; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.io.OutputStreamWriter; | ||
import java.io.Writer; | ||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Type; | ||
|
||
/** | ||
* Created by ronald on 5/6/15. | ||
*/ | ||
@Provider | ||
@Produces("text/plain") | ||
public class VariantGraphDotMessageBodyWriter implements MessageBodyWriter<VariantGraph> { | ||
@Override | ||
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { | ||
return VariantGraph.class.isAssignableFrom(type); | ||
} | ||
|
||
@Override | ||
public long getSize(VariantGraph variantGraph, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public void writeTo(VariantGraph graph, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { | ||
httpHeaders.add(HttpHeaders.CONTENT_TYPE, "text/plain"); | ||
httpHeaders.add(HttpHeaders.CONTENT_ENCODING, "utf-8"); | ||
try (final Writer out = new OutputStreamWriter(entityStream)) { | ||
new SimpleVariantGraphSerializer(graph).toDot(out); | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...rvlet/src/main/java/eu/interedition/collatex/io/VariantGraphGraphMLMessageBodyWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package eu.interedition.collatex.io; | ||
|
||
import eu.interedition.collatex.VariantGraph; | ||
import eu.interedition.collatex.simple.SimpleVariantGraphSerializer; | ||
|
||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.WebApplicationException; | ||
import javax.ws.rs.core.HttpHeaders; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.MultivaluedMap; | ||
import javax.ws.rs.ext.MessageBodyWriter; | ||
import javax.ws.rs.ext.Provider; | ||
import javax.xml.stream.XMLOutputFactory; | ||
import javax.xml.stream.XMLStreamException; | ||
import javax.xml.stream.XMLStreamWriter; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Type; | ||
|
||
/** | ||
* Created by ronald on 5/6/15. | ||
*/ | ||
@Provider | ||
@Produces("application/graphml+xml") | ||
public class VariantGraphGraphMLMessageBodyWriter implements MessageBodyWriter<VariantGraph> { | ||
@Override | ||
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { | ||
return VariantGraph.class.isAssignableFrom(type); | ||
} | ||
|
||
@Override | ||
public long getSize(VariantGraph variantGraph, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public void writeTo(VariantGraph graph, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { | ||
XMLStreamWriter xml = null; | ||
try { | ||
httpHeaders.add(HttpHeaders.CONTENT_TYPE, "application/graphml+xml"); | ||
try (OutputStream responseStream = entityStream) { | ||
xml = XMLOutputFactory.newInstance().createXMLStreamWriter(responseStream); | ||
xml.writeStartDocument(); | ||
new SimpleVariantGraphSerializer(graph).toGraphML(xml); | ||
xml.writeEndDocument(); | ||
} finally { | ||
if (xml != null) { | ||
xml.close(); | ||
} | ||
} | ||
} catch (XMLStreamException e) { | ||
throw new WebApplicationException(e); | ||
} | ||
|
||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...x-servlet/src/main/java/eu/interedition/collatex/io/VariantGraphTEIMessageBodyWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package eu.interedition.collatex.io; | ||
|
||
import eu.interedition.collatex.VariantGraph; | ||
import eu.interedition.collatex.simple.SimpleVariantGraphSerializer; | ||
|
||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.WebApplicationException; | ||
import javax.ws.rs.core.HttpHeaders; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.MultivaluedMap; | ||
import javax.ws.rs.ext.MessageBodyWriter; | ||
import javax.ws.rs.ext.Provider; | ||
import javax.xml.stream.XMLOutputFactory; | ||
import javax.xml.stream.XMLStreamException; | ||
import javax.xml.stream.XMLStreamWriter; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Type; | ||
|
||
/** | ||
* Created by ronald on 5/6/15. | ||
*/ | ||
@Provider | ||
@Produces("application/tei+xml") | ||
public class VariantGraphTEIMessageBodyWriter implements MessageBodyWriter<VariantGraph> { | ||
@Override | ||
public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { | ||
return VariantGraph.class.isAssignableFrom(type); | ||
} | ||
|
||
@Override | ||
public long getSize(VariantGraph variantGraph, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public void writeTo(VariantGraph graph, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { | ||
XMLStreamWriter xml = null; | ||
try { | ||
httpHeaders.add(HttpHeaders.CONTENT_TYPE, "application/tei+xml"); | ||
try (OutputStream responseStream = entityStream) { | ||
xml = XMLOutputFactory.newInstance().createXMLStreamWriter(responseStream); | ||
xml.writeStartDocument(); | ||
new SimpleVariantGraphSerializer(graph).toTEI(xml); | ||
xml.writeEndDocument(); | ||
} finally { | ||
if (xml != null) { | ||
xml.close(); | ||
} | ||
} | ||
} catch (XMLStreamException e) { | ||
throw new WebApplicationException(e); | ||
} | ||
} | ||
} |