package com.bitrazor.test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.Iterator;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import com.bitrazor.jaxb.regionmaster.Region;
import com.bitrazor.jaxb.regionmaster.RegionDescriptionFileURLs;
import com.bitrazor.jaxb.regionmaster.Regions;

public class RegionMaster {
	public static final String jaxbPackage = "com.bitrazor.jaxb.regionmaster";

	URL masterURL = null;

	String masterFilename = null;
	private Regions regions = null;
	private JAXBContext jaxbContext = null;
	private Unmarshaller unmarshaller = null;

	public RegionMaster(URL masterURL) {
		setMasterURL(masterURL);
		setMasterFilename(null);
		setJaxbContext(RegionMaster.setContext(jaxbPackage));
		unmarshaller = RegionMaster.getUnmarshaller(jaxbContext);
		try {
			regions = (Regions) unmarshaller.unmarshal(masterURL);
		} catch (JAXBException e) {
			e.printStackTrace();
		}
	}

	public RegionMaster(String masterFilename) {
		setMasterFilename(masterFilename);
		setMasterURL(null);
		JAXBContext jc = RegionMaster.setContext(jaxbPackage);
		unmarshaller = RegionMaster.getUnmarshaller(jc);
		try {
			regions = (Regions)unmarshaller.unmarshal(new FileInputStream("RegionMaster.xml"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (JAXBException e) {
			e.printStackTrace();
		}

	}

	public void dump() {
		List regionList = regions.getRegion();
		// For each region
		for (Iterator iter = regionList.iterator(); iter.hasNext();) {
			dump((Region) iter.next());
		}
	}


	public String getMasterFilename() {
		return masterFilename;
	}

	public void setMasterFilename(String filename) {
		this.masterFilename = filename;
	}

	public URL getMasterURL() {
		return masterURL;
	}

	public void setMasterURL(URL masterURL) {
		this.masterURL = masterURL;
	}

	public static JAXBContext setContext(String jaxbPackage) {
		JAXBContext jc = null;
		try {

			// Set the JAXB context to the package name we uses
			// when we created all the JAXB classes from the XSD
			jc = JAXBContext.newInstance(jaxbPackage);
		} catch (JAXBException e) {
			e.printStackTrace();
		}
		return jc;
	}

	public static Unmarshaller getUnmarshaller(JAXBContext jc) {
		Unmarshaller u = null;

		try {
			// Create an unmarshaller (unmarshal from XML to Java objects)
			u = jc.createUnmarshaller();
		} catch (JAXBException e) {
			e.printStackTrace();
		}
		return u;
	}

	public static Regions getRegions(URL masterURL, Unmarshaller u) {
		Regions regions = null;

		return regions;
	}

	// Display contents for a region to the console. This should be called
	// for each region in the RegionMaster file.
	public static void dump(Region region) {
		System.out.println("Region found:");
		System.out.println("  ID: " + region.getRegionID() + ", Name: "
				+ region.getRegionName());

		RegionDescriptionFileURLs rdfurls = (RegionDescriptionFileURLs) region
				.getRegionDescriptionFileURLs();
		List rdfUrlList = rdfurls.getRegionDescriptionFileURL();

		// Print out each RDF URL
		for (Iterator rdfurl = rdfUrlList.iterator(); rdfurl.hasNext();) {
			System.out.println("  URL: " + rdfurl.next());
		}

	}

	public JAXBContext getJaxbContext() {
		return jaxbContext;
	}
	

	public void setJaxbContext(JAXBContext jc) {
		this.jaxbContext = jc;
	}
	

	public Unmarshaller getUnmarshaller() {
		return unmarshaller;
	}
	

	public void setU(Unmarshaller unmarshaller) {
		this.unmarshaller = unmarshaller;
	}
	

}

