001package org.apache.maven.scm.provider.vss.commands.checkin;
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 * @since 1.3
037 */
038public class VssCheckInConsumer
039    extends AbstractConsumer
040    implements StreamConsumer
041{
042
043    /**
044     * expecting file information
045     */
046    private static final int GET_UNKNOWN = 0;
047
048    /**
049     * expecting file information
050     */
051    private static final int GET_FILE = 1;
052
053    /**
054     * expecting file information
055     */
056    private static final int REPLACE_FILE = 2;
057
058    /**
059     * expecting file path information
060     */
061    private static final int GET_FILE_PATH = 3;
062
063    /**
064     * expecting writable copy
065     */
066    private static final int IS_WRITABLE_COPY = 4;
067
068    /**
069     * expecting working folder
070     */
071    private static final int SET_WORKING_FOLDER = 5;
072
073    /**
074     * Marks start of file data
075     */
076    private static final String START_FILE_PATH = "$/";
077
078    /**
079     * Marks getting a new File
080     */
081    private static final String START_GETTING = "Getting";
082
083    /**
084     * Marks replacing a old File
085     */
086    private static final String START_REPLACING = "Replacing local copy of ";
087
088    /**
089     * Marks a writable copy of a File / maybe a conflict
090     */
091    private static final String START_WRITABLE_COPY = "A writable ";
092
093    /**
094     * Marks "Set the default folder for project" question
095     */
096    private static final String CONTAINS_SET_DEFAULT_WORKING_FOLDER = "as the default folder for project";
097
098    private String currentPath = "";
099
100    private List<ScmFile> updatedFiles = new ArrayList<ScmFile>();
101
102    private VssScmProviderRepository repo;
103
104    public VssCheckInConsumer( VssScmProviderRepository repo, ScmLogger logger )
105    {
106        super( logger );
107        this.repo = repo;
108    }
109
110    /** {@inheritDoc} */
111    public void consumeLine( String line )
112    {
113        if ( getLogger().isDebugEnabled() )
114        {
115            getLogger().debug( line );
116        }
117
118        switch ( getLineStatus( line ) )
119        {
120            case GET_FILE_PATH:
121                processGetFilePath( line );
122                break;
123            case GET_FILE:
124                processGetFile( line );
125                break;
126            case REPLACE_FILE:
127                processReplaceFile( line );
128                break;
129            case IS_WRITABLE_COPY:
130                // will be overwritten and uses REPLACE_FILE
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     * Process the current input line in the Replace File state.
157     * 
158     * @param line a line of text from the VSS log output
159     */
160    private void processReplaceFile( String line )
161    {
162        updatedFiles.add( new ScmFile( currentPath + "/" + line.substring( START_REPLACING.length() ),
163                                       ScmFileStatus.UPDATED ) );
164        if ( getLogger().isInfoEnabled() )
165        {
166            getLogger().info( START_REPLACING + currentPath + "/" + line.substring( START_REPLACING.length() ) );
167        }
168    }
169
170    /**
171     * Process the current input line in the Get File Path state.
172     * 
173     * @param line a line of text from the VSS log output
174     */
175    private void processGetFilePath( String line )
176    {
177        currentPath = line.substring( ( VssConstants.PROJECT_PREFIX + repo.getProject() ).length(), line.length() - 1 );
178    }
179
180    /**
181     * Identify the status of a vss get line
182     * 
183     * @param line The line to process
184     * @return status
185     */
186    private int getLineStatus( String line )
187    {
188        int argument = GET_UNKNOWN;
189        if ( line.startsWith( START_FILE_PATH ) )
190        {
191            argument = GET_FILE_PATH;
192        }
193        else if ( line.startsWith( START_GETTING ) )
194        {
195            argument = GET_FILE;
196        }
197        else if ( line.startsWith( START_REPLACING ) )
198        {
199            argument = REPLACE_FILE;
200        }
201        else if ( line.startsWith( START_WRITABLE_COPY ) )
202        {
203            argument = IS_WRITABLE_COPY;
204        }
205        else if ( line.indexOf( CONTAINS_SET_DEFAULT_WORKING_FOLDER ) != -1 )
206        {
207            argument = SET_WORKING_FOLDER;
208        }
209
210        return argument;
211    }
212
213    public List<ScmFile> getUpdatedFiles()
214    {
215        return updatedFiles;
216    }
217
218}