/*
 * ProfileJaxbTest.java
 */


package edu.missouri.cecs.ideal.profiles;

import edu.missouri.cecs.ideal.profiles.jaxb.*; // bad, but it will do for now..
//import edu.missouri.cecs.ideal.util.XercesUtils;
//import org.w3c.dom.Element;
import javax.xml.bind.Element;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.util.Iterator;
import java.util.List;


/**
 * A simple test of profile marshalling and unmarshalling.
 *
 * @author Steven Cummings [<a href="mailto:steven@mizzou.edu">Email</a>]
 */
public class ProfileJaxbTest extends Object {

    private ProfileJaxbTest() {}

    /**
     * The entry point for the test-program.
     *
     * @param args the command line arguments.
     * @throws Exception If there is any problem running the program.
     */
    public static void main(String[] args) throws Exception {
        String inputFileName = "C:\\IDEALWork\\sample-profile.xml";
        File file = new File(inputFileName);
        JAXBContext jaxbContext = JAXBContext.newInstance(
                "edu.missouri.cecs.ideal.profiles.jaxb");
        Marshaller marshaller = jaxbContext.createMarshaller();
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        unmarshaller.setValidating(false);
        pl("unmarshaller is validating:" + unmarshaller.isValidating());
        Learnerinformation learnerinformation = (Learnerinformation)
                unmarshaller.unmarshal(file);
        pl("Unmarshalled " + inputFileName + " successfully.");
        pl("lang = " + learnerinformation.getLang());
        comment(learnerinformation.getComment());
        contenttype(learnerinformation.getContentype());
        pl("getIdentificationOrGoalOrQcl items:");
        Iterator identificationOrGoalOrQcl =
                learnerinformation.getIdentificationOrGoalOrQcl().iterator();
        for(; identificationOrGoalOrQcl.hasNext(); )
            pl("\t" + identificationOrGoalOrQcl.next().getClass().getName());
        pl("\nPreparing to re-marshall the objects to System.out:");
        marshaller.marshal(learnerinformation, System.out);
    }

    private static void p(String s)  { System.out.print(s);   }
    private static void pl(String s) { System.out.println(s); }
    private static String getPrefix(int level) {
        String prefix = "";
        for(int i = 0; i < level; ++i)
            prefix += "\t";
        return prefix;
    }
    private static void comment(CommentType comment) {
        comment(comment, 0);
    }
    private static void comment(CommentType comment, int level) {
        String prefix = getPrefix(level);
        pl(prefix + "CommentType:");
        if(comment != null) {
            pl(prefix + "\tlang = " + comment.getLang());
            pl(prefix + "\tvalue = " + comment.getValue());
        } else {
            pl(prefix + "\tnull");
        }
    }
    private static void contenttype(ContentypeType contenttype) {
        contenttype(contenttype, 0);
    }
    private static void contenttype(ContentypeType contenttype, int level) {
        String prefix = getPrefix(level);
        pl(prefix + "ContentypeType:");
        if(contenttype != null) {
            comment(contenttype.getComment(), level + 1);
            extcontenttype(contenttype.getExtContentype(), level + 1);
            Iterator referentialOrTemporalOrPrivacy =
                    contenttype.getReferentialOrTemporalOrPrivacy().iterator();
            pl(prefix + "\treferentialOrTemporalOrPrivacy items:");
            for(; referentialOrTemporalOrPrivacy.hasNext(); )
                pl(prefix + "\t\t" + referentialOrTemporalOrPrivacy.next().getClass().getName());
        } else {
            pl(prefix + "\tnull");
        }
    }
    private static void extcontenttype(ExtensionType extcontenttype) {
        extcontenttype(extcontenttype, 0);
    }
    private static void extcontenttype(ExtensionType extcontenttype, int level) {
        String prefix = getPrefix(level);
        pl(prefix + "ExtensionType:");
        if(extcontenttype != null) {
            Element any = extcontenttype.getAny();
            pl(prefix + "\tany = " + any.toString());
        } else {
            pl(prefix + "\tnull");
        }
    }
}