/**
 * ScheduleMarshaller.java
 * Created Jun 20, 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.EntityMarshaller;

/**
 * Marshals a schedule
 * 
 * @author Sahil Verma
 */
public class ScheduleMarshaller {

	private static final String MAPPING = "com/tandbergtv/watchpoint/pmm/entities/bind/mapping.xml";
	
	protected ScheduleMarshaller() {
	}

	/**
	 * Returns a new instance of the marshaller
	 * 
	 * @return
	 */
	public static ScheduleMarshaller newInstance() {
		return new ScheduleMarshaller();
	}

	/**
	 * Marshals the schedule and returns the root of the converted DOM
	 * 
	 * @param schedule
	 * @return
	 */
	public Node marshal(Schedule schedule) {
		InputStream stream = getClass().getClassLoader().getResourceAsStream(MAPPING);
		
		if (stream == null)
			throw new NullPointerException();
		
		Mapping mapping = new Mapping();
		
		mapping.loadMapping(new InputSource(stream));
		
		EntityMarshaller<Schedule> marshaller = EntityMarshaller.newInstance(); 
		
		return marshaller.marshal(schedule, mapping);
	}
}
