Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6b3a5909aea1158ff753809deb9682a7aaa3e80b (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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*******************************************************************************
 * Copyright (c) 2000, 2010 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.egit.internal.relengtools;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.Path;
import org.eclipse.egit.core.GitTag;

/**
 * This class provides access to information stored in RelEng map files
 */
public class MapEntry {
	public static final GitTag DEFAULT = new GitTag("master");

	private static final String HEAD = "HEAD";

	private static final String KEY_TAG = "tag"; //$NON-NLS-1$

	private static final String KEY_PATH = "path"; //$NON-NLS-1$

	private static final String REPO = "repo";

	private static final String EMPTY_STRING = ""; //$NON-NLS-1$

	private boolean valid = false;

	private String type = EMPTY_STRING;

	private String id = EMPTY_STRING;

	private OrderedMap arguments = new OrderedMap();

	private boolean legacy = false;

	private String version;

	public static void main(String[] args) {
		// For testing only

		final String[] strings = {
				"",
				" ",
				"type",
				"type@",
				"type@id",
				"type@id=",
				"type@id=tag,",
				"type@id=tag, connectString",
				"type@id=tag, connectString,",
				"type@id=tag, connectString,password",
				"type@id=tag, connectString,password,",
				"type@id=tag, connectString,password,moduleName",
				"type@id=tag, connectString,,moduleName",
				"!***************  FEATURE CONTRIBUTION  ******************************************************",
				"@",
				"=",
				",,,",
				"@=,,,,",
				"type@id,version=CVS,tag=myTag,cvsRoot=myCvsRoot,password=password,path=myPath", };

		for (int i = 0; i < strings.length; i++) {
			final String string = strings[i];
			final MapEntry anEntry = new MapEntry(string);

			System.out
					.println("-----------------------------------------------");
			System.out.println("input: " + string);
			System.out.println("map string: " + anEntry.getMapString());
			// System.out.println(anEntry.getReferenceString());
			anEntry.display();
		}

	}

	private void display() {
		// For testing only
		System.out.println("Is Valid: " + isValid());
		System.out.println("Type: " + getType());
		System.out.println("Project Name: " + getId());
		System.out.println("Tag: " + getTagName());
		if (version != null)
			System.out.println("Version: " + version);
		System.out.println("Connect: " + getRepo());
		System.out.println("Path: " + getPath());
	}

	public MapEntry(String entryLine) {
		init(entryLine);
	}

	/**
	 * Parse a map file entry line
	 * 
	 * @param entryLine
	 */
	private void init(String entryLine) {
		valid = false;

		// check for commented out entry
		if (entryLine.startsWith("#") || entryLine.startsWith("!"))
			return;

		// Type
		int start = 0;
		int end = entryLine.indexOf('@');
		if (end == -1)
			return;
		type = entryLine.substring(start, end).trim();

		// Project Name
		start = end + 1;
		end = entryLine.indexOf('=', start);
		if (end == -1)
			return;
		id = entryLine.substring(start, end).trim();
		// we have a version that we have to strip off
		final int comma = id.indexOf(',');
		if (comma != -1) {
			version = id.substring(comma + 1);
			id = id.substring(0, comma);
		}

		final String[] args = getArrayFromStringWithBlank(
				entryLine.substring(end + 1), ",");
		this.arguments = populate(args);
		final String tag = (String) arguments.get(KEY_TAG);
		final String repo = (String) arguments.get(REPO);
		if (tag == null || tag.length() == 0 || repo == null
				|| repo.length() == 0)
			return;
		valid = true;
	}

	/*
	 * Build a table from the given array. In the new format,the array contains
	 * key=value elements. Otherwise we fill in the key based on the old format.
	 */
	private OrderedMap populate(String[] entries) {
		final OrderedMap result = new OrderedMap();
		for (int i = 0; i < entries.length; i++) {
			final String entry = entries[i];
			final int index = entry.indexOf('=');
			if (index == -1) {
				// we only handle CVS entries
				if (i == 0 && "GIT".equalsIgnoreCase(entry))
					continue;
				// legacy story...
				return legacyPopulate(entries);
			}
			final String key = entry.substring(0, index);
			final String value = entry.substring(index + 1);
			result.put(key, value);
		}
		result.toString();
		return result;
	}

	private OrderedMap legacyPopulate(String[] entries) {
		legacy = true;
		final OrderedMap result = new OrderedMap();
		// must have at least tag and connect string
		if (entries.length >= 2) {
			// Version
			result.put(KEY_TAG, entries[0]);
			// Repo Connect String
			result.put(REPO, entries[1]);

			// Optional CVS Module Name
			if (entries.length >= 3)
				result.put(KEY_PATH, entries[3]);
		}
		return result;
	}

	/**
	 * Convert a list of tokens into an array. The list separator has to be
	 * specified. The specificity of this method is that it returns an empty
	 * element when to same separators are following each others. For example
	 * the string a,,b returns the following array [a, ,b]
	 * 
	 */
	public static String[] getArrayFromStringWithBlank(String list,
			String separator) {
		if (list == null || list.trim().length() == 0)
			return new String[0];
		final List result = new ArrayList();
		boolean previousWasSeparator = true;
		for (final StringTokenizer tokens = new StringTokenizer(list,
				separator, true); tokens.hasMoreTokens();) {
			final String token = tokens.nextToken().trim();
			if (token.equals(separator)) {
				if (previousWasSeparator)
					result.add(""); //$NON-NLS-1$
				previousWasSeparator = true;
			} else {
				result.add(token);
				previousWasSeparator = false;
			}
		}
		return (String[]) result.toArray(new String[result.size()]);
	}

	public String getTagName() {
		final String value = (String) arguments.get(KEY_TAG);
		return value == null || HEAD.equals(value) ? EMPTY_STRING : value;
	}

	public GitTag getTag() {
		if (getTagName().equals(HEAD) || getTagName().equals(""))
			return DEFAULT;
		return new GitTag(getTagName());
	}

	public String getId() {
		return id;
	}

	private String internalGetCVSModule() {
		final String module = (String) arguments.get(KEY_PATH);
		return module == null ? id : module;
	}

	public String getPath() {
		final String value = (String) arguments.get(KEY_PATH);
		return value == null ? EMPTY_STRING : value;
	}

	public String getRepo() {
		final String value = (String) arguments.get(REPO);
		return value == null ? EMPTY_STRING : value;
	}

	public String getType() {
		return type;
	}

	public boolean isValid() {
		return valid;
	}

	public String getReferenceString() {
		if (!isValid())
			return null;
		// This is the format used by the CVS IProjectSerializer
		final String projectName = new Path(internalGetCVSModule())
				.lastSegment();
		return "1.0," + getRepo() + "," + internalGetCVSModule() + ","
				+ projectName + "," + getTagName();
	}

	public String getMapString() {
		final StringBuffer result = new StringBuffer();
		if (legacy) {
			result.append(getType());
			result.append('@');
			result.append(getId());
			if (version != null) {
				result.append(',');
				result.append(version);
			}
			result.append('=');
			result.append(getTagName());
			result.append(',');
			result.append(getRepo());
			result.append(',');
			result.append(getPath());
			return result.toString();
		}
		result.append(getType());
		result.append('@');
		result.append(getId());
		if (version != null) {
			result.append(',');
			result.append(version);
		}
		result.append('=');
		result.append("GIT");
		for (final Iterator iter = arguments.keys().iterator(); iter.hasNext();) {
			final String key = (String) iter.next();
			final String value = (String) arguments.get(key);
			if (value != null && value.length() > 0)
				result.append(',' + key + '=' + value);
		}
		return result.toString();
	}

	/*
	 * Return the version specified for this entry. Can be null.
	 */
	public String getVersion() {
		return version;
	}

	public void setId(String projectID) {
		this.id = projectID;
	}

	public void setCVSModule(String path) {
		arguments.put(KEY_PATH, path);
	}

	public void setRepo(String repo) {
		arguments.put(REPO, repo);
	}

	public void setTagName(String tagName) {
		arguments.put(KEY_TAG, tagName);
	}

	public void setType(String type) {
		this.type = type;
	}

	public void setValid(boolean valid) {
		this.valid = valid;
	}

	@Override
	public String toString() {
		return "Entry: " + getMapString();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof MapEntry) {
			return ((MapEntry) obj).getMapString().equals(getMapString());
		}
		return super.equals(obj);
	}

	/**
	 * Return <code>true</code> if the entry is mapped to the given project and
	 * <code>false</code> otherwise.
	 */
	public boolean isMappedTo(IProject project) {
		// RepositoryProvider provider =
		// RepositoryProvider.getProvider(project);
		if (id.equals(project.getName())) {
			return true;
		}
		return false;
	}

}

Back to the top