001package org.apache.maven.scm.provider.svn.svnexe.command.update;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
023import java.util.ArrayList;
024import java.util.Arrays;
025import java.util.List;
026
027import org.apache.maven.scm.ChangeFile;
028import org.apache.maven.scm.ChangeSet;
029import org.apache.maven.scm.ScmFile;
030import org.apache.maven.scm.ScmFileStatus;
031import org.apache.maven.scm.log.ScmLogger;
032import org.apache.maven.scm.provider.svn.svnexe.command.AbstractFileCheckingConsumer;
033
034/**
035 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
036 *
037 */
038public class SvnUpdateConsumer
039    extends AbstractFileCheckingConsumer
040{
041    private static final String UPDATED_TO_REVISION_TOKEN = "Updated to revision";
042
043    private static final String AT_REVISION_TOKEN = "At revision";
044
045    private static final String EXPORTED_REVISION_TOKEN = "Exported revision";
046
047    private static final String RESTORED_TOKEN = "Restored";
048    
049    private List<ChangeSet> changeSets = new ArrayList<ChangeSet>();
050
051    // ----------------------------------------------------------------------
052    //
053    // ----------------------------------------------------------------------
054
055    public SvnUpdateConsumer( ScmLogger logger, File workingDirectory )
056    {
057        super( logger, workingDirectory );
058    }
059
060    // ----------------------------------------------------------------------
061    // StreamConsumer Implementation
062    // ----------------------------------------------------------------------
063
064    /** {@inheritDoc} */
065    protected void parseLine( String line )
066    {
067        line = line.trim();
068
069        String statusString = line.substring( 0, 1 );
070
071        String file = line.substring( 3 ).trim();
072        //[SCM-368]
073        if ( file.startsWith( workingDirectory.getAbsolutePath() ) )
074        {
075            if ( file.length() == workingDirectory.getAbsolutePath().length() )
076            {
077                file = ".";
078            }
079            else
080            {
081                file = file.substring( this.workingDirectory.getAbsolutePath().length() + 1 );
082            }
083        }
084
085        ScmFileStatus status;
086
087        if ( line.startsWith( UPDATED_TO_REVISION_TOKEN ) )
088        {
089            String revisionString = line.substring( UPDATED_TO_REVISION_TOKEN.length() + 1, line.length() - 1 );
090
091            revision = parseInt( revisionString );
092
093            return;
094        }
095        else if ( line.startsWith( AT_REVISION_TOKEN ) )
096        {
097            String revisionString = line.substring( AT_REVISION_TOKEN.length() + 1, line.length() - 1 );
098
099            revision = parseInt( revisionString );
100
101            return;
102        }
103        else if ( line.startsWith( EXPORTED_REVISION_TOKEN ) )
104        {
105            String revisionString = line.substring( EXPORTED_REVISION_TOKEN.length() + 1, line.length() - 1 );
106
107            revision = parseInt( revisionString );
108
109            return;
110        }
111        else if ( line.startsWith( RESTORED_TOKEN ) )
112        {
113            return;
114        }
115        else if ( statusString.equals( "A" ) )
116        {
117            status = ScmFileStatus.ADDED;
118        }
119        else if ( statusString.equals( "U" ) || statusString.equals( "M" ) )
120        {
121            status = ScmFileStatus.UPDATED;
122        }
123        else if ( statusString.equals( "D" ) )
124        {
125            status = ScmFileStatus.DELETED;
126        }
127        else
128        {
129            //Do nothing
130
131            return;
132        }
133
134        addFile( new ScmFile( file, status ) );
135        
136        List<ChangeFile>
137        changeFiles =
138            Arrays.asList( new ChangeFile[] { new ChangeFile( line, Integer.valueOf( revision ).toString() ) } );
139
140        ChangeSet changeSet = new ChangeSet( null, null, null, changeFiles );
141        changeSets.add( changeSet );
142    }
143
144    public List<ScmFile> getUpdatedFiles()
145    {
146        return getFiles();
147    }
148
149    public List<ChangeSet> getChangeSets()
150    {
151        return changeSets;
152    }
153
154    public void setChangeSets( List<ChangeSet> changeSets )
155    {
156        this.changeSets = changeSets;
157    }
158}