001package org.apache.maven.scm.provider.tfs.command.consumer;
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 java.util.ArrayList;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026
027import org.apache.maven.scm.ScmFile;
028import org.apache.maven.scm.ScmFileStatus;
029import org.apache.maven.scm.log.ScmLogger;
030import org.codehaus.plexus.util.cli.StreamConsumer;
031
032public class ChangedFileConsumer
033    implements StreamConsumer
034{
035
036    private ScmLogger logger;
037
038    private static final String KEY_CHANGE = "Change";
039
040    private static final String KEY_LOCAL_ITEM = "Local item";
041
042    private static final String CHANGE_EDIT = "edit";
043
044    private static final String CHANGE_ADD = "add";
045
046    private Map<String,String> values = new HashMap<String,String>();
047
048    private List<ScmFile> changedFiles = new ArrayList<ScmFile>();
049
050    public ChangedFileConsumer( ScmLogger logger )
051    {
052        this.logger = logger;
053    }
054
055    public void consumeLine( String line )
056    {
057        if ( line.indexOf( ':' ) >= 0 )
058        {
059            String[] s = line.split( ":", 2 );
060            if ( s.length > 1 )
061                values.put( s[0].trim(), s[1].trim() );
062        }
063        if ( line.trim().equals( "" ) )
064        {
065            extractChangedFile();
066        }
067        logger.debug( "line -" + line );
068    }
069
070    private void extractChangedFile()
071    {
072        String change = getChange();
073        if ( change != null )
074        {
075            ScmFileStatus stat = ScmFileStatus.UNKNOWN;
076            if ( change.equals( ChangedFileConsumer.CHANGE_EDIT ) )
077                stat = ScmFileStatus.MODIFIED;
078            if ( change.equals( ChangedFileConsumer.CHANGE_ADD ) )
079                stat = ScmFileStatus.ADDED;
080            changedFiles.add( new ScmFile( getLocalPath(), stat ) );
081            values.clear();
082        }
083    }
084
085    public List<ScmFile> getChangedFiles()
086    {
087        if ( values.size() > 0 )
088        {
089            extractChangedFile();
090        }
091        return changedFiles;
092    }
093
094    private String getChange()
095    {
096        return (String) values.get( KEY_CHANGE );
097    }
098
099    private String getLocalPath()
100    {
101        String local = (String) values.get( KEY_LOCAL_ITEM );
102        if ( local != null )
103        {
104            local = local.split( "]", 2 )[1].trim();
105        }
106        return local;
107    }
108
109}