package ws.prova;

/*
 * Copyright (C) 2002-2007 <a href="mailto:a.kozlenkov@city.ac.uk">Alex Kozlenkov</a>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import org.w3c.dom.NodeList;

import java.util.List;

/**
 * Class encapsulating XML node lists
 * <p>Title: Prova</p>
 * <p>Description: Class encapsulating XML node lists</p>
 * <p>Copyright: Copyright (C) 2002-2007</p>
 * <p>Company: City University, London, United Kingdom</p>
 *
 * @author Alex Kozlenkov
 * @version 2.0 <25 March 2007>
 */
public class XML_NodeList {
	private NodeList nl;

	public XML_NodeList(NodeList nl) {
		this.nl = nl;
	}

	public int getLength() {
		return nl.getLength();
	}

	/**
	 * Accessor extracting an XML_Node given its index in the XML_NodeList
	 *
	 * @param index int
	 * @return XML_Node
	 */
	public XML_Node item(int index) {
		return XML_Node.createNode(nl.item(index));
	}

	/**
	 * Non-deterministically enumerate nodes in the NodeList.
	 * This works from Prova rules like this:
	 * Elements = Root.getElementsByTagName("server"),
	 * Elements.nodes(Element), ...
	 *
	 * @param r        An array with one element, a variable for instantiating it
	 *                 with an XML_Node. We are not testing it because it is an output only.
	 *                 Subsequent unification gets rid of wrong parameters automatically.
	 * @param bindings contains the nodes one each in the array in a list element
	 * @return always Boolean.TRUE
	 */
	public Boolean nodes(final Object[] r, final List bindings) {
		for (int i = 0; i < nl.getLength(); i++) {
			Object[] retobj = new Object[1];
			retobj[0] = item(i);
			bindings.add(retobj);
		}
		return Boolean.TRUE;
	}

}
