View Javadoc
1   package org.apache.maven.scm.provider.tfs.command.consumer;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.apache.maven.scm.ScmFile;
28  import org.apache.maven.scm.ScmFileStatus;
29  import org.apache.maven.scm.log.ScmLogger;
30  import org.codehaus.plexus.util.cli.StreamConsumer;
31  
32  public class ChangedFileConsumer
33      implements StreamConsumer
34  {
35  
36      private ScmLogger logger;
37  
38      private static final String KEY_CHANGE = "Change";
39  
40      private static final String KEY_LOCAL_ITEM = "Local item";
41  
42      private static final String CHANGE_EDIT = "edit";
43  
44      private static final String CHANGE_ADD = "add";
45  
46      private Map<String,String> values = new HashMap<String,String>();
47  
48      private List<ScmFile> changedFiles = new ArrayList<ScmFile>();
49  
50      public ChangedFileConsumer( ScmLogger logger )
51      {
52          this.logger = logger;
53      }
54  
55      public void consumeLine( String line )
56      {
57          if ( line.indexOf( ':' ) >= 0 )
58          {
59              String[] s = line.split( ":", 2 );
60              if ( s.length > 1 )
61                  values.put( s[0].trim(), s[1].trim() );
62          }
63          if ( line.trim().equals( "" ) )
64          {
65              extractChangedFile();
66          }
67          logger.debug( "line -" + line );
68      }
69  
70      private void extractChangedFile()
71      {
72          String change = getChange();
73          if ( change != null )
74          {
75              ScmFileStatus stat = ScmFileStatus.UNKNOWN;
76              if ( change.equals( ChangedFileConsumer.CHANGE_EDIT ) )
77                  stat = ScmFileStatus.MODIFIED;
78              if ( change.equals( ChangedFileConsumer.CHANGE_ADD ) )
79                  stat = ScmFileStatus.ADDED;
80              changedFiles.add( new ScmFile( getLocalPath(), stat ) );
81              values.clear();
82          }
83      }
84  
85      public List<ScmFile> getChangedFiles()
86      {
87          if ( values.size() > 0 )
88          {
89              extractChangedFile();
90          }
91          return changedFiles;
92      }
93  
94      private String getChange()
95      {
96          return (String) values.get( KEY_CHANGE );
97      }
98  
99      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 }