package uk.org.taverna.scufl2.api.common;

import java.net.URI;

import uk.org.taverna.scufl2.api.container.WorkflowBundle;

public class URITools {

	public URI relativeUriForBean(WorkflowBean bean, WorkflowBean relativeToBean) {
		URI rootUri = uriForBean(relativeToBean);
		URI beanUri = uriForBean(bean);
		return rootUri.relativize(beanUri);
	}

	private URI uriForBean(WorkflowBean bean) {
		if (bean instanceof Root) {
			Root root = (Root) bean;
			if (root.getSameBaseAs() == null) {
				if (root instanceof WorkflowBundle) {
					root.setSameBaseAs(WorkflowBundle.generateIdentifier());
				} else {
					throw new IllegalArgumentException(
							"sameBaseAs is null for bean " + bean);
				}
			}
			return root.getSameBaseAs();
		}
		if (bean instanceof Child) {
			Child child = (Child) bean;
			WorkflowBean parent = child.getParent();
			URI parentUri = uriForBean(parent);

			if (!parentUri.getPath().endsWith("/")) {
				parentUri = parentUri.resolve(parentUri.getPath() + "/");
			}
			String relation = child.getClass().getSimpleName() + "/";
			return parentUri.resolve(relation);
		}
		throw new IllegalArgumentException("Unsupported type "
				+ bean.getClass() + " for bean " + bean);
	}
}

@@ -389,16 +389,99 @@
 bean) %7B%0A
+%09%09if (bean == null) %7B%0A%09%09%09throw new NullPointerException(%22Bean can't be null%22);%0A%09%09%7D%0A
 %09%09if (be
@@ -927,16 +927,130 @@
 rent();%0A
+%09%09%09if (parent == null) %7B%0A%09%09%09%09throw new IllegalStateException(%22Bean does not have a parent: %22%0A%09%09%09%09%09%09+ child);%0A%09%09%09%7D%0A
 %09%09%09URI p

Check for parent null