View Javadoc

1   package org.apache.maven.scm.provider.starteam.command.checkin;
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@gmail.com">Dan T. Tran</a>
33   * @version $Id: StarteamCheckInConsumer.java 483105 2006-12-06 15:07:54Z evenisse $
34   */
35  public class StarteamCheckInConsumer
36      implements StreamConsumer
37  {
38      private String workingDirectory;
39  
40      private ScmLogger logger;
41  
42      private List files = new ArrayList();
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 CHECKIN_MARKER = ": checked in";
58  
59      /**
60       * Marks skipped file during update
61       */
62      private static final String SKIPPED_MARKER = ": skipped";
63  
64      /**
65       * Marks current file data
66       */
67      private static final String LINKTO_MARKER = ": linked to";
68  
69      public StarteamCheckInConsumer( ScmLogger logger, File basedir )
70      {
71          this.logger = logger;
72          this.workingDirectory = basedir.getPath().replace( '\\', '/' );
73      }
74  
75      public void consumeLine( String line )
76      {
77          logger.debug( line );
78  
79          int pos = 0;
80  
81          if ( ( pos = line.indexOf( DIR_MARKER ) ) != -1 )
82          {
83              processDirectory( line, pos );
84          }
85          else if ( ( pos = line.indexOf( CHECKIN_MARKER ) ) != -1 )
86          {
87              processCheckedInFile( line, pos );
88          }
89          else if ( ( pos = line.indexOf( SKIPPED_MARKER ) ) != -1 )
90          {
91              processSkippedFile( line, pos );
92          }
93          else if ( ( pos = line.indexOf( LINKTO_MARKER ) ) != -1 )
94          {
95              //ignore
96          }
97          else
98          {
99              this.logger.warn( "Unknown checkin ouput: " + line );
100         }
101 
102     }
103 
104     public List getCheckedInFiles()
105     {
106         return files;
107     }
108 
109     private void processDirectory( String line, int pos )
110     {
111         String dirPath = line.substring( pos + DIR_MARKER.length(), line.length() - 1 ).replace( '\\', '/' );
112 
113         if ( !dirPath.startsWith( workingDirectory ) )
114         {
115             logger.info( "Working directory: " + workingDirectory );
116 
117             logger.info( "Checkin directory: " + dirPath );
118 
119             throw new IllegalStateException( "Working and checkin directories are not on the same tree" );
120         }
121 
122         this.currentDir = "." + dirPath.substring( workingDirectory.length() );
123     }
124 
125     private void processCheckedInFile( String line, int pos )
126     {
127         String checkedInFilePath = this.currentDir + "/" + line.substring( 0, pos );
128 
129         this.files.add( new ScmFile( checkedInFilePath, ScmFileStatus.CHECKED_OUT ) );
130 
131         this.logger.info( "Checked in: " + checkedInFilePath );
132     }
133 
134     private void processSkippedFile( String line, int pos )
135     {
136         String skippedFilePath = this.currentDir + "/" + line.substring( 0, pos );
137 
138         this.logger.info( "Skipped: " + skippedFilePath );
139     }
140 
141 }