|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2004, 2007 Mylyn project committers and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
*******************************************************************************/ |
| 8 |
|
| 9 |
package org.eclipse.mylyn.internal.java.tasks; |
| 10 |
|
| 11 |
import java.util.ArrayList; |
| 12 |
import java.util.List; |
| 13 |
import java.util.regex.Matcher; |
| 14 |
import java.util.regex.Pattern; |
| 15 |
|
| 16 |
import org.eclipse.jface.text.BadLocationException; |
| 17 |
import org.eclipse.jface.text.IDocument; |
| 18 |
import org.eclipse.jface.text.IRegion; |
| 19 |
import org.eclipse.jface.text.ITextViewer; |
| 20 |
import org.eclipse.jface.text.Region; |
| 21 |
import org.eclipse.jface.text.hyperlink.AbstractHyperlinkDetector; |
| 22 |
import org.eclipse.jface.text.hyperlink.IHyperlink; |
| 23 |
|
| 24 |
/** |
| 25 |
* Java Class Hyperlink Dectector, e.g. detecting org.eclipse.mylyn.internal.java.tasks.JavaClassHyperlinkDetector |
| 26 |
* |
| 27 |
* @author Jingwen Ou |
| 28 |
*/ |
| 29 |
public class JavaClassHyperlinkDetector extends AbstractHyperlinkDetector { |
| 30 |
|
| 31 |
private static final Pattern javaClassPattern = Pattern.compile("(\\w+\\.)+\\w+", Pattern.CASE_INSENSITIVE); |
| 32 |
|
| 33 |
private static final Pattern stackTracePattern = Pattern.compile("\\S*\\.java:\\d*\\)", Pattern.CASE_INSENSITIVE); |
| 34 |
|
| 35 |
public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) { |
| 36 |
List<IHyperlink> hyperLinks = new ArrayList<IHyperlink>(); |
| 37 |
|
| 38 |
try { |
| 39 |
IDocument document = textViewer.getDocument(); |
| 40 |
if (document == null) { |
| 41 |
return null; |
| 42 |
} |
| 43 |
int offset = region.getOffset(); |
| 44 |
|
| 45 |
IRegion lineInfo = document.getLineInformationOfOffset(offset); |
| 46 |
String line = document.get(lineInfo.getOffset(), lineInfo.getLength()); |
| 47 |
|
| 48 |
// try not to conflict with JavaStackTraceHyperlinkDetector, eclipse 3.4 can display multiple links |
| 49 |
if (stackTracePattern.matcher(line).find()) { |
| 50 |
return null; |
| 51 |
} |
| 52 |
|
| 53 |
Matcher m = javaClassPattern.matcher(line); |
| 54 |
|
| 55 |
while (m.find()) { |
| 56 |
// ignore URL hyperlinks and words like "i.e." |
| 57 |
int beforeStart = lineInfo.getOffset() + m.start() - 1; |
| 58 |
int afterEnd = lineInfo.getOffset() + m.end(); |
| 59 |
if ((beforeStart >= 0 && document.get(beforeStart, 1).equalsIgnoreCase("/")) |
| 60 |
|| ((afterEnd <= lineInfo.getOffset() + lineInfo.getLength()) && document.get(afterEnd, 1) |
| 61 |
.equalsIgnoreCase("."))) { |
| 62 |
continue; |
| 63 |
} |
| 64 |
|
| 65 |
IRegion urlRegion = new Region(lineInfo.getOffset() + m.start(), m.end() - m.start()); |
| 66 |
hyperLinks.add(new JavaClassFileHyperlink(urlRegion, m.group())); |
| 67 |
} |
| 68 |
|
| 69 |
} catch (BadLocationException e) { |
| 70 |
return null; |
| 71 |
} |
| 72 |
|
| 73 |
return hyperLinks.size() > 0 ? hyperLinks.toArray(new IHyperlink[hyperLinks.size()]) : null; |
| 74 |
} |
| 75 |
|
| 76 |
public static void main(String str[]) { |
| 77 |
Pattern javaClassPattern = Pattern.compile("(\\w+\\.)+\\w+", Pattern.CASE_INSENSITIVE); |
| 78 |
Matcher m = javaClassPattern.matcher(" http:// www.eclipse// \n ww.dd ff. e.g."); |
| 79 |
while (m.find()) { |
| 80 |
System.out.println(m.group()); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
} |