use of gate.annotation.AnnotationSetImpl in project gate-core by GateNLP.
the class InlineXMLExporter method export.
@Override
public void export(Document doc, OutputStream out, FeatureMap options) throws IOException {
Integer rootID = null;
AnnotationSet withRoot = null;
AnnotationSet originalMarkups = null;
AnnotationSet backupOriginalMarkups = null;
try {
AnnotationSet allAnnots = doc.getAnnotations((String) options.get("annotationSetName"));
if (!(Boolean) options.get("includeOriginalMarkups")) {
originalMarkups = doc.getAnnotations(GateConstants.ORIGINAL_MARKUPS_ANNOT_SET_NAME);
backupOriginalMarkups = new AnnotationSetImpl(originalMarkups);
originalMarkups.clear();
}
// first transfer the annotation types from a list to a set
@SuppressWarnings("unchecked") Set<String> types2Export = new HashSet<String>((List<String>) options.get("annotationTypes"));
// then get the annotations for export
AnnotationSet annots2Export = allAnnots.get(types2Export);
withRoot = new AnnotationSetImpl(doc);
withRoot.addAll(annots2Export);
String rootType = (String) options.get("rootElement");
if (rootType != null && !"".equals(rootType)) {
// add the root element to the set
rootID = withRoot.add(0L, doc.getContent().size(), (String) options.get("rootElement"), Factory.newFeatureMap());
}
// create a writer using the specified encoding
OutputStreamWriter writer = new OutputStreamWriter(out, (String) options.get("encoding"));
// write the document
writer.write(doc.toXml(withRoot, (Boolean) options.get("includeFeatures")));
// make sure it gets written
writer.flush();
} catch (InvalidOffsetException e) {
throw new IOException(e);
} finally {
// delete the fake root element
if (rootID != null)
withRoot.remove(withRoot.get(rootID));
// restore the original markups
if (backupOriginalMarkups != null)
originalMarkups.addAll(backupOriginalMarkups);
}
}
Aggregations