in taverna-scufl2-t2flow/src/main/java/org/apache/taverna/scufl2/translator/t2flow/T2FlowParser.java [653:758]
public void parseAnnotations(WorkflowBean annotatedBean,
Annotations annotations) throws ReaderException {
// logger.fine("Checking annotations for " + annotatedSubject);
Map<String, NetSfTavernaT2AnnotationAnnotationAssertionImpl> annotationElems = new HashMap<>();
if (annotations == null
|| annotations.getAnnotationChainOrAnnotationChain22() == null)
return;
for (JAXBElement<AnnotationChain> el : annotations
.getAnnotationChainOrAnnotationChain22()) {
NetSfTavernaT2AnnotationAnnotationAssertionImpl ann = el.getValue()
.getNetSfTavernaT2AnnotationAnnotationChainImpl()
.getAnnotationAssertions()
.getNetSfTavernaT2AnnotationAnnotationAssertionImpl();
String annClass = ann.getAnnotationBean().getClazz();
if (annotationElems.containsKey(annClass)
&& annotationElems.get(annClass).getDate()
.compareToIgnoreCase(ann.getDate()) > 0)
// ann.getDate() is less than current 'latest' annotation, skip
continue;
annotationElems.put(annClass, ann);
}
for (String clazz : annotationElems.keySet()) {
NetSfTavernaT2AnnotationAnnotationAssertionImpl ann = annotationElems.get(clazz);
Calendar cal = parseDate(ann.getDate());
String value = null;
String semanticMediaType = TEXT_TURTLE;
for (Object obj : ann.getAnnotationBean().getAny()) {
if (!(obj instanceof Element))
continue;
Element elem = (Element) obj;
if (!(elem.getNamespaceURI() == null))
continue;
if (elem.getLocalName().equals("text")) {
value = elem.getTextContent().trim();
break;
}
if (clazz.equals(SEMANTIC_ANNOTATION)) {
if (elem.getLocalName().equals("content"))
value = elem.getTextContent().trim();
if (elem.getLocalName().equals("mimeType"))
semanticMediaType = elem.getTextContent().trim();
}
}
if (value != null) {
// Add the annotation
Annotation annotation = new Annotation();
WorkflowBundle workflowBundle = parserState.get()
.getCurrentWorkflowBundle();
annotation.setParent(workflowBundle);
String path = "annotation/" + annotation.getName() + ".ttl";
URI bodyURI = URI.create(path);
annotation.setTarget(annotatedBean);
annotation.setAnnotatedAt(cal);
// annotation.setAnnotator();
annotation.setSerializedBy(t2flowParserURI);
annotation.setSerializedAt(new GregorianCalendar());
URI annotatedSubject = uriTools.relativeUriForBean(
annotatedBean, annotation);
String body;
if (clazz.equals(SEMANTIC_ANNOTATION)) {
String baseStr = "@base <"
+ annotatedSubject.toASCIIString() + "> .\n";
body = baseStr + value;
} else {
// Generate Turtle from 'classic' annotation
URI predicate = getPredicatesForClass().get(clazz);
if (predicate == null) {
if (isStrict())
throw new ReaderException(
"Unsupported annotation class " + clazz);
return;
}
StringBuilder turtle = new StringBuilder();
turtle.append("<");
turtle.append(annotatedSubject.toASCIIString());
turtle.append("> ");
turtle.append("<");
turtle.append(predicate.toASCIIString());
turtle.append("> ");
// A potentially multi-line string
turtle.append("\"\"\"");
// Escape existing \ to \\
String escaped = value.replace("\\", "\\\\");
// Escape existing " to \" (beware Java's escaping of \ and " below)
escaped = escaped.replace("\"", "\\\"");
turtle.append(escaped);
turtle.append("\"\"\"");
turtle.append(" .");
body = turtle.toString();
}
try {
workflowBundle.getResources().addResource(body, path, semanticMediaType);
} catch (IOException e) {
throw new ReaderException("Could not store annotation body to " + path, e);
}
annotation.setBody(bodyURI);
}
}
}