001    package org.apache.maven.scm.provider.svn.svnexe.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    
022    import java.io.File;
023    import java.util.ArrayList;
024    import java.util.Iterator;
025    import java.util.List;
026    
027    import org.apache.maven.scm.ScmFile;
028    import org.apache.maven.scm.ScmFileStatus;
029    import org.apache.maven.scm.log.ScmLogger;
030    import org.codehaus.plexus.util.cli.StreamConsumer;
031    
032    /**
033     * @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a>
034     * @version $Id: AbstractFileCheckingConsumer.java 1373268 2012-08-15 08:32:31Z hboutemy $
035     */
036    public 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            try
069            {
070                parseLine( line );
071            }
072            catch ( RuntimeException re )
073            {
074                logger.warn( "RuntimeException while parsing: " + line , re );
075                throw re;
076            }
077        }
078    
079        protected abstract void parseLine( String line );
080    
081        protected List<ScmFile> getFiles()
082        {
083            
084            if ( !filtered )
085            {
086                for ( Iterator<ScmFile> ite = files.iterator(); ite.hasNext(); )
087                {
088                    ScmFile file = ite.next();
089                    if ( !file.getStatus().equals( ScmFileStatus.DELETED )
090                        && !new File( workingDirectory, file.getPath() ).isFile() )
091                    {
092                        ite.remove();
093                    }
094                }
095    
096                filtered = true;
097            }
098    
099            return files;
100        }
101    
102        protected final int parseInt( String revisionString )
103        {
104            try
105            {
106                return Integer.parseInt( revisionString );
107            }
108            catch ( NumberFormatException ex )
109            {
110                return 0;
111            }
112        }
113    
114        protected void addFile( ScmFile file )
115        {
116            files.add( file );
117        }
118    
119        public final int getRevision()
120        {
121            return revision;
122        }
123    
124        public File getWorkingDirectory()
125        {
126            return workingDirectory;
127        }
128    
129    }