001package org.apache.maven.scm.provider.perforce.command.tag;
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
022import java.util.ArrayList;
023import java.util.List;
024import java.util.regex.Matcher;
025import java.util.regex.Pattern;
026
027import org.apache.maven.scm.ScmFile;
028import org.apache.maven.scm.ScmFileStatus;
029import org.apache.maven.scm.provider.perforce.command.AbstractPerforceConsumer;
030import org.codehaus.plexus.util.cli.StreamConsumer;
031
032/**
033 * @author Mike Perham
034 * @author Olivier Lamy
035 *
036 */
037public class PerforceTagConsumer
038    extends AbstractPerforceConsumer
039    implements StreamConsumer
040{
041
042    private static final Pattern LABEL_PATTERN = Pattern.compile( "^Label ([^ ]+) saved.$" );
043
044    private static final Pattern SYNC_PATTERN = Pattern.compile( "^([^#]+)#\\d+ - (.*)" );
045
046    public static final int STATE_CREATE = 1;
047
048    public static final int STATE_SYNC = 2;
049
050    public static final int STATE_ERROR = 3;
051
052    private int currentState = STATE_CREATE;
053
054    private List<ScmFile> tagged = new ArrayList<ScmFile>();
055
056    /**
057     * Return a list of Strings formatted like:
058     * <p/>
059     * <pre>
060     * //depot/modules/cordoba/runtime-ear/pom.xml
061     * //depot/modules/cordoba/runtime-ear/.runtime
062     * </pre>
063     */
064    public List<ScmFile> getTagged()
065    {
066        return tagged;
067    }
068
069    /*
070     * We consume the output from 'p4 label -i' and 'p4 labelsync -l <tag>
071     * <files...>'
072     */
073    /*
074     * Label maven-scm-test saved.
075     */
076    /*
077     * //depot/modules/cordoba/runtime-ear/pom.xml#4 - added
078     * //depot/modules/cordoba/runtime-ear/.runtime#1 - added
079     */
080    /** {@inheritDoc} */
081    public void consumeLine( String line )
082    {
083        switch ( currentState )
084        {
085            case STATE_CREATE:
086                if ( !LABEL_PATTERN.matcher( line ).matches() )
087                {
088                    error( line );
089                    break;
090                }
091                currentState = STATE_SYNC;
092                break;
093            case STATE_SYNC:
094                Matcher matcher = SYNC_PATTERN.matcher( line );
095                if ( !matcher.matches() )
096                {
097                    error( line );
098                    break;
099                }
100                tagged.add( new ScmFile( matcher.group( 1 ), ScmFileStatus.TAGGED ) );
101                break;
102            default:
103                error( line );
104                break;
105        }
106    }
107
108    private void error( String line )
109    {
110        currentState = STATE_ERROR;
111        output.println( line );
112    }
113
114    public boolean isSuccess()
115    {
116        return currentState == STATE_SYNC;
117    }
118
119}