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