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 *
033 */
034public abstract class AbstractFileCheckingConsumer extends AbstractConsumer {
035    protected File workingDirectory;
036
037    private final List<ScmFile> files = new ArrayList<>();
038
039    protected int revision;
040
041    private boolean filtered;
042
043    public AbstractFileCheckingConsumer(File workingDirectory) {
044        this.workingDirectory = workingDirectory;
045    }
046
047    /** {@inheritDoc} */
048    public final void consumeLine(String line) {
049        if (line.length() <= 3) {
050            return;
051        }
052
053        if (logger.isDebugEnabled()) {
054            logger.debug(line);
055        }
056
057        parseLine(line);
058    }
059
060    protected abstract void parseLine(String line);
061
062    protected List<ScmFile> getFiles() {
063        if (!filtered) {
064            for (Iterator<ScmFile> it = files.iterator(); it.hasNext(); ) {
065                if (!new File(workingDirectory, it.next().getPath()).isFile()) {
066                    it.remove();
067                }
068            }
069
070            filtered = true;
071        }
072
073        return files;
074    }
075
076    protected final int parseInt(String revisionString) {
077        try {
078            return Integer.parseInt(revisionString);
079        } catch (NumberFormatException ex) {
080            return 0;
081        }
082    }
083
084    protected void addFile(ScmFile file) {
085        files.add(file);
086    }
087
088    public final int getRevision() {
089        return revision;
090    }
091}