Search in sources :

Example 11 with SCorpusGraph

use of org.corpus_tools.salt.common.SCorpusGraph in project ANNIS by korpling.

the class GraphMLProvider method writeTo.

@Override
public void writeTo(SaltProject t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
    // collect document graphs from the salt project
    List<SDocument> docs = new LinkedList<>();
    List<SCorpusGraph> corpusGraphs = t.getCorpusGraphs();
    if (corpusGraphs != null) {
        for (SCorpusGraph c : corpusGraphs) {
            if (c.getDocuments() != null) {
                docs.addAll(c.getDocuments());
            }
        }
    }
    GraphMLWriter.writeDocuments(entityStream, docs);
}
Also used : SDocument(org.corpus_tools.salt.common.SDocument) LinkedList(java.util.LinkedList) SCorpusGraph(org.corpus_tools.salt.common.SCorpusGraph)

Example 12 with SCorpusGraph

use of org.corpus_tools.salt.common.SCorpusGraph 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)

Example 13 with SCorpusGraph

use of org.corpus_tools.salt.common.SCorpusGraph in project ANNIS by korpling.

the class SaltProjectProvider method readFrom.

@Override
public SaltProject readFrom(Class<SaltProject> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException {
    SaltProject result = SaltFactory.createSaltProject();
    SAXParser parser;
    XMLReader xmlReader;
    SAXParserFactory factory = SAXParserFactory.newInstance();
    MixedContentHandler handler = new MixedContentHandler();
    try {
        parser = factory.newSAXParser();
        xmlReader = parser.getXMLReader();
        xmlReader.setContentHandler(handler);
        InputSource source = new InputSource(entityStream);
        source.setEncoding("UTF-8");
        xmlReader.parse(source);
        for (SDocumentGraph g : handler.getDocGraphs()) {
            // create a separate corpus graph for each document
            SCorpusGraph corpusGraph = SaltFactory.createSCorpusGraph();
            SCorpus parentCorpus = null;
            SDocument doc = null;
            List<SNode> nodes = g.getNodes();
            Iterator<String> it;
            if (nodes != null && !nodes.isEmpty()) {
                // the path of each node ID is always the document/corpus path
                it = nodes.get(0).getPath().segmentsList().iterator();
            } else {
                // Old salt versions had a separate ID for the document graph
                // which was the document name with the suffix "_graph".
                // Thus this method of getting the corpus path is only the fallback.
                it = g.getPath().segmentsList().iterator();
            }
            while (it.hasNext()) {
                String name = it.next();
                if (it.hasNext()) {
                    // this is a sub-corpus
                    parentCorpus = corpusGraph.createCorpus(parentCorpus, name);
                } else {
                    // no more path elements left, must be a document
                    doc = corpusGraph.createDocument(parentCorpus, name);
                    break;
                }
            }
            if (doc != null) {
                result.addCorpusGraph(corpusGraph);
                doc.setDocumentGraph(g);
            }
        }
    } catch (ParserConfigurationException | SAXException ex) {
        log.error("Error when parsing XMI", ex);
    }
    return result;
}
Also used : InputSource(org.xml.sax.InputSource) SNode(org.corpus_tools.salt.core.SNode) SDocument(org.corpus_tools.salt.common.SDocument) SaltProject(org.corpus_tools.salt.common.SaltProject) SCorpusGraph(org.corpus_tools.salt.common.SCorpusGraph) SAXException(org.xml.sax.SAXException) SCorpus(org.corpus_tools.salt.common.SCorpus) SDocumentGraph(org.corpus_tools.salt.common.SDocumentGraph) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Aggregations

SCorpusGraph (org.corpus_tools.salt.common.SCorpusGraph)13 SDocument (org.corpus_tools.salt.common.SDocument)12 SDocumentGraph (org.corpus_tools.salt.common.SDocumentGraph)7 SaltProject (org.corpus_tools.salt.common.SaltProject)5 SNode (org.corpus_tools.salt.core.SNode)5 Match (annis.service.objects.Match)3 LinkedList (java.util.LinkedList)3 SCorpus (org.corpus_tools.salt.common.SCorpus)3 SToken (org.corpus_tools.salt.common.SToken)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Random (java.util.Random)2 TreeSet (java.util.TreeSet)2 AnnisUser (annis.libgui.AnnisUser)1 InstanceConfig (annis.libgui.InstanceConfig)1 LoginDataLostException (annis.libgui.LoginDataLostException)1 ResolverProviderImpl (annis.libgui.ResolverProviderImpl)1 VisualizerInput (annis.libgui.visualizers.VisualizerInput)1 VisualizerPlugin (annis.libgui.visualizers.VisualizerPlugin)1 Annotation (annis.model.Annotation)1