package xml;

import uk.ac.bbk.dcs.automed.modelmanagement.XMLDSSModelDef;
import uk.ac.bbk.dcs.automed.qproc.QueryProcessor;
import uk.ac.bbk.dcs.automed.qproc.QueryProcessorConfiguration;
import uk.ac.bbk.dcs.automed.qproc.iql.ASG;
import uk.ac.bbk.dcs.automed.xml.XMLConstants;
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.bbk.dcs.automed.xml.wrappers.XMLWrapper;
import uk.ac.bbk.dcs.automed.xml.wrappers.XMLWrapperFactory;
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) {
		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";

			XMLWrapperFactory xwf = new DOMWrapperFactory();
			xwf.getModelDefinition().setFeatures(XMLDSSModelDef.WHITESPACE_COLLAPSE);
			XMLWrapper xw = (XMLWrapper) AutoMedWrapper.selectNewAutoMedWrapper("", "", null, XMLConstants.driverDOM, URI, sn, xwf);

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

			PrintUtility.prettyPrint(((DOMWrapper)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());
				QueryProcessorConfiguration qpc = new QueryProcessorConfiguration();
				QueryProcessor qp = new QueryProcessor();

				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);
					qp.process(q, src, tgts, qpc);
					System.out.println("Query after evaluator: " + q);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		} catch (Throwable t) {
			t.printStackTrace();
		}
	}
}