org.exist.git.xquery.Create.java Source code

Java tutorial

Introduction

Here is the source code for org.exist.git.xquery.Create.java

Source

/*
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2012-2013 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.git.xquery;

import static org.exist.git.xquery.Module.FS;

import java.io.File;

import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.RepositoryBuilder;
import org.exist.dom.QName;
import org.exist.util.io.Resource;
import org.exist.xquery.*;
import org.exist.xquery.value.*;

/**
 * 
 * @author <a href="mailto:shabanovd@gmail.com">Dmitriy Shabanov</a>
 */
public class Create extends BasicFunction {

    public final static FunctionSignature signatures[] = {
            new FunctionSignature(new QName("create", Module.NAMESPACE_URI, Module.PREFIX), "",
                    new SequenceType[] { new FunctionParameterSequenceType("localPath", Type.STRING,
                            Cardinality.EXACTLY_ONE, "Local path") },
                    new FunctionReturnSequenceType(Type.BOOLEAN, Cardinality.EXACTLY_ONE,
                            "true if success, false otherwise")) };

    public Create(XQueryContext context, FunctionSignature signature) {
        super(context, signature);
    }

    @Override
    public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {

        try {
            String localPath = args[0].getStringValue();
            if (!(localPath.endsWith("/")))
                localPath += File.separator;

            Repository newRepo = new RepositoryBuilder().setFS(FS).setGitDir(new Resource(localPath + ".git"))
                    .setMustExist(false).build();

            newRepo.create();

            return BooleanValue.TRUE;
        } catch (Throwable e) {
            throw new XPathException(this, Module.EXGIT001, e);
        }
    }
}