/*
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2010 The eXist Project
 *  http://exist-db.org
 *
 *  This program 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 program 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *  $Id$
 */
package org.exist.versioning.svn.xquery;

import java.util.List;

import org.exist.dom.QName;
import org.exist.memtree.MemTreeBuilder;
import org.exist.versioning.svn.WorkingCopy;
import org.exist.xquery.Cardinality;
import org.exist.xquery.FunctionSignature;
import org.exist.xquery.XPathException;
import org.exist.xquery.XQueryContext;
import org.exist.xquery.value.FunctionParameterSequenceType;
import org.exist.xquery.value.Sequence;
import org.exist.xquery.value.SequenceType;
import org.exist.xquery.value.Type;
import org.tmatesoft.svn.core.SVNDepth;
import org.tmatesoft.svn.core.SVNDirEntry;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNNodeKind;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.wc.SVNRevision;
import org.xml.sax.helpers.AttributesImpl;

/**
 * Reports the directory entry, and possibly children, for url at revision. 
 * 
 * @author <a href="mailto:amir.akhmedov@gmail.com">Amir Akhmedov</a>
 * @author <a href="mailto:shabanovd@gmail.com">Dmitriy Shabanov</a>
 */
public class SVNList extends AbstractSVNFunction {

	public final static FunctionSignature signature =
		new FunctionSignature(
			new QName("list", SVNModule.NAMESPACE_URI, SVNModule.PREFIX),
			"Reports the directory entry, and possibly children, for url at revision.",
			new SequenceType[] {
				SVN_URI
            },
			new FunctionParameterSequenceType("list", Type.ELEMENT, Cardinality.EXACTLY_ONE, "a sequence containing the list entries"));

    public SVNList(XQueryContext context) {
		super(context, signature);
	}

    private final static QName ENTRIES_ELEMENT = new QName("entries", "", "");
    private final static QName ENTRY_ELEMENT = new QName("entry", "", "");
    private final static AttributesImpl EMPTY_ATTRIBS = new AttributesImpl();

	@Override
	public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
		WorkingCopy wc = new WorkingCopy("", "");
        String uri = args[0].getStringValue();
		List<SVNDirEntry> entries;
		try {
			entries = wc.list(SVNURL.parseURIEncoded(uri), SVNRevision.HEAD, SVNRevision.HEAD, false, SVNDepth.IMMEDIATES, 1);
			MemTreeBuilder builder = context.getDocumentBuilder();
			int nodeNr = builder.startElement(ENTRIES_ELEMENT, EMPTY_ATTRIBS);
			
			for (SVNDirEntry entry : entries) {
				String path = entry.getRelativePath();
				if(!path.equals("")) {
					AttributesImpl attributes = new AttributesImpl();
					if (entry.getKind() == SVNNodeKind.DIR) {
						attributes.addAttribute("", "type", "", "CDATA", "directory");
		                builder.startElement(ENTRY_ELEMENT, attributes);
		                builder.characters(entry.getName());
		                builder.endElement();
					}else if (entry.getKind() == SVNNodeKind.FILE) {
						attributes.addAttribute("", "type", "", "CDATA", "file");
		                builder.startElement(ENTRY_ELEMENT, attributes);
		                builder.characters(entry.getName());
		                builder.endElement();
					}
				}
			}
			builder.endElement();
			return builder.getDocument().getNode(nodeNr);
		} catch (SVNException e) {
            throw new XPathException(this, e.getMessage(), e);
		}
	}

}

@@ -2221,16 +2221,65 @@
 %09SVN_URI
+,%0A                LOGIN,%0A                PASSWORD
 %0A       
@@ -2829,78 +2829,134 @@
 n %7B%0A
-%09%09WorkingCopy wc = new WorkingCopy(%22%22, %22%22);%0A        String uri
+        String uri = args%5B0%5D.getStringValue();%0A        String user = args%5B1%5D.getStringValue();%0A        String password
  = args%5B
 0%5D.g
@@ -2943,33 +2943,33 @@
 password = args%5B
-0
+2
 %5D.getStringValue
@@ -2971,16 +2971,72 @@
 Value();
+%0A%0A%09%09WorkingCopy wc = new WorkingCopy(user, password);%0A%09%09
 %0A%09%09List%3C

add username & password to subversion:list xquery function