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

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

&lt;!ELEMENT input EMPTY>

&lt;!ATTLIST input 
&nbsp;&nbsp;&nbsp;&nbsp;ID CDATA #IMPLIED
&nbsp;&nbsp;&nbsp;&nbsp;<a href="OCMsourceByName.html#schemaName">inputSchema </a>CDATA #IMPLIED

>
</pre>
 */

public class OCMsourceByName implements OCMsource
{
	/**
	 * The input schema name.
	 */
	public String schemaName="";

	// implements an OCMbase method
	public OCMbase.SubBase spawn(HashMap prior)
	{
		OCMbase.SubBase rv=(OCMbase.SubBase)prior.get(this);
		if (rv!=null)
			return rv;
		SubSourceByName base=new SubSourceByName(prior);
		prior.put(this,base);
		return base;
	}


	/**
	 * A processing object spawned from an OCMsourceByName object.
	 */
	public class SubSourceByName implements OCMsource.SubSource
	{
		/**
		 * Input document URL.
		 */
		public Object docName=null;

		/**
		 * The config properties
		 */
		public Map properties=new HashMap();

		/**
		 * The output document.
		 */
		public QDoc outputDoc=null;

		public SubSourceByName(HashMap prior)
		{
		}

		// implements OCMsource method
		public QDoc getSchemaDoc()
			throws SAXException, IOException
		{
			try
			{
				return Quick.parseSchema(schemaName);
			}
			catch (SAXException se)
			{
				System.err.println("Schema file not found: "+schemaName);
				throw se;
			}
			catch (IOException ioe)
			{
				System.err.println("Schema file not found: "+schemaName);
				throw ioe;
			}
		}

		// implements OCMsource method
		public void setInputDocName(Object docName)
			throws SAXException, IOException
		{
			this.docName=docName;
		}

		// implements OCMsource method
		public QDoc getDoc()
			throws SAXException, IOException
		{
			if (outputDoc!=null)
				return outputDoc;
			if (docName instanceof QDoc)
				outputDoc=(QDoc)docName;
			else if (docName instanceof String)
			{
				QDoc schema=getSchemaDoc();
				try
				{
					outputDoc=Quick.uniqueParse(schema,docName.toString());
				}
				catch (SAXException se)
				{
					System.err.println("Input document file not found: "+docName.toString());
					throw se;
				}
				catch (IOException ioe)
				{
					System.err.println("Input document file not found: "+docName.toString());
					throw ioe;
				}
			}
			else
				outputDoc=Quick.createDoc(docName,null);
			return outputDoc;
		}	

		/**
		 * Sets the config properties
		 */
		public void setProperties(Map properties)
		{
			this.properties=properties;
		}
	}
}
