View Javadoc

1   package org.apache.maven.scm.provider.starteam.command.edit;
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   *
34   */
35  public class StarteamEditConsumer
36      implements StreamConsumer
37  {
38      private String workingDirectory;
39  
40      private ScmLogger logger;
41  
42      private List<ScmFile> files = new ArrayList<ScmFile>();
43  
44      /**
45       * the current directory entry being processed by the parser
46       */
47      private String currentDir = "";
48  
49      /**
50       * Marks current directory data
51       */
52      private static final String DIR_MARKER = "(working dir: ";
53  
54      /**
55       * Marks current file data
56       */
57      private static final String LOCKED_MARKER = ": locked";
58  
59  
60      public StarteamEditConsumer( ScmLogger logger, File basedir )
61      {
62          this.logger = logger;
63          this.workingDirectory = basedir.getPath().replace( '\\', '/' );
64      }
65  
66      /** {@inheritDoc} */
67      public void consumeLine( String line )
68      {
69          if ( logger.isDebugEnabled() )
70          {
71              logger.debug( line );
72          }
73          int pos = 0;
74  
75          if ( ( pos = line.indexOf( DIR_MARKER ) ) != -1 )
76          {
77              processDirectory( line, pos );
78          }
79          else if ( ( pos = line.indexOf( LOCKED_MARKER ) ) != -1 )
80          {
81              processLockedFile( line, pos );
82          }
83          else
84          {
85              if ( logger.isWarnEnabled() )
86              {
87                  logger.warn( "Unknown edit ouput: " + line );
88              }
89          }
90      }
91  
92      public List<ScmFile> getEditedFiles()
93      {
94          return files;
95      }
96  
97      private void processDirectory( String line, int pos )
98      {
99          String dirPath = line.substring( pos + DIR_MARKER.length(), line.length() - 1 ).replace( '\\', '/' );
100 
101         if ( !dirPath.startsWith( workingDirectory ) )
102         {
103             if ( logger.isInfoEnabled() )
104             {
105                 logger.info( "Working directory: " + workingDirectory );
106                 logger.info( "Edit directory: " + dirPath );
107             }
108 
109             throw new IllegalStateException( "Working and edit directories are not on the same tree" );
110         }
111 
112         this.currentDir = "." + dirPath.substring( workingDirectory.length() );
113     }
114 
115     private void processLockedFile( String line, int pos )
116     {
117         String lockedFilePath = this.currentDir + "/" + line.substring( 0, pos );
118 
119         this.files.add( new ScmFile( lockedFilePath, ScmFileStatus.EDITED ) );
120 
121         if ( logger.isInfoEnabled() )
122         {
123             logger.info( "Locked: " + lockedFilePath );
124         }
125     }
126 
127 
128 }