KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > command > BuildableCommand


1 /*****************************************************************************
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is the CVS Client Library.
16  * The Initial Developer of the Original Software is Robert Greig.
17  * Portions created by Robert Greig are Copyright (C) 2000.
18  * All Rights Reserved.
19  *
20  * Contributor(s): Robert Greig.
21  *****************************************************************************/

22 package org.netbeans.lib.cvsclient.command;
23
24 import org.netbeans.lib.cvsclient.*;
25 import org.netbeans.lib.cvsclient.connection.*;
26 import org.netbeans.lib.cvsclient.event.*;
27
28 import java.io.UnsupportedEncodingException JavaDoc;
29
30 /**
31  * A class that provides common functionality for many of the CVS command
32  * that send similar sequences of requests.
33  * @author Robert Greig
34  */

35 public abstract class BuildableCommand extends Command {
36
37     /**
38      * An implementation of Builder interface that constructs a FileContainerInfo object from
39      * the server's output..
40      */

41     protected Builder builder;
42
43     private final StringBuffer JavaDoc taggedLineBuffer = new StringBuffer JavaDoc();
44
45     /**
46      * A boolean value indicating if the user has used the setBuilder() method.
47      */

48     private boolean builderSet;
49
50     /**
51      * Execute a command. This implementation sends a Root request, followed
52      * by as many Directory and Entry requests as is required by the recurse
53      * setting and the file arguments that have been set. Subclasses should
54      * call this first, and tag on the end of the requests list any further
55      * requests and, finally, the actually request that does the command (e.g.
56      * <pre>update</pre>, <pre>status</pre> etc.)
57      * @param client the client services object that provides any necessary
58      * services to this command, including the ability to actually process
59      * all the requests
60      * @throws CommandException if an error occurs executing the command
61      */

62     public void execute(ClientServices client, EventManager eventManager)
63             throws CommandException, AuthenticationException {
64         super.execute(client, eventManager);
65
66         if (builder == null && !isBuilderSet()) {
67             builder = createBuilder(eventManager);
68         }
69     }
70
71     /**
72      * Method that is called while the command is being executed.
73      * Descendants can override this method to return a Builder instance
74      * that will parse the server's output and create data structures.
75      */

76     public Builder createBuilder(EventManager eventManager) {
77         return null;
78     }
79
80     public void messageSent(BinaryMessageEvent e) {
81         super.messageSent(e);
82
83         if (builder == null) {
84             return;
85         }
86
87         if (builder instanceof BinaryBuilder) { // XXX assert it?
88
BinaryBuilder binaryBuilder = (BinaryBuilder) builder;
89             binaryBuilder.parseBytes(e.getMessage(), e.getMessageLength());
90         }
91     }
92
93     public void messageSent(MessageEvent e) {
94         super.messageSent(e);
95         if (builder == null) {
96             return;
97         }
98
99         if (e instanceof EnhancedMessageEvent) {
100             EnhancedMessageEvent eEvent = (EnhancedMessageEvent)e;
101             builder.parseEnhancedMessage(eEvent.getKey(), eEvent.getValue());
102             return;
103         }
104
105         if (e.isTagged()) {
106             String JavaDoc message = MessageEvent.parseTaggedMessage(taggedLineBuffer, e.getMessage());
107             if (message != null) {
108                 builder.parseLine(message, false);
109                 taggedLineBuffer.setLength(0);
110             }
111         }
112         else {
113             if (taggedLineBuffer.length() > 0) {
114                 builder.parseLine(taggedLineBuffer.toString(), false);
115                 taggedLineBuffer.setLength(0);
116             }
117             //#67337 do not interpret piped data using platform default encoding
118
// UTF-8 causes problems as raw data (non UTf-8) can contain confusing sequences
119
// use safe encoding that does not interpret byte sequences
120
if (builder instanceof PipedFilesBuilder && e.isError() == false) {
121                 try {
122                     String JavaDoc iso88591 = new String JavaDoc(e.getRawData(), "ISO-8859-1");
123                     builder.parseLine(iso88591, e.isError());
124                 } catch (UnsupportedEncodingException JavaDoc e1) {
125                     assert false;
126                 }
127             } else {
128                 builder.parseLine(e.getMessage(), e.isError());
129             }
130         }
131     }
132
133     /**
134      * Returns whether the builder is set.
135      */

136     protected boolean isBuilderSet() {
137         return builderSet;
138     }
139
140     /**
141      * Used for setting user-defined builder.
142      * Can be also set null, in that case the builder mechanism is not used at
143      * all.
144      */

145     public void setBuilder(Builder builder) {
146         this.builder = builder;
147         builderSet = true;
148     }
149
150     /**
151      * Called when server responses with "ok" or "error", (when the command finishes).
152      */

153     public void commandTerminated(TerminationEvent e) {
154         if (builder == null) {
155             return;
156         }
157
158         if (taggedLineBuffer.length() > 0) {
159             builder.parseLine(taggedLineBuffer.toString(), false);
160             taggedLineBuffer.setLength(0);
161         }
162         builder.outputDone();
163     }
164 }
165
Popular Tags