1 package org.apache.maven.scm.provider.perforce.command.tag;
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 java.util.ArrayList;
23 import java.util.List;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26
27 import org.apache.maven.scm.ScmFile;
28 import org.apache.maven.scm.ScmFileStatus;
29 import org.apache.maven.scm.provider.perforce.command.AbstractPerforceConsumer;
30 import org.codehaus.plexus.util.cli.StreamConsumer;
31
32 /**
33 * @author Mike Perham
34 * @author Olivier Lamy
35 *
36 */
37 public class PerforceTagConsumer
38 extends AbstractPerforceConsumer
39 implements StreamConsumer
40 {
41
42 private static final Pattern LABEL_PATTERN = Pattern.compile( "^Label ([^ ]+) saved.$" );
43
44 private static final Pattern SYNC_PATTERN = Pattern.compile( "^([^#]+)#\\d+ - (.*)" );
45
46 public static final int STATE_CREATE = 1;
47
48 public static final int STATE_SYNC = 2;
49
50 public static final int STATE_ERROR = 3;
51
52 private int currentState = STATE_CREATE;
53
54 private List<ScmFile> tagged = new ArrayList<ScmFile>();
55
56 /**
57 * Return a list of Strings formatted like:
58 * <p/>
59 * <pre>
60 * //depot/modules/cordoba/runtime-ear/pom.xml
61 * //depot/modules/cordoba/runtime-ear/.runtime
62 * </pre>
63 */
64 public List<ScmFile> getTagged()
65 {
66 return tagged;
67 }
68
69 /*
70 * We consume the output from 'p4 label -i' and 'p4 labelsync -l <tag>
71 * <files...>'
72 */
73 /*
74 * Label maven-scm-test saved.
75 */
76 /*
77 * //depot/modules/cordoba/runtime-ear/pom.xml#4 - added
78 * //depot/modules/cordoba/runtime-ear/.runtime#1 - added
79 */
80 /** {@inheritDoc} */
81 public void consumeLine( String line )
82 {
83 switch ( currentState )
84 {
85 case STATE_CREATE:
86 if ( !LABEL_PATTERN.matcher( line ).matches() )
87 {
88 error( line );
89 break;
90 }
91 currentState = STATE_SYNC;
92 break;
93 case STATE_SYNC:
94 Matcher matcher = SYNC_PATTERN.matcher( line );
95 if ( !matcher.matches() )
96 {
97 error( line );
98 break;
99 }
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 }