serialize.AnnotationSerializer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of annotationpro-format Show documentation
Show all versions of annotationpro-format Show documentation
Library to read and write Annotation Pro files
package serialize;
import model.Annotation;
import model.Layer;
import model.Segment;
import java.util.HashMap;
import java.util.Map;
public class AnnotationSerializer {
public String serializeAnnotation(Annotation annotation) {
StringBuilder xmlContent = new StringBuilder();
StringBuilder segmentsContent = new StringBuilder();
xmlContent.append(createAnnotationHeader());
// layers and segments
for (Layer layer : annotation.getLayers()) {
xmlContent.append(serializeLayer(layer));
for (Segment segment : layer.getSegments()) {
segmentsContent.append(serializeSegment(segment));
}
}
xmlContent.append(segmentsContent.toString());
// configuration items
xmlContent.append(serializeConfiguration(annotation.getConfiguration()));
xmlContent.append(createAnnotationFooter());
return xmlContent.toString();
}
private String serializeSegment(Segment segment) {
String template = " \n" +
" %s \n" +
" %s \n" +
" \n" +
" %s \n" +
" %s \n" +
" %s \n" +
" %s \n" +
" %s \n" +
" %s \n" +
" %s \n" +
" %s \n" +
" %s \n" +
" %s \n" +
" %s \n" +
" %s \n" +
" %s \n" +
" %s \n" +
" %s \n" +
" %s \n" +
" \n";
return String.format(template,
segment.getId(),
segment.getIdLayer(),
segment.getLabel(),
segment.getForeColor(),
segment.getBackColor(),
segment.getBorderColor(),
segment.getStart(),
segment.getDuration(),
segment.isSelected(),
segment.getFeature(),
segment.getLanguage(),
segment.getGroup(),
segment.getName(),
segment.getParameter1(),
segment.getParameter2(),
segment.getParameter3(),
segment.isMarkerEnabled(),
segment.getMarker(),
segment.getRScript()
);
}
private String serializeLayer(Layer layer) {
String template = " \n" +
" %s \n" +
" %s \n" +
" %s \n" +
" %s \n" +
" %s \n" +
" %s \n" +
" %s \n" +
" %s \n" +
" %s \n" +
" %s \n" +
" %s \n" +
" %s \n" +
" %s \n" +
" %s \n" +
" %s \n" +
" %s \n" +
" %s \n" +
" %s \n" +
" %s \n" +
" %s \n" +
" \n";
return String.format(template,
layer.getId(),
layer.getName(),
layer.getForeColor(),
layer.getBackColor(),
layer.isSelected(),
layer.getHeight(),
layer.getCoordinateControlStyle(),
layer.isLocked(),
layer.isClosed(),
layer.isShowOnSpectrogram(),
layer.isShowAsChart(),
layer.getChartMinimum(),
layer.getChartMaximum(),
layer.isShowBoundaries(),
layer.isIncludeInFrequency(),
layer.getParameter1Name(),
layer.getParameter2Name(),
layer.getParameter3Name(),
layer.isVisible(),
layer.getFontSize()
);
}
private String serializeConfiguration(HashMap configuration) {
String template = " \n" +
" %s \n" +
" %s \n" +
" \n";
StringBuilder builder = new StringBuilder();
for (Map.Entry entry : configuration.entrySet()) {
builder.append(String.format(template,
entry.getKey(),
entry.getValue())
);
}
return builder.toString();
}
private String createAnnotationHeader() {
return "\n" +
"\n";
}
private String createAnnotationFooter() {
return " ";
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy