package semistructured.xml.validation;

import uk.ac.bbk.dcs.automed.qproc.FunctionTable;
import uk.ac.bbk.dcs.automed.qproc.StandardFunctionTable;
import uk.ac.bbk.dcs.automed.qproc.annotate.FragmentProcessor;
import uk.ac.bbk.dcs.automed.qproc.evaluate.Evaluator;
import uk.ac.bbk.dcs.automed.qproc.iql.ASG;
import uk.ac.bbk.dcs.automed.qproc.reformulate.GAVQueryReformulationProvider;
import uk.ac.bbk.dcs.automed.xml.utilities.PrintUtility;
import uk.ac.bbk.dcs.automed.xml.wrappers.DOMWrapper;
import uk.ac.bbk.dcs.automed.xml.wrappers.DOMWrapperFactory;
import uk.ac.ic.doc.automed.reps.Schema;
import uk.ac.ic.doc.automed.reps.SchemaObject;
import uk.ac.ic.doc.automed.wrappers.AutoMedWrapper;

public class DTDValidation {

    public static void main(String[] args) {
        char separator = '$';
        try {
            //////////////////////////////////////
            //Create wrapper instance for XML file
            //Wrapper instance creates XMLDataSourceSchema in the repository from its constructor
            String URI = "http://www.dcs.bbk.ac.uk/~lucas/automed/examples/recipes.xml";
            String sn = "testDOM";

            if (Schema.exists(sn))
                Schema.retract(sn);

            DOMWrapper xw = (DOMWrapper) AutoMedWrapper.selectNewAutoMedWrapper("", "", null, "org.w3c.dom", URI, null, null);
            DOMWrapperFactory xwf = (DOMWrapperFactory) xw.getAutoMedWrapperFactory();

            Schema s = xw.getSchema(sn);
            s.listSchema();

            PrintUtility.prettyPrint(xw.getInputDoc(), xw.getSchemaName());
            PrintUtility.prettyPrint(xw.getSchemaUIDDoc(), xw.getSchemaName());

            ////////////////////////////////////
            //TEST WITH QUERY PROCESSOR
            SchemaObject[] so = s.getSchemaObjects();

            try {
                Schema src = Schema.getSchema(s.getName());
                Schema[] tgts = new Schema[1];
                tgts[0] = Schema.getSchema(s.getName());
                GAVQueryReformulationProvider qr = new GAVQueryReformulationProvider(src, tgts);
                FragmentProcessor fp = new FragmentProcessor();
                FunctionTable ft = new StandardFunctionTable();
                Evaluator e = new Evaluator(ft);

                ASG q;
                System.out.println();
                PrintUtility.title("IQL Scheme Queries");
                for (int z = 0; z < so.length; z++) {
                    System.out.println(so[z].getSchemeNamesString().trim());
                    q = new ASG(so[z].getSchemeNamesString().trim());
                    if (q.toString().indexOf(',') != -1)
                        if (q.toString().substring(q.toString().indexOf(',')).indexOf(',') != -1)
                            q = new ASG("<<" + q.toString().substring(q.toString().indexOf(',') + 1));
                    System.out.println("\nOriginal query: " + q);
                    qr.reformulate(q);
                    System.out.println("Query after query reformulator: " + q);
                    fp.process(q);
                    System.out.println("Query after fragment processor: " + q);
                    e.evaluate(q);
                    System.out.println("Query after evaluator: " + q);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
}