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  /**
33   * 
34   */
35  public class ChangedFileConsumer
36      implements StreamConsumer
37  {
38  
39      private ScmLogger logger;
40  
41      private static final String KEY_CHANGE = "Change";
42  
43      private static final String KEY_LOCAL_ITEM = "Local item";
44  
45      private static final String CHANGE_EDIT = "edit";
46  
47      private static final String CHANGE_ADD = "add";
48  
49      private Map<String, String> values = new HashMap<String, String>();
50  
51      private List<ScmFile> changedFiles = new ArrayList<ScmFile>();
52  
53      public ChangedFileConsumer( ScmLogger logger )
54      {
55          this.logger = logger;
56      }
57  
58      public void consumeLine( String line )
59      {
60          if ( line.indexOf( ':' ) >= 0 )
61          {
62              String[] s = line.split( ":", 2 );
63              if ( s.length > 1 )
64              {
65                  values.put( s[0].trim(), s[1].trim() );
66              }
67          }
68          if ( line.trim().equals( "" ) )
69          {
70              extractChangedFile();
71          }
72          logger.debug( "line -" + line );
73      }
74  
75      private void extractChangedFile()
76      {
77          String change = getChange();
78          if ( change != null )
79          {
80              ScmFileStatus stat = ScmFileStatus.UNKNOWN;
81              if ( change.equals( ChangedFileConsumer.CHANGE_EDIT ) )
82              {
83                  stat = ScmFileStatus.MODIFIED;
84              }
85              if ( change.equals( ChangedFileConsumer.CHANGE_ADD ) )
86              {
87                  stat = ScmFileStatus.ADDED;
88              }
89              changedFiles.add( new ScmFile( getLocalPath(), stat ) );
90              values.clear();
91          }
92      }
93  
94      public List<ScmFile> getChangedFiles()
95      {
96          if ( values.size() > 0 )
97          {
98              extractChangedFile();
99          }
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 }