001package org.apache.maven.scm.provider.starteam.command.checkout; 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 022import org.apache.maven.scm.ScmFile; 023import org.apache.maven.scm.ScmFileStatus; 024import org.apache.maven.scm.log.ScmLogger; 025import org.apache.maven.scm.provider.starteam.command.StarteamCommandLineUtils; 026import org.codehaus.plexus.util.cli.StreamConsumer; 027 028import java.io.File; 029import java.util.ArrayList; 030import java.util.List; 031 032/** 033 * @author <a href="mailto:dantran@gmail.com">Dan T. Tran</a> 034 * @author Olivier Lamy 035 * 036 */ 037public class StarteamCheckOutConsumer 038 implements StreamConsumer 039{ 040 private ScmLogger logger; 041 042 private String workingDirectory; 043 044 private String currentDir = ""; 045 046 private List<ScmFile> files = new ArrayList<ScmFile>(); 047 048 /** 049 * Marks current directory data 050 */ 051 private static final String DIR_MARKER = "(working dir: "; 052 053 /** 054 * Marks current file data 055 */ 056 private static final String CHECKOUT_MARKER = ": checked out"; 057 058 /** 059 * Marks skipped file during update 060 */ 061 private static final String SKIPPED_MARKER = ": skipped"; 062 063 public StarteamCheckOutConsumer( ScmLogger logger, File workingDirectory ) 064 { 065 this.logger = logger; 066 067 this.workingDirectory = workingDirectory.getPath().replace( '\\', '/' ); 068 } 069 070 /** {@inheritDoc} */ 071 public void consumeLine( String line ) 072 { 073 if ( logger.isDebugEnabled() ) 074 { 075 logger.debug( line ); 076 } 077 078 int pos = 0; 079 080 if ( ( pos = line.indexOf( CHECKOUT_MARKER ) ) != -1 ) 081 { 082 processCheckedOutFile( line, pos ); 083 } 084 else if ( ( pos = line.indexOf( DIR_MARKER ) ) != -1 ) 085 { 086 processDirectory( line, pos ); 087 } 088 else if ( ( pos = line.indexOf( CHECKOUT_MARKER ) ) != -1 ) 089 { 090 processCheckedOutFile( line, pos ); 091 } 092 else if ( ( pos = line.indexOf( SKIPPED_MARKER ) ) != -1 ) 093 { 094 processSkippedFile( line, pos ); 095 } 096 else 097 { 098 if ( logger.isWarnEnabled() ) 099 { 100 logger.warn( "Unknown checkout ouput: " + line ); 101 } 102 } 103 } 104 105 public List<ScmFile> getCheckedOutFiles() 106 { 107 return files; 108 } 109 110 private void processDirectory( String line, int pos ) 111 { 112 String dirPath = line.substring( pos + DIR_MARKER.length(), line.length() - 1 ).replace( '\\', '/' ); 113 114 try 115 { 116 this.currentDir = StarteamCommandLineUtils.getRelativeChildDirectory( this.workingDirectory, dirPath ); 117 } 118 catch ( IllegalStateException e ) 119 { 120 String error = "Working and checkout directories are not on the same tree"; 121 122 if ( logger.isErrorEnabled() ) 123 { 124 logger.error( error ); 125 logger.error( "Working directory: " + workingDirectory ); 126 logger.error( "Checked out directory: " + dirPath ); 127 } 128 129 throw new IllegalStateException( error ); 130 } 131 132 } 133 134 private void processCheckedOutFile( String line, int pos ) 135 { 136 String checkedOutFilePath = this.currentDir + "/" + line.substring( 0, pos ); 137 138 this.files.add( new ScmFile( checkedOutFilePath, ScmFileStatus.CHECKED_OUT ) ); 139 140 if ( logger.isInfoEnabled() ) 141 { 142 logger.info( "Checked out: " + checkedOutFilePath ); 143 } 144 } 145 146 private void processSkippedFile( String line, int pos ) 147 { 148 String skippedFilePath = this.currentDir + "/" + line.substring( 0, pos ); 149 150 if ( logger.isDebugEnabled() ) 151 { 152 logger.debug( "Skipped: " + skippedFilePath ); 153 } 154 } 155 156}