Search in sources :

Example 6 with AnnotationSetImpl

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);
    }
}
Also used : AnnotationSetImpl(gate.annotation.AnnotationSetImpl) AnnotationSet(gate.AnnotationSet) OutputStreamWriter(java.io.OutputStreamWriter) InvalidOffsetException(gate.util.InvalidOffsetException) IOException(java.io.IOException) HashSet(java.util.HashSet)

Aggregations

AnnotationSetImpl (gate.annotation.AnnotationSetImpl)6 AnnotationSet (gate.AnnotationSet)5 InvalidOffsetException (gate.util.InvalidOffsetException)3 HashSet (java.util.HashSet)3 Annotation (gate.Annotation)2 DocumentEvent (gate.event.DocumentEvent)2 GateRuntimeException (gate.util.GateRuntimeException)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 FeatureMap (gate.FeatureMap)1 Token (gate.creole.annic.apache.lucene.analysis.Token)1 Document (gate.creole.annic.apache.lucene.document.Document)1 StatusListener (gate.event.StatusListener)1 OffsetComparator (gate.util.OffsetComparator)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Set (java.util.Set)1 Stack (java.util.Stack)1 TreeSet (java.util.TreeSet)1