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.svn.svnexe.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.ScmFileStatus;
28  import org.apache.maven.scm.util.AbstractConsumer;
29  
30  /**
31   * @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a>
32   */
33  public abstract class AbstractFileCheckingConsumer extends AbstractConsumer {
34      protected File workingDirectory;
35  
36      private final List<ScmFile> files = new ArrayList<>();
37  
38      protected int revision;
39  
40      private boolean filtered;
41  
42      public AbstractFileCheckingConsumer(File workingDirectory) {
43          this.workingDirectory = workingDirectory;
44      }
45  
46      /**
47       * {@inheritDoc}
48       */
49      public final void consumeLine(String line) {
50          if (line.length() <= 3) {
51              return;
52          }
53  
54          if (logger.isDebugEnabled()) {
55              logger.debug(line);
56          }
57  
58          try {
59              parseLine(line);
60          } catch (RuntimeException re) {
61              logger.warn("RuntimeException while parsing: " + line, re);
62              throw re;
63          }
64      }
65  
66      protected abstract void parseLine(String line);
67  
68      protected List<ScmFile> getFiles() {
69  
70          if (!filtered) {
71              for (Iterator<ScmFile> ite = files.iterator(); ite.hasNext(); ) {
72                  ScmFile file = ite.next();
73                  if (!file.getStatus().equals(ScmFileStatus.DELETED)
74                          && !new File(workingDirectory, file.getPath()).isFile()) {
75                      ite.remove();
76                  }
77              }
78  
79              filtered = true;
80          }
81  
82          return files;
83      }
84  
85      protected final int parseInt(String revisionString) {
86          try {
87              return Integer.parseInt(revisionString);
88          } catch (NumberFormatException ex) {
89              return 0;
90          }
91      }
92  
93      protected void addFile(ScmFile file) {
94          files.add(file);
95      }
96  
97      public final int getRevision() {
98          return revision;
99      }
100 
101     public File getWorkingDirectory() {
102         return workingDirectory;
103     }
104 }