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;
027import org.apache.regexp.RE;
028import org.apache.regexp.RESyntaxException;
029
030import java.util.ArrayList;
031import java.util.List;
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 String DOWNLOAD_PATTERN = "^Downloading\\s(.*)\\s\\s\\(\\d.*B\\)$";
042
043    /**
044     * @see #DOWNLOAD_PATTERN
045     */
046    private RE downloadRegexp;
047
048    protected String fCurrentDir = "";
049
050    private List<ScmFile> fCheckedOutFiles = new ArrayList<ScmFile>();
051
052    /**
053     * Construct the JazzCheckOutCommand consumer.
054     *
055     * @param repository The repository we are working with.
056     * @param logger     The logger to use.
057     */
058    public JazzCheckOutConsumer( ScmProviderRepository repository, ScmLogger logger )
059    {
060        super( repository, logger );
061
062        try
063        {
064            downloadRegexp = new RE( DOWNLOAD_PATTERN );
065        }
066        catch ( RESyntaxException ex )
067        {
068            throw new RuntimeException(
069                "INTERNAL ERROR: Could not create regexp to parse jazz scm checkout output. This shouldn't happen. Something is probably wrong with the oro installation.",
070                ex );
071        }
072    }
073
074    /**
075     * Process one line of output from the execution of the "scm load" command.
076     *
077     * @param line The line of output from the external command that has been pumped to us.
078     * @see org.codehaus.plexus.util.cli.StreamConsumer#consumeLine(java.lang.String)
079     */
080    public void consumeLine( String line )
081    {
082        // Examples:
083        // Downloading /checkout-test/src/emptyFile.txt (0 B)
084        // Downloading /checkout-test/src/folder with spaces/file with spaces.java (24.0 KB)
085        if ( downloadRegexp.match( line ) )
086        {
087            fCheckedOutFiles.add( new ScmFile( downloadRegexp.getParen( 1 ), ScmFileStatus.CHECKED_OUT ) );
088        }
089    }
090
091    public List<ScmFile> getCheckedOutFiles()
092    {
093        return fCheckedOutFiles;
094    }
095
096}