Search in sources :

Example 1 with SaltXML10Writer

use of org.corpus_tools.salt.util.internal.persistence.SaltXML10Writer in project ANNIS by korpling.

the class QueryDaoImpl method exportCorpus.

@Override
@Transactional(readOnly = true)
public void exportCorpus(String toplevelCorpus, File outputDirectory) {
    // check if the corpus really exists
    mapCorpusNameToId(toplevelCorpus);
    SaltProject corpusProject = SaltFactory.createSaltProject();
    SCorpusGraph corpusGraph = SaltFactory.createSCorpusGraph();
    corpusGraph.setSaltProject(corpusProject);
    SCorpus rootCorpus = corpusGraph.createCorpus(null, toplevelCorpus);
    // add all root metadata
    for (Annotation metaAnno : listCorpusAnnotations(toplevelCorpus)) {
        rootCorpus.createMetaAnnotation(metaAnno.getNamespace(), metaAnno.getName(), metaAnno.getValue());
    }
    File documentRootDir = new File(outputDirectory, toplevelCorpus);
    if (!outputDirectory.exists()) {
        if (!outputDirectory.mkdirs()) {
            log.warn("Could not create output directory \"{}\" for exporting the corpus", outputDirectory.getAbsolutePath());
        }
    }
    List<Annotation> docs = listDocuments(toplevelCorpus);
    int i = 1;
    for (Annotation docAnno : docs) {
        log.info("Loading document {} from database ({}/{})", docAnno.getName(), i, docs.size());
        SaltProject docProject = retrieveAnnotationGraph(toplevelCorpus, docAnno.getName(), null);
        if (docProject != null && docProject.getCorpusGraphs() != null && !docProject.getCorpusGraphs().isEmpty()) {
            List<Annotation> docMetaData = listCorpusAnnotations(toplevelCorpus, docAnno.getName(), true);
            SCorpusGraph docCorpusGraph = docProject.getCorpusGraphs().get(0);
            // TODO: we could re-use the actual corpus structure instead of just adding a flat list of documents
            if (docCorpusGraph.getDocuments() != null) {
                for (SDocument doc : docCorpusGraph.getDocuments()) {
                    log.info("Removing SFeatures from {} ({}/{})", docAnno.getName(), i, docs.size());
                    // remove all ANNIS specific features that require a special Java class
                    SDocumentGraph graph = doc.getDocumentGraph();
                    if (graph != null) {
                        if (graph.getNodes() != null) {
                            for (SNode n : graph.getNodes()) {
                                n.removeLabel(AnnisConstants.ANNIS_NS, AnnisConstants.FEAT_RELANNIS_NODE);
                            }
                        }
                        if (graph.getRelations() != null) {
                            for (SRelation e : graph.getRelations()) {
                                e.removeLabel(AnnisConstants.ANNIS_NS, AnnisConstants.FEAT_RELANNIS_EDGE);
                            }
                        }
                    }
                    log.info("Saving document {} ({}/{})", doc.getName(), i, docs.size());
                    SaltUtil.saveDocumentGraph(graph, URI.createFileURI(new File(documentRootDir, doc.getName() + "." + SaltUtil.FILE_ENDING_SALT_XML).getAbsolutePath()));
                    SDocument docCopy = corpusGraph.createDocument(rootCorpus, doc.getName());
                    log.info("Adding metadata to document {} ({}/{})", doc.getName(), i, docs.size());
                    for (Annotation metaAnno : docMetaData) {
                        docCopy.createMetaAnnotation(metaAnno.getNamespace(), metaAnno.getName(), metaAnno.getValue());
                    }
                }
            }
        }
        i++;
    }
    // end for each document
    // save the actual SaltProject
    log.info("Saving corpus structure");
    File projectFile = new File(outputDirectory, SaltUtil.FILE_SALT_PROJECT);
    SaltXML10Writer writer = new SaltXML10Writer(projectFile);
    writer.writeSaltProject(corpusProject);
}
Also used : SCorpus(org.corpus_tools.salt.common.SCorpus) SRelation(org.corpus_tools.salt.core.SRelation) SNode(org.corpus_tools.salt.core.SNode) SDocumentGraph(org.corpus_tools.salt.common.SDocumentGraph) SDocument(org.corpus_tools.salt.common.SDocument) SaltXML10Writer(org.corpus_tools.salt.util.internal.persistence.SaltXML10Writer) SaltProject(org.corpus_tools.salt.common.SaltProject) File(java.io.File) Annotation(annis.model.Annotation) SCorpusGraph(org.corpus_tools.salt.common.SCorpusGraph) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with SaltXML10Writer

use of org.corpus_tools.salt.util.internal.persistence.SaltXML10Writer in project ANNIS by korpling.

the class SaltProjectProvider method writeTo.

@Override
public void writeTo(SaltProject project, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
    SaltXML10Writer writer = new SaltXML10Writer();
    try {
        XMLStreamWriter xml = outFactory.createXMLStreamWriter(entityStream, "UTF-8");
        xml.writeStartDocument("1.1");
        xml.writeCharacters("\n");
        long startTime = System.currentTimeMillis();
        // output XMI root element
        writer.writeXMIRootElement(xml);
        for (SCorpusGraph corpusGraph : project.getCorpusGraphs()) {
            for (SDocument doc : corpusGraph.getDocuments()) {
                // make sure that any ANNIS feature on the document is copied to the document graph
                SDocumentGraph docGraph = doc.getDocumentGraph();
                for (SFeature feat : doc.getFeatures()) {
                    if (AnnisConstants.ANNIS_NS.equals(feat.getNamespace())) {
                        SFeature newFeat = SaltFactory.createSFeature();
                        feat.copy(newFeat);
                        docGraph.addFeature(newFeat);
                    }
                }
                writer.writeObjects(xml, docGraph);
            }
        }
        xml.writeEndDocument();
        long endTime = System.currentTimeMillis();
        log.debug("Saving XMI (" + mediaType.toString() + ") needed {} ms", endTime - startTime);
    } catch (XMLStreamException ex) {
        log.error("exception when serializing SDocumentGraph", ex);
    }
}
Also used : XMLStreamException(javax.xml.stream.XMLStreamException) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) SDocumentGraph(org.corpus_tools.salt.common.SDocumentGraph) SaltXML10Writer(org.corpus_tools.salt.util.internal.persistence.SaltXML10Writer) SDocument(org.corpus_tools.salt.common.SDocument) SCorpusGraph(org.corpus_tools.salt.common.SCorpusGraph) SFeature(org.corpus_tools.salt.core.SFeature)

Aggregations

SCorpusGraph (org.corpus_tools.salt.common.SCorpusGraph)2 SDocument (org.corpus_tools.salt.common.SDocument)2 SDocumentGraph (org.corpus_tools.salt.common.SDocumentGraph)2 SaltXML10Writer (org.corpus_tools.salt.util.internal.persistence.SaltXML10Writer)2 Annotation (annis.model.Annotation)1 File (java.io.File)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)1 SCorpus (org.corpus_tools.salt.common.SCorpus)1 SaltProject (org.corpus_tools.salt.common.SaltProject)1 SFeature (org.corpus_tools.salt.core.SFeature)1 SNode (org.corpus_tools.salt.core.SNode)1 SRelation (org.corpus_tools.salt.core.SRelation)1 Transactional (org.springframework.transaction.annotation.Transactional)1