001    package org.apache.maven.scm.provider.jazz.command.add;
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.apache.maven.scm.provider.ScmProviderRepository;
026    import org.apache.maven.scm.provider.jazz.command.consumer.AbstractRepositoryConsumer;
027    
028    import java.io.File;
029    import java.util.ArrayList;
030    import java.util.List;
031    
032    /**
033     * Consume the output of the scm command for the "add" operation.
034     *
035     * @author <a href="mailto:ChrisGWarp@gmail.com">Chris Graham</a>
036     */
037    public class JazzAddConsumer
038        extends AbstractRepositoryConsumer
039    {
040        // A flag to indicate that we have seen the "Changes:" line in the output.
041        // After that, we have the files themselves.
042        private boolean haveSeenChanges = false;
043    
044        protected String fCurrentDir = "";
045    
046        // The list of files that we have checked in.
047        private List<ScmFile> fCheckedInFiles = new ArrayList<ScmFile>();
048    
049        /**
050         * Construct the JazzAddCommand consumer.
051         *
052         * @param repository The repository we are working with.
053         * @param logger     The logger to use.
054         */
055        public JazzAddConsumer( ScmProviderRepository repository, ScmLogger logger )
056        {
057            super( repository, logger );
058        }
059    
060        /**
061         * Process one line of output from the execution of the "scm xxxx" command.
062         *
063         * @param line The line of output from the external command that has been pumped to us.
064         * @see org.codehaus.plexus.util.cli.StreamConsumer#consumeLine(java.lang.String)
065         */
066        public void consumeLine( String line )
067        {
068            super.consumeLine( line );
069            // The Jazz SCM "checkin" command does not output a list of each file that was checked in.
070            // An example output is shown below, perhaps in the future we may need to
071            // consume the "Workspace", "Component", Stream or "Change sets"
072    
073            /*
074                Committing...
075                Workspace: (1004) "Release Repository Workspace" <-> (1005) "Maven Release Plugin Stream"
076                  Component: (1006) "Release Component"
077                    Outgoing:
078                      Change sets:
079                        (1008) --@ <No comment>
080                        
081            Or:
082            
083                Committing...
084                Workspace: (1903) "MavenSCMTestWorkspace_1332908068770" <-> (1903) "MavenSCMTestWorkspace_1332908068770"
085                  Component: (1768) "MavenSCMTestComponent"
086                    Outgoing:
087                      Change sets:
088                        (1907)  *--@  "Commit message"
089                          Changes:
090                            --a-- \src\main\java\Me.java
091                            --a-- \src\main\java\Me1.java
092                            --a-- \src\main\java\Me2.java        
093            */
094    
095            if ( haveSeenChanges )
096            {
097                // We have already seen the "Changes:" line, so we must now be processing files.
098                String trimmed = line.trim();
099                int spacePos = trimmed.indexOf( " " );
100                // NOTE: The second + 1 is to remove the leading slash
101                String path = trimmed.substring( spacePos + 1 + 1 );
102                fCheckedInFiles.add( new ScmFile( path, ScmFileStatus.CHECKED_OUT ) );
103            }
104            else
105            {
106                if ( "Changes:".equals( line.trim() ) )
107                {
108                    haveSeenChanges = true;
109                }
110            }
111        }
112    
113        protected ScmFile getScmFile( String filename )
114        {
115            return new ScmFile( new File( fCurrentDir, filename ).getAbsolutePath(), ScmFileStatus.CHECKED_OUT );
116        }
117    
118        public List<ScmFile> getFiles()
119        {
120            return fCheckedInFiles;
121        }
122    }