KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > response > WrapperSendResponse


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 NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lib.cvsclient.response;
21
22 import java.io.*;
23 import java.util.*;
24
25 import org.netbeans.lib.cvsclient.util.*;
26 import org.netbeans.lib.cvsclient.util.SimpleStringPattern;
27 import org.netbeans.lib.cvsclient.command.KeywordSubstitutionOptions;
28
29 /**
30  * This class handles the response from the server to a wrapper-sendme-rcsOptions
31  * request
32  * @author Sriram Seshan
33  */

34 public class WrapperSendResponse implements Response {
35
36     public static Map parseWrappers(String JavaDoc line) {
37         StringTokenizer tokenizer = new StringTokenizer(line);
38
39         // the first token is the pattern
40
SimpleStringPattern pattern = new SimpleStringPattern(tokenizer.nextToken());
41
42         // it is followed by option value pairs
43
String JavaDoc option, value;
44         
45         Map wrappersMap = null;
46
47         while (tokenizer.hasMoreTokens()) {
48             option = tokenizer.nextToken();
49             value = tokenizer.nextToken();
50
51             // do not bother with the -m Options now
52
if (option.equals("-k")) { //NOI18N
53

54                 // This is a keyword substitution option
55
// strip the quotes
56
int first = value.indexOf('\'');
57                 int last = value.lastIndexOf('\'');
58                 if (first >=0 && last >= 0) {
59                     value = value.substring(first+1, last);
60                 }
61
62                 KeywordSubstitutionOptions keywordOption = KeywordSubstitutionOptions.findKeywordSubstOption(value);
63                 if (wrappersMap == null) {
64                     if (!tokenizer.hasMoreTokens()) {
65                         wrappersMap = Collections.singletonMap(pattern, keywordOption);
66                     } else {
67                         wrappersMap = new LinkedHashMap();
68                         wrappersMap.put(pattern, keywordOption);
69                     }
70                 } else {
71                     wrappersMap.put(pattern, keywordOption);
72                 }
73             }
74         }
75         return wrappersMap;
76     }
77     
78     /**
79      * Process the data for the response.
80      * @param dis the data inputstream allowing the client to read the server's
81      * response. Note that the actual response name has already been read
82      * and the input stream is positioned just before the first argument, if
83      * any.
84      */

85     public void process(LoggedDataInputStream dis, ResponseServices services)
86             throws ResponseException {
87         try {
88             
89             String JavaDoc wrapperSettings = dis.readLine();
90             Map wrappers = parseWrappers(wrapperSettings);
91             for (Iterator it = wrappers.keySet().iterator(); it.hasNext(); ) {
92                 StringPattern pattern = (StringPattern) it.next();
93                 KeywordSubstitutionOptions keywordOption = (KeywordSubstitutionOptions) wrappers.get(pattern);
94                 services.addWrapper(pattern, keywordOption);
95             }
96         }
97         catch (EOFException ex) {
98             throw new ResponseException(ex, ResponseException.getLocalMessage("CommandException.EndOfFile", null)); //NOI18N
99
}
100         catch (IOException ex) {
101             throw new ResponseException(ex);
102         }
103         catch (NoSuchElementException nse) {
104             throw new ResponseException(nse);
105         }
106     }
107
108     /**
109      * Is this a terminal response, i.e. should reading of responses stop
110      * after this response. This is true for responses such as OK or
111      * an error response
112      */

113     public boolean isTerminalResponse() {
114         return false;
115     }
116     
117 }
118
Popular Tags