001    package org.apache.maven.scm.provider.starteam.command.status;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     * http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import org.apache.maven.scm.ScmFile;
023    import org.apache.maven.scm.ScmFileStatus;
024    import org.apache.maven.scm.log.ScmLogger;
025    import org.codehaus.plexus.util.cli.StreamConsumer;
026    
027    import java.io.File;
028    import java.util.ArrayList;
029    import java.util.List;
030    
031    /**
032     * @author <a href="mailto:dantran@gmail.com">Dan T. Tran</a>
033     * @version $Id: StarteamStatusConsumer.java 1056991 2011-01-09 18:09:10Z olamy $
034     */
035    public class StarteamStatusConsumer
036        implements StreamConsumer
037    {
038        private ScmLogger logger;
039    
040        private String workingDirectory;
041    
042        private List<ScmFile> changedFiles = new ArrayList<ScmFile>();
043    
044        /**
045         * Marks current directory data
046         */
047        private static final String DIR_MARKER = "(working dir: ";
048    
049        /**
050         * Marks current file data
051         */
052        private static final String FILE_MARKER = "History for: ";
053    
054        /**
055         * Marks current file status
056         */
057        private static final String STATUS_MARKER = "Status: ";
058    
059        /**
060         * Marks current file status
061         */
062        private static final String OUTDATE_MARKER = "Out of Date";
063    
064        private static final String MISSING_MARKER = "Missing";
065    
066        private static final String CURRENT_MARKER = "Current";
067    
068        private static final String MERGE_MARKER = "Merge";
069    
070        private static final String MODIFIED_MARKER = "Modified";
071    
072        private String currentDir = "";
073    
074        private String currentFile = "";
075    
076        public StarteamStatusConsumer( ScmLogger logger, File basedir )
077        {
078            this.logger = logger;
079    
080            this.workingDirectory = basedir.getPath().replace( '\\', '/' );
081        }
082    
083        /** {@inheritDoc} */
084        public void consumeLine( String line )
085        {
086            if ( logger.isDebugEnabled() )
087            {
088                logger.debug( line );
089            }
090    
091            int pos = 0;
092    
093            if ( ( pos = line.indexOf( DIR_MARKER ) ) != -1 )
094            {
095                processGetDir( line, pos );
096            }
097            else if ( ( pos = line.indexOf( FILE_MARKER ) ) != -1 )
098            {
099                processGetFile( line, pos );
100            }
101            else if ( ( pos = line.indexOf( STATUS_MARKER ) ) != -1 )
102            {
103                processStatus( line, pos );
104            }
105            else
106            {
107                //do nothing
108            }
109        }
110    
111        private void processGetDir( String line, int pos )
112        {
113            String dirPath = line.substring( pos + DIR_MARKER.length(), line.length() - 1 ).replace( '\\', '/' );
114    
115            this.currentDir = "." + dirPath.substring( workingDirectory.length() );
116        }
117    
118        private void processGetFile( String line, int pos )
119        {
120            String fileName = line.substring( pos + FILE_MARKER.length(), line.length() );
121    
122            String checkedOutFilePath = this.currentDir + "/" + fileName;
123    
124            this.currentFile = checkedOutFilePath;
125        }
126    
127        private void processStatus( String line, int pos )
128        {
129            String status = line.substring( pos + STATUS_MARKER.length(), line.length() );
130    
131            if ( status.equals( OUTDATE_MARKER ) )
132            {
133                changedFiles.add( new ScmFile( this.currentFile, ScmFileStatus.MODIFIED ) );
134    
135                if ( logger.isInfoEnabled() )
136                {
137                    logger.info( "Out of Date file: " + this.currentFile );
138                }
139            }
140            else if ( status.equals( MODIFIED_MARKER ) )
141            {
142                changedFiles.add( new ScmFile( this.currentFile, ScmFileStatus.MODIFIED ) );
143    
144                if ( logger.isInfoEnabled() )
145                {
146                    logger.info( "Modified file: " + this.currentFile );
147                }
148            }
149            else if ( status.equals( MISSING_MARKER ) )
150            {
151                changedFiles.add( new ScmFile( this.currentFile, ScmFileStatus.ADDED ) );
152    
153                if ( logger.isInfoEnabled() )
154                {
155                    logger.info( "Missing file: " + this.currentFile );
156                }
157            }
158            else if ( status.equals( MERGE_MARKER ) )
159            {
160                changedFiles.add( new ScmFile( this.currentFile, ScmFileStatus.CONFLICT ) );
161    
162                if ( logger.isInfoEnabled() )
163                {
164                    logger.info( "Conflict file: " + this.currentFile );
165                }
166            }
167            else if ( status.equals( CURRENT_MARKER ) )
168            {
169                //ignore
170            }
171            else
172            {
173                if ( logger.isWarnEnabled() )
174                {
175                    logger.warn( "status unknown (" + status + "): " + this.currentFile );
176                }
177            }
178        }
179    
180        public List<ScmFile> getChangedFiles()
181        {
182            return changedFiles;
183        }
184    
185    }