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);
}
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);
}
}
Aggregations