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;
024
025import org.apache.maven.scm.ScmFile;
026import org.apache.maven.scm.ScmFileStatus;
027import org.apache.maven.scm.provider.perforce.command.AbstractPerforceConsumer;
028import org.apache.regexp.RE;
029import org.apache.regexp.RESyntaxException;
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 String LABEL_PATTERN = "^Label ([^ ]+) saved.$";
043
044    private static final String SYNC_PATTERN = "^([^#]+)#\\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    private RE syncRegexp;
057
058    public PerforceTagConsumer()
059    {
060        try
061        {
062            syncRegexp = new RE( SYNC_PATTERN );
063        }
064        catch ( RESyntaxException ignored )
065        {
066            ignored.printStackTrace();
067        }
068    }
069
070    /**
071     * Return a list of Strings formatted like:
072     * <p/>
073     * <pre>
074     * //depot/modules/cordoba/runtime-ear/pom.xml
075     * //depot/modules/cordoba/runtime-ear/.runtime
076     * </pre>
077     */
078    public List<ScmFile> getTagged()
079    {
080        return tagged;
081    }
082
083    /*
084     * We consume the output from 'p4 label -i' and 'p4 labelsync -l <tag>
085     * <files...>'
086     */
087    /*
088     * Label maven-scm-test saved.
089     */
090    /*
091     * //depot/modules/cordoba/runtime-ear/pom.xml#4 - added
092     * //depot/modules/cordoba/runtime-ear/.runtime#1 - added
093     */
094    /** {@inheritDoc} */
095    public void consumeLine( String line )
096    {
097        switch ( currentState )
098        {
099            case STATE_CREATE:
100                if ( !new RE( LABEL_PATTERN ).match( line ) )
101                {
102                    error( line );
103                    break;
104                }
105                currentState = STATE_SYNC;
106                break;
107            case STATE_SYNC:
108                if ( !syncRegexp.match( line ) )
109                {
110                    error( line );
111                    break;
112                }
113                tagged.add( new ScmFile( syncRegexp.getParen( 1 ), ScmFileStatus.TAGGED ) );
114                break;
115            default:
116                error( line );
117                break;
118        }
119    }
120
121    private void error( String line )
122    {
123        currentState = STATE_ERROR;
124        output.println( line );
125    }
126
127    public boolean isSuccess()
128    {
129        return currentState == STATE_SYNC;
130    }
131
132}