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.log.ScmLogger;
24  import org.codehaus.plexus.util.cli.StreamConsumer;
25  
26  import java.io.File;
27  import java.util.ArrayList;
28  import java.util.Iterator;
29  import java.util.List;
30  
31  /**
32   * @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a>
33   * @author Olivier Lamy
34   *
35   */
36  public abstract class AbstractFileCheckingConsumer
37      implements StreamConsumer
38  {
39      protected ScmLogger logger;
40  
41      protected File workingDirectory;
42  
43      private List<ScmFile> files = new ArrayList<ScmFile>();
44  
45      protected int revision;
46  
47      private boolean filtered;
48  
49      public AbstractFileCheckingConsumer( ScmLogger logger, File workingDirectory )
50      {
51          this.logger = logger;
52          this.workingDirectory = workingDirectory;
53      }
54  
55      /** {@inheritDoc} */
56      public final void consumeLine( String line )
57      {
58          if ( line.length() <= 3 )
59          {
60              return;
61          }
62  
63          if ( logger.isDebugEnabled() )
64          {
65              logger.debug( line );
66          }
67  
68          parseLine( line );
69      }
70  
71      protected abstract void parseLine( String line );
72  
73      protected List<ScmFile> getFiles()
74      {
75          if ( !filtered )
76          {
77              for ( Iterator<ScmFile> it = files.iterator(); it.hasNext(); )
78              {
79                  if ( !new File( workingDirectory, it.next().getPath() ).isFile() )
80                  {
81                      it.remove();
82                  }
83              }
84  
85              filtered = true;
86          }
87  
88          return files;
89      }
90  
91      protected final int parseInt( String revisionString )
92      {
93          try
94          {
95              return Integer.parseInt( revisionString );
96          }
97          catch ( NumberFormatException ex )
98          {
99              return 0;
100         }
101     }
102 
103     protected void addFile( ScmFile file )
104     {
105         files.add( file );
106     }
107 
108     public final int getRevision()
109     {
110         return revision;
111     }
112 
113 }