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

package com.jxml.quick.util.qjml2qdml;

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

/**
 * Transcribes data to a new set of classes, 
 * but using the same data model.
<pre>
DTD:

&lt;!ELEMENT qjml2qdml ((<a href="../../ocm/OCMsinkSupport.html#source">source|sourceLink</a>),((<a href="../../ocm/OCMsinkSupport.html#validator">validate|validateLink</a>)?,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<a href="../../ocm/OCMcontextSupport.html#factory">factory|factoryLink</a>))>

&lt;!ATTLIST qjml2qdml 
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../ocm/OCMsinkSupport.html#prefix">docNamePrefix </a>CDATA #IMPLIED
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../ocm/OCMsinkSupport.html#expressDefaults">expressDefaults</a> CDATA "False"
&nbsp;&nbsp;&nbsp;&nbsp;ID CDATA #IMPLIED
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../ocm/OCMtranscribe.html#schemaName">outputSchema</a> CDATA #IMPLIED
>
</pre>
 */
public class Qjml2Qdml extends OCMtranscribe
{

	// 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 SubQjml2Qdml(prior);
		prior.put(this,base);
		return base;
	}

	public class SubQjml2Qdml extends SubTranscribe 
	{

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

		// implements SubSource method
		public QDoc getDoc()
			throws SAXException, IOException
		{
			QDoc inputDoc=getInputDoc();
			QDoc schema=inputDoc.getSchema();
			QMQJML qjml=(QMQJML)inputDoc.getRoot();
			ArrayList includes=qjml.includes;
			QMQJML clone=(QMQJML)qjml.clone();
			ArrayList iclone=clone.includes;
			clone.tagName="qdml";
			int s,i;
			s=includes.size();
			for (i=0;i<s;++i)
			{
				QMQJML q=(QMQJML)includes.get(i);
				String oldHRef=QRefHash.getHRef(q);
				if (!oldHRef.endsWith(".qjml"))
					throw new SAXException("Include.href does not end with .qjml: "+oldHRef);
				String newHRef=oldHRef.substring(0,oldHRef.length()-4)+"qdml";
				QMQJML c=(QMQJML)iclone.get(i);
				Quick.createDoc(newHRef,c,schema);
			}
			return Quick.createDoc(docName,clone,getSchemaDoc());
		}	
	}
}
