/*
 * Created on Aug 21, 2003
 */
package fr.eisti.lassi.elearning;

import java.io.IOException;

import javax.xml.transform.TransformerException;

import org.apache.cocoon.ProcessingException;
import org.apache.cocoon.xml.dom.DOMUtil;
import org.xml.sax.SAXException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * Object responsible for building a ProcessMap out of an XML description of it.
 * @author Cedric Vidal
 * @see fr.eisti.lassi.elearning.ProcessMap
 */
public class DocumentProcessMapLoader {
	private Document doc = null;
	private ProcessMap processMap = null;
	
	public DocumentProcessMapLoader(Document doc) throws ProcessingException, SAXException, IOException {
		this.doc = doc;
	}
	
	public ProcessMap load() throws TransformerException {
		Node rootNode = doc.getFirstChild();
		NodeList relations = DOMUtil.selectNodeList(rootNode, "relations/relation");
		int length = relations.getLength();
		processMap = new ProcessMap();
		for( int i = 0; i < length; i++ ) {
			Node relation = relations.item(i);
			String from = relation.getAttributes().getNamedItem("from").getNodeValue();
			String to = relation.getAttributes().getNamedItem("to").getNodeValue();
			processMap.setStepAfter(from, to);
		}
		return processMap;
	}
}
