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
25 import org.apache.maven.scm.ScmFile;
26 import org.apache.maven.scm.ScmFileStatus;
27 import org.apache.maven.scm.provider.perforce.command.AbstractPerforceConsumer;
28 import org.apache.regexp.RE;
29 import org.apache.regexp.RESyntaxException;
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 String LABEL_PATTERN = "^Label ([^ ]+) saved.$";
43
44 private static final String SYNC_PATTERN = "^([^#]+)#\\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 private RE syncRegexp;
57
58 public PerforceTagConsumer()
59 {
60 try
61 {
62 syncRegexp = new RE( SYNC_PATTERN );
63 }
64 catch ( RESyntaxException ignored )
65 {
66 ignored.printStackTrace();
67 }
68 }
69
70 /**
71 * Return a list of Strings formatted like:
72 * <p/>
73 * <pre>
74 * //depot/modules/cordoba/runtime-ear/pom.xml
75 * //depot/modules/cordoba/runtime-ear/.runtime
76 * </pre>
77 */
78 public List<ScmFile> getTagged()
79 {
80 return tagged;
81 }
82
83 /*
84 * We consume the output from 'p4 label -i' and 'p4 labelsync -l <tag>
85 * <files...>'
86 */
87 /*
88 * Label maven-scm-test saved.
89 */
90 /*
91 * //depot/modules/cordoba/runtime-ear/pom.xml#4 - added
92 * //depot/modules/cordoba/runtime-ear/.runtime#1 - added
93 */
94 /** {@inheritDoc} */
95 public void consumeLine( String line )
96 {
97 switch ( currentState )
98 {
99 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 }