package com.bitrazor.test;

import java.net.MalformedURLException;
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.RegionDescriptionFileURL;
import com.bitrazor.jaxb.regionmaster.RegionDescriptionFileURLs;
import com.bitrazor.jaxb.regionmaster.Regions;

public class Main {

	public static void main(String[] args) {
		JAXBContext jc = null;
		Unmarshaller u = null;
		Regions regions = 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("com.bitrazor.jaxb.regionmaster");
		} catch (JAXBException e) {
			e.printStackTrace();
		}
		
		try {
			// Create an unmarshaller (unmarshal from XML to Java objects)
			u = jc.createUnmarshaller();
		} catch (JAXBException e) {
			e.printStackTrace();
		}
		
		try {
			 URL url = new URL( "http://bitrazor.com/content/tivo/hme/trafficcam/RegionMaster.xml" );
			 // Use the following line to read from a file instead of a URL:
			 // regions = (Regions)u.unmarshal(new FileInputStream("RegionMaster.xml"));
			regions = (Regions)u.unmarshal(url);
		} catch (JAXBException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}

        // regionList get assigned to the first region in regions
		List regionList = regions.getRegion(); 
		
        // For each region
		for (Iterator iter = regionList.iterator(); iter.hasNext();) {
			Region region = (Region)iter.next();
			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());
			}
		}
	}
	
}

