package com.jxml.quick.util.qjml4;

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

public class QMAttribute extends QGBase implements QGBaseIntf {

    public String name;

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

    /**
     *   values =
     * "id|idref|int|short|long|byte|boolean|float|double|string|base64|url|char|BigDecimal"
     */
    public String type="";

    /**   values = "REQUIRED|FIXED" */
    public String status="";

    public String value="";

    public QGBase access;

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

    public QGBase target;

    /** list of QMValue */
    public ArrayList values = new ArrayList();


    public QMAttribute() {
    }

    /** full constructor */
    public QMAttribute(String name, boolean generate, String type, String status, String value, QGBase access, ArrayList comments, QGBase target, ArrayList values) {
        this.name = name;
        this.generate = generate;
        this.type = type;
        this.status = status;
        this.value = value;
        this.access = access;
        this.comments = comments;
        this.target = target;
        this.values = values;
    }

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

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

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

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

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

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

    public void cvtItem(String parentLabel)
	throws SAXException
    {
	if (!"REQUIRED".equals(status))
	    addBooleanAttribute("optional",true);
	if ("#ANY".equals(name))
	{
	    addBooleanAttribute("repeating",true);
	    addStringAttribute("coin",parentLabel+".att.ANY");
	}
	else
	    addStringAttribute("coin",parentLabel+".att."+name);
	if ("FIXED".equals(status))
	    addBooleanAttribute("fixed",true);
	if (!"".equals(value))
	    addStringAttribute("value",value);
	startElement("item");

	if ("id".equals(type))
	{
	    startElement("id");
	    endElement("id");
	}
	else if (access!=null)
	    access.cvt();

	endElement("item");
    }

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

	if ("#ANY".equals(name))
	{
	    addBooleanAttribute("wild",true);
	    l=parentLabel+".att.ANY";
	}
	else
	{
	    addStringAttribute("tag",name);
	    l=parentLabel+".att."+name;
	}
	addStringAttribute("label",l);
	if (!"".equals(type) 
	&& !"string".equals(type)
	&& !"id".equals(type))
	    addStringAttribute("type",type);
	startElement("text");

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

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

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

	endElement("text");
    }

    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 QMValue getQMValue(int i) {
        return((QMValue)values.get(i));
    }

    public void setQMValue(int i, QMValue qMValue) {
        values.set(i, qMValue);
    }

    public int qMValueCount() {
        return(values.size());
    }

    public void print(PrintWriter pw, String indent) {
        pw.println(indent + "element attribute");
        if (name != null) {
            pw.println(indent + "name = " + name);
        } else
            pw.println(indent + "name = null");
        pw.println(indent + "generate = " + generate);
        if (type != null) {
            pw.println(indent + "type = " + type);
        } else
            pw.println(indent + "type = null");
        if (status != null) {
            pw.println(indent + "status = " + status);
        } else
            pw.println(indent + "status = null");
        if (value != null) {
            pw.println(indent + "value = " + value);
        } else
            pw.println(indent + "value = null");
        if (access != null) {
            if (access instanceof QGBaseIntf)
                ((QGBaseIntf)access).print(pw, indent + "    ");
            else
                pw.println(indent + "access = " + access);
        } else
            pw.println(indent + "access = null");
        for (int i=0; i<comments.size(); i++)
            getQMRem(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<values.size(); i++)
            getQMValue(i).print(pw, indent + "    ");
    }

}
