package com.jxml.quick.ocm; // Open Conversion Model
import com.jxml.quick.*;
import org.xml.sax.*;
import java.io.*;
import java.util.*;
import com.jxml.protocol.*;

/**
 * Support class for OCMvalidator.
<pre>
DTD:

&lt;!ELEMENT validatorSupport((<a href="OCMsinkSupport.html#source">(contextSupport | transcribe | input) | sourceLink</a>),(<a href="OCMsinkSupport.html#validator">validatorSupport | validateLink</a>)?)>

&lt;!ATTLIST validatorSupport
&nbsp;&nbsp;&nbsp;&nbsp;<a href="OCMsinkSupport.html#prefix">docNamePrefix</a> CDATA #IMPLIED
&nbsp;&nbsp;&nbsp;&nbsp;<a href="OCMsinkSupport.html#expressDefaults">expressDefaults</a> CDATA "False"
&nbsp;&nbsp;&nbsp;&nbsp;ID CDATA "False"
>
</pre>
 */
public class OCMvalidatorSupport extends OCMsinkSupport implements OCMvalidator
{
	// implements an OCMbase method
	public OCMbase.SubBase spawn(HashMap prior)
	{
		OCMbase.SubBase rv=(OCMbase.SubBase)prior.get(this);
		if (rv!=null)
			return rv;
		OCMbase.SubBase base=new SubValidatorSupport(prior);
		prior.put(this,base);
		return base;
	}

	public class SubValidatorSupport extends SubSinkSupport implements OCMvalidator.SubValidator
	{

		/**
		 * The source of the input document.
 		 */
		public OCMsource.SubSource echoSubSource=null;

		public SubValidatorSupport(HashMap prior)
		{
			super(prior);
		}

		/**
		 * Returns the number of error messages.
		 */
		public int errorCount()
			throws SAXException, IOException
		{
			QDoc doc=getInputDoc();
			OCMErrorRoot e=(OCMErrorRoot)doc.getRoot();
			int s=e.errors.size();
			return s;
		}

		public void expressErrors()
			throws SAXException, IOException
		{
			OCMsink.SubSink ss=(OCMsink.SubSink)subSource;
			ss.expressFile();
		}

		public void expressInputWithIDs()
			throws SAXException, IOException
		{
			OCMsink.SubSink ss=(OCMsink.SubSink)subSource;
			OCMsource.SubSource ssource=ss.getSubSource();
			((OCMsink.SubSink)ssource).expressFile(); // input with IDs added
		}

		public int validTest()
			throws SAXException, IOException
		{
			try
			{
				int errors=errorCount();
				if (errors==0)
					return 0;
				expressErrors();
				expressInputWithIDs();
				return errors;
			}
			catch (OCMinvalidException ie)
			{
				String msg=ie.getMessage();
				Integer i=new Integer(msg);
				return i.intValue();
			}
		}

		// implements method on SubValidator
		public void valid()
			throws OCMinvalidException, SAXException, IOException
		{
			int errors=validTest();
			if (errors==0)
				return;
			throw new OCMinvalidException(""+errors);
		}

		// Overrides SubSinkSupport method.
		public String express()
			throws SAXException, IOException
		{
			int errors=0;
			try
			{
				errors=validTest();
			}
			catch (OCMinvalidRootException ire)
			{
				String m=ire.getMessage();
				if ("".equals(m))
					return "Invalid Tag Name on the Root Element";
				return "Invalid Tag Name on the Root Element: "+m;
			}
			if (errors==0)
				return "No Errors";
			return ""+errors+" Errors";
		}

		// Overrides SubSinkSupport method.
		public void express(PrintWriter pw)
			throws SAXException, IOException
		{
			pw.println(express());
		}
	}
}
