KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.netbeans.lib.cvsclient.command;
20
21 import org.netbeans.lib.cvsclient.util.SimpleStringPattern;
22 import org.netbeans.lib.cvsclient.ClientServices;
23
24 import java.io.*;
25 import java.util.Map JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27
28 /**
29  * Support for <tt>.cvswrappers</tt> parsing and merging.
30  */

31 public class WrapperUtils {
32
33     /**
34      * Reads the wrappers from the specified source and populates the specified
35      * map
36      *
37      * @param reader The source of wrappers which is being processed
38      * @param theMap The map which is being updated
39      */

40     private static void parseWrappers(BufferedReader reader, Map JavaDoc theMap)
41                  throws IOException {
42
43         String JavaDoc line;
44         while ((line = reader.readLine()) != null){
45             StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(line);
46
47             // the first token is the pattern
48
SimpleStringPattern pattern = new SimpleStringPattern(tokenizer.nextToken());
49
50             // it is followed by option value pairs
51
String JavaDoc option, value;
52
53             while (tokenizer.hasMoreTokens()) {
54                 option = tokenizer.nextToken();
55                 value = tokenizer.nextToken();
56
57                 // do not bother with the -m Options now
58
if (option.equals("-k")) { //NOI18N
59

60                     // This is a keyword substitution option
61
// strip the quotes
62
int first = value.indexOf('\'');
63                     int last = value.lastIndexOf('\'');
64                     if (first >=0 && last >= 0) {
65                         value = value.substring(first+1, last);
66                     }
67
68                     KeywordSubstitutionOptions keywordOption = KeywordSubstitutionOptions.findKeywordSubstOption(value);
69                     if (!theMap.containsKey(pattern)) {
70                         theMap.put(pattern, keywordOption);
71                     }
72                 }
73             }
74         }
75     }
76
77     /**
78      * Reads the wrappers from the specified file and populates the specified
79      * map
80      *
81      * @param file The File object corresponding to the file which is being processed
82      * @param wrapperMap The map which is being updated
83      */

84     public static void readWrappersFromFile(File file, Map JavaDoc wrapperMap) throws IOException, FileNotFoundException{
85         parseWrappers(new BufferedReader(new FileReader(file)), wrapperMap);
86     }
87
88     /**
89      * Reads the wrappers from the specified System property and populates the specified
90      * map. The map is unchanged if the property is not set.
91      *
92      * @param envVar The system variable name
93      * @param wrapperMap The map which is being updated
94      */

95     private static void readWrappersFromProperty(String JavaDoc envVar, Map JavaDoc wrapperMap) throws IOException {
96         String JavaDoc propertyValue = System.getenv(envVar);
97         if (propertyValue != null)
98         {
99             parseWrappers(new BufferedReader(new StringReader(propertyValue)), wrapperMap);
100         }
101     }
102
103     /**
104      * This method consolidates the wrapper map so that it follows CVS prioritization
105      * rules for the wrappers. Both AddCommand and ImportCommand will be calling
106      * this.
107      */

108     public static Map JavaDoc mergeWrapperMap(ClientServices client) throws CommandException
109     {
110         String JavaDoc wrapperSource = null;
111         Map JavaDoc wrappersMap = new java.util.HashMap JavaDoc(client.getWrappersMap());
112         try
113         {
114             File home = new File(System.getProperty("user.home")); // NOI18N
115
File wrappers = new File(home, "./cvswrappers"); //NOI18N
116

117             wrapperSource = CommandException.getLocalMessage("WrapperUtils.clientDotWrapper.text"); //NOI18N
118

119             if (wrappers.exists()) {
120                 readWrappersFromFile(wrappers, wrappersMap);
121             }
122
123             wrapperSource = CommandException.getLocalMessage("WrapperUtils.environmentWrapper.text"); //NOI18N
124

125             //process the Environment variable CVSWRAPPERS
126
readWrappersFromProperty("CVSWRAPPERS", wrappersMap); //NOI18N
127
}
128         catch (FileNotFoundException fnex) {
129             // should not happen as we check for file existence. Even if it does
130
// it just means the .cvswrappers are not there and can be ignored
131
}
132         catch (Exception JavaDoc ioex) {
133             Object JavaDoc [] parms = new Object JavaDoc[1];
134             parms[0] = wrapperSource;
135             String JavaDoc localizedMessage = CommandException.getLocalMessage("WrapperUtils.wrapperError.text", parms); //NOI18N
136
throw new CommandException(ioex, localizedMessage);
137         }
138
139         return wrappersMap;
140     }
141
142
143 }
144
Popular Tags