129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2007 IBM Corporation 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 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Benjamin Muskalla <b.muskalla@gmx.net> - https://bugs.eclipse.org/bugs/show_bug.cgi?id=156433
|
|
11 * Port to the D programming language:
|
|
12 * Frank Benoit <benoit@tionex.de>
|
|
13 *******************************************************************************/
|
|
14 module dwtx.jface.text.hyperlink.URLHyperlinkDetector;
|
|
15
|
131
|
16 import dwtx.jface.text.hyperlink.IHyperlinkPresenterExtension; // packageimport
|
|
17 import dwtx.jface.text.hyperlink.MultipleHyperlinkPresenter; // packageimport
|
|
18 import dwtx.jface.text.hyperlink.HyperlinkManager; // packageimport
|
|
19 import dwtx.jface.text.hyperlink.URLHyperlink; // packageimport
|
|
20 import dwtx.jface.text.hyperlink.IHyperlinkDetectorExtension2; // packageimport
|
|
21 import dwtx.jface.text.hyperlink.IHyperlinkDetector; // packageimport
|
|
22 import dwtx.jface.text.hyperlink.IHyperlinkPresenter; // packageimport
|
|
23 import dwtx.jface.text.hyperlink.DefaultHyperlinkPresenter; // packageimport
|
|
24 import dwtx.jface.text.hyperlink.AbstractHyperlinkDetector; // packageimport
|
|
25 import dwtx.jface.text.hyperlink.IHyperlinkDetectorExtension; // packageimport
|
|
26 import dwtx.jface.text.hyperlink.HyperlinkMessages; // packageimport
|
|
27 import dwtx.jface.text.hyperlink.IHyperlink; // packageimport
|
|
28
|
|
29
|
129
|
30 import dwt.dwthelper.utils;
|
|
31
|
|
32 import java.net.MalformedURLException;
|
|
33 import java.net.URL;
|
|
34 import java.util.StringTokenizer;
|
|
35
|
|
36 import dwtx.jface.text.BadLocationException;
|
|
37 import dwtx.jface.text.IDocument;
|
|
38 import dwtx.jface.text.IRegion;
|
|
39 import dwtx.jface.text.ITextViewer;
|
|
40 import dwtx.jface.text.Region;
|
|
41
|
|
42
|
|
43 /**
|
|
44 * URL hyperlink detector.
|
|
45 *
|
|
46 * @since 3.1
|
|
47 */
|
|
48 public class URLHyperlinkDetector : AbstractHyperlinkDetector {
|
|
49
|
|
50
|
|
51 /**
|
|
52 * Creates a new URL hyperlink detector.
|
|
53 *
|
|
54 * @since 3.2
|
|
55 */
|
|
56 public URLHyperlinkDetector() {
|
|
57 }
|
|
58
|
|
59 /**
|
|
60 * Creates a new URL hyperlink detector.
|
|
61 *
|
|
62 * @param textViewer the text viewer in which to detect the hyperlink
|
|
63 * @deprecated As of 3.2, replaced by {@link URLHyperlinkDetector}
|
|
64 */
|
|
65 public URLHyperlinkDetector(ITextViewer textViewer) {
|
|
66 }
|
|
67
|
|
68 /*
|
|
69 * @see dwtx.jface.text.hyperlink.IHyperlinkDetector#detectHyperlinks(dwtx.jface.text.ITextViewer, dwtx.jface.text.IRegion, bool)
|
|
70 */
|
|
71 public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, bool canShowMultipleHyperlinks) {
|
|
72 if (region is null || textViewer is null)
|
|
73 return null;
|
|
74
|
|
75 IDocument document= textViewer.getDocument();
|
|
76
|
|
77 int offset= region.getOffset();
|
|
78
|
|
79 String urlString= null;
|
|
80 if (document is null)
|
|
81 return null;
|
|
82
|
|
83 IRegion lineInfo;
|
|
84 String line;
|
|
85 try {
|
|
86 lineInfo= document.getLineInformationOfOffset(offset);
|
|
87 line= document.get(lineInfo.getOffset(), lineInfo.getLength());
|
|
88 } catch (BadLocationException ex) {
|
|
89 return null;
|
|
90 }
|
|
91
|
|
92 int offsetInLine= offset - lineInfo.getOffset();
|
|
93
|
|
94 bool startDoubleQuote= false;
|
|
95 int urlOffsetInLine= 0;
|
|
96 int urlLength= 0;
|
|
97
|
|
98 int urlSeparatorOffset= line.indexOf("://"); //$NON-NLS-1$
|
|
99 while (urlSeparatorOffset >= 0) {
|
|
100
|
|
101 // URL protocol (left to "://")
|
|
102 urlOffsetInLine= urlSeparatorOffset;
|
|
103 char ch;
|
|
104 do {
|
|
105 urlOffsetInLine--;
|
|
106 ch= ' ';
|
|
107 if (urlOffsetInLine > -1)
|
|
108 ch= line.charAt(urlOffsetInLine);
|
|
109 startDoubleQuote= ch is '"';
|
|
110 } while (Character.isUnicodeIdentifierStart(ch));
|
|
111 urlOffsetInLine++;
|
|
112
|
|
113 // Right to "://"
|
|
114 StringTokenizer tokenizer= new StringTokenizer(line.substring(urlSeparatorOffset + 3), " \t\n\r\f<>", false); //$NON-NLS-1$
|
|
115 if (!tokenizer.hasMoreTokens())
|
|
116 return null;
|
|
117
|
|
118 urlLength= tokenizer.nextToken().length() + 3 + urlSeparatorOffset - urlOffsetInLine;
|
|
119 if (offsetInLine >= urlOffsetInLine && offsetInLine <= urlOffsetInLine + urlLength)
|
|
120 break;
|
|
121
|
|
122 urlSeparatorOffset= line.indexOf("://", urlSeparatorOffset + 1); //$NON-NLS-1$
|
|
123 }
|
|
124
|
|
125 if (urlSeparatorOffset < 0)
|
|
126 return null;
|
|
127
|
|
128 if (startDoubleQuote) {
|
|
129 int endOffset= -1;
|
|
130 int nextDoubleQuote= line.indexOf('"', urlOffsetInLine);
|
|
131 int nextWhitespace= line.indexOf(' ', urlOffsetInLine);
|
|
132 if (nextDoubleQuote !is -1 && nextWhitespace !is -1)
|
|
133 endOffset= Math.min(nextDoubleQuote, nextWhitespace);
|
|
134 else if (nextDoubleQuote !is -1)
|
|
135 endOffset= nextDoubleQuote;
|
|
136 else if (nextWhitespace !is -1)
|
|
137 endOffset= nextWhitespace;
|
|
138 if (endOffset !is -1)
|
|
139 urlLength= endOffset - urlOffsetInLine;
|
|
140 }
|
|
141
|
|
142 // Set and validate URL string
|
|
143 try {
|
|
144 urlString= line.substring(urlOffsetInLine, urlOffsetInLine + urlLength);
|
|
145 new URL(urlString);
|
|
146 } catch (MalformedURLException ex) {
|
|
147 urlString= null;
|
|
148 return null;
|
|
149 }
|
|
150
|
|
151 IRegion urlRegion= new Region(lineInfo.getOffset() + urlOffsetInLine, urlLength);
|
|
152 return new IHyperlink[] {new URLHyperlink(urlRegion, urlString)};
|
|
153 }
|
|
154
|
|
155 }
|