001 package org.apache.maven.scm.provider.starteam.command.remove;
002
003 /*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements. See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership. The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License. You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied. See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022 import org.apache.maven.scm.ScmFile;
023 import org.apache.maven.scm.ScmFileStatus;
024 import org.apache.maven.scm.log.ScmLogger;
025 import org.apache.maven.scm.provider.starteam.command.StarteamCommandLineUtils;
026 import org.codehaus.plexus.util.cli.StreamConsumer;
027
028 import java.io.File;
029 import java.util.ArrayList;
030 import java.util.List;
031
032 /**
033 * @author <a href="mailto:dantran@gmail.com">Dan T. Tran</a>
034 *
035 */
036 public class StarteamRemoveConsumer
037 implements StreamConsumer
038 {
039 private ScmLogger logger;
040
041 private String workingDirectory;
042
043 /**
044 * the current directory entry being processed by the parser
045 */
046 private String currentDir;
047
048 private List<ScmFile> files = new ArrayList<ScmFile>();
049
050 /**
051 * Marks current directory data
052 */
053 private static final String DIR_MARKER = "(working dir: ";
054
055 /**
056 * Marks current file data
057 */
058 private static final String ADDED_MARKER = ": removed";
059
060 /**
061 * Marks current file data
062 */
063 private static final String LINKTO_MARKER = ": linked to";
064
065 public StarteamRemoveConsumer( ScmLogger logger, File basedir )
066 {
067 this.logger = logger;
068
069 this.workingDirectory = basedir.getPath().replace( '\\', '/' );
070 }
071
072 /** {@inheritDoc} */
073 public void consumeLine( String line )
074 {
075 if ( logger.isDebugEnabled() )
076 {
077 logger.debug( line );
078 }
079
080 int pos = 0;
081
082 if ( ( pos = line.indexOf( DIR_MARKER ) ) != -1 )
083 {
084 processDirectory( line, pos );
085 }
086 else if ( ( pos = line.indexOf( ADDED_MARKER ) ) != -1 )
087 {
088 processRemovedFile( line, pos );
089 }
090 else if ( ( pos = line.indexOf( LINKTO_MARKER ) ) != -1 )
091 {
092 //ignore
093 }
094 else
095 {
096 if ( logger.isWarnEnabled() )
097 {
098 logger.warn( "Unknown remove ouput: " + line );
099 }
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 }