1 12 package org.eclipse.jface.text.hyperlink; 13 14 import java.net.MalformedURLException ; 15 import java.net.URL ; 16 import java.util.StringTokenizer ; 17 18 import org.eclipse.jface.text.BadLocationException; 19 import org.eclipse.jface.text.IDocument; 20 import org.eclipse.jface.text.IRegion; 21 import org.eclipse.jface.text.ITextViewer; 22 import org.eclipse.jface.text.Region; 23 24 25 30 public class URLHyperlinkDetector extends AbstractHyperlinkDetector { 31 32 33 38 public URLHyperlinkDetector() { 39 } 40 41 47 public URLHyperlinkDetector(ITextViewer textViewer) { 48 } 49 50 53 public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) { 54 if (region == null || textViewer == null) 55 return null; 56 57 IDocument document= textViewer.getDocument(); 58 59 int offset= region.getOffset(); 60 61 String urlString= null; 62 if (document == null) 63 return null; 64 65 IRegion lineInfo; 66 String line; 67 try { 68 lineInfo= document.getLineInformationOfOffset(offset); 69 line= document.get(lineInfo.getOffset(), lineInfo.getLength()); 70 } catch (BadLocationException ex) { 71 return null; 72 } 73 74 int offsetInLine= offset - lineInfo.getOffset(); 75 76 boolean startDoubleQuote= false; 77 int urlOffsetInLine= 0; 78 int urlLength= 0; 79 80 int urlSeparatorOffset= line.indexOf("://"); while (urlSeparatorOffset >= 0) { 82 83 urlOffsetInLine= urlSeparatorOffset; 85 char ch; 86 do { 87 urlOffsetInLine--; 88 ch= ' '; 89 if (urlOffsetInLine > -1) 90 ch= line.charAt(urlOffsetInLine); 91 startDoubleQuote= ch == '"'; 92 } while (Character.isUnicodeIdentifierStart(ch)); 93 urlOffsetInLine++; 94 95 StringTokenizer tokenizer= new StringTokenizer (line.substring(urlSeparatorOffset + 3), " \t\n\r\f<>", false); if (!tokenizer.hasMoreTokens()) 98 return null; 99 100 urlLength= tokenizer.nextToken().length() + 3 + urlSeparatorOffset - urlOffsetInLine; 101 if (offsetInLine >= urlOffsetInLine && offsetInLine <= urlOffsetInLine + urlLength) 102 break; 103 104 urlSeparatorOffset= line.indexOf("://", urlSeparatorOffset + 1); } 106 107 if (urlSeparatorOffset < 0) 108 return null; 109 110 if (startDoubleQuote) { 111 int endOffset= -1; 112 int nextDoubleQuote= line.indexOf('"', urlOffsetInLine); 113 int nextWhitespace= line.indexOf(' ', urlOffsetInLine); 114 if (nextDoubleQuote != -1 && nextWhitespace != -1) 115 endOffset= Math.min(nextDoubleQuote, nextWhitespace); 116 else if (nextDoubleQuote != -1) 117 endOffset= nextDoubleQuote; 118 else if (nextWhitespace != -1) 119 endOffset= nextWhitespace; 120 if (endOffset != -1) 121 urlLength= endOffset - urlOffsetInLine; 122 } 123 124 try { 126 urlString= line.substring(urlOffsetInLine, urlOffsetInLine + urlLength); 127 new URL (urlString); 128 } catch (MalformedURLException ex) { 129 urlString= null; 130 return null; 131 } 132 133 IRegion urlRegion= new Region(lineInfo.getOffset() + urlOffsetInLine, urlLength); 134 return new IHyperlink[] {new URLHyperlink(urlRegion, urlString)}; 135 } 136 137 } 138 | Popular Tags |