View Javadoc
1   package org.apache.maven.scm.provider.starteam.command.remove;
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.ScmFileStatus;
24  import org.apache.maven.scm.log.ScmLogger;
25  import org.apache.maven.scm.provider.starteam.command.StarteamCommandLineUtils;
26  import org.codehaus.plexus.util.cli.StreamConsumer;
27  
28  import java.io.File;
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  /**
33   * @author <a href="mailto:dantran@gmail.com">Dan T. Tran</a>
34   *
35   */
36  public class StarteamRemoveConsumer
37      implements StreamConsumer
38  {
39      private ScmLogger logger;
40  
41      private String workingDirectory;
42  
43      /**
44       * the current directory entry being processed by the parser
45       */
46      private String currentDir;
47  
48      private List<ScmFile> files = new ArrayList<ScmFile>();
49  
50      /**
51       * Marks current directory data
52       */
53      private static final String DIR_MARKER = "(working dir: ";
54  
55      /**
56       * Marks current file data
57       */
58      private static final String ADDED_MARKER = ": removed";
59  
60      /**
61       * Marks current file data
62       */
63      private static final String LINKTO_MARKER = ": linked to";
64  
65      public StarteamRemoveConsumer( ScmLogger logger, File basedir )
66      {
67          this.logger = logger;
68  
69          this.workingDirectory = basedir.getPath().replace( '\\', '/' );
70      }
71  
72      /** {@inheritDoc} */
73      public void consumeLine( String line )
74      {
75          if ( logger.isDebugEnabled() )
76          {
77              logger.debug( line );
78          }
79  
80          int pos = 0;
81  
82          if ( ( pos = line.indexOf( DIR_MARKER ) ) != -1 )
83          {
84              processDirectory( line, pos );
85          }
86          else if ( ( pos = line.indexOf( ADDED_MARKER ) ) != -1 )
87          {
88              processRemovedFile( line, pos );
89          }
90          else if ( ( pos = line.indexOf( LINKTO_MARKER ) ) != -1 )
91          {
92              //ignore
93          }
94          else
95          {
96              if ( logger.isWarnEnabled() )
97              {
98                  logger.warn( "Unknown remove ouput: " + line );
99              }
100         }
101     }
102 
103     public List<ScmFile> getRemovedFiles()
104     {
105         return files;
106     }
107 
108     private void processDirectory( String line, int pos )
109     {
110         String dirPath = line.substring( pos + DIR_MARKER.length(), line.length() - 1 ).replace( '\\', '/' );
111 
112         try
113         {
114             this.currentDir = StarteamCommandLineUtils.getRelativeChildDirectory( this.workingDirectory, dirPath );
115         }
116         catch ( IllegalStateException e )
117         {
118             String error = "Working and checkout directories are not on the same tree";
119 
120             if ( logger.isErrorEnabled() )
121             {
122                 logger.error( error );
123                 logger.error( "Working directory: " + workingDirectory );
124                 logger.error( "Checked out directory: " + dirPath );
125             }
126 
127             throw new IllegalStateException( error );
128         }
129     }
130 
131     private void processRemovedFile( String line, int pos )
132     {
133         String addedFilePath = this.currentDir + "/" + line.substring( 0, pos );
134 
135         this.files.add( new ScmFile( addedFilePath, ScmFileStatus.DELETED ) );
136 
137         if ( logger.isInfoEnabled() )
138         {
139             logger.info( "Removed: " + addedFilePath );
140         }
141     }
142 
143 }