/**
 * ScheduleUnmarshaller.java
 * Created Jun 23, 2008
 * Copyright (c) TANDBERG Television 2007-2008
 */
package com.tandbergtv.watchpoint.pmm.schedule.bind;

import java.io.InputStream;

import org.exolab.castor.mapping.Mapping;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;

import com.tandbergtv.watchpoint.pmm.entities.Schedule;
import com.tandbergtv.watchpoint.pmm.entities.bind.EntityUnmarshaller;

/**
 * Unmarshals a schedule - pitch or planner
 * @author Sahil Verma
 */
public class ScheduleUnmarshaller {
	
	private static final String MAPPING = "com/tandbergtv/watchpoint/pmm/entities/bind/mapping.xml";
	
	protected ScheduleUnmarshaller() {
	}
	
	/**
	 * Returns a new instance of the unmarshaller
	 * 
	 * @return
	 */
	public static ScheduleUnmarshaller newInstance() {
		return new ScheduleUnmarshaller();
	}
	
	/**
	 * Unmarshals the specified DOM equivalent of a schedule into the corresponding object
	 * 
	 * @param node
	 * @return
	 */
	public Schedule unmarshal(Node node) {
		Schedule schedule = null;
		InputStream stream = getClass().getClassLoader().getResourceAsStream(MAPPING);
		
		if (stream == null)
			throw new NullPointerException();
		
		Mapping mapping = new Mapping();
		
		mapping.loadMapping(new InputSource(stream));
		
		EntityUnmarshaller<Schedule> unmarshaller = EntityUnmarshaller.newInstance(); 
		schedule = unmarshaller.unmarshal(node, mapping);
		
		return schedule;
	}
}
