package com.jxml.quick.util.qdml2dtd;
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.*;

/**
 * Extracts DTD data.
 */
public class QCBean extends OCMpeerSupport
{
	public TreeMap coinMap;
	public QDMBean dm;

	/**
	 * User-provided code to perform the conversion.
	 */
	public void cvt(Object param)
		throws SAXException, IOException
	{
		dm=(QDMBean)dataModel;
		QDMQDML qdmlDM=(QDMQDML)param;
		coinMap=qdmlDM.coinMap;
		int i,s;
		ArrayList remList =dm.remList;
		s=remList.size();
		for(i=0;i<s;++i)
		{
			startElement("rem");
			QDMRem rem=(QDMRem)remList.get(i);
			addStringContent(rem.text);
			endElement("rem");
		}
		addStringAttribute("name",dm.tag);
		startElement("element");
		doCvt(dm.elements,this);
		endElement("element");

		QDMAttributes as=dm.attributes;
		Map items=as.getAtts();
		Collection col=items.values();
		Iterator it=col.iterator();
		while (it.hasNext())
		{
			QDMItem item=(QDMItem)it.next();
			String coinName=item.coinName;
			QDMText text=(QDMText)coinMap.get(coinName);
			genAttribute(text,dm.tag,item);
		}
	}

	void genAttribute(QDMText dm, String elementName,QDMItem item)
		throws SAXException, IOException
	{
		addStringAttribute("element",elementName);
		startElement("attList");
		addStringAttribute("name",dm.tag);
		if (!"".equals(item.value))
		{
			addStringAttribute("decl","VALUE");
			addStringAttribute("value",item.value);
		}
		else if (item.optional)
			addStringAttribute("decl","IMPLIED");
		else
			addStringAttribute("decl","REQUIRED");
		startElement("attribute");
		ArrayList valids=dm.getValidList();
		if (valids.size()>0)
		{
			startElement("enumeration");
			int s,i;
			s=valids.size();
			for (i=0;i<s;++i)
			{
				QDMEnum enum=(QDMEnum)valids.get(i);
				startElement("value");
				addStringContent(enum.value);
				endElement("value");
			}
			endElement("enumeration");
		}
		else
		{
			startElement("type");
			if (item.fixed)
				addStringContent("FIXED");
			else if (item.id!=null)
				addStringContent("ID");
			else
			{
				QDMCoin coin=item.coin;
				if (coin==null || !(coin instanceof QDMText))
					addStringContent("CDATA");
				else
				{
					QDMText text=(QDMText)coin;
					if (!text.type.equals("idref"))
						addStringContent("CDATA");
					else
						addStringContent("IDREF");
				}
			}
			endElement("type");
		}
		endElement("attribute");
		endElement("attList");
	}
}
