001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.provider.git.gitexe.command;
020
021import java.io.File;
022import java.util.ArrayList;
023import java.util.Iterator;
024import java.util.List;
025
026import org.apache.maven.scm.ScmFile;
027import org.apache.maven.scm.util.AbstractConsumer;
028
029/**
030 * @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a>
031 * @author Olivier Lamy
032 */
033public abstract class AbstractFileCheckingConsumer extends AbstractConsumer {
034    protected File workingDirectory;
035
036    private final List<ScmFile> files = new ArrayList<>();
037
038    protected int revision;
039
040    private boolean filtered;
041
042    public AbstractFileCheckingConsumer(File workingDirectory) {
043        this.workingDirectory = workingDirectory;
044    }
045
046    /**
047     * {@inheritDoc}
048     */
049    public final void consumeLine(String line) {
050        if (line.length() <= 3) {
051            return;
052        }
053
054        if (logger.isDebugEnabled()) {
055            logger.debug(line);
056        }
057
058        parseLine(line);
059    }
060
061    protected abstract void parseLine(String line);
062
063    protected List<ScmFile> getFiles() {
064        if (!filtered) {
065            for (Iterator<ScmFile> it = files.iterator(); it.hasNext(); ) {
066                if (!new File(workingDirectory, it.next().getPath()).isFile()) {
067                    it.remove();
068                }
069            }
070
071            filtered = true;
072        }
073
074        return files;
075    }
076
077    protected final int parseInt(String revisionString) {
078        try {
079            return Integer.parseInt(revisionString);
080        } catch (NumberFormatException ex) {
081            return 0;
082        }
083    }
084
085    protected void addFile(ScmFile file) {
086        files.add(file);
087    }
088
089    public final int getRevision() {
090        return revision;
091    }
092}