// MobyCollection.java
//
//    Created: August 2005
//
// This file is a component of the BioMoby project.
// Copyright Martin Senger (martin.senger@gmail.com).
//

package org.biomoby.shared.parser;

import org.jdom.Element;

import java.util.Vector;
import java.util.Enumeration;

/**
 * A specialized "envelope" that can carry a collection of other
 * envelopes (represented by {@link MobySimple}s objects), and giving
 * them a name known as an <em>article name for top-level
 * object</em>. <p>
 *
 * @author <A HREF="mailto:martin.senger@gmail.com">Martin Senger</A>
 * @version $Id: MobyCollection.java,v 1.3 2006/02/12 18:47:40 senger Exp $
 */

public class MobyCollection extends MobyDataElement {

    protected Vector data = new Vector();   // of type MobySimple

    /**************************************************************************
     * Default constructor.
     *************************************************************************/
    public MobyCollection() {
    }

    /**************************************************************************
     *
     *************************************************************************/
    public String toString() {
	StringBuffer buf = new StringBuffer();
	buf.append ("(Collection) ");
	buf.append (super.toString());
	buf.append ("Data elements:\n");
	int count = 1;
	for (Enumeration en = data.elements(); en.hasMoreElements(); ) {
	    buf.append ("(" + count++ + ") ");
	    buf.append (((MobySimple)en.nextElement()).format (1));
	}
	return new String (buf);
    }

    /**************************************************************************
     *
     *************************************************************************/
    public Element toXML() {
	Element[] simples = new Element [data.size()];
	int i = 0;
	for (Enumeration en = data.elements(); en.hasMoreElements(); )
	    simples[i++] = ((MobySimple)en.nextElement()).toXML();
	return toXML (getName(), simples); 

// 	Element elem = super.toXML();
// 	elem.setName (MobyTags.COLLECTION);
// 	for (Enumeration en = data.elements(); en.hasMoreElements(); )
// 	    elem.addContent ( ((MobySimple)en.nextElement()).toXML() );
// 	return elem;
    }

    /**************************************************************************
     *
     *************************************************************************/
    public static Element toXML (String articleName, Element[] simples) {
	Element elem = MobyDataElement.toXML (articleName);
	elem.setName (MobyTags.COLLECTION);
	for (int i = 0; i < simples.length; i++)
	    elem.addContent (simples[i]);
	return elem;
    }

    /**************************************************************************
     * Fill this envelope with the objects carrying inside real data
     * objects.
     *************************************************************************/
    public void setData (MobySimple[] data) {
	this.data = new Vector();
	for (int i = 0; i < data.length; i++)
	    this.data.addElement (data[i]);
    }

    /**************************************************************************
     * Add one more element to this envelope.
     *************************************************************************/
    public void addData (MobySimple data) {
	this.data.addElement (data);
    }

    /**************************************************************************
     * Get the contents of this envelope.
     *************************************************************************/
    public MobySimple[] getData() {
	MobySimple[] result = new MobySimple [ data.size() ];
	data.copyInto (result);
	return result;
    }

}
