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