public Annotation createNewAnnotation()

in taverna-scufl2-annotation/src/main/java/org/apache/taverna/scufl2/annotation/AnnotationTools.java [152:207]


	public Annotation createNewAnnotation(WorkflowBundle workflowBundle,
			Child<?> subject, URI predicate, String value) throws IOException {
		Object parent = subject.getParent();
		while (parent instanceof Child)
			parent = ((Child<?>) parent).getParent();
		if (parent != workflowBundle)
			throw new IllegalStateException(
					"annotations can only be added to bundles that their subjects are already a member of");
		if (predicate == null)
			throw new IllegalArgumentException(
					"annotation predicate must be non-null");
		if (value == null)
			throw new IllegalArgumentException(
					"annotation value must be non-null");
		
		// Add the annotation
		Annotation annotation = new Annotation();
		Calendar now = new GregorianCalendar();
		annotation.setParent(workflowBundle);

		String path = "annotation/" + annotation.getName() + ".ttl";
		URI bodyURI = URI.create(path);

		annotation.setTarget(subject);
		annotation.setAnnotatedAt(now);
		// annotation.setAnnotator();//FIXME
		annotation.setSerializedAt(now);
		URI annotatedSubject = uritools.relativeUriForBean(subject, annotation);
		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(" .");
		try {
			workflowBundle.getResources().addResource(turtle.toString(), path,
					"text/turtle");
		} catch (IOException e) {
			workflowBundle.getAnnotations().remove(annotation);
			throw e;
		}
        annotation.setBody(bodyURI);
        return annotation;
	}