package com.example;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeModel;
import javax.xml.bind.JAXBException;

import uk.org.taverna.scufl2.api.common.Scufl2Tools;
import uk.org.taverna.scufl2.api.common.URITools;
import uk.org.taverna.scufl2.api.configurations.Configuration;
import uk.org.taverna.scufl2.api.container.WorkflowBundle;
import uk.org.taverna.scufl2.api.core.Processor;
import uk.org.taverna.scufl2.api.core.Workflow;
import uk.org.taverna.scufl2.api.io.ReaderException;
import uk.org.taverna.scufl2.api.io.WorkflowBundleIO;
import uk.org.taverna.scufl2.api.profiles.Profile;
import uk.org.taverna.scufl2.api.property.PropertyException;
import uk.org.taverna.scufl2.api.property.PropertyResource;

public class ProcessorNames {

	public static void main(String[] args) throws JAXBException, IOException,
			ReaderException {
		WorkflowBundleIO io = new WorkflowBundleIO();
		ProcessorNames processorNames = new ProcessorNames();
		for (String filename : args) {
			// NOTE: We give 'null' as filetype so it will guess based on filename
			WorkflowBundle ro = io.readBundle(new File(filename), null);
//			System.out.print(filename + ": ");
//			System.out.println(processorNames.showProcessorNames(ro));
			System.out.println(processorNames.showProcessorTree(ro));
		}
	}
	private Scufl2Tools scufl2Tools = new Scufl2Tools();

	private URITools uriTools = new URITools();

	private Workflow findNestedWorkflow(Processor processor) {
		URI NESTED_WORKFLOW = URI
				.create("http://ns.taverna.org.uk/2010/activity/nested-workflow");

		WorkflowBundle bundle = processor.getParent().getParent();
		// Look for nested workflows
		Profile mainProfile = bundle.getMainProfile();
		Configuration activityConfig = scufl2Tools
				.configurationForActivityBoundToProcessor(processor,
						mainProfile);
		if (activityConfig != null
				&& activityConfig.getConfigurableType().equals(
						NESTED_WORKFLOW.resolve("#Config"))) {
			PropertyResource props = activityConfig.getPropertyResource();
			try {
				URI nestedWfRel = props
						.getPropertyAsResourceURI(NESTED_WORKFLOW
								.resolve("#workflow"));
				URI nestedWf = uriTools.uriForBean(mainProfile).resolve(
						nestedWfRel);
				Workflow wf = (Workflow) uriTools.resolveUri(nestedWf, bundle);
				return wf;
			} catch (PropertyException ex) {
			}
		}
		return null;
	}

	private void findProcessors(WorkflowBundle ro, Workflow workflow,
			DefaultMutableTreeNode parent) {
		for (Processor processor : workflow.getProcessors()) {
			DefaultMutableTreeNode processorNode = new DefaultMutableTreeNode(
					processor.getName());
			parent.add(processorNode);
			Workflow wf = findNestedWorkflow(processor);
			if (wf != null) {
				findProcessors(ro, wf, processorNode);
			}
		}

	}

	public TreeModel makeProcessorTree(WorkflowBundle workflowBundle)
			throws JAXBException, IOException {
		Workflow workflow = workflowBundle.getMainWorkflow();
		TreeModel treeModel = new DefaultTreeModel(new DefaultMutableTreeNode(
				workflow.getName()));
		DefaultMutableTreeNode parent = (DefaultMutableTreeNode) treeModel
				.getRoot();

		findProcessors(workflowBundle, workflow, parent);
		return treeModel;
	}

	public List<String> showProcessorNames(WorkflowBundle ro)
			throws JAXBException, IOException {
		ArrayList<String> names = new ArrayList<String>();
		for (Processor processor : ro.getMainWorkflow().getProcessors()) {
			names.add(processor.getName());
		}
		Collections.sort(names);
		return names;
	}

	public String showProcessorTree(WorkflowBundle ro) throws JAXBException,
			IOException {
		TreeModel treeModel = makeProcessorTree(ro);
		return treeModelAsString(treeModel);
	}

	public String treeModelAsString(TreeModel treeModel) {
		StringBuffer sb = new StringBuffer();
		Object root = treeModel.getRoot();
		treeModelAsString(treeModel, root, sb, "");
		return sb.toString();
	}

	protected void treeModelAsString(TreeModel treeModel, Object parent,
			StringBuffer sb, String indentation) {
		sb.append(indentation);
		int childCount = treeModel.getChildCount(parent);
		if (childCount == 0) {
			sb.append("- ");
		} else {
			sb.append("+ ");
			indentation = indentation + "  ";
		}
		sb.append(parent);
		sb.append("\n");
		for (int i = 0; i < childCount; i++) {
			Object child = treeModel.getChild(parent, i);
			treeModelAsString(treeModel, child, sb, indentation);
		}
	}
}
@@ -68,29 +68,8 @@
 on;%0A
-import java.net.URI;%0A
 impo
@@ -92,16 +92,16 @@
 ayList;%0A
+
 import j
@@ -416,71 +416,8 @@
 ls;%0A
-import uk.org.taverna.scufl2.api.configurations.Configuration;%0A
 impo
@@ -729,129 +729,8 @@
 ile;
-%0Aimport uk.org.taverna.scufl2.api.property.PropertyException;%0Aimport uk.org.taverna.scufl2.api.property.PropertyResource;
 %0A%0Apu
@@ -1468,900 +1468,145 @@
 %7B%0A%09%09
-URI NESTED_WORKFLOW = URI%0A%09%09%09%09.create(%22http://ns.taverna.org.uk/2010/activity/nested-workflow%22);%0A%0A%09%09WorkflowBundle bundle = processor.getParent().getParent();%0A%09%09// Look for nested workflows%0A%09%09Profile mainProfile = bundle.getMainProfile();%0A%09%09Configuration activityConfig = scufl2Tools%0A%09%09%09%09.configurationForActivityBoundToProcessor(processor,%0A%09%09%09%09%09%09mainProfile);%0A%09%09if (activityConfig != null%0A%09%09%09%09&& activityConfig.getConfigurableType().equals(%0A%09%09%09%09%09%09NESTED_WORKFLOW.resolve(%22#Config%22))) %7B%0A%09%09%09PropertyResource props = activityConfig.getPropertyResource();%0A%09%09%09try %7B%0A%09%09%09%09URI nestedWfRel = props%0A%09%09%09%09%09%09.getPropertyAsResourceURI(NESTED_WORKFLOW%0A%09%09%09%09%09%09%09%09.resolve(%22#workflow%22));%0A%09%09%09%09URI nestedWf = uriTools.uriForBean(mainProfile).resolve(%0A%09%09%09%09%09%09nestedWfRel);%0A%09%09%09%09Workflow wf = (Workflow) uriTools.resolveUri(nestedWf, bundle);%0A%09%09%09%09return wf;%0A%09%09%09%7D catch (PropertyException ex) %7B%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return null
+Profile profile = processor.getParent().getParent().getMainProfile();%0A%09%09return scufl2Tools.nestedWorkflowForProcessor(processor, profile)
 ;%0A%09%7D

Use scufl2tools.nestedWorkflowForProcessor instead