Skip to main content
summaryrefslogtreecommitdiffstats
blob: 3a7c7d0cacc90a3d60ee65cc58b439f7fda48412 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package org.eclipse.team.internal.ccvs.core.util;

/*
 * (c) Copyright IBM Corp. 2000, 2001.
 * All Rights Reserved.
 */

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.team.ccvs.core.CVSProviderPlugin;
import org.eclipse.team.internal.ccvs.core.CVSException;
import org.eclipse.team.internal.ccvs.core.client.Session;

/**
 * Unsorted static helper-methods 
 */
public class Util {
	
	private static final String AT = "@";
	private static final String COLON = ":";
	
	
	// private static final String newLine = System.getProperty("line.separator");
	
	/**
	 * Looks for search in original. If it findes search it puts excange
	 * in the result. Otherwise it copies original in the result.
	 */
	public static byte[] replace(final byte[] original,final byte[] search,final byte[] excange) {
		
		final int tmpMulti;
		final byte[] tmpResult;
		final byte[] result;

		int size=0;
		boolean replaced;
		
		// create an array, that can hold the result for sure
		tmpMulti = (int) Math.ceil((double)excange.length/(double)search.length);

		Assert.isTrue((long)tmpMulti * (long)original.length < Integer.MAX_VALUE);
		tmpResult = new byte[tmpMulti * original.length];
		
		int i=0;
		while (i < original.length) {
			replaced = false;
			for (int j = 0; j < search.length; j++) {
				
				if ((i+j >= original.length) || (original[i+j] != search[j])) {
					// Send another letter to the result
					// array
					break;
				}
				
				if (j == search.length-1) {
					// We have found the search and going to 
					// put excange intead of it
					System.arraycopy(excange,0,tmpResult,size,excange.length);
					size += excange.length;
					i += search.length;
					replaced = true;
				}
			}
			
			if (!replaced) {
				tmpResult[size] = original[i];
				i++;
				size++;
			}
		}
		
		result = new byte[size];
		System.arraycopy(tmpResult,0,result,0,size);
		return result;
	}
	
	/**
	 * @see Util#getOptions(String[],String,boolean)
	 */
	public static String getOption(String[] options, String key, boolean deleteOption) {
		
		String[] result;
		
		result = getOptions(options,key,deleteOption);
		
		if (result.length == 0) {
			return null;
		} else {
			return result[0];
		}
	}
	
	/**
	 * Get an option out of an array of options. It assumes, that 
	 * the next field to the key contains the parameter to the 
	 * option.
	 * 
	 * @param options not null
	 * @param key not null
	 * @param deleteOption nulls both the option-tag and the information 
	 * @return String[0] if the option could not be found
	 */	
	public static String[] getOptions(String[] options, String key, boolean deleteOption) {
		
		String[] tmpResult;
		String[] result;
		int size = 0;
		
		Assert.isNotNull(options);
		Assert.isNotNull(key);
		
		tmpResult = new String[options.length];
		
		for (int i=0; i<options.length; i++) {
			if (key.equals(options[i]) && i<options.length-1) {
				tmpResult[size++] = options[i+1];
				
				// This should be done in another way maybe we should 
				// have an options Object or give the array modified
				// back.
				// Maybe we are going to change that.
				if (deleteOption) {
					options[i] = null;
					options[i+1] = null;
				}
			}
		}
		
		result = new String[size];
		System.arraycopy(tmpResult,0,result,0,size);
		return result;
	}
	
	/**
	 * Checks wether the Array options contains the String 
	 * key.
	 * @param options not null
	 * @param key not null
	 */	
	public static boolean isOption(String[] options, String key) {

		Assert.isNotNull(options);
		Assert.isNotNull(key);

		for (int i=0; i<options.length; i++) {
			if (key.equals(options[i])) {
				return true;
			}
		}
		return false;
	}

	/**
	 * Remove or get the password out of a repoString. 
	 */
	// FIXME: This is only used for tests ... move it	
	private static String passwordHandle(String repoName, boolean remove) {
		
		int atPlace = -1;
		int colonPlace = -1;
		int colonCount = 0;
		String currentChar; 
		
		Assert.isTrue(repoName.indexOf(AT) != -1);
		Assert.isTrue(repoName.indexOf(COLON) != -1);
		
		for (int i=0; i<repoName.length(); i++) {
			
			currentChar = repoName.substring(i,i+1);
			
			if (currentChar.equals(COLON)) {
				colonCount++;
				
				if (colonCount == 3) {
					colonPlace = i;
				}
			}
			
			if (currentChar.equals(AT)) {
				if (colonPlace == -1) {
					
					// If the @ comes before the third colon, then 
					// we do not have a password and return with the
					// same string
					return repoName;
				} else {
					atPlace = i;
				}
				
			}
		}
		
		if (atPlace == -1) {
			return repoName;
		}
		
		if (remove) {
			return repoName.substring(0,colonPlace) + repoName.substring(atPlace);
		} else {
			return repoName.substring(colonPlace + 1, atPlace);
		}
	}

	/**
	 * returns ":pserver:nkrambro@fiji:/home/nkrambro/repo"
	 *         when you insert ":pserver:nkrambro:password@fiji:/home/nkrambro/repo"
	 */
	// FIXME: This is only used for tests ... move it	
	public static String removePassword(String root) {
		return passwordHandle(root,true);
	}
	
	/**
	 * 
	 * returns "password"
	 *         when you insert ":pserver:nkrambro:password@fiji:/home/nkrambro/repo"
	 */	
	// FIXME: This is only used for tests ... move it	
	public static String getPassword(String root) {
		return passwordHandle(root,false);
	}
	
	// FIXME: This is only used for tests ... move it	
	public static String mergeRoot(String rootWithoutPwd, String password) {
		
		StringBuffer result = new StringBuffer();

		Assert.isTrue(rootWithoutPwd.indexOf(AT) != -1);
		Assert.isTrue(rootWithoutPwd.indexOf(COLON) != -1);
		
		if (password == null) {
			return rootWithoutPwd;
		}
		
		result.append(rootWithoutPwd.substring(0,rootWithoutPwd.indexOf(AT)));
		result.append(COLON);
		result.append(password);
		result.append(rootWithoutPwd.substring(rootWithoutPwd.indexOf(AT)));
		
		return result.toString();
	}
	
	/**
	 * Get the extention of the path of resource
	 * relative to the path of root
	 * 
	 * @throws CVSException if root is not a root-folder of resource
	 */
	public static String getRelativePath(String rootName, String resourceName) 
		throws CVSException {

		if (!resourceName.startsWith(rootName)) {
			throw new CVSException("Internal error, resource does not start with root.");
		}
		
		// Otherwise we would get an ArrayOutOfBoundException
		// in case of two equal Resources
		if (rootName.length() == resourceName.length()) {
			return "";
		}
		
		// Get rid of the seperator, that would be in the 
		// beginning, if we did not go from +1
		return resourceName.substring(rootName.length() + 1).replace('\\', '/');
	}
	
	/**
	 * Append the prefix and suffix to form a valid CVS path.
	 */
	public static String appendPath(String prefix, String suffix) {
		if (prefix.endsWith(Session.SERVER_SEPARATOR)) {
			if (suffix.startsWith(Session.SERVER_SEPARATOR))
				return prefix + suffix.substring(1);
			else
				return prefix + suffix;
		} else if (suffix.startsWith(Session.SERVER_SEPARATOR))
			return prefix + suffix;
		else
			return prefix + Session.SERVER_SEPARATOR + suffix;
	}
	
	
	public static void logError(String message, Throwable throwable) {
		CVSProviderPlugin.log(new Status(IStatus.ERROR, CVSProviderPlugin.ID, IStatus.ERROR, message, throwable));
	}
}

Back to the top