001package org.apache.maven.scm.provider.vss.commands.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 org.apache.maven.scm.ScmFile;
023import org.apache.maven.scm.ScmFileStatus;
024import org.apache.maven.scm.log.ScmLogger;
025import org.apache.maven.scm.provider.vss.commands.VssConstants;
026import org.apache.maven.scm.provider.vss.repository.VssScmProviderRepository;
027import org.apache.maven.scm.util.AbstractConsumer;
028import org.codehaus.plexus.util.cli.StreamConsumer;
029
030import java.util.ArrayList;
031import java.util.List;
032
033/**
034 * @author <a href="mailto:triek@thrx.de">Thorsten Riek</a>
035 *
036 */
037public class VssUpdateConsumer
038    extends AbstractConsumer
039    implements StreamConsumer
040{
041
042    /**
043     * expecting file information
044     */
045    private static final int GET_UNKNOWN = 0;
046
047    /**
048     * expecting file information
049     */
050    private static final int GET_FILE = 1;
051
052    /**
053     * expecting file information
054     */
055    private static final int REPLACE_FILE = 2;
056
057    /**
058     * expecting file path information
059     */
060    private static final int GET_FILE_PATH = 3;
061
062    /**
063     * expecting writable copy
064     */
065    private static final int IS_WRITABLE_COPY = 4;
066
067    /**
068     * expecting working folder
069     */
070    private static final int SET_WORKING_FOLDER = 5;
071
072    /**
073     * Marks start of file data
074     */
075    private static final String START_FILE_PATH = "$/";
076
077    /**
078     * Marks getting a new File
079     */
080    private static final String START_GETTING = "Getting";
081
082    /**
083     * Marks replacing a old File
084     */
085    private static final String START_REPLACING = "Replacing local copy of ";
086
087    /**
088     * Marks a writable copy of a File / maybe a conflict
089     */
090    private static final String START_WRITABLE_COPY = "A writable ";
091
092    /**
093     * Marks "Set the default folder for project" question
094     */
095    private static final String CONTAINS_SET_DEFAULT_WORKING_FOLDER = "as the default folder for project";
096
097    private String currentPath = "";
098
099    private List<ScmFile> updatedFiles = new ArrayList<ScmFile>();
100
101    private VssScmProviderRepository repo;
102
103    public VssUpdateConsumer( VssScmProviderRepository repo, ScmLogger logger )
104    {
105        super( logger );
106        this.repo = repo;
107    }
108
109    /** {@inheritDoc} */
110    public void consumeLine( String line )
111    {
112        if ( getLogger().isDebugEnabled() )
113        {
114            getLogger().debug( line );
115        }
116
117        switch ( getLineStatus( line ) )
118        {
119            case GET_FILE_PATH:
120                processGetFilePath( line );
121                break;
122            case GET_FILE:
123                processGetFile( line );
124                break;
125            case REPLACE_FILE:
126                processReplaceFile( line );
127                break;
128            case IS_WRITABLE_COPY:
129                // FIXME is actually in error stream if command is build without -G-
130                processWritableFile( line );
131                break;
132            case SET_WORKING_FOLDER:
133                // to trash
134                break;
135            default:
136                break;
137        }
138    }
139
140    /**
141     * Process the current input line in the Get File state.
142     *
143     * @param line a line of text from the VSS log output
144     */
145    private void processGetFile( String line )
146    {
147        String[] fileLine = line.split( " " );
148        updatedFiles.add( new ScmFile( currentPath + "/" + fileLine[1], ScmFileStatus.UPDATED ) );
149        if ( getLogger().isInfoEnabled() )
150        {
151            getLogger().info( fileLine[0] + ": " + currentPath + "/" + fileLine[1] );
152        }
153
154    }
155
156    /**
157     * Process the current input line in the Replace File state.
158     *
159     * @param line a line of text from the VSS log output
160     */
161    private void processReplaceFile( String line )
162    {
163        updatedFiles.add(
164            new ScmFile( currentPath + "/" + line.substring( START_REPLACING.length() ), ScmFileStatus.UPDATED ) );
165        if ( getLogger().isInfoEnabled() )
166        {
167            getLogger().info( START_REPLACING + currentPath + "/" + line.substring( START_REPLACING.length() ) );
168        }
169    }
170
171    /**
172     * Process the current input line in the Get File Path state.
173     *
174     * @param line a line of text from the VSS log output
175     */
176    private void processGetFilePath( String line )
177    {
178        currentPath = line.substring( ( VssConstants.PROJECT_PREFIX + repo.getProject() ).length(), line.length() - 1 );
179    }
180
181    /**
182     * Process the current input line in the writable File state.
183     *
184     * @param line a line of text from the VSS log output
185     */
186    private void processWritableFile( String line )
187    {
188        // FIXME extract file name
189        //        String[] fileLine = line.split( " " );
190        //        updatedFiles.add( new ScmFile( currentPath + "/" + fileLine[1], ScmFileStatus.MODIFIED ) );
191        //        getLogger().info( fileLine[0] + ": " + currentPath + "/" + fileLine[1] );
192
193    }
194
195    /**
196     * Identify the status of a vss get line
197     *
198     * @param line The line to process
199     * @return status
200     */
201    private int getLineStatus( String line )
202    {
203        int argument = GET_UNKNOWN;
204        if ( line.startsWith( START_FILE_PATH ) )
205        {
206            argument = GET_FILE_PATH;
207        }
208        else if ( line.startsWith( START_GETTING ) )
209        {
210            argument = GET_FILE;
211        }
212        else if ( line.startsWith( START_REPLACING ) )
213        {
214            argument = REPLACE_FILE;
215        }
216        else if ( line.startsWith( START_WRITABLE_COPY ) )
217        {
218            argument = IS_WRITABLE_COPY;
219        }
220        else if ( line.indexOf( CONTAINS_SET_DEFAULT_WORKING_FOLDER ) != -1 )
221        {
222            argument = SET_WORKING_FOLDER;
223        }
224
225        return argument;
226    }
227
228    public List<ScmFile> getUpdatedFiles()
229    {
230        return updatedFiles;
231    }
232
233}