diff options
Diffstat (limited to 'org.eclipse.mylyn.bugzilla.ui/src/org/eclipse/mylyn/internal/bugzilla/ui/BugzillaHyperlinkUtil.java')
-rw-r--r-- | org.eclipse.mylyn.bugzilla.ui/src/org/eclipse/mylyn/internal/bugzilla/ui/BugzillaHyperlinkUtil.java | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/org.eclipse.mylyn.bugzilla.ui/src/org/eclipse/mylyn/internal/bugzilla/ui/BugzillaHyperlinkUtil.java b/org.eclipse.mylyn.bugzilla.ui/src/org/eclipse/mylyn/internal/bugzilla/ui/BugzillaHyperlinkUtil.java new file mode 100644 index 000000000..ffbbc56ff --- /dev/null +++ b/org.eclipse.mylyn.bugzilla.ui/src/org/eclipse/mylyn/internal/bugzilla/ui/BugzillaHyperlinkUtil.java @@ -0,0 +1,84 @@ +/******************************************************************************* + * Copyright (c) 2004 - 2006 University Of British Columbia 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: + * University Of British Columbia - initial API and implementation + *******************************************************************************/ +package org.eclipse.mylyn.internal.bugzilla.ui; + +import java.util.ArrayList; +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.tasks.core.TaskRepository; +import org.eclipse.mylyn.tasks.ui.TaskHyperlink; +import org.eclipse.mylyn.tasks.ui.TasksUiPlugin; + +/** + * @author Rob Elves (multiple bug/task hyperlink support) + * @author Mik Kersten + */ +public class BugzillaHyperlinkUtil { + + private static final int TASK_NUM_GROUP = 3; + + private static final String regexp = "(duplicate of|bug|task)(\\s#|#|#\\s|\\s|)(\\s\\d+|\\d+)"; + + private static final Pattern PATTERN = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE); + + public static IHyperlink[] findBugHyperlinks(String repositoryUrl, int offset, String comment, int lineOffset) { + ArrayList<IHyperlink> hyperlinksFound = new ArrayList<IHyperlink>(); + + Matcher m = PATTERN.matcher(comment); + while (m.find()) { + if (offset >= m.start() && offset <= m.end()) { + IHyperlink link = extractHyperlink(repositoryUrl, lineOffset, m); + if (link != null) + hyperlinksFound.add(link); + } + } + + if (hyperlinksFound.size() > 0) { + return hyperlinksFound.toArray(new IHyperlink[1]); + } + return null; + } + + private static IHyperlink extractHyperlink(String repositoryUrl, int lineOffset, Matcher m) { + + int start = -1; + + if(m.group().startsWith("duplicate")) { + start = m.start() + m.group().indexOf(m.group(TASK_NUM_GROUP)); + } else { + start = m.start(); + } + + int end = m.end(); + + if (end == -1) + end = m.group().length(); + + TaskRepository repository = TasksUiPlugin.getRepositoryManager().getRepository(repositoryUrl); + + try { + + String bugId = m.group(TASK_NUM_GROUP).trim(); + start += lineOffset; + end += lineOffset; + + IRegion sregion = new Region(start, end - start); + return new TaskHyperlink(sregion, repository, bugId); + + } catch (NumberFormatException e) { + return null; + } + } +} |