Skip to main content
summaryrefslogtreecommitdiffstats
blob: 162f9d09695591457fa175b71df77a27f1128ac0 (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
/*******************************************************************************
 * Copyright (c) 2004, 2007 Mylyn project committers 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
 *******************************************************************************/

package org.eclipse.mylyn.internal.trac.ui;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.hyperlink.IHyperlink;
import org.eclipse.mylyn.internal.trac.core.ITracClient;
import org.eclipse.mylyn.monitor.core.StatusHandler;
import org.eclipse.mylyn.tasks.core.TaskRepository;
import org.eclipse.mylyn.tasks.ui.TaskHyperlink;
import org.eclipse.mylyn.tasks.ui.WebHyperlink;

public class TracHyperlinkUtil {

	static Pattern ticketPattern = Pattern.compile("(ticket:|#)(\\d+)");

	static Pattern commentPattern = Pattern.compile("comment:ticket:(\\d+):(\\d+)");

	static Pattern reportPattern1 = Pattern.compile("report:(\\d+)");

	static Pattern reportPattern2 = Pattern.compile("\\{(\\d+)\\}");

	static Pattern changesetPattern1 = Pattern.compile("(r|changeset:)(\\d+)(/\\w+)?");

	static Pattern changesetPattern2 = Pattern.compile("\\[(\\d+)(/\\w+)?\\]");

	static Pattern revisionLogPattern1 = Pattern.compile("r(\\d+):(\\d+)");

	static Pattern revisionLogPattern2 = Pattern.compile("\\[(\\d+):(\\d+)\\]");

	static Pattern revisionLogPattern3 = Pattern.compile("log:(\\w+)?@(\\d+):(\\d+)");

	static Pattern diffPattern1 = Pattern.compile("diff:@(\\d+):(\\d+)");

	static Pattern diffPattern2 = Pattern.compile("diff:([\\w\\./-]+)(@(\\d+))?//([\\w\\./-]+)(@(\\d+))?");

	static Pattern wikiPattern1 = Pattern.compile("wiki:(\\w+)");

	static Pattern wikiPattern2 = Pattern.compile("[A-Z][a-z0-9]+[A-Z]\\w*");

	static Pattern milestonePattern = Pattern.compile("milestone:([\\w\\.]+)");

	static Pattern attachmentPattern = Pattern.compile("attachment:ticket:(\\d+):([\\w\\.]+)");

	static Pattern filesPattern = Pattern.compile("source:/*([\\w\\./\\-_]+)(@(\\d+)(#L(\\d+))?)?");

	/**
	 * <ul>
	 * <li>Tickets: #1 or ticket:1
	 * <li>Ticket comments: comment:ticket:1:2
	 * <li>Reports: {1} or report:1
	 * <li>Changesets: r1, [1], changeset:1 or (restricted) [1/trunk], changeset:1/trunk
	 * <li>Revision log: r1:3, [1:3] or log:@1:3, log:trunk@1:3
	 * <li>Diffs: diff:@1:3, diff:tags/trac-0.9.2/wiki-default//tags/trac-0.9.3/wiki-default or
	 * diff:trunk/trac@3538//sandbox/vc-refactoring@3539
	 * <li>Wiki pages: CamelCase or wiki:CamelCase
	 * <li>Milestones: milestone:1.0
	 * <li>Attachment: attachment:ticket:944:attachment.1073.diff
	 * <li>Files: source:trunk/COPYING
	 * <li>A specific file revision: source:/trunk/COPYING@200
	 * <li>A particular line of a specific file revision: source:/trunk/COPYING@200#L25
	 * </ul>
	 * 
	 * @see http://trac.edgewall.org/wiki/TracLinks
	 */
	public static IHyperlink[] findHyperlinks(TaskRepository repository, String text, int lineOffset, int regionOffset) {
		List<IHyperlink> links = new ArrayList<IHyperlink>();

		Matcher m = commentPattern.matcher(text);
		while (m.find()) {
			if (isInRegion(lineOffset, m)) {
				String id = m.group(1);
				// String comment = m.group(2);
				links.add(new TaskHyperlink(determineRegion(regionOffset, m), repository, id));
			}
		}

		m = ticketPattern.matcher(text);
		while (m.find()) {
			if (isInRegion(lineOffset, m)) {
				String id = m.group(2);
				links.add(new TaskHyperlink(determineRegion(regionOffset, m), repository, id));
			}
		}

		m = reportPattern1.matcher(text);
		while (m.find()) {
			if (isInRegion(lineOffset, m)) {
				String id = m.group(1);
				links.add(new WebHyperlink(determineRegion(regionOffset, m), repository.getUrl()
						+ ITracClient.REPORT_URL + id));
			}
		}

		m = reportPattern2.matcher(text);
		while (m.find()) {
			if (isInRegion(lineOffset, m)) {
				String id = m.group(1);
				links.add(new WebHyperlink(determineRegion(regionOffset, m), repository.getUrl()
						+ ITracClient.REPORT_URL + id));
			}
		}

		m = revisionLogPattern1.matcher(text);
		while (m.find()) {
			if (isInRegion(lineOffset, m)) {
				String rev = m.group(1);
				String stopRev = m.group(2);
				String url = repository.getUrl() + ITracClient.REVISION_LOG_URL + "?rev=" + rev + "&stop_rev="
						+ stopRev;
				links.add(new WebHyperlink(determineRegion(regionOffset, m), url));
			}
		}

		m = revisionLogPattern2.matcher(text);
		while (m.find()) {
			if (isInRegion(lineOffset, m)) {
				String rev = m.group(1);
				String stopRev = m.group(2);
				String url = repository.getUrl() + ITracClient.REVISION_LOG_URL + "?rev=" + rev + "&stop_rev="
						+ stopRev;
				links.add(new WebHyperlink(determineRegion(regionOffset, m), url));
			}
		}

		m = revisionLogPattern3.matcher(text);
		while (m.find()) {
			if (isInRegion(lineOffset, m)) {
				String branch = m.group(1);
				String rev = m.group(2);
				String stopRev = m.group(3);
				String url = repository.getUrl() + ITracClient.REVISION_LOG_URL;
				if (branch != null) {
					url += branch;
				}
				url += "?rev=" + rev + "&stop_rev=" + stopRev;
				links.add(new WebHyperlink(determineRegion(regionOffset, m), url));
			}
		}

		m = changesetPattern1.matcher(text);
		while (m.find()) {
			if (isInRegion(lineOffset, m)) {
				String rev = m.group(2);
				String branch = m.group(3);
				String url = repository.getUrl() + ITracClient.CHANGESET_URL + rev;
				if (branch != null) {
					url += branch;
				}
				links.add(new WebHyperlink(determineRegion(regionOffset, m), url));
			}
		}

		m = changesetPattern2.matcher(text);
		while (m.find()) {
			if (isInRegion(lineOffset, m)) {
				String rev = m.group(1);
				String branch = m.group(2);
				String url = repository.getUrl() + ITracClient.CHANGESET_URL + rev;
				if (branch != null) {
					url += branch;
				}
				links.add(new WebHyperlink(determineRegion(regionOffset, m), url));
			}
		}

		m = diffPattern1.matcher(text);
		while (m.find()) {
			if (isInRegion(lineOffset, m)) {
				String old_rev = m.group(1);
				String new_rev = m.group(2);
				String url = repository.getUrl() + ITracClient.CHANGESET_URL;
				url += "?new=" + new_rev + "&old=" + old_rev;
				links.add(new WebHyperlink(determineRegion(regionOffset, m), url));
			}
		}

		m = diffPattern2.matcher(text);
		while (m.find()) {
			if (isInRegion(lineOffset, m)) {
				String old_path = m.group(1);
				String old_rev = m.group(3);
				String new_path = m.group(4);
				String new_rev = m.group(6);
				String url = repository.getUrl() + ITracClient.CHANGESET_URL;
				try {
					url += "?new_path=" + URLEncoder.encode(new_path, "UTF-8");
					url += "&old_path=" + URLEncoder.encode(old_path, "UTF-8");
				} catch (UnsupportedEncodingException e) {
					StatusHandler.fail(e, "Unexpected exception", false);
					continue;
				}
				if (new_rev != null) {
					url += "&new=" + new_rev;
				}
				if (old_rev != null) {
					url += "&old=" + old_rev;
				}
				links.add(new WebHyperlink(determineRegion(regionOffset, m), url));
			}
		}

		m = wikiPattern1.matcher(text);
		while (m.find()) {
			if (isInRegion(lineOffset, m)) {
				String page = m.group(1);
				links.add(new WebHyperlink(determineRegion(regionOffset, m), repository.getUrl() + ITracClient.WIKI_URL
						+ page));
			}
		}

		m = wikiPattern2.matcher(text);
		while (m.find()) {
			if (isInRegion(lineOffset, m)) {
				String page = m.group(0);
				links.add(new WebHyperlink(determineRegion(regionOffset, m), repository.getUrl() + ITracClient.WIKI_URL
						+ page));
			}
		}

		m = milestonePattern.matcher(text);
		while (m.find()) {
			if (isInRegion(lineOffset, m)) {
				String milestone = m.group(1);
				links.add(new WebHyperlink(determineRegion(regionOffset, m), repository.getUrl()
						+ ITracClient.MILESTONE_URL + milestone));
			}
		}

		m = attachmentPattern.matcher(text);
		while (m.find()) {
			if (isInRegion(lineOffset, m)) {
				String id = m.group(1);
				// String attachment = m.group(2);
				links.add(new TaskHyperlink(determineRegion(regionOffset, m), repository, id));
			}
		}

		m = filesPattern.matcher(text);
		while (m.find()) {
			if (isInRegion(lineOffset, m)) {
				String filename = m.group(1);
				String rev = m.group(3);
				String line = m.group(5);
				String url = repository.getUrl() + ITracClient.BROWSER_URL + filename;
				if (rev != null) {
					url += "?rev=" + rev;
					if (line != null) {
						url += "#L" + line;
					}
				}
				links.add(new WebHyperlink(determineRegion(regionOffset, m), url));
			}
		}

		return links.isEmpty() ? null : links.toArray(new IHyperlink[0]);
	}

	private static boolean isInRegion(int lineOffset, Matcher m) {
		return (lineOffset >= m.start() && lineOffset <= m.end());
	}

	private static IRegion determineRegion(int regionOffset, Matcher m) {
		return new Region(regionOffset + m.start(), m.end() - m.start());
	}

}

Back to the top