~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to javacvs/libsrc/org/netbeans/lib/cvsclient/command/unedit/UneditCommand.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
package org.netbeans.lib.cvsclient.command.unedit;
 
42
 
 
43
import java.io.*;
 
44
 
 
45
import org.netbeans.lib.cvsclient.*;
 
46
import org.netbeans.lib.cvsclient.admin.*;
 
47
import org.netbeans.lib.cvsclient.command.*;
 
48
import org.netbeans.lib.cvsclient.command.edit.*;
 
49
import org.netbeans.lib.cvsclient.connection.*;
 
50
import org.netbeans.lib.cvsclient.event.*;
 
51
import org.netbeans.lib.cvsclient.file.*;
 
52
import org.netbeans.lib.cvsclient.request.*;
 
53
 
 
54
/**
 
55
 * @author  Thomas Singer
 
56
 */
 
57
public class UneditCommand extends BasicCommand {
 
58
 
 
59
    private Watch temporaryWatch;
 
60
 
 
61
    /**
 
62
     * Construct a new editors command.
 
63
     */
 
64
    public UneditCommand() {
 
65
        resetCVSCommand();
 
66
    }
 
67
 
 
68
    /**
 
69
     * Execute the command.
 
70
     *
 
71
     * @param client the client services object that provides any necessary
 
72
     *               services to this command, including the ability to actually
 
73
     *               process all the requests.
 
74
     */
 
75
    public void execute(ClientServices clientServices, EventManager eventManager)
 
76
            throws CommandException, AuthenticationException {
 
77
        clientServices.ensureConnection();
 
78
 
 
79
        try {
 
80
            super.execute(clientServices, eventManager);
 
81
 
 
82
            addRequestForWorkingDirectory(clientServices);
 
83
            addRequest(CommandRequest.NOOP);
 
84
 
 
85
            clientServices.processRequests(requests);
 
86
        }
 
87
        catch (CommandException ex) {
 
88
            throw ex;
 
89
        }
 
90
        catch (EOFException ex) {
 
91
            throw new CommandException(ex, CommandException.getLocalMessage("CommandException.EndOfFile", null)); //NOI18N
 
92
        }
 
93
        catch (Exception ex) {
 
94
            throw new CommandException(ex, ex.getLocalizedMessage());
 
95
        }
 
96
        finally {
 
97
            requests.clear();
 
98
        }
 
99
    }
 
100
 
 
101
    protected void addRequestForFile(File file, Entry entry) {
 
102
        String temporaryWatch = Watch.getWatchString(getTemporaryWatch());
 
103
        requests.add(new NotifyRequest(file, "U", temporaryWatch)); // NOI18N
 
104
 
 
105
        try {
 
106
            uneditFile(file);
 
107
        }
 
108
        catch (IOException ex) {
 
109
            // ignore
 
110
        }
 
111
    }
 
112
 
 
113
    /**
 
114
     * Called when server responses with "ok" or "error", (when the command
 
115
     * finishes).
 
116
     */
 
117
    public void commandTerminated(TerminationEvent e) {
 
118
        if (builder != null) {
 
119
            builder.outputDone();
 
120
        }
 
121
    }
 
122
 
 
123
    /**
 
124
     * This method returns how the tag command would looklike when typed on the
 
125
     * command line.
 
126
     */
 
127
    public String getCVSCommand() {
 
128
        StringBuffer cvsCommandLine = new StringBuffer("unedit "); //NOI18N
 
129
        cvsCommandLine.append(getCVSArguments());
 
130
        appendFileArguments(cvsCommandLine);
 
131
        return cvsCommandLine.toString();
 
132
    }
 
133
 
 
134
    /**
 
135
     * Takes the arguments and sets the command.
 
136
     * To be mainly used for automatic settings (like parsing the .cvsrc file)
 
137
     * @return true if the option (switch) was recognized and set
 
138
     */
 
139
    public boolean setCVSCommand(char opt, String optArg) {
 
140
        if (opt == 'R') {
 
141
            setRecursive(true);
 
142
        }
 
143
        else if (opt == 'l') {
 
144
            setRecursive(false);
 
145
        }
 
146
        else {
 
147
            return false;
 
148
        }
 
149
        return true;
 
150
    }
 
151
 
 
152
    /**
 
153
     * String returned by this method defines which options are available for
 
154
     * this command.
 
155
     */
 
156
    public String getOptString() {
 
157
        return "Rl"; //NOI18N
 
158
    }
 
159
 
 
160
    /**
 
161
     * Resets all switches in the command.
 
162
     * After calling this method, the command should have no switches defined
 
163
     * and should behave defaultly.
 
164
     */
 
165
    public void resetCVSCommand() {
 
166
        setRecursive(true);
 
167
    }
 
168
 
 
169
    /**
 
170
     * Returns the arguments of the command in the command-line style.
 
171
     * Similar to getCVSCommand() however without the files and command's name
 
172
     */
 
173
    public String getCVSArguments() {
 
174
        StringBuffer cvsArguments = new StringBuffer();
 
175
        if (!isRecursive()) {
 
176
            cvsArguments.append("-l "); //NOI18N
 
177
        }
 
178
        return cvsArguments.toString();
 
179
    }
 
180
 
 
181
    /**
 
182
     * Returns the temporary watch.
 
183
     */
 
184
    public Watch getTemporaryWatch() {
 
185
        return temporaryWatch;
 
186
    }
 
187
 
 
188
    /**
 
189
     * Sets the temporary watch.
 
190
     */
 
191
    public void setTemporaryWatch(Watch temporaryWatch) {
 
192
        this.temporaryWatch = temporaryWatch;
 
193
    }
 
194
 
 
195
    private void uneditFile(File file) throws IOException {
 
196
        removeBaserevEntry(file);
 
197
        EditCommand.getEditBackupFile(file).delete();
 
198
        FileUtils.setFileReadOnly(file, true);
 
199
    }
 
200
 
 
201
    private void removeBaserevEntry(File file) throws IOException {
 
202
        File baserevFile = new File(file.getParentFile(), "CVS/Baserev"); // NOI18N
 
203
        File backupFile = new File(baserevFile.getAbsolutePath() + '~');
 
204
 
 
205
        BufferedReader reader = null;
 
206
        BufferedWriter writer = null;
 
207
        final String entryStart = 'B' + file.getName() + '/';
 
208
        try {
 
209
            writer = new BufferedWriter(new FileWriter(backupFile));
 
210
            reader = new BufferedReader(new FileReader(baserevFile));
 
211
 
 
212
            for (String line = reader.readLine();
 
213
                 line != null;
 
214
                 line = reader.readLine()) {
 
215
 
 
216
                if (line.startsWith(entryStart)) {
 
217
                    continue;
 
218
                }
 
219
 
 
220
                writer.write(line);
 
221
                writer.newLine();
 
222
            }
 
223
        }
 
224
        catch (FileNotFoundException ex) {
 
225
            // ignore
 
226
        }
 
227
        finally {
 
228
            if (writer != null) {
 
229
                try {
 
230
                    writer.close();
 
231
                }
 
232
                catch (IOException ex) {
 
233
                    // ignore
 
234
                }
 
235
            }
 
236
            if (reader != null) {
 
237
                try {
 
238
                    reader.close();
 
239
                }
 
240
                catch (IOException ex) {
 
241
                    // ignore
 
242
                }
 
243
            }
 
244
        }
 
245
        baserevFile.delete();
 
246
        if (backupFile.length() > 0) {
 
247
            backupFile.renameTo(baserevFile);
 
248
        }
 
249
        else {
 
250
            backupFile.delete();
 
251
        }
 
252
    }
 
253
 
 
254
}