View Javadoc
1   package org.apache.maven.scm.provider.git.gitexe.command;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.scm.ScmFile;
23  import org.apache.maven.scm.util.AbstractConsumer;
24  
25  import java.io.File;
26  import java.util.ArrayList;
27  import java.util.Iterator;
28  import java.util.List;
29  
30  /**
31   * @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a>
32   * @author Olivier Lamy
33   *
34   */
35  public abstract class AbstractFileCheckingConsumer
36          extends AbstractConsumer
37  {
38      protected File workingDirectory;
39  
40      private final List<ScmFile> files = new ArrayList<>();
41  
42      protected int revision;
43  
44      private boolean filtered;
45  
46      public AbstractFileCheckingConsumer( File workingDirectory )
47      {
48          this.workingDirectory = workingDirectory;
49      }
50  
51      /** {@inheritDoc} */
52      public final void consumeLine( String line )
53      {
54          if ( line.length() <= 3 )
55          {
56              return;
57          }
58  
59          if ( logger.isDebugEnabled() )
60          {
61              logger.debug( line );
62          }
63  
64          parseLine( line );
65      }
66  
67      protected abstract void parseLine( String line );
68  
69      protected List<ScmFile> getFiles()
70      {
71          if ( !filtered )
72          {
73              for ( Iterator<ScmFile> it = files.iterator(); it.hasNext(); )
74              {
75                  if ( !new File( workingDirectory, it.next().getPath() ).isFile() )
76                  {
77                      it.remove();
78                  }
79              }
80  
81              filtered = true;
82          }
83  
84          return files;
85      }
86  
87      protected final int parseInt( String revisionString )
88      {
89          try
90          {
91              return Integer.parseInt( revisionString );
92          }
93          catch ( NumberFormatException ex )
94          {
95              return 0;
96          }
97      }
98  
99      protected void addFile( ScmFile file )
100     {
101         files.add( file );
102     }
103 
104     public final int getRevision()
105     {
106         return revision;
107     }
108 
109 }