// copyright=> http://www.jxml.com/mdsax/License.txt

package com.jxml.quick.util.dtd2xml;

import java.io.*;
import java.util.*;
import com.wutka.dtd.*;
import com.jxml.quick.*;
import com.jxml.quick.ocm.*;
import org.xml.sax.*;

/**
 * Retrives DTD by file name.
<pre>
DTD:

&lt;!ELEMENT dtdInput EMPTY>

&lt;!ATTLIST dtdInput 
&nbsp;&nbsp;&nbsp;&nbsp;<a href="DTDbyName.html#schemaName">inputSchema </a>CDATA #IMPLIED
&nbsp;&nbsp;&nbsp;&nbsp;ID CDATA #IMPLIED
>
</pre>
 */
public class DTDbyName extends OCMsourceByName 
{
	// 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 SubDTDbyName(prior);
		prior.put(this,base);
		return base;
	}


	/**
	 * A processing object spawned from an OCMsourceByName object.
	 */
	public class SubDTDbyName extends OCMsourceByName.SubSourceByName 
	{
		public SubDTDbyName(HashMap prior)
		{
			super(prior);
		}

		// implements OCMsource method
		public QDoc getDoc()
			throws SAXException, IOException
		{
			if (outputDoc!=null)
				return outputDoc;
			QDoc schema=getSchemaDoc();
			try
			{
				FileReader fr=new FileReader(docName.toString());
				DTDParser p=new DTDParser(fr);
				DTD dtd=p.parse();
				outputDoc=Quick.createDoc(dtd,schema);
			}
			catch (IOException ioe)
			{
				System.err.println("Input document file not found: "+docName.toString());
				throw ioe;
			}
			return outputDoc;
		}	
	}
}
