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.svn.svnexe.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.ScmFileStatus;
028import org.apache.maven.scm.util.AbstractConsumer;
029
030/**
031 * @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a>
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        try {
059            parseLine(line);
060        } catch (RuntimeException re) {
061            logger.warn("RuntimeException while parsing: " + line, re);
062            throw re;
063        }
064    }
065
066    protected abstract void parseLine(String line);
067
068    protected List<ScmFile> getFiles() {
069
070        if (!filtered) {
071            for (Iterator<ScmFile> ite = files.iterator(); ite.hasNext(); ) {
072                ScmFile file = ite.next();
073                if (!file.getStatus().equals(ScmFileStatus.DELETED)
074                        && !new File(workingDirectory, file.getPath()).isFile()) {
075                    ite.remove();
076                }
077            }
078
079            filtered = true;
080        }
081
082        return files;
083    }
084
085    protected final int parseInt(String revisionString) {
086        try {
087            return Integer.parseInt(revisionString);
088        } catch (NumberFormatException ex) {
089            return 0;
090        }
091    }
092
093    protected void addFile(ScmFile file) {
094        files.add(file);
095    }
096
097    public final int getRevision() {
098        return revision;
099    }
100
101    public File getWorkingDirectory() {
102        return workingDirectory;
103    }
104}