diff --git a/src/main/java/co/zeroae/gate/AnnotationSetExporter.java b/src/main/java/co/zeroae/gate/AnnotationSetExporter.java index 9e7d92e..da06f6c 100644 --- a/src/main/java/co/zeroae/gate/AnnotationSetExporter.java +++ b/src/main/java/co/zeroae/gate/AnnotationSetExporter.java @@ -1,6 +1,7 @@ package co.zeroae.gate; import com.sun.xml.fastinfoset.stax.StAXDocumentSerializer; +import gate.AnnotationSet; import gate.Document; import gate.DocumentExporter; import gate.FeatureMap; @@ -11,6 +12,8 @@ import javax.xml.stream.XMLStreamWriter; import java.io.IOException; import java.io.OutputStream; +import java.util.Set; +import java.util.function.Supplier; import static gate.corpora.DocumentStaxUtils.GATE_XML_VERSION; @@ -28,17 +31,30 @@ public AnnotationSetExporter(String fileType, String defaultExtension, String mi } protected void export(Document doc, XMLStreamWriter xsw, FeatureMap options) throws XMLStreamException { + final AnnotationSet annotations = filterAnnotationSet(doc.getAnnotations(), options); final String namespaceURI = ""; xsw.writeStartDocument("1.0"); xsw.setDefaultNamespace(namespaceURI); xsw.writeStartElement(namespaceURI, "GateDocument"); xsw.writeAttribute("version", GATE_XML_VERSION); - DocumentStaxUtils.writeAnnotationSet(doc.getAnnotations(), xsw, namespaceURI); + DocumentStaxUtils.writeAnnotationSet(annotations, xsw, namespaceURI); xsw.writeEndDocument(); xsw.flush(); xsw.close(); } + @SuppressWarnings("unchecked") + private AnnotationSet filterAnnotationSet(AnnotationSet annotations, FeatureMap options) { + final Set annotationTypes = ((Supplier>) () -> { + Object types = options.get("annotationTypes"); + if (types instanceof Set) { + return (Set) types; + } else + return null; + }).get(); + return annotationTypes != null ? annotations.get(annotationTypes) : annotations; + } + static class GATEFastInfoset extends AnnotationSetExporter { /** * Creates a new exporter instance that excludes the original text. diff --git a/src/main/java/co/zeroae/gate/App.java b/src/main/java/co/zeroae/gate/App.java index 3af6356..04c8fb6 100644 --- a/src/main/java/co/zeroae/gate/App.java +++ b/src/main/java/co/zeroae/gate/App.java @@ -236,8 +236,6 @@ private APIGatewayProxyResponseEvent export( // Take *all* annotation types and filter based on AnnotationSelector final AnnotationSet defaultAnnots = doc.getAnnotations(); - - // This if for GateJSONExporter (but should be for all exporters...) final Set includeTypes = new HashSet<>(defaultAnnots.getAllTypes()); if (annotationSelector != null) includeTypes.removeIf((type) -> !annotationSelector.contains(":" + type));