View Javadoc
1   package org.apache.maven.scm.provider.starteam.command.unedit;
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 org.apache.maven.scm.ScmFile;
23  import org.apache.maven.scm.ScmFileStatus;
24  import org.apache.maven.scm.log.ScmLogger;
25  import org.codehaus.plexus.util.cli.StreamConsumer;
26  
27  import java.io.File;
28  import java.util.ArrayList;
29  import java.util.List;
30  
31  /**
32   * @author <a href="mailto:dantran@apache.org">Dan T. Tran</a>
33   * @author Olivier Lamy
34   *
35   */
36  public class StarteamUnEditConsumer
37      implements StreamConsumer
38  {
39      private String workingDirectory;
40  
41      private ScmLogger logger;
42  
43      private List<ScmFile> files = new ArrayList<ScmFile>();
44  
45      /**
46       * the current directory entry being processed by the parser
47       */
48      private String currentDir = "";
49  
50      /**
51       * Marks current directory data
52       */
53      private static final String DIR_MARKER = "(working dir: ";
54  
55      /**
56       * Marks current file data
57       */
58      private static final String UNLOCKED_MARKER = ": unlocked";
59  
60  
61      public StarteamUnEditConsumer( ScmLogger logger, File basedir )
62      {
63          this.logger = logger;
64          this.workingDirectory = basedir.getPath().replace( '\\', '/' );
65      }
66  
67      /** {@inheritDoc} */
68      public void consumeLine( String line )
69      {
70          if ( logger.isDebugEnabled() )
71          {
72              logger.debug( line );
73          }
74  
75          int pos = 0;
76  
77          if ( ( pos = line.indexOf( DIR_MARKER ) ) != -1 )
78          {
79              processDirectory( line, pos );
80          }
81          else if ( ( pos = line.indexOf( UNLOCKED_MARKER ) ) != -1 )
82          {
83              processUnLockedFile( line, pos );
84          }
85          else
86          {
87              if ( logger.isWarnEnabled() )
88              {
89                  logger.warn( "Unknown unedit ouput: " + line );
90              }
91          }
92      }
93  
94      public List<ScmFile> getUnEditFiles()
95      {
96          return files;
97      }
98  
99      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 }