View Javadoc

1   package org.apache.maven.scm.provider.svn.svnexe.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 java.io.File;
23  import java.util.ArrayList;
24  import java.util.Iterator;
25  import java.util.List;
26  
27  import org.apache.maven.scm.ScmFile;
28  import org.apache.maven.scm.ScmFileStatus;
29  import org.apache.maven.scm.log.ScmLogger;
30  import org.codehaus.plexus.util.cli.StreamConsumer;
31  
32  /**
33   * @author <a href="mailto:kenney@apache.org">Kenney Westerhof</a>
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          try
69          {
70              parseLine( line );
71          }
72          catch ( RuntimeException re )
73          {
74              logger.warn( "RuntimeException while parsing: " + line , re );
75              throw re;
76          }
77      }
78  
79      protected abstract void parseLine( String line );
80  
81      protected List<ScmFile> getFiles()
82      {
83          
84          if ( !filtered )
85          {
86              for ( Iterator<ScmFile> ite = files.iterator(); ite.hasNext(); )
87              {
88                  ScmFile file = ite.next();
89                  if ( !file.getStatus().equals( ScmFileStatus.DELETED )
90                      && !new File( workingDirectory, file.getPath() ).isFile() )
91                  {
92                      ite.remove();
93                  }
94              }
95  
96              filtered = true;
97          }
98  
99          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 }