package Latest;


import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import javax.xml.transform.Source;

import javax.imageio.ImageIO;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.transform.stream.StreamSource;
import java.io.*;



public class JAXBTest {

    public static void main(String[] args) {
        
            try
			{
				JAXBTest.textPlainTest();
			   JAXBTest.textXmlTest();
			  JAXBTest.imageJpegTest();}
			/*catch(Exception e)
			{e.printStackTrace();
			
			  try{JAXBTest.imageJpegTest();}
			  catch(JAXBException jaxbe) {
            jaxbe.printStackTrace();
        } catch(IOException ioe) {
            ioe.printStackTrace();}
			  catch(Exception ne)
			{ne.printStackTrace();}*/

			catch(JAXBException jaxbe) {
            jaxbe.printStackTrace();
        } catch(IOException ioe) {
            ioe.printStackTrace();}

			  
            
		       
        
    }
    
    public static void textPlainTest() throws JAXBException {
        System.out.println("Running text/plain test.");
        
        org.textplain.test.ImageDepot imageDepot = new org.textplain.test.ObjectFactory().createImageDepot();
        imageDepot.setImageData("test input");
        org.textplain.test.ObjectFactory factory = new org.textplain.test.ObjectFactory();
        org.textplain.test.Invoke request = factory.createInvoke();
        request.setInput(imageDepot);
        
        JAXBContext jbc = JAXBContext.newInstance("org.textplain.test");
        
        StringWriter sw = new StringWriter();
        
        Marshaller marshaller = jbc.createMarshaller();
        marshaller.setAttachmentMarshaller((new AttachmentMarshallerTestImpl()));
        marshaller.marshal(request, sw);
						        //throw new Exception();

        
		System.out.println("This output should be preceded by a print statement from AttachmentMarshallerTestImpl.");
        System.out.println(sw.toString());
    }
    
    public static void imageJpegTest() throws JAXBException, IOException {
        System.out.println("Running image/jpeg test.");
        
        Image image = ImageIO.read(new File("image1.jpeg"));
        
        org.imagejpeg.test.ImageDepot imageDepot = new org.imagejpeg.test.ObjectFactory().createImageDepot();
        imageDepot.setImageData(image);
        org.imagejpeg.test.ObjectFactory factory = new org.imagejpeg.test.ObjectFactory();
        org.imagejpeg.test.Invoke request = factory.createInvoke();
        request.setInput(imageDepot);
        
        JAXBContext jbc = JAXBContext.newInstance("org.imagejpeg.test");
        
        StringWriter sw = new StringWriter();
        
        Marshaller marshaller = jbc.createMarshaller();
        marshaller.setAttachmentMarshaller((new AttachmentMarshallerTestImpl()));
        marshaller.marshal(request, sw);

        
        System.out.println("This output should be preceded by a print statement from AttachmentMarshallerTestImpl.");
        System.out.println(sw.toString());
				       // throw new Exception();

    }
	    public static void textXmlTest() throws  JAXBException ,IOException {
        System.out.println("Running text/xml test.");
		//Source source = null;
		try
		{
		Source source = (Source)new StreamSource(new FileInputStream(new File("modes.xml")));
		        org.textxml.test.ImageDepot imageDepot = new org.textxml.test.ObjectFactory().createImageDepot();
        imageDepot.setImageData(source);
        org.textxml.test.ObjectFactory factory = new org.textxml.test.ObjectFactory();
        org.textxml.test.Invoke request = factory.createInvoke();
        request.setInput(imageDepot);
        
        JAXBContext jbc = JAXBContext.newInstance("org.textxml.test");
        
        StringWriter sw = new StringWriter();
        
        Marshaller marshaller = jbc.createMarshaller();
        marshaller.setAttachmentMarshaller((new AttachmentMarshallerTestImpl()));
        marshaller.marshal(request, sw);
				System.out.println("This output should be preceded by a print statement from AttachmentMarshallerTestImpl.");
        System.out.println(sw.toString());


		}
		catch (IOException e)
		{e.printStackTrace();
		}
        
						        //throw new Exception();

        
    }

}