Skip to content
Snippets Groups Projects
Commit 03392996 authored by Sören Henning's avatar Sören Henning
Browse files

added JAXB marshal stages

parent 232aa24a
No related branches found
No related tags found
1 merge request!17Get impletemented stages and Java 8
...@@ -30,7 +30,7 @@ public class GraphMLExporter { ...@@ -30,7 +30,7 @@ public class GraphMLExporter {
ObjectFactory objectFactory = new ObjectFactory(); ObjectFactory objectFactory = new ObjectFactory();
JAXBElement<GraphmlType> rootElement = objectFactory.createGraphml(graphmlType); JAXBElement<GraphmlType> rootElement = objectFactory.createGraphml(graphmlType);
JAXBContext context = JAXBContext.newInstance(rootElement.getValue().getClass()); JAXBContext context = JAXBContext.newInstance(rootElement.getDeclaredType());
Marshaller marshaller = context.createMarshaller(); Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, FORMATTED_OUTPUT); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, FORMATTED_OUTPUT);
......
package kieker.analysis.util;
import java.io.OutputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import teetime.framework.AbstractConsumerStage;
/**
* This stage marshals the content tree rooted at an incoming element into an
* output stream.
*
* A class object has to be passed at creation. Only elements of this type or
* elements of this type wrapped in a {@code JAXBElement} could be marshaled.
*
* Incoming elements have to be an instance of {@code JAXBElement} or are
* annotated with {@code @XmlRootElement}.
*
* Marshaling with this stage is faster than using
* {@link UniversalJAXBMarshalStage}. So It should be used if the type of the
* incoming elements is known.
*
* @author Sören Henning
*
*/
public class JAXBMarshalStage extends AbstractConsumerStage<Object> {
private static final Boolean FORMATTED_OUTPUT_DEFAULT = Boolean.TRUE;
private final Marshaller marshaller;
private final OutputStream outputStream;
public JAXBMarshalStage(final Class<?> elementsClass, final OutputStream outputStream) {
this(elementsClass, outputStream, FORMATTED_OUTPUT_DEFAULT);
}
public JAXBMarshalStage(final Class<?> elementsClass, final OutputStream outputStream, final Boolean formattedOutput) {
this.outputStream = outputStream;
try {
this.marshaller = JAXBContext.newInstance(elementsClass).createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, formattedOutput);
} catch (JAXBException e) {
// TODO Exception
throw new IllegalStateException(e);
}
}
@Override
protected void execute(final Object element) {
try {
marshaller.marshal(element, outputStream);
} catch (JAXBException e) {
// TODO Exception
throw new IllegalStateException("The received element could not be marshalled.", e);
}
}
}
package kieker.analysis.util;
import java.io.OutputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import teetime.framework.AbstractConsumerStage;
/**
* This stage marshals the content tree rooted at an incoming element into an
* output stream.
*
* Incoming elements have to be an instance of {@code JAXBElement} or are
* annotated with {@code @XmlRootElement}.
*
* {@link JAXBMarshalStage} should be preferred if all incoming elements have
* the same type and the type is known, because {@link JAXBMarshalStage} is
* faster.
*
* @author Sören Henning
*
*/
public class UniversalJAXBMarshalStage extends AbstractConsumerStage<Object> {
private static final Boolean FORMATTED_OUTPUT_DEFAULT = Boolean.TRUE;
private final OutputStream outputStream;
private final Boolean formattedOutput;
public UniversalJAXBMarshalStage(final OutputStream outputStream) {
this(outputStream, FORMATTED_OUTPUT_DEFAULT);
}
public UniversalJAXBMarshalStage(final OutputStream outputStream, final Boolean formattedOutput) {
this.outputStream = outputStream;
this.formattedOutput = formattedOutput;
}
@Override
protected void execute(final Object element) {
// Determine whether the incoming object is wrapped in a JAXBElement
Class<?> elementClass;
if (element instanceof JAXBElement) {
elementClass = ((JAXBElement<?>) element).getDeclaredType();
} else {
elementClass = element.getClass();
}
try {
final Marshaller marshaller = JAXBContext.newInstance(elementClass).createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, formattedOutput);
marshaller.marshal(element, outputStream);
} catch (JAXBException e) {
// TODO Exception
throw new IllegalStateException("The received element could not be marshalled.", e);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment