001package org.apache.maven.scm.provider.jazz.command.checkout;
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.ScmProviderRepository;
026import org.apache.maven.scm.provider.jazz.command.consumer.AbstractRepositoryConsumer;
027
028import java.util.ArrayList;
029import java.util.List;
030import java.util.regex.Matcher;
031import java.util.regex.Pattern;
032
033/**
034 * Consume the output of the scm command for the "load" operation.
035 *
036 * @author <a href="mailto:ChrisGWarp@gmail.com">Chris Graham</a>
037 */
038public class JazzCheckOutConsumer
039    extends AbstractRepositoryConsumer
040{
041    private static final Pattern DOWNLOAD_PATTERN = Pattern.compile( "^Downloading\\s(.*)\\s\\s\\(\\d.*B\\)$" );
042
043    protected String fCurrentDir = "";
044
045    private List<ScmFile> fCheckedOutFiles = new ArrayList<ScmFile>();
046
047    /**
048     * Construct the JazzCheckOutCommand consumer.
049     *
050     * @param repository The repository we are working with.
051     * @param logger     The logger to use.
052     */
053    public JazzCheckOutConsumer( ScmProviderRepository repository, ScmLogger logger )
054    {
055        super( repository, logger );
056    }
057
058    /**
059     * Process one line of output from the execution of the "scm load" command.
060     *
061     * @param line The line of output from the external command that has been pumped to us.
062     * @see org.codehaus.plexus.util.cli.StreamConsumer#consumeLine(java.lang.String)
063     */
064    public void consumeLine( String line )
065    {
066        // Examples:
067        // Downloading /checkout-test/src/emptyFile.txt (0 B)
068        // Downloading /checkout-test/src/folder with spaces/file with spaces.java (24.0 KB)
069        Matcher matcher = DOWNLOAD_PATTERN.matcher( line );
070        if ( matcher.matches() )
071        {
072            fCheckedOutFiles.add( new ScmFile( matcher.group( 1 ), ScmFileStatus.CHECKED_OUT ) );
073        }
074    }
075
076    public List<ScmFile> getCheckedOutFiles()
077    {
078        return fCheckedOutFiles;
079    }
080
081}