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