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

Java tutorial

Introduction

Here is the source code for org.exist.git.xquery.Cat.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 java.io.InputStream;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevTree;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.PathFilter;
import org.exist.dom.QName;
import org.exist.util.io.Resource;
import org.exist.util.io.ResourceInputStream;
import org.exist.xquery.*;
import org.exist.xquery.value.*;

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

    private static final QName CAT = new QName("cat", Module.NAMESPACE_URI, Module.PREFIX);
    private static final QName CAT_WORKING_COPY = new QName("cat-working-copy", Module.NAMESPACE_URI,
            Module.PREFIX);

    private static final FunctionParameterSequenceType LOCAL_PATH = new FunctionParameterSequenceType("localPath",
            Type.STRING, Cardinality.EXACTLY_ONE, "Local path");

    private static final FunctionParameterSequenceType PATH = new FunctionParameterSequenceType("Path", Type.STRING,
            Cardinality.EXACTLY_ONE, "File path");

    private static final FunctionReturnSequenceType RETURN = new FunctionReturnSequenceType(Type.BASE64_BINARY,
            Cardinality.EXACTLY_ONE, "");

    public final static FunctionSignature signatures[] = {
            new FunctionSignature(CAT, "", new SequenceType[] { LOCAL_PATH, PATH }, RETURN),
            new FunctionSignature(CAT_WORKING_COPY, "", new SequenceType[] { LOCAL_PATH, PATH }, RETURN) };

    public Cat(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;

            String objPath = args[1].getStringValue();

            if (mySignature.getName() == CAT_WORKING_COPY) {
                Resource resource = new Resource(localPath + objPath);

                InputStream is = new ResourceInputStream(resource);
                ;

                Base64BinaryDocument b64doc = Base64BinaryDocument.getInstance(context, is);
                return b64doc;

            }

            Git git = Git.open(new Resource(localPath), FS);
            Repository repository = git.getRepository();

            // find the HEAD
            ObjectId lastCommitId = repository.resolve(Constants.HEAD);
            // now we have to get the commit
            RevWalk revWalk = new RevWalk(repository);
            RevCommit commit = revWalk.parseCommit(lastCommitId);
            // and using commit's tree find the path
            RevTree tree = commit.getTree();
            TreeWalk treeWalk = new TreeWalk(repository);
            treeWalk.addTree(tree);
            treeWalk.setRecursive(true);
            treeWalk.setFilter(PathFilter.create(args[1].getStringValue()));
            if (!treeWalk.next()) {
                return Sequence.EMPTY_SEQUENCE;
            }
            ObjectId objectId = treeWalk.getObjectId(0);
            ObjectLoader loader = repository.open(objectId);

            // and then one can use either
            InputStream is = loader.openStream();

            Base64BinaryDocument b64doc = Base64BinaryDocument.getInstance(context, is);
            return b64doc;
        } catch (Throwable e) {
            throw new XPathException(this, Module.EXGIT001, e);
        }
    }
}