package com.jxml.quick.util.qjml4;

import java.io.PrintWriter;
import java.util.ArrayList;
import org.xml.sax.*;

public class QMElement extends QGBase implements QGBaseIntf {

    public String name="";

    public String label="";

    /** default = "false" */
    public boolean generate = true;

    /** default = "false" */
    public boolean abst = false;

    /** default = "false" */
    public boolean intf = false;

    /** default = "false" */
    public boolean wild = false;

    /**
     *   values =
     * "PCDATA|CDATA|BIMODAL|EMPTY|int|short|long|byte|boolean|float|double|string|base64|url|char|BigDecimal"
     */
    public String content="";

    /** list of QMRem */
    public ArrayList comments = new ArrayList();

    /** list of QMExtends */
    public ArrayList ext = new ArrayList();

    /** list of QMImplements */
    public ArrayList imp = new ArrayList();

    public QGBase target;

    /** list of QMConnector */
    public ArrayList connectors = new ArrayList();

    /** list of QMAttribute */
    public ArrayList attributes = new ArrayList();

    public ArrayList items = new ArrayList();


    public QMElement() {
    }

    /** full constructor */
    public QMElement(String name, String label, boolean generate, boolean abst, boolean intf, boolean wild, String content, ArrayList comments, ArrayList ext, ArrayList imp, QGBase target, ArrayList connectors, ArrayList attributes, ArrayList items) {
        this.name = name;
        this.label = label;
        this.generate = generate;
        this.abst = abst;
        this.intf = intf;
        this.wild = wild;
        this.content = content;
        this.comments = comments;
        this.ext = ext;
        this.imp = imp;
        this.target = target;
        this.connectors = connectors;
        this.attributes = attributes;
        this.items = items;
    }

    /** minimum constructor */
    public QMElement(String name) {
        this.name = name;
    }

    public void setDestination(DocumentHandler destination)
    {
	super.setDestination(destination);
	int i,s;
	QGBase b;

	s=comments.size();
	for (i=0;i<s;++i)
	{
	    b=(QGBase)comments.get(i);
	    b.setDestination(destination);
	}

	s=ext.size();
	for (i=0;i<s;++i)
	{
	    b=(QGBase)ext.get(i);
	    b.setDestination(destination);
	}

	s=imp.size();
	for (i=0;i<s;++i)
	{
	    b=(QGBase)imp.get(i);
	    b.setDestination(destination);
	}

	b=(QGBase)target;
	if (b!=null)
		b.setDestination(destination);

	s=connectors.size();
	for (i=0;i<s;++i)
	{
	    b=(QGBase)connectors.get(i);
	    b.setDestination(destination);
	}

	s=attributes.size();
	for (i=0;i<s;++i)
	{
	    b=(QGBase)attributes.get(i);
	    b.setDestination(destination);
	}

	s=items.size();
	for (i=0;i<s;++i)
	{
	    b=(QGBase)items.get(i);
	    b.setDestination(destination);
	}
    }

    public String getLabel()
    {
	if (!"".equals(label))
	    return label;
	return name;
    }

    public void cvt()
	throws SAXException
    {
	int i,s;

	if (intf)
	{
	    addStringAttribute("label",getLabel());
	    if (!generate)
		addBooleanAttribute("generate",generate);

	    startElement("interface");

	    s=comments.size();
	    for (i=0;i<s;++i)
	    {
		QMRem b=(QMRem)comments.get(i);
		b.cvt();
	    }

	    s=ext.size();
	    for (i=0;i<s;++i)
	    {
		QMExtends b=(QMExtends)ext.get(i);
		b.cvt();
	    }

	    if (target!=null)
		target.cvt();

	    endElement("interface");
	}
	else if (abst)
	{
	    addStringAttribute("label",getLabel());
	    if (!generate)
		addBooleanAttribute("generate",generate);

	    startElement("abstract");

	    s=comments.size();
	    for (i=0;i<s;++i)
	    {
		QMRem b=(QMRem)comments.get(i);
		b.cvt();
	    }

	    s=ext.size();
	    for (i=0;i<s;++i)
	    {
		QMExtends b=(QMExtends)ext.get(i);
		b.cvt();
	    }

	    s=imp.size();
	    for (i=0;i<s;++i)
	    {
		QMImplements b=(QMImplements)imp.get(i);
		b.cvt();
	    }

	    if (target!=null)
		target.cvt();

	    cvtAttributes();

	    endElement("abstract");
	}
	else if ("".equals(content)
	|| "EMPTY".equals(content)
	|| "BIMODAL".equals(content))
	{
	    if (!"".equals(label))
		addStringAttribute("label",label);
	    if (wild)
	    {
		addBooleanAttribute("wild",true);
		if ("".equals(label))
		    addStringAttribute("label",name);
	    }
	    else
		addStringAttribute("tag",name);
	    if ("BIMODAL".equals(content))
		addStringAttribute("type",content);
	    if (!generate)
		addBooleanAttribute("generate",generate);

	    startElement("bean");

	    s=comments.size();
	    for (i=0;i<s;++i)
	    {
		QMRem b=(QMRem)comments.get(i);
		b.cvt();
	    }

	    s=ext.size();
	    for (i=0;i<s;++i)
	    {
		QMExtends b=(QMExtends)ext.get(i);
		b.cvt();
	    }

	    s=imp.size();
	    for (i=0;i<s;++i)
	    {
		QMImplements b=(QMImplements)imp.get(i);
		b.cvt();
	    }

	    if (target!=null)
		target.cvt();

	    cvtAttributes();

	    cvtElements();

	    endElement("bean");
	}
	else
	{
	    if (!"".equals(label))
		addStringAttribute("label",label);
	    if (wild)
	    {
		addBooleanAttribute("wild",true);
		if ("".equals(label))
		    addStringAttribute("label",name);
	    }
	    else
		addStringAttribute("tag",name);
	    if (!"PCDATA".equals(content)
	    && !"string".equals(content))
		addStringAttribute("type",content);
	    if (!generate)
		addBooleanAttribute("generate",generate);

	    startElement("text");

	    s=comments.size();
	    for (i=0;i<s;++i)
	    {
		QMRem b=(QMRem)comments.get(i);
		b.cvt();
	    }

	    s=ext.size();
	    for (i=0;i<s;++i)
	    {
		QMExtends b=(QMExtends)ext.get(i);
		b.cvt();
	    }

	    s=imp.size();
	    for (i=0;i<s;++i)
	    {
		QMImplements b=(QMImplements)imp.get(i);
		b.cvt();
	    }

	    if (target!=null)
		target.cvt();

	    cvtAttributes();

	    endElement("text");
	}

	s=attributes.size();
	for (i=0;i<s;++i)
	{
	    QMAttribute a=(QMAttribute)attributes.get(i);
	    a.cvt(getLabel());
	}
    }

    public void cvtAttributes()
	throws SAXException
    {
	int s,i;

	s=attributes.size();
	if (s==0)
	    return;

	startElement("attributes");
	for (i=0;i<s;++i)
	{
	    QMAttribute a=(QMAttribute)attributes.get(i);
	    a.cvtItem(getLabel());
	}
	endElement("attributes");
    }

    public void cvtElements()
	throws SAXException
    {
	int s,i;

	s=items.size();
	if (s==0)
	    return;

	startElement("elements");
	for (i=0;i<s;++i)
	{
	    QGBase b=(QGBase)items.get(i);
	    b.cvt();
	}
	endElement("elements");
    }

    public QMRem getQMRem(int i) {
        return((QMRem)comments.get(i));
    }

    public void setQMRem(int i, QMRem qMRem) {
        comments.set(i, qMRem);
    }

    public int qMRemCount() {
        return(comments.size());
    }

    public QMExtends getQMExtends(int i) {
        return((QMExtends)ext.get(i));
    }

    public void setQMExtends(int i, QMExtends qMExtends) {
        ext.set(i, qMExtends);
    }

    public int qMExtendsCount() {
        return(ext.size());
    }

    public QMImplements getQMImplements(int i) {
        return((QMImplements)imp.get(i));
    }

    public void setQMImplements(int i, QMImplements qMImplements) {
        imp.set(i, qMImplements);
    }

    public int qMImplementsCount() {
        return(imp.size());
    }

    public QMConnector getQMConnector(int i) {
        return((QMConnector)connectors.get(i));
    }

    public void setQMConnector(int i, QMConnector qMConnector) {
        connectors.set(i, qMConnector);
    }

    public int qMConnectorCount() {
        return(connectors.size());
    }

    public QMAttribute getQMAttribute(int i) {
        return((QMAttribute)attributes.get(i));
    }

    public void setQMAttribute(int i, QMAttribute qMAttribute) {
        attributes.set(i, qMAttribute);
    }

    public int qMAttributeCount() {
        return(attributes.size());
    }

    public void print(PrintWriter pw, String indent) {
        pw.println(indent + "element element");
        if (name != null) {
            pw.println(indent + "name = " + name);
        } else
            pw.println(indent + "name = null");
        if (label != null) {
            pw.println(indent + "label = " + label);
        } else
            pw.println(indent + "label = null");
        pw.println(indent + "generate = " + generate);
        pw.println(indent + "abst = " + abst);
        pw.println(indent + "intf = " + intf);
        pw.println(indent + "wild = " + wild);
        if (content != null) {
            pw.println(indent + "content = " + content);
        } else
            pw.println(indent + "content = null");
        for (int i=0; i<comments.size(); i++)
            getQMRem(i).print(pw, indent + "    ");
        for (int i=0; i<ext.size(); i++)
            getQMExtends(i).print(pw, indent + "    ");
        for (int i=0; i<imp.size(); i++)
            getQMImplements(i).print(pw, indent + "    ");
        if (target != null) {
            if (target instanceof QGBaseIntf)
                ((QGBaseIntf)target).print(pw, indent + "    ");
            else
                pw.println(indent + "target = " + target);
        } else
            pw.println(indent + "target = null");
        for (int i=0; i<connectors.size(); i++)
            getQMConnector(i).print(pw, indent + "    ");
        for (int i=0; i<attributes.size(); i++)
            getQMAttribute(i).print(pw, indent + "    ");
        for (int i=0; i<items.size(); i++) {
            if (items.get(i) instanceof QGBaseIntf)
                ((QGBaseIntf)items.get(i)).print(pw, indent + "    ");
            else
                pw.println(indent + "items["+i+"] = " + items.get(i));
        }
    }

}
