KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > perforce > P4Change


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 /*
19  * Portions of this software are based upon public domain software
20  * originally written at the National Center for Supercomputing Applications,
21  * University of Illinois, Urbana-Champaign.
22  */

23
24 package org.apache.tools.ant.taskdefs.optional.perforce;
25
26 import org.apache.tools.ant.BuildException;
27 import org.apache.tools.ant.Project;
28
29 /**
30  * Requests a new changelist from the Perforce server.
31  * P4Change creates a new changelist in perforce. P4Change sets the property
32  * ${p4.change} with the new changelist number. This should then be passed into
33  * p4edit and p4submit.
34  *
35  *
36  * @see P4Edit
37  * @see P4Submit
38  *
39  * @ant.task category="scm"
40  */

41 public class P4Change extends P4Base {
42     // CheckStyle:VisibilityModifier OFF - bc
43

44     protected String JavaDoc emptyChangeList = null;
45     protected String JavaDoc description = "AutoSubmit By Ant";
46     // CheckStyle:VisibilityModifier ON
47

48     /**
49      * creates a new Perforce change list
50      * sets the p4.change property to the number of the new change list
51      * @throws BuildException if the word error appears in the output coming from Perforce
52      */

53     public void execute() throws BuildException {
54
55         if (emptyChangeList == null) {
56             emptyChangeList = getEmptyChangeList();
57         }
58         final Project myProj = getProject();
59
60         P4Handler handler = new P4HandlerAdapter() {
61             public void process(String JavaDoc line) {
62                 if (util.match("/Change/", line)) {
63
64                     //Remove any non-numerical chars - should leave the change number
65
line = util.substitute("s/[^0-9]//g", line);
66
67                     int changenumber = Integer.parseInt(line);
68                     log("Change Number is " + changenumber, Project.MSG_INFO);
69                     myProj.setProperty("p4.change", "" + changenumber);
70
71                 } else if (util.match("/error/", line)) {
72                     throw new BuildException("Perforce Error, check client settings and/or server");
73                 }
74
75             }
76         };
77
78         handler.setOutput(emptyChangeList);
79
80         execP4Command("change -i", handler);
81     }
82
83     /**
84      * returns the text of an empty change list
85      * @return the text of an empty change list
86      * @throws BuildException if the text error is displayed
87      * in the Perforce output outside of a comment line
88      */

89     public String JavaDoc getEmptyChangeList() throws BuildException {
90         final StringBuffer JavaDoc stringbuf = new StringBuffer JavaDoc();
91
92         execP4Command("change -o", new P4HandlerAdapter() {
93             public void process(String JavaDoc line) {
94                 if (!util.match("/^#/", line)) {
95                     if (util.match("/error/", line)) {
96                         log("Client Error", Project.MSG_VERBOSE);
97                         throw new BuildException("Perforce Error, "
98                         + "check client settings and/or server");
99                     } else if (util.match("/<enter description here>/", line)) {
100                         // we need to escape the description in case there are /
101
description = backslash(description);
102                         line = util.substitute("s/<enter description here>/"
103                             + description + "/", line);
104                     } else if (util.match("/\\/\\//", line)) {
105                         //Match "//" for begining of depot filespec
106
return;
107                     }
108                     stringbuf.append(line);
109                     stringbuf.append("\n");
110                 }
111             }
112         });
113         return stringbuf.toString();
114     }
115
116     /**
117      * Ensure that a string is backslashing slashes so that it does not
118      * confuse them with Perl substitution delimiter in Oro. Backslashes are
119      * always backslashes in a string unless they escape the delimiter.
120      * @param value the string to backslash for slashes
121      * @return the backslashed string
122      * @see <a HREF="http://jakarta.apache.org/oro/api/org/apache/oro/text/perl/Perl5Util.html
123      * #substitute(java.lang.String,%20java.lang.String)">Oro</a>
124      */

125     public static final String JavaDoc backslash(String JavaDoc value) {
126         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc(value.length());
127         final int len = value.length();
128         for (int i = 0; i < len; i++) {
129             char c = value.charAt(i);
130             if (c == '/') {
131                 buf.append('\\');
132             }
133             buf.append(c);
134         }
135         return buf.toString();
136     }
137
138     /**
139      * Description for ChangeList;optional.
140      * If none is specified, it will default to "AutoSubmit By Ant"
141      * @param desc description for the change list
142      */

143     public void setDescription(String JavaDoc desc) {
144         this.description = desc;
145     }
146
147 } //EoF
148
Popular Tags