001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.provider.svn.svnexe.command.checkout;
020
021import java.io.File;
022import java.util.ArrayList;
023import java.util.List;
024
025import org.apache.commons.lang3.StringUtils;
026import org.apache.maven.scm.ScmFile;
027import org.apache.maven.scm.ScmFileStatus;
028import org.apache.maven.scm.provider.svn.svnexe.command.AbstractFileCheckingConsumer;
029
030/**
031 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
032 * @author Olivier Lamy
033 *
034 */
035public class SvnCheckOutConsumer extends AbstractFileCheckingConsumer {
036    private static final String CHECKED_OUT_REVISION_TOKEN = "Checked out revision";
037
038    private final List<ScmFile> files = new ArrayList<>();
039
040    public SvnCheckOutConsumer(File workingDirectory) {
041        super(workingDirectory);
042    }
043
044    /**
045     * {@inheritDoc}
046     */
047    protected void parseLine(String line) {
048        String statusString = line.substring(0, 1);
049
050        String file = line.substring(3).trim();
051        // [SCM-368]
052        if (file.startsWith(getWorkingDirectory().getAbsolutePath())) {
053            file = StringUtils.substring(
054                    file, getWorkingDirectory().getAbsolutePath().length() + 1);
055        }
056
057        ScmFileStatus status;
058
059        if (line.startsWith(CHECKED_OUT_REVISION_TOKEN)) {
060            String revisionString = line.substring(CHECKED_OUT_REVISION_TOKEN.length() + 1, line.length() - 1);
061
062            revision = parseInt(revisionString);
063
064            return;
065        } else if (statusString.equals("A")) {
066            status = ScmFileStatus.ADDED;
067        } else if (statusString.equals("U")) {
068            status = ScmFileStatus.UPDATED;
069        } else {
070            // Do nothing
071
072            return;
073        }
074
075        addFile(new ScmFile(file, status));
076    }
077
078    // ----------------------------------------------------------------------
079    //
080    // ----------------------------------------------------------------------
081
082    public List<ScmFile> getCheckedOutFiles() {
083        return getFiles();
084    }
085
086    protected void addFile(ScmFile file) {
087        files.add(file);
088    }
089
090    protected List<ScmFile> getFiles() {
091        List<ScmFile> onlyFiles = new ArrayList<>();
092        for (ScmFile file : files) {
093            // second part is for svn 1.7 as the co output is now relative not a full path as for svn 1.7-
094            if (!(!file.getStatus().equals(ScmFileStatus.DELETED)
095                            && !new File(getWorkingDirectory(), file.getPath()).isFile())
096                    || !(!file.getStatus().equals(ScmFileStatus.DELETED)
097                            && !new File(getWorkingDirectory().getParent(), file.getPath()).isFile())) {
098                onlyFiles.add(file);
099            }
100        }
101
102        return onlyFiles;
103    }
104}