use of de.catma.tag.Property in project catma by forTEXT.
the class TeiUserMarkupCollectionDeserializer method addProperties.
private void addProperties(TagDefinition tagDefinition, AddPropertyHandler addPropertyHandler, Nodes propertyElements) {
for (int i = 0; i < propertyElements.size(); i++) {
try {
TeiElement curPropertyElement = (TeiElement) propertyElements.get(i);
PropertyDefinition propertyDefinition = tagDefinition.getPropertyDefinition(curPropertyElement.getAttributeValue(Attribute.f_name));
if (curPropertyElement.getChildElements().size() == 0) {
addPropertyHandler.addProperty(new Property(propertyDefinition.getUuid(), Collections.emptyList()));
} else {
TeiElement valueElement = (TeiElement) curPropertyElement.getChildElements().get(0);
if (valueElement.is(TeiElementName.numeric)) {
addPropertyHandler.addProperty(new Property(propertyDefinition.getUuid(), new NumericPropertyValueFactory(curPropertyElement).getValueAsList()));
} else if (valueElement.is(TeiElementName.string)) {
StringPropertyValueFactory stringPropFact = new StringPropertyValueFactory(curPropertyElement);
if (!stringPropFact.getValue().trim().isEmpty()) {
addPropertyHandler.addProperty(new Property(propertyDefinition.getUuid(), stringPropFact.getValueAsList()));
}
} else if (valueElement.is(TeiElementName.vRange)) {
TeiElement vColl = (TeiElement) valueElement.getChildElements().get(0);
if (vColl.hasChildElements()) {
List<String> valueList = new ArrayList<String>();
for (int j = 0; j < vColl.getChildElements().size(); j++) {
valueList.add(new StringPropertyValueFactory(vColl, j).getValue());
}
addPropertyHandler.addProperty(new Property(propertyDefinition.getUuid(), valueList));
}
} else {
throw new UnknownElementException(valueElement.getLocalName() + " is not supported!");
}
}
} catch (UnknownElementException ue) {
logger.log(Level.SEVERE, "error adding properties", ue);
}
}
}
use of de.catma.tag.Property in project catma by forTEXT.
the class TeiUserMarkupCollectionDeserializer method createTagInstance.
private TagInstance createTagInstance(String tagInstanceID) {
TeiElement tagInstanceElement = teiDocument.getElementByID(tagInstanceID);
TagDefinition tagDefinition = tagLibrary.getTagDefinition(tagInstanceElement.getAttributeValue(Attribute.type));
if (!old2newTagInstanceIDs.containsKey(tagInstanceElement.getID())) {
old2newTagInstanceIDs.put(tagInstanceElement.getID(), new IDGenerator().generate());
}
final TagInstance tagInstance = new TagInstance(old2newTagInstanceIDs.get(tagInstanceElement.getID()), tagDefinition.getUuid(), tagDefinition.getAuthor(), ZonedDateTime.now().format(DateTimeFormatter.ofPattern(Version.DATETIMEPATTERN)), tagDefinition.getUserDefinedPropertyDefinitions(), tagDefinition.getTagsetDefinitionUuid());
Nodes systemPropertyElements = tagInstanceElement.getChildNodes(TeiElementName.f, AttributeValue.f_name_catma_system_property.getStartsWithFilter());
addProperties(tagDefinition, new AddPropertyHandler() {
public void addProperty(Property property) {
tagInstance.addSystemProperty(property);
}
}, systemPropertyElements);
Nodes userDefinedPropertyElements = tagInstanceElement.getChildNodes(TeiElementName.f, AttributeValue.f_name_catma_system_property.getNotStartsWithFilter());
addProperties(tagDefinition, new AddPropertyHandler() {
public void addProperty(Property property) {
tagInstance.addUserDefinedProperty(property);
}
}, userDefinedPropertyElements);
return tagInstance;
}
use of de.catma.tag.Property in project catma by forTEXT.
the class TeiUserMarkupCollectionSerializer method writeTagInstance.
private void writeTagInstance(TagInstance tagInstance, TeiElement textElement, TagLibrary tagLibrary) {
TeiElement fs = new TeiElement(TeiElementName.fs);
textElement.appendChild(fs);
fs.setID(tagInstance.getUuid());
fs.setAttributeValue(Attribute.type, tagInstance.getTagDefinitionId());
writeProperty(PropertyDefinition.SystemPropertyName.catma_markupauthor.name(), tagInstance.getAuthor(), fs);
writeProperty(PropertyDefinition.SystemPropertyName.catma_markuptimestamp.name(), tagInstance.getTimestamp(), fs);
writeProperty(PropertyDefinition.SystemPropertyName.catma_displaycolor.name(), tagLibrary.getTagDefinition(tagInstance.getTagDefinitionId()).getColor(), fs);
for (Property p : tagInstance.getUserDefinedProperties()) {
writeProperty(p, fs, tagInstance.getTagDefinitionId(), tagLibrary);
}
}
use of de.catma.tag.Property in project catma by forTEXT.
the class JsonLdWebAnnotation method getTagInstance.
public TagInstance getTagInstance() throws Exception {
TagInstance tagInstance = new TagInstance(this.getTagInstanceUuid(), getBody().getTag().substring(getBody().getTag().lastIndexOf('/') + 1), // author gets redefined with the system properties below
"", ZonedDateTime.now().format(DateTimeFormatter.ofPattern(Version.DATETIMEPATTERN)), // these get added with the user defined properties below
Collections.emptyList(), getBody().getTagset().substring(getBody().getTagset().lastIndexOf('/') + 1));
TreeMap<String, TreeMap<String, TreeSet<String>>> properties = this.body.getProperties();
for (Map.Entry<String, TreeMap<String, TreeSet<String>>> entry : properties.entrySet()) {
for (Map.Entry<String, TreeSet<String>> subEntry : entry.getValue().entrySet()) {
Property property = new Property(subEntry.getKey(), subEntry.getValue());
if (entry.getKey().equals(JsonLdWebAnnotationBody_Dataset.SYSTEM_PROPERTIES_KEY)) {
tagInstance.addSystemProperty(property);
} else {
tagInstance.addUserDefinedProperty(property);
}
}
}
return tagInstance;
}
use of de.catma.tag.Property in project catma by forTEXT.
the class XmlMarkupCollectionSerializationHandler method scanElements.
private void scanElements(StringBuilder contentBuilder, Element element, Stack<String> elementStack, TagManager tagManager, TagLibrary tagLibrary, Map<String, String> namespacePrefixToTagsetIdMap, AnnotationCollection userMarkupCollection, String docId, int docLength) throws Exception {
int start = contentBuilder.length();
StringBuilder pathBuilder = new StringBuilder();
for (int j = 0; j < elementStack.size(); j++) {
pathBuilder.append("/" + elementStack.get(j));
}
String parentPath = pathBuilder.toString();
elementStack.push(element.getLocalName());
String path = parentPath + "/" + elementStack.peek();
String tagName = element.getLocalName();
String prefix = element.getNamespacePrefix();
String tagsetId = namespacePrefixToTagsetIdMap.get(prefix);
if (tagsetId == null) {
tagsetId = KnownTagsetDefinitionName.DEFAULT_INTRINSIC_XML.asTagsetId();
}
TagsetDefinition tagset = tagLibrary.getTagsetDefinition(tagsetId);
String tagId = idGenerator.generate();
TagDefinition tagDefinition = tagset.getTagDefinitionsByName(tagName).findFirst().orElse(null);
String pathPropertyDefId = null;
if (tagDefinition == null) {
tagDefinition = new TagDefinition(tagId, elementStack.peek(), // no parent, hierarchy is collected in annotation property
null, tagsetId);
tagDefinition.addSystemPropertyDefinition(new PropertyDefinition(idGenerator.generate(PropertyDefinition.SystemPropertyName.catma_displaycolor.name()), PropertyDefinition.SystemPropertyName.catma_displaycolor.name(), Collections.singletonList(ColorConverter.toRGBIntAsString(ColorConverter.randomHex()))));
tagDefinition.addSystemPropertyDefinition(new PropertyDefinition(idGenerator.generate(PropertyDefinition.SystemPropertyName.catma_markupauthor.name()), PropertyDefinition.SystemPropertyName.catma_markupauthor.name(), Collections.singletonList(author)));
pathPropertyDefId = idGenerator.generate();
PropertyDefinition pathDef = new PropertyDefinition(pathPropertyDefId, "path", Collections.emptyList());
tagDefinition.addUserDefinedPropertyDefinition(pathDef);
tagManager.addTagDefinition(tagset, tagDefinition);
} else {
pathPropertyDefId = tagDefinition.getPropertyDefinition("path").getUuid();
}
for (int idx = 0; idx < element.getChildCount(); idx++) {
Node curChild = element.getChild(idx);
if (curChild instanceof Text) {
xmlContentHandler.addTextContent(contentBuilder, element, curChild.getValue());
} else if (curChild instanceof Element) {
// descent
scanElements(contentBuilder, (Element) curChild, elementStack, tagManager, tagLibrary, namespacePrefixToTagsetIdMap, userMarkupCollection, docId, docLength);
}
}
if (element.getChildCount() != 0) {
xmlContentHandler.addBreak(contentBuilder, element);
}
int end = contentBuilder.length();
Range range = new Range(start, end);
if (range.isSinglePoint()) {
int newStart = range.getStartPoint();
if (newStart > 0) {
newStart = newStart - 1;
}
int newEnd = range.getEndPoint();
if (newEnd < docLength - 1) {
newEnd = newEnd + 1;
}
range = new Range(newStart, newEnd);
}
TagInstance tagInstance = new TagInstance(idGenerator.generate(), tagDefinition.getUuid(), author, ZonedDateTime.now().format(DateTimeFormatter.ofPattern(Version.DATETIMEPATTERN)), tagDefinition.getUserDefinedPropertyDefinitions(), tagDefinition.getTagsetDefinitionUuid());
for (int i = 0; i < element.getAttributeCount(); i++) {
PropertyDefinition propertyDefinition = tagDefinition.getPropertyDefinition(element.getAttribute(i).getQualifiedName());
if (propertyDefinition == null) {
propertyDefinition = new PropertyDefinition(idGenerator.generate(), element.getAttribute(i).getQualifiedName(), Collections.singleton(element.getAttribute(i).getValue()));
tagManager.addUserDefinedPropertyDefinition(tagDefinition, propertyDefinition);
} else if (!propertyDefinition.getPossibleValueList().contains(element.getAttribute(i).getValue())) {
List<String> newValueList = new ArrayList<>();
newValueList.addAll(propertyDefinition.getPossibleValueList());
newValueList.add(element.getAttribute(i).getValue());
propertyDefinition.setPossibleValueList(newValueList);
}
Property property = new Property(propertyDefinition.getUuid(), Collections.singleton(element.getAttribute(i).getValue()));
tagInstance.addUserDefinedProperty(property);
}
Property pathProperty = new Property(pathPropertyDefId, Collections.singletonList(path));
tagInstance.addUserDefinedProperty(pathProperty);
TagReference tagReference = new TagReference(tagInstance, docId, range, userMarkupCollection.getId());
userMarkupCollection.addTagReference(tagReference);
elementStack.pop();
}
Aggregations