001package org.apache.maven.scm.provider.starteam.command.status; 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.codehaus.plexus.util.cli.StreamConsumer; 026 027import java.io.File; 028import java.util.ArrayList; 029import java.util.List; 030 031/** 032 * @author <a href="mailto:dantran@gmail.com">Dan T. Tran</a> 033 * 034 */ 035public class StarteamStatusConsumer 036 implements StreamConsumer 037{ 038 private ScmLogger logger; 039 040 private String workingDirectory; 041 042 private List<ScmFile> changedFiles = new ArrayList<ScmFile>(); 043 044 /** 045 * Marks current directory data 046 */ 047 private static final String DIR_MARKER = "(working dir: "; 048 049 /** 050 * Marks current file data 051 */ 052 private static final String FILE_MARKER = "History for: "; 053 054 /** 055 * Marks current file status 056 */ 057 private static final String STATUS_MARKER = "Status: "; 058 059 /** 060 * Marks current file status 061 */ 062 private static final String OUTDATE_MARKER = "Out of Date"; 063 064 private static final String MISSING_MARKER = "Missing"; 065 066 private static final String CURRENT_MARKER = "Current"; 067 068 private static final String MERGE_MARKER = "Merge"; 069 070 private static final String MODIFIED_MARKER = "Modified"; 071 072 private String currentDir = ""; 073 074 private String currentFile = ""; 075 076 public StarteamStatusConsumer( ScmLogger logger, File basedir ) 077 { 078 this.logger = logger; 079 080 this.workingDirectory = basedir.getPath().replace( '\\', '/' ); 081 } 082 083 /** {@inheritDoc} */ 084 public void consumeLine( String line ) 085 { 086 if ( logger.isDebugEnabled() ) 087 { 088 logger.debug( line ); 089 } 090 091 int pos = 0; 092 093 if ( ( pos = line.indexOf( DIR_MARKER ) ) != -1 ) 094 { 095 processGetDir( line, pos ); 096 } 097 else if ( ( pos = line.indexOf( FILE_MARKER ) ) != -1 ) 098 { 099 processGetFile( line, pos ); 100 } 101 else if ( ( pos = line.indexOf( STATUS_MARKER ) ) != -1 ) 102 { 103 processStatus( line, pos ); 104 } 105 else 106 { 107 //do nothing 108 } 109 } 110 111 private void processGetDir( String line, int pos ) 112 { 113 String dirPath = line.substring( pos + DIR_MARKER.length(), line.length() - 1 ).replace( '\\', '/' ); 114 115 this.currentDir = "." + dirPath.substring( workingDirectory.length() ); 116 } 117 118 private void processGetFile( String line, int pos ) 119 { 120 String fileName = line.substring( pos + FILE_MARKER.length(), line.length() ); 121 122 this.currentFile = this.currentDir + "/" + fileName; 123 } 124 125 private void processStatus( String line, int pos ) 126 { 127 String status = line.substring( pos + STATUS_MARKER.length(), line.length() ); 128 129 if ( status.equals( OUTDATE_MARKER ) ) 130 { 131 changedFiles.add( new ScmFile( this.currentFile, ScmFileStatus.MODIFIED ) ); 132 133 if ( logger.isInfoEnabled() ) 134 { 135 logger.info( "Out of Date file: " + this.currentFile ); 136 } 137 } 138 else if ( status.equals( MODIFIED_MARKER ) ) 139 { 140 changedFiles.add( new ScmFile( this.currentFile, ScmFileStatus.MODIFIED ) ); 141 142 if ( logger.isInfoEnabled() ) 143 { 144 logger.info( "Modified file: " + this.currentFile ); 145 } 146 } 147 else if ( status.equals( MISSING_MARKER ) ) 148 { 149 changedFiles.add( new ScmFile( this.currentFile, ScmFileStatus.ADDED ) ); 150 151 if ( logger.isInfoEnabled() ) 152 { 153 logger.info( "Missing file: " + this.currentFile ); 154 } 155 } 156 else if ( status.equals( MERGE_MARKER ) ) 157 { 158 changedFiles.add( new ScmFile( this.currentFile, ScmFileStatus.CONFLICT ) ); 159 160 if ( logger.isInfoEnabled() ) 161 { 162 logger.info( "Conflict file: " + this.currentFile ); 163 } 164 } 165 else if ( status.equals( CURRENT_MARKER ) ) 166 { 167 //ignore 168 } 169 else 170 { 171 if ( logger.isWarnEnabled() ) 172 { 173 logger.warn( "status unknown (" + status + "): " + this.currentFile ); 174 } 175 } 176 } 177 178 public List<ScmFile> getChangedFiles() 179 { 180 return changedFiles; 181 } 182 183}