comparison org.eclipse.jface.text/src/org/eclipse/jface/text/hyperlink/URLHyperlinkDetector.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children dbfb303e8fb0
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
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 org.eclipse.jface.text.hyperlink.URLHyperlinkDetector;
15
16 import org.eclipse.jface.text.hyperlink.IHyperlinkPresenterExtension; // packageimport
17 import org.eclipse.jface.text.hyperlink.MultipleHyperlinkPresenter; // packageimport
18 import org.eclipse.jface.text.hyperlink.HyperlinkManager; // packageimport
19 import org.eclipse.jface.text.hyperlink.URLHyperlink; // packageimport
20 import org.eclipse.jface.text.hyperlink.IHyperlinkDetectorExtension2; // packageimport
21 import org.eclipse.jface.text.hyperlink.IHyperlinkDetector; // packageimport
22 import org.eclipse.jface.text.hyperlink.IHyperlinkPresenter; // packageimport
23 import org.eclipse.jface.text.hyperlink.DefaultHyperlinkPresenter; // packageimport
24 import org.eclipse.jface.text.hyperlink.AbstractHyperlinkDetector; // packageimport
25 import org.eclipse.jface.text.hyperlink.IHyperlinkDetectorExtension; // packageimport
26 import org.eclipse.jface.text.hyperlink.HyperlinkMessages; // packageimport
27 import org.eclipse.jface.text.hyperlink.IHyperlink; // packageimport
28
29 import java.lang.all;
30 import java.util.Set;
31
32 import org.eclipse.dwtxhelper.MalformedURLException;
33 import org.eclipse.dwtxhelper.URL;
34 import org.eclipse.dwtxhelper.StringTokenizer;
35
36 import org.eclipse.jface.text.BadLocationException;
37 import org.eclipse.jface.text.IDocument;
38 import org.eclipse.jface.text.IRegion;
39 import org.eclipse.jface.text.ITextViewer;
40 import org.eclipse.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 this() {
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 this(ITextViewer textViewer) {
66 }
67
68 /*
69 * @see org.eclipse.jface.text.hyperlink.IHyperlinkDetector#detectHyperlinks(org.eclipse.jface.text.ITextViewer, org.eclipse.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 URLHyperlink(urlRegion, urlString)];
153 }
154
155 }