001package org.apache.maven.scm.provider.git.gitexe.command;
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.log.ScmLogger;
024import org.codehaus.plexus.util.cli.StreamConsumer;
025
026import java.io.File;
027import java.util.ArrayList;
028import java.util.Iterator;
029import java.util.List;
030
031/**
032 * @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a>
033 * @author Olivier Lamy
034 *
035 */
036public abstract class AbstractFileCheckingConsumer
037    implements StreamConsumer
038{
039    protected ScmLogger logger;
040
041    protected File workingDirectory;
042
043    private List<ScmFile> files = new ArrayList<ScmFile>();
044
045    protected int revision;
046
047    private boolean filtered;
048
049    public AbstractFileCheckingConsumer( ScmLogger logger, File workingDirectory )
050    {
051        this.logger = logger;
052        this.workingDirectory = workingDirectory;
053    }
054
055    /** {@inheritDoc} */
056    public final void consumeLine( String line )
057    {
058        if ( line.length() <= 3 )
059        {
060            return;
061        }
062
063        if ( logger.isDebugEnabled() )
064        {
065            logger.debug( line );
066        }
067
068        parseLine( line );
069    }
070
071    protected abstract void parseLine( String line );
072
073    protected List<ScmFile> getFiles()
074    {
075        if ( !filtered )
076        {
077            for ( Iterator<ScmFile> it = files.iterator(); it.hasNext(); )
078            {
079                if ( !new File( workingDirectory, it.next().getPath() ).isFile() )
080                {
081                    it.remove();
082                }
083            }
084
085            filtered = true;
086        }
087
088        return files;
089    }
090
091    protected final int parseInt( String revisionString )
092    {
093        try
094        {
095            return Integer.parseInt( revisionString );
096        }
097        catch ( NumberFormatException ex )
098        {
099            return 0;
100        }
101    }
102
103    protected void addFile( ScmFile file )
104    {
105        files.add( file );
106    }
107
108    public final int getRevision()
109    {
110        return revision;
111    }
112
113}