KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Vector JavaDoc;
29
30 /** Submits a numbered changelist to Perforce.
31  *
32  * <B>Note:</B> P4Submit cannot (yet) submit the default changelist.
33  * This shouldn't be a problem with the ANT task as the usual flow is
34  * P4Change to create a new numbered change followed by P4Edit then P4Submit.
35  *
36  * Example Usage:-<br>
37  * &lt;p4submit change="${p4.change}" /&gt;
38  *
39  * @ant.task category="scm"
40  */

41 public class P4Submit extends P4Base {
42
43     // CheckStyle:VisibilityModifier OFF - bc
44
//ToDo: If dealing with default cl need to parse out <enter description here>
45
/**
46      * change list number
47      */

48     public String JavaDoc change;
49     // CheckStyle:VisibilityModifier ON
50
/**
51      * change property
52      */

53     private String JavaDoc changeProperty;
54     /**
55      * needsresolveproperty
56      */

57     private String JavaDoc needsResolveProperty;
58     /**
59      * set the change list number to submit
60      * @param change The changelist number to submit; required.
61      */

62     public void setChange(String JavaDoc change) {
63         this.change = change;
64     }
65     /**
66      * property defining the change number if the change number gets renumbered
67      * @param changeProperty name of a new property to which the change number
68      * will be assigned if it changes
69      * @since ant 1.6.1
70      */

71     public void setChangeProperty(String JavaDoc changeProperty) {
72         this.changeProperty = changeProperty;
73     }
74     /**
75      * property defining the need to resolve the change list
76      * @param needsResolveProperty a property which will be set if the change needs resolve
77      * @since ant 1.6.1
78      */

79     public void setNeedsResolveProperty(String JavaDoc needsResolveProperty) {
80         this.needsResolveProperty = needsResolveProperty;
81     }
82
83     /**
84      * do the work
85      * @throws BuildException if no change list specified
86      */

87     public void execute() throws BuildException {
88         if (change != null) {
89             execP4Command("submit -c " + change, (P4HandlerAdapter) new P4SubmitAdapter(this));
90         } else {
91             //here we'd parse the output from change -o into submit -i
92
//in order to support default change.
93
throw new BuildException("No change specified (no support for default change yet....");
94         }
95     }
96
97     /**
98      * internal class used to process the output of p4 submit
99      */

100     public class P4SubmitAdapter extends SimpleP4OutputHandler {
101         /**
102          * Constructor.
103          * @param parent a P4Base instance.
104          */

105         public P4SubmitAdapter(P4Base parent) {
106             super(parent);
107         }
108         /**
109          * process a line of stdout/stderr coming from Perforce
110          * @param line line of stdout or stderr coming from Perforce
111          */

112         public void process(String JavaDoc line) {
113             super.process(line);
114             getProject().setProperty("p4.needsresolve", "0");
115             // this type of output might happen
116
// Change 18 renamed change 20 and submitted.
117
if (util.match("/renamed/", line)) {
118                 try {
119                     Vector JavaDoc myarray = new Vector JavaDoc();
120                     util.split(myarray, line);
121                     boolean found = false;
122                     for (int counter = 0; counter < myarray.size(); counter++) {
123                         if (found) {
124                             String JavaDoc chnum = (String JavaDoc) myarray.elementAt(counter + 1);
125                             int changenumber = Integer.parseInt(chnum);
126                             log("Perforce change renamed " + changenumber, Project.MSG_INFO);
127                             getProject().setProperty("p4.change", "" + changenumber);
128                             if (changeProperty != null) {
129                                 getProject().setNewProperty(changeProperty, chnum);
130                             }
131                             found = false;
132                         }
133                         if (((myarray.elementAt(counter))).equals("renamed")) {
134                             found = true;
135                         }
136                     }
137                 // NumberFormatException or ArrayOutOfBondsException could happen here
138
} catch (Exception JavaDoc e) {
139                     String JavaDoc msg = "Failed to parse " + line + "\n"
140                             + " due to " + e.getMessage();
141                     throw new BuildException(msg, e, getLocation());
142                 }
143             }
144             if (util.match("/p4 submit -c/", line)) {
145                 getProject().setProperty("p4.needsresolve", "1");
146                 if (needsResolveProperty != null) {
147                     getProject().setNewProperty(needsResolveProperty, "true");
148                 }
149             }
150
151         }
152     }
153
154 }
155
Popular Tags