package com.foo ;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Iterator;
import java.util.List ;

import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Unmarshaller;
import org.exolab.castor.xml.ValidationException;

public class MappingTest {
	
	// public static String dataDir = "c:/castortest/data/full/" ;
	public static String dataDir = "c:/castortest/data/working/" ;
    public static String dataFileName = dataDir + "OrderExample.xml" ;
	// public static String mappingFileDir = "c:/castortest/mapping/full/" ;
	public static String mappingDir = "c:/castortest/mapping/working/" ;
	public static String mappingFileName = mappingDir + "OrderMapping.xml" ;

	public static void main(String[] args) {
		try {
			File inputXMLData = new File(dataFileName) ;
			Reader inputXMLReader = new FileReader(inputXMLData) ;
			MappingTest test = new MappingTest() ;
			Order order = test.test(inputXMLReader, mappingFileName) ;
		} catch (FileNotFoundException e) {
			e.printStackTrace() ;
		} catch (Throwable t) {
			t.printStackTrace() ;
		}
	}

	public Order test(Reader inputXMLReader, String mappingFileName) {
		try {
			Mapping testMapping= new org.exolab.castor.mapping.Mapping();
			testMapping.loadMapping(mappingFileName);
			Unmarshaller unmarshaller = new Unmarshaller(Order.class);
			unmarshaller.setMapping(testMapping);   
			Order order = (Order)(unmarshaller.unmarshal(inputXMLReader));
			System.out.println("" + order) ;
			return order ;
		} catch (MarshalException e) {
			e.printStackTrace() ;
			throw new RuntimeException("Castor MarshallException") ;
		} catch (MappingException e) {
			e.printStackTrace();
			throw new RuntimeException("Castor mapping exception") ;
		} catch (ValidationException e) {   
			e.printStackTrace() ;
			throw new RuntimeException("XML Validation exception") ;
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException("IOexception") ;
		}
	}
    
}
