// copyright=> http://www.jxml.com/mdsax/License.txt

package com.jxml.quick.util.qjml2java;

import java.io.*;
import java.util.ArrayList;

import com.jxml.quick.model.qjml.*;


/**
 * This class represents either a link between an XML element and a child
 * element, or a link between an XML element and an attribute. The child
 * element or attribute is defined in eldef.
 */
public class Child {
    public String fieldName;
    public boolean property; // property or public field
    public String defaultValue;
    public Element eldef;
    public boolean optional;
    boolean inited = false;
    
    public Child() {
    }
    
    public void init() {
        if (inited)
            return;
        inited = true;
        eldef.init();
    }
    
    public void makeFields(ArrayList fields, boolean attributes) {
        if (fieldName == null) {
            System.err.println("Warning: could not create a class field for " +
                eldef.tagName + "(no field name is specified)");
            return;
        }
        String comment = null;
        String nl = System.getProperty("line.separator");
        if ((attributes || !eldef.newClass) && eldef.comments != null) {
            for (int i=0; i<eldef.comments.size(); i++) {
                if (comment == null)
                    comment = "";
                else
                    comment += nl;
                comment += (String)eldef.comments.get(i);
            }
        }
        if (comment == null) {
            ArrayList values = eldef.values;
            if (defaultValue != null || (values != null && values.size() != 0)) {
                comment = "";
                if (defaultValue != null)
                    comment = "default = \"" + defaultValue + "\"";
                if (values != null && values.size() != 0) {
                    String svalues = null;
                    for (int i=0; i<values.size(); i++)
                        if (svalues == null)
                            svalues = (String)values.get(i);
                        else
                            svalues += "|" + (String)values.get(i);
                    comment += "  values = \"" + svalues + "\"";
                }
            }
        }
        ArrayList xchildren = eldef.subXChildren();
        // check if field already exists (this is the case if QMProperty
        // or QMField was used in QJML)
        ClassField cf = null;
        for (int i=0; i<fields.size(); i++)
            if (((ClassField)fields.get(i)).name.equals(fieldName)) {
                cf = (ClassField)fields.get(i);
                break;
            }
        if (cf != null) {
            if (cf.classpath == null)
                cf.classpath = eldef.targetClass;
            if (property != cf.property)
                System.err.println("Warning: is " + fieldName + " a property" +
                    " or not ?");
            cf.newClass = eldef.newClass;
            cf.hasAClass = eldef.hasAClass;
            cf.comment = comment;
            cf.tagName = eldef.tagName;
            cf.attribute = attributes;
            cf.optional = optional;
            if (cf.defaultValue == null)
                cf.defaultValue = defaultValue;
            cf.xchildren = xchildren;
        } else {
            ClassField.addNewField(fields, new ClassField(fieldName,
                eldef.targetClass, property,
                eldef.newClass, eldef.hasAClass, comment,
                eldef.tagName, attributes, optional, defaultValue,
                false, null, false, null, null, xchildren));
        }
    }
    
}

