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

/**
 * Extracts QJML data.
 */
public class QCDTDElement extends OCMpeerSupport
{
	public ArrayList comments=new ArrayList();
	public ArrayList attributes=new ArrayList();
	public DTDElement dm;

	public boolean isText()
	{
		if (attributes.size()>0)
			return false;
		Object content=dm.content;
		if (content == null)
			return false;
		if (!(content instanceof DTDMixed))
			return false;
		DTDMixed mixed=(DTDMixed)content;
		Vector cms=mixed.getItemsVec();
		if (cms.size()!=1)
			return false;
		Object cm=cms.get(0);
		return cm instanceof DTDPCData;
	}

	public boolean isBiModal()
	{
		Object content=dm.content;
		if (content == null)
			return false;
		if (!(content instanceof DTDMixed))
			return false;
		DTDMixed mixed=(DTDMixed)content;
		Vector cms=mixed.getItemsVec();
		if (cms.size()!=1)
			return false;
		Object cm=cms.get(0);
		return cm instanceof DTDPCData;
	}

	/**
	 * User-provided code to perform the conversion.
	 */
	public void cvt(Object param)
		throws SAXException, IOException
	{
		dm=(DTDElement)dataModel;
		int i,s;

		if (param instanceof QCDTD)
		{
			if (isText())
			{
				addStringAttribute("tag",dm.name);
				startElement("text");
				s=comments.size();
				for(i=0;i<s;i++)
				{
					startElement("rem");
					String comment=(String)comments.get(i);
					addStringContent(comment);
					endElement("rem");
				}
				endElement("text");
			}
			else
			{
				if (dm.content instanceof DTDMixed)
				{
					if (isBiModal())
						addStringAttribute("type","BIMODAL");
					else
						addStringAttribute("type","MIXED");
				}
				addStringAttribute("tag",dm.name);
				startElement("bean");
				s=comments.size();
				for(i=0;i<s;i++)
				{
					startElement("rem");
					String comment=(String)comments.get(i);
					addStringContent(comment);
					endElement("rem");
				}
				int t=attributes.size();
				if (t>0)
				{
					startElement("attributes");
					for(i=0;i<t;i++)
					{
						DTDAttribute att=(DTDAttribute)attributes.get(i);
						String attName=att.name;
						addStringAttribute("coin",dm.name+"."+attName);
						DTDDecl decl=att.decl;
						if (decl!=null)
						{	
							if (!decl.equals(DTDDecl.REQUIRED))
							{
								addBooleanAttribute("optional",true);
								if (decl.equals(DTDDecl.FIXED))
								{
									addBooleanAttribute("fixed",true);
								}
								if (att.defaultValue!=null && !"".equals(att.defaultValue))
								{
									addStringAttribute("value",att.defaultValue);
								}
							}
						}
						startElement("item");
						if ("ID".equals(att.type))
						{
							startElement("id");
							endElement("id");
						}
						endElement("item");
					}
					endElement("attributes");
				}
				if (!(dm.content instanceof DTDEmpty) && !isBiModal())
				{
					startElement("elements");
					if (dm.content instanceof DTDChoice)
					{
						DTDChoice choice=(DTDChoice)dm.content;
						Vector v=choice.getItemsVec();
						if (v.size()==1)
						{
							if (DTDCardinal.NONE.equals(choice.cardinal))
							{
								doCvt(v.get(0),this);
							}
							else
								doCvt(dm.content,this);
						}
						else
							doCvt(dm.content,this);
					}
					else if (dm.content instanceof DTDSequence)
					{
						DTDSequence sequence=(DTDSequence)dm.content;
						if (DTDCardinal.NONE.equals(sequence.cardinal))
						{
							Vector v=sequence.getItemsVec();
							s=v.size();
							for (i=0;i<s;++i)
							{
								doCvt(v.get(i),this);
							}
						}
						else
							doCvt(dm.content,this);
					}
					else
						doCvt(dm.content,this);
					endElement("elements");
				}
				endElement("bean");
				for(i=0;i<t;i++)
				{
					doCvt(attributes.get(i),this);
				}
			}
		}
		else if (param instanceof ArrayList)
		{
			comments=(ArrayList)param;
		}
		else if (param instanceof DTDAttribute)
		{
			attributes.add(param);
		}
	}
}
