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