package com.jxml.quick.util.validate;
import com.jxml.quick.model.qdml.*;
import com.jxml.quick.*;
import org.xml.sax.*;
import java.util.*;
import com.jxml.quick.ocm.*;
import java.io.*;

/**
 * Applies QDML schema to the input data.
 */
public class QVQDML extends OCMrootPeerSupportLite 
{
	/**
	 * The output model root.
	 */
	public OCMErrorRoot qe=null;

	/**
	 * The list of errors.
	 */
	public ArrayList errors=null;

	public QDMQDML qdml=null;
	public QVErrorContext.SubErrorContext errorContext=null;
	public OCMcontextSupport.SubContextSupport scs=null;
	public QDoc dataDoc=null;
	public OCMelement element=null;
	public String tagNameOfRootData=null;
	public String rootLabel=null;
	public Map coinMap=null;

	// Implement method on OCMpeer
	public void cvt(Object param)
		throws SAXException, IOException
	{
		qdml=(QDMQDML)dataModel;
		errorContext=(QVErrorContext.SubErrorContext)context;
		scs=(OCMcontextSupport.SubContextSupport)errorContext.subDataSource;
		dataDoc=scs.getDoc();
		element=(OCMelement)dataDoc.getRoot();

		qdml.init();

		// Test the root element--can this element be the root???

		tagNameOfRootData=element.name; // is this a valid root tag name?
		rootLabel=qdml.root;
		coinMap=qdml.coinMap;

		validateRoot();
	}

	public void validateRoot()
		throws SAXException, IOException
	{
		if (rootLabel.equals(tagNameOfRootData))
			return;
/*
		QDMCoin coin=(QDMCoin)coinMap.get(rootLabel);
		QVCoin coinPeer=(QVCoin)peer(coin);
		if (coinPeer.implementsTag(tagNameOfRootData))
			return;
*/
		OCMError e=new OCMError();
		e.name="invalidRoot";
		e.list.add(element);
		errors.add(e);
	}

	// Override method on OCMrootPeerSupportLite
	public Object getOutputRoot()
		throws SAXException, IOException
	{
		qe=new OCMErrorRoot();
		errors=qe.errors;

		cvt(null);

		return qe;
	}
}
