/**
 * TitleUnmarshaller.java
 * Created Jun 20, 2008
 * Copyright (c) TANDBERG Television 2007-2008
 */
package com.tandbergtv.watchpoint.pmm.title.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.Title;
import com.tandbergtv.watchpoint.pmm.entities.bind.EntityUnmarshaller;

/**
 * Unmarshals the DOM equivalent of a title
 * 
 * @author Sahil Verma
 */
public class TitleUnmarshaller {
	
	private static final String MAPPING = "com/tandbergtv/watchpoint/pmm/entities/bind/mapping.xml";
	
	/**
	 * Creates a TitleUnmarshaller
	 */
	protected TitleUnmarshaller() {
	}
	
	/**
	 * Returns a new instance of the unmarshaller
	 * 
	 * @return
	 */
	public static TitleUnmarshaller newInstance() {
		return new TitleUnmarshaller();
	}
	
	/**
	 * Unmarshals the DOM of the title and returns the object
	 * 
	 * @param node
	 * @return
	 */
	public Title unmarshal(Node node) {
		Title title = null;
		InputStream stream = getClass().getClassLoader().getResourceAsStream(MAPPING);
		
		if (stream == null)
			throw new NullPointerException();
		
		Mapping mapping = new Mapping();
		
		mapping.loadMapping(new InputSource(stream));
		
		EntityUnmarshaller<Title> unmarshaller = EntityUnmarshaller.newInstance(); 
		title = unmarshaller.unmarshal(node, mapping);
		
		return title;
	}
}
