001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.lib.cvsclient.command.annotate;
042:
043: import java.io.*;
044: import java.util.*;
045:
046: import org.netbeans.lib.cvsclient.*;
047: import org.netbeans.lib.cvsclient.command.*;
048: import org.netbeans.lib.cvsclient.connection.*;
049: import org.netbeans.lib.cvsclient.event.*;
050: import org.netbeans.lib.cvsclient.request.*;
051:
052: /**
053: * The rannotate command is similar to anootate, but doens't operate on currently checked
054: * out sources.
055:
056: *
057: * @author MIlos Kleint
058: */
059: public class RannotateCommand extends BasicCommand {
060:
061: /**
062: * The modules to checkout. These names are unexpanded and will be passed
063: * to a module-expansion request.
064: */
065: private final List modules = new LinkedList();
066:
067: /**
068: * The expanded modules.
069: */
070: private final List expandedModules = new LinkedList();
071:
072: /**
073: * Use head revision if a revision meeting criteria set by switches -r/-D
074: * (tag/date) is not found.
075: */
076: private boolean useHeadIfNotFound;
077:
078: /**
079: * equals the -D switch of command line cvs.
080: */
081: private String annotateByDate;
082:
083: /**
084: * Equals the -r switch of command-line cvs.
085: */
086: private String annotateByRevision;
087:
088: /**
089: * Holds value of property headerAndDescOnly.
090: */
091: private boolean headerAndDescOnly;
092:
093: public RannotateCommand() {
094: resetCVSCommand();
095: }
096:
097: /**
098: * Set the modules to export.
099: * @param theModules the names of the modules to export
100: */
101: public void setModule(String module) {
102: modules.add(module);
103: }
104:
105: /**
106: * clears the list of modules for export.
107: */
108:
109: public void clearModules() {
110: this .modules.clear();
111: }
112:
113: /**
114: * Set the modules to export.
115: * @param theModules the names of the modules to export
116: */
117: public void setModules(String[] modules) {
118: clearModules();
119: if (modules == null) {
120: return;
121: }
122: for (int i = 0; i < modules.length; i++) {
123: String module = modules[i];
124: this .modules.add(module);
125: }
126: }
127:
128: public String[] getModules() {
129: String[] mods = new String[modules.size()];
130: mods = (String[]) modules.toArray(mods);
131: return mods;
132: }
133:
134: private void processExistingModules(String localPath) {
135: if (expandedModules.size() == 0) {
136: return;
137: }
138:
139: String[] directories = new String[expandedModules.size()];
140: directories = (String[]) expandedModules.toArray(directories);
141: setModules(directories);
142: }
143:
144: /**
145: * Execute this command.
146: * @param client the client services object that provides any necessary
147: * services to this command, including the ability to actually process
148: * all the requests
149: */
150: public void execute(ClientServices client, EventManager em)
151: throws CommandException, AuthenticationException {
152:
153: client.ensureConnection();
154:
155: requests = new LinkedList();
156: if (client.isFirstCommand()) {
157: requests.add(new RootRequest(client.getRepository()));
158: }
159: for (Iterator it = modules.iterator(); it.hasNext();) {
160: String module = (String) it.next();
161: requests.add(new ArgumentRequest(module));
162: }
163: expandedModules.clear();
164: requests.add(new DirectoryRequest(".", client.getRepository())); //NOI18N
165: requests.add(new ExpandModulesRequest());
166: try {
167: client.processRequests(requests);
168: } catch (CommandException ex) {
169: throw ex;
170: } catch (Exception ex) {
171: throw new CommandException(ex, ex.getLocalizedMessage());
172: }
173: requests.clear();
174: postExpansionExecute(client, em);
175: }
176:
177: /**
178: * This is called when the server has responded to an expand-modules
179: * request.
180: */
181: public void moduleExpanded(ModuleExpansionEvent e) {
182: expandedModules.add(e.getModule());
183: }
184:
185: /**
186: * Execute this command
187: * @param client the client services object that provides any necessary
188: * services to this command, including the ability to actually process
189: * all the requests
190: */
191: private void postExpansionExecute(ClientServices client,
192: EventManager em) throws CommandException,
193: AuthenticationException {
194:
195: // processExistingModules(client.getLocalPath());
196: super .execute(client, em);
197:
198: //
199: // moved modules code to the end of the other arguments --GAR
200: //
201: if (!isRecursive()) {
202: requests.add(1, new ArgumentRequest("-l")); //NOI18N
203: }
204: if (useHeadIfNotFound) {
205: requests.add(1, new ArgumentRequest("-f")); //NOI18N
206: }
207: if (annotateByDate != null && annotateByDate.length() > 0) {
208: requests.add(1, new ArgumentRequest("-D")); //NOI18N
209: requests.add(2, new ArgumentRequest(getAnnotateByDate()));
210: }
211: if (annotateByRevision != null
212: && annotateByRevision.length() > 0) {
213: requests.add(1, new ArgumentRequest("-r")); //NOI18N
214: requests.add(2,
215: new ArgumentRequest(getAnnotateByRevision()));
216: }
217:
218: for (Iterator it = modules.iterator(); it.hasNext();) {
219: String module = (String) it.next();
220: requests.add(new ArgumentRequest(module));
221: }
222:
223: requests.add(new DirectoryRequest(".", client.getRepository())); //NOI18N
224: requests.add(CommandRequest.RANNOTATE);
225: try {
226: client.processRequests(requests);
227: requests.clear();
228:
229: } catch (CommandException ex) {
230: throw ex;
231: } catch (Exception ex) {
232: throw new CommandException(ex, ex.getLocalizedMessage());
233: }
234: }
235:
236: public String getCVSCommand() {
237: StringBuffer toReturn = new StringBuffer("rannotate "); //NOI18N
238: toReturn.append(getCVSArguments());
239: if (modules != null && modules.size() > 0) {
240: for (Iterator it = modules.iterator(); it.hasNext();) {
241: String module = (String) it.next();
242: toReturn.append(module);
243: toReturn.append(' ');
244: }
245: } else {
246: String localizedMsg = CommandException
247: .getLocalMessage("ExportCommand.moduleEmpty.text"); //NOI18N
248: toReturn.append(" "); //NOI18N
249: toReturn.append(localizedMsg);
250: }
251: return toReturn.toString();
252: }
253:
254: public String getCVSArguments() {
255: StringBuffer toReturn = new StringBuffer(""); //NOI18N
256: if (!isRecursive()) {
257: toReturn.append("-l "); //NOI18N
258: }
259: if (getAnnotateByRevision() != null) {
260: toReturn.append("-r "); //NOI18N
261: toReturn.append(getAnnotateByRevision());
262: toReturn.append(" "); //NOI18N
263: }
264: if (getAnnotateByDate() != null) {
265: toReturn.append("-D "); //NOI18N
266: toReturn.append(getAnnotateByDate());
267: toReturn.append(" "); //NOI18N
268: }
269: if (isUseHeadIfNotFound()) {
270: toReturn.append("-f "); //NOI18N
271: }
272: return toReturn.toString();
273: }
274:
275: public boolean setCVSCommand(char opt, String optArg) {
276: if (opt == 'R') {
277: setRecursive(true);
278: } else if (opt == 'l') {
279: setRecursive(false);
280: } else if (opt == 'r') {
281: setAnnotateByRevision(optArg);
282: } else if (opt == 'D') {
283: setAnnotateByDate(optArg);
284: } else if (opt == 'f') {
285: setUseHeadIfNotFound(true);
286: } else {
287: return false;
288: }
289: return true;
290: }
291:
292: public void resetCVSCommand() {
293: setRecursive(true);
294: setAnnotateByDate(null);
295: setAnnotateByRevision(null);
296: setUseHeadIfNotFound(false);
297: }
298:
299: /**
300: * String returned by this method defines which options are available for this particular command
301: */
302: public String getOptString() {
303: return "Rlr:D:f"; //NOI18N
304: }
305:
306: /**
307: * Create a builder for this command.
308: * @param eventMan the event manager used to receive events.
309: */
310: public Builder createBuilder(EventManager eventMan) {
311: return new AnnotateBuilder(eventMan, this );
312:
313: }
314:
315: /** called when server responses with "ok" or "error", (when the command finishes)
316: */
317: public void commandTerminated(TerminationEvent e) {
318: if (builder != null) {
319: builder.outputDone();
320: }
321: }
322:
323: /**
324: * Getter for property useHeadIfNotFound.
325: * @return Value of property useHeadIfNotFound.
326: */
327: public boolean isUseHeadIfNotFound() {
328: return useHeadIfNotFound;
329: }
330:
331: /**
332: * Setter for property useHeadIfNotFound.
333: * @param useHeadIfNotFound New value of property useHeadIfNotFound.
334: */
335: public void setUseHeadIfNotFound(boolean useHeadIfNotFound) {
336: this .useHeadIfNotFound = useHeadIfNotFound;
337: }
338:
339: /**
340: * Getter for property annotateByDate.
341: * @return Value of property annotateByDate.
342: */
343: public String getAnnotateByDate() {
344: return annotateByDate;
345: }
346:
347: /**
348: * Setter for property annotateByDate.
349: * @param annotateByDate New value of property annotateByDate.
350: */
351: public void setAnnotateByDate(String annotateByDate) {
352: this .annotateByDate = annotateByDate;
353: }
354:
355: /**
356: * Getter for property annotateByRevision.
357: * @return Value of property annotateByRevision.
358: */
359: public String getAnnotateByRevision() {
360: return annotateByRevision;
361: }
362:
363: /**
364: * Setter for property annotateByRevision.
365: * @param annotateByRevision New value of property annotateByRevision.
366: */
367: public void setAnnotateByRevision(String annotateByRevision) {
368: this.annotateByRevision = annotateByRevision;
369: }
370:
371: }
|