001package org.apache.maven.scm.provider.starteam.command.edit; 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@apache.org">Dan T. Tran</a> 033 * 034 */ 035public class StarteamEditConsumer 036 implements StreamConsumer 037{ 038 private String workingDirectory; 039 040 private ScmLogger logger; 041 042 private List<ScmFile> files = new ArrayList<ScmFile>(); 043 044 /** 045 * the current directory entry being processed by the parser 046 */ 047 private String currentDir = ""; 048 049 /** 050 * Marks current directory data 051 */ 052 private static final String DIR_MARKER = "(working dir: "; 053 054 /** 055 * Marks current file data 056 */ 057 private static final String LOCKED_MARKER = ": locked"; 058 059 060 public StarteamEditConsumer( ScmLogger logger, File basedir ) 061 { 062 this.logger = logger; 063 this.workingDirectory = basedir.getPath().replace( '\\', '/' ); 064 } 065 066 /** {@inheritDoc} */ 067 public void consumeLine( String line ) 068 { 069 if ( logger.isDebugEnabled() ) 070 { 071 logger.debug( line ); 072 } 073 int pos = 0; 074 075 if ( ( pos = line.indexOf( DIR_MARKER ) ) != -1 ) 076 { 077 processDirectory( line, pos ); 078 } 079 else if ( ( pos = line.indexOf( LOCKED_MARKER ) ) != -1 ) 080 { 081 processLockedFile( line, pos ); 082 } 083 else 084 { 085 if ( logger.isWarnEnabled() ) 086 { 087 logger.warn( "Unknown edit ouput: " + line ); 088 } 089 } 090 } 091 092 public List<ScmFile> getEditedFiles() 093 { 094 return files; 095 } 096 097 private void processDirectory( String line, int pos ) 098 { 099 String dirPath = line.substring( pos + DIR_MARKER.length(), line.length() - 1 ).replace( '\\', '/' ); 100 101 if ( !dirPath.startsWith( workingDirectory ) ) 102 { 103 if ( logger.isInfoEnabled() ) 104 { 105 logger.info( "Working directory: " + workingDirectory ); 106 logger.info( "Edit directory: " + dirPath ); 107 } 108 109 throw new IllegalStateException( "Working and edit directories are not on the same tree" ); 110 } 111 112 this.currentDir = "." + dirPath.substring( workingDirectory.length() ); 113 } 114 115 private void processLockedFile( String line, int pos ) 116 { 117 String lockedFilePath = this.currentDir + "/" + line.substring( 0, pos ); 118 119 this.files.add( new ScmFile( lockedFilePath, ScmFileStatus.EDITED ) ); 120 121 if ( logger.isInfoEnabled() ) 122 { 123 logger.info( "Locked: " + lockedFilePath ); 124 } 125 } 126 127 128}