Search in sources :

Example 1 with TagReference

use of de.catma.document.annotation.TagReference in project catma by forTEXT.

the class ProjectResourceExportApiRequestHandler method serializeProjectResources.

private String serializeProjectResources() {
    try {
        Export export = new Export();
        for (SourceDocument sourceDocument : project.getSourceDocuments()) {
            ArrayList<AnnotationCollection> annotationCollections = new ArrayList<>();
            for (AnnotationCollectionReference annotationCollectionReference : sourceDocument.getUserMarkupCollectionRefs()) {
                annotationCollections.add(project.getUserMarkupCollection(annotationCollectionReference));
            }
            ArrayList<TagDefinition> tagDefinitions = new ArrayList<>();
            ArrayList<TagReference> tagReferences = new ArrayList<>();
            for (AnnotationCollection annotationCollection : annotationCollections) {
                for (TagsetDefinition tagsetDefinition : annotationCollection.getTagLibrary().getTagsetDefinitions()) {
                    tagDefinitions.addAll(tagsetDefinition.stream().collect(Collectors.toList()));
                }
                tagReferences.addAll(annotationCollection.getTagReferences());
            }
            ExportDocument exportDocument = new ExportDocument(new PreApiSourceDocument(sourceDocument, String.format("%s%s/doc/%s", BASE_URL, handlerPath.substring(1), sourceDocument.getUuid().toLowerCase())), tagDefinitions.stream().map(PreApiTagDefinition::new).collect(Collectors.toList()), tagReferences.stream().map((TagReference tagReference) -> {
                try {
                    return new PreApiAnnotation(tagReference, tagDefinitions.stream().filter(td -> td.getUuid().equals(tagReference.getTagDefinitionId())).findFirst().get(), sourceDocument);
                } catch (IOException e) {
                    logger.log(Level.WARNING, String.format("Error serializing TagReference: %s", tagReference), e);
                    return null;
                }
            }).collect(Collectors.toList()));
            export.addExportDocument(exportDocument);
        }
        return new SerializationHelper<Export>().serialize(export);
    } catch (Exception e) {
        logger.log(Level.SEVERE, "Failed to serialize project resources", e);
        return "{\"error\": \"Failed to serialize project resources, please contact CATMA support\"}";
    }
}
Also used : RequestHandler(com.vaadin.server.RequestHandler) ExportDocument(de.catma.api.pre.serialization.models.ExportDocument) VaadinRequest(com.vaadin.server.VaadinRequest) PreApiAnnotation(de.catma.api.pre.serialization.model_wrappers.PreApiAnnotation) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Export(de.catma.api.pre.serialization.models.Export) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) TagsetDefinition(de.catma.tag.TagsetDefinition) IDGenerator(de.catma.util.IDGenerator) NoSuchElementException(java.util.NoSuchElementException) OutputStream(java.io.OutputStream) PreApiSourceDocument(de.catma.api.pre.serialization.model_wrappers.PreApiSourceDocument) CATMAPropertyKey(de.catma.properties.CATMAPropertyKey) VaadinResponse(com.vaadin.server.VaadinResponse) AnnotationCollectionReference(de.catma.document.annotation.AnnotationCollectionReference) Project(de.catma.project.Project) IOException(java.io.IOException) SourceDocument(de.catma.document.source.SourceDocument) PreApiTagDefinition(de.catma.api.pre.serialization.model_wrappers.PreApiTagDefinition) AnnotationCollection(de.catma.document.annotation.AnnotationCollection) SerializationHelper(de.catma.repository.git.serialization.SerializationHelper) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) TagReference(de.catma.document.annotation.TagReference) VaadinSession(com.vaadin.server.VaadinSession) TagDefinition(de.catma.tag.TagDefinition) PreApiTagDefinition(de.catma.api.pre.serialization.model_wrappers.PreApiTagDefinition) TagDefinition(de.catma.tag.TagDefinition) AnnotationCollection(de.catma.document.annotation.AnnotationCollection) PreApiSourceDocument(de.catma.api.pre.serialization.model_wrappers.PreApiSourceDocument) PreApiSourceDocument(de.catma.api.pre.serialization.model_wrappers.PreApiSourceDocument) SourceDocument(de.catma.document.source.SourceDocument) ArrayList(java.util.ArrayList) AnnotationCollectionReference(de.catma.document.annotation.AnnotationCollectionReference) IOException(java.io.IOException) ExportDocument(de.catma.api.pre.serialization.models.ExportDocument) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException) PreApiTagDefinition(de.catma.api.pre.serialization.model_wrappers.PreApiTagDefinition) TagsetDefinition(de.catma.tag.TagsetDefinition) PreApiAnnotation(de.catma.api.pre.serialization.model_wrappers.PreApiAnnotation) Export(de.catma.api.pre.serialization.models.Export) TagReference(de.catma.document.annotation.TagReference)

Example 2 with TagReference

use of de.catma.document.annotation.TagReference in project catma by forTEXT.

the class TeiUserMarkupCollectionSerializer method mergeTagReferences.

private HashMap<Range, List<TagReference>> mergeTagReferences(List<TagReference> tagReferences, Range initialRange) {
    HashMap<Range, List<TagReference>> mergedTagReferences = new HashMap<Range, List<TagReference>>();
    mergedTagReferences.put(initialRange, new ArrayList<TagReference>());
    for (TagReference tagReference : tagReferences) {
        Range targetRange = tagReference.getRange();
        List<Range> affectedRanges = getAffectedRanges(mergedTagReferences.keySet(), targetRange);
        for (Range affectedRange : affectedRanges) {
            if (affectedRange.isInBetween(targetRange)) {
                mergedTagReferences.get(affectedRange).add(tagReference);
            } else {
                List<TagReference> existingReferences = mergedTagReferences.get(affectedRange);
                Range overlappingRange = affectedRange.getOverlappingRange(targetRange);
                List<Range> disjointRanges = affectedRange.getDisjointRanges(targetRange);
                // range outside of the overlapping range
                // left or right depending on the position of the overlapping range
                Range firstDisjointRange = disjointRanges.get(0);
                List<TagReference> firstCopy = new ArrayList<TagReference>();
                firstCopy.addAll(existingReferences);
                mergedTagReferences.put(firstDisjointRange, firstCopy);
                // the overlapping range sits in the middle
                if (disjointRanges.size() == 2) {
                    // range right of the overlappting range
                    Range secondDisjointRange = disjointRanges.get(1);
                    List<TagReference> secondCopy = new ArrayList<TagReference>();
                    secondCopy.addAll(existingReferences);
                    mergedTagReferences.put(secondDisjointRange, secondCopy);
                }
                existingReferences.add(tagReference);
                mergedTagReferences.put(overlappingRange, existingReferences);
                mergedTagReferences.remove(affectedRange);
            }
        }
    }
    return mergedTagReferences;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) TagReference(de.catma.document.annotation.TagReference) Range(de.catma.document.Range)

Example 3 with TagReference

use of de.catma.document.annotation.TagReference in project catma by forTEXT.

the class TeiUserMarkupCollectionSerializer method serialize.

public void serialize(AnnotationCollection userMarkupCollection, SourceDocument sourceDocument) throws IOException {
    String targetURI = makeTargetURI(sourceDocument);
    if (userMarkupCollection.isEmpty()) {
        return;
    }
    TeiElement textElement = (TeiElement) teiDocument.getNodes(TeiElementName.text).get(0);
    TeiElement ptrParentElement = (TeiElement) teiDocument.getNodes(TeiElementName.ab, AttributeValue.type_catma).get(0);
    Set<String> addedTagInstances = new HashSet<String>();
    HashMap<Range, List<TagReference>> mergedTagReferences = mergeTagReferences(userMarkupCollection.getTagReferences(), new Range(0, sourceDocument.getLength()));
    TreeSet<Range> sortedRanges = new TreeSet<Range>();
    sortedRanges.addAll(mergedTagReferences.keySet());
    for (Range range : sortedRanges) {
        List<TagReference> currentReferences = mergedTagReferences.get(range);
        TeiElement parent = ptrParentElement;
        if (!currentReferences.isEmpty()) {
            parent = writeSegment(currentReferences, ptrParentElement, textElement, addedTagInstances, userMarkupCollection.getTagLibrary());
        }
        writeText(targetURI, range, parent, sourceDocument);
    }
}
Also used : TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) List(java.util.List) TagReference(de.catma.document.annotation.TagReference) Range(de.catma.document.Range) HashSet(java.util.HashSet)

Example 4 with TagReference

use of de.catma.document.annotation.TagReference in project catma by forTEXT.

the class TeiUserMarkupCollectionSerializer method writeSegment.

private TeiElement writeSegment(List<TagReference> tagReferences, TeiElement abElement, TeiElement textElement, Set<String> addedTagInstances, TagLibrary tagLibrary) {
    List<TagInstance> tagInstances = new ArrayList<TagInstance>();
    for (TagReference tr : tagReferences) {
        if (!addedTagInstances.contains(tr.getTagInstanceId())) {
            writeTagInstance(tr.getTagInstance(), textElement, tagLibrary);
            addedTagInstances.add(tr.getTagInstanceId());
        }
        tagInstances.add(tr.getTagInstance());
    }
    AnaValueHandler anaValueHandler = new AnaValueHandler();
    TeiElement seg = new TeiElement(TeiElementName.seg);
    seg.setAttributeValue(Attribute.ana, anaValueHandler.makeValueFrom(tagInstances));
    abElement.appendChild(seg);
    return seg;
}
Also used : TagInstance(de.catma.tag.TagInstance) ArrayList(java.util.ArrayList) TagReference(de.catma.document.annotation.TagReference)

Example 5 with TagReference

use of de.catma.document.annotation.TagReference in project catma by forTEXT.

the class JsonLdWebAnnotation method toTagReferenceList.

public List<TagReference> toTagReferenceList(String projectId, String markupCollectionId) throws Exception {
    TagInstance tagInstance = this.getTagInstance();
    String sourceDocumentUri = this.getSourceDocumentUri();
    List<Range> ranges = this.getRanges();
    List<TagReference> tagReferences = new ArrayList<>();
    try {
        for (Range range : ranges) {
            tagReferences.add(new TagReference(tagInstance, sourceDocumentUri, range, markupCollectionId));
        }
    } catch (URISyntaxException e) {
        throw new IOException(String.format("error loading Collection %1$s of project %2$s ", markupCollectionId, projectId), e);
    }
    return tagReferences;
}
Also used : TagInstance(de.catma.tag.TagInstance) ArrayList(java.util.ArrayList) TagReference(de.catma.document.annotation.TagReference) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Range(de.catma.document.Range)

Aggregations

TagReference (de.catma.document.annotation.TagReference)25 ArrayList (java.util.ArrayList)16 TagInstance (de.catma.tag.TagInstance)15 Property (de.catma.tag.Property)12 TagDefinition (de.catma.tag.TagDefinition)12 List (java.util.List)12 AnnotationCollection (de.catma.document.annotation.AnnotationCollection)11 TagsetDefinition (de.catma.tag.TagsetDefinition)10 AnnotationCollectionReference (de.catma.document.annotation.AnnotationCollectionReference)8 SourceDocument (de.catma.document.source.SourceDocument)8 Logger (java.util.logging.Logger)8 PropertyDefinition (de.catma.tag.PropertyDefinition)7 TagLibrary (de.catma.tag.TagLibrary)7 IOException (java.io.IOException)7 Level (java.util.logging.Level)7 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)6 Range (de.catma.document.Range)6 User (de.catma.user.User)6 File (java.io.File)6 Multimap (com.google.common.collect.Multimap)5