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.update;
020
021import java.io.File;
022import java.util.ArrayList;
023import java.util.Arrays;
024import java.util.List;
025
026import org.apache.maven.scm.ChangeFile;
027import org.apache.maven.scm.ChangeSet;
028import org.apache.maven.scm.ScmFile;
029import org.apache.maven.scm.ScmFileStatus;
030import org.apache.maven.scm.provider.svn.svnexe.command.AbstractFileCheckingConsumer;
031
032/**
033 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
034 */
035public class SvnUpdateConsumer extends AbstractFileCheckingConsumer {
036    private static final String UPDATED_TO_REVISION_TOKEN = "Updated to revision";
037
038    private static final String AT_REVISION_TOKEN = "At revision";
039
040    private static final String EXPORTED_REVISION_TOKEN = "Exported revision";
041
042    private static final String RESTORED_TOKEN = "Restored";
043
044    private List<ChangeSet> changeSets = new ArrayList<>();
045
046    // ----------------------------------------------------------------------
047    //
048    // ----------------------------------------------------------------------
049
050    public SvnUpdateConsumer(File workingDirectory) {
051        super(workingDirectory);
052    }
053
054    // ----------------------------------------------------------------------
055    // StreamConsumer Implementation
056    // ----------------------------------------------------------------------
057
058    /**
059     * {@inheritDoc}
060     */
061    protected void parseLine(String line) {
062        line = line.trim();
063
064        String statusString = line.substring(0, 1);
065
066        String file = line.substring(3).trim();
067        // [SCM-368]
068        if (file.startsWith(workingDirectory.getAbsolutePath())) {
069            if (file.length() == workingDirectory.getAbsolutePath().length()) {
070                file = ".";
071            } else {
072                file = file.substring(this.workingDirectory.getAbsolutePath().length() + 1);
073            }
074        }
075
076        ScmFileStatus status;
077
078        if (line.startsWith(UPDATED_TO_REVISION_TOKEN)) {
079            String revisionString = line.substring(UPDATED_TO_REVISION_TOKEN.length() + 1, line.length() - 1);
080
081            revision = parseInt(revisionString);
082
083            return;
084        } else if (line.startsWith(AT_REVISION_TOKEN)) {
085            String revisionString = line.substring(AT_REVISION_TOKEN.length() + 1, line.length() - 1);
086
087            revision = parseInt(revisionString);
088
089            return;
090        } else if (line.startsWith(EXPORTED_REVISION_TOKEN)) {
091            String revisionString = line.substring(EXPORTED_REVISION_TOKEN.length() + 1, line.length() - 1);
092
093            revision = parseInt(revisionString);
094
095            return;
096        } else if (line.startsWith(RESTORED_TOKEN)) {
097            return;
098        } else if (statusString.equals("A")) {
099            status = ScmFileStatus.ADDED;
100        } else if (statusString.equals("U") || statusString.equals("M")) {
101            status = ScmFileStatus.UPDATED;
102        } else if (statusString.equals("D")) {
103            status = ScmFileStatus.DELETED;
104        } else {
105            // Do nothing
106
107            return;
108        }
109
110        addFile(new ScmFile(file, status));
111
112        List<ChangeFile> changeFiles = Arrays.asList(
113                new ChangeFile[] {new ChangeFile(line, Integer.valueOf(revision).toString())});
114
115        ChangeSet changeSet = new ChangeSet(null, null, null, changeFiles);
116        changeSets.add(changeSet);
117    }
118
119    public List<ScmFile> getUpdatedFiles() {
120        return getFiles();
121    }
122
123    public List<ChangeSet> getChangeSets() {
124        return changeSets;
125    }
126
127    public void setChangeSets(List<ChangeSet> changeSets) {
128        this.changeSets = changeSets;
129    }
130}