package com.jxml.quick.util.qjml2java;
import com.jxml.quick.model.qjml.*;
import com.jxml.quick.*;
import org.xml.sax.*;
import java.util.*;
import com.jxml.quick.ocm.*;
import java.io.*;

/**
 * Extracts QJML data.
 */
public class QCItem extends OCMpeerSupport {
    /**
     * User-provided code to perform the conversion.
     */
    public void cvt(Object param) throws SAXException, IOException {
	QMItem dm=(QMItem)dataModel;
        //System.out.println("QCItem " + dm.coinName);
	Conv.SubConv conv=(Conv.SubConv)context;
        boolean makechild = conv.makeChild;
        if (dm.repeating && !makechild) {
            // create a elList
            
            QMAccess access = dm.access;
	    addStringAttribute("fieldName",access.name);
            String childTargetClass = null;
	    addStringAttribute("targetClass","java.util.ArrayList");
	    addBooleanAttribute("property",access instanceof QMProperty);
	    addBooleanAttribute("typedList",true);
            /*if (dm.coin != null) {
                QMTarget target = dm.coin.target;
                childTargetClass = Element.getTargetText(target);
	        addStringAttribute("childTargetClass",childTargetClass);
            }*/
            // childTargetClass will be initialized later
	    addBooleanAttribute("newClass",false);
	    addBooleanAttribute("hasAClass",true);
	    // childName to be initialized later ? let's try to use coin...
            if (dm.coin != null) {
                String childName = null;
                if (dm.coin instanceof QMBean)
                    childName = ((QMBean)dm.coin).tag;
                else if (dm.coin instanceof QMText)
                    childName = ((QMText)dm.coin).tag;
                else if (dm.coin instanceof QMLink)
                    childName = ((QMLink)dm.coin).tag;
                if (childName != null)
                    addStringAttribute("childName",childName);
            }
	    addBooleanAttribute("optional",dm.optional);

	    startElement("listInList");
            
            conv.makeChild = true;
	    doCvt(dm,null);

	    endElement("listInList");
        } else {
            // create a child
            
            conv.makeChild = false;
            QMAccess access = dm.access;
            if (access instanceof QMIgnore || access instanceof QMId)
                return;
            if (access != null)
	        addStringAttribute("fieldName",access.name);
	    addBooleanAttribute("property",access instanceof QMProperty);
            
            String defvalue = dm.value;
            if (defvalue != null && defvalue.length() > 1 && dm.coin != null &&
                "java.lang.Boolean".equals(dm.coin.getClassName()))
                // default value: transform False or True into false or true
                defvalue = defvalue.substring(0,1).toLowerCase() +
                    defvalue.substring(1);
	    addStringAttribute("defaultValue",defvalue);
            
	    addBooleanAttribute("optional",dm.optional);

            // how to use accessClass ? (qjml2java uses el.targetClass)
            
	    startElement("child");
            
            // element definition
            if (conv.done.get(dm.coinName) == null) {
                QMCoin coin = (QMCoin)conv.coinMap.get(dm.coinName);
                doCvt(coin,null);
                //System.out.println("eldef: coin " + dm.coinName);
            } else {
	        addStringAttribute("href","#"+dm.coinName);
	        emptyElement("link");
                //System.out.println("eldef: link to " + dm.coinName);
            }

	    endElement("child");
        }
    }
}
