comparison dwtx/jface/text/hyperlink/URLHyperlinkDetector.d @ 129:eb30df5ca28b

Added JFace Text sources
author Frank Benoit <benoit@tionex.de>
date Sat, 23 Aug 2008 19:10:48 +0200
parents
children c4fb132a086c
comparison
equal deleted inserted replaced
128:8df1d4193877 129:eb30df5ca28b
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
16 import dwt.dwthelper.utils;
17
18 import java.net.MalformedURLException;
19 import java.net.URL;
20 import java.util.StringTokenizer;
21
22 import dwtx.jface.text.BadLocationException;
23 import dwtx.jface.text.IDocument;
24 import dwtx.jface.text.IRegion;
25 import dwtx.jface.text.ITextViewer;
26 import dwtx.jface.text.Region;
27
28
29 /**
30 * URL hyperlink detector.
31 *
32 * @since 3.1
33 */
34 public class URLHyperlinkDetector : AbstractHyperlinkDetector {
35
36
37 /**
38 * Creates a new URL hyperlink detector.
39 *
40 * @since 3.2
41 */
42 public URLHyperlinkDetector() {
43 }
44
45 /**
46 * Creates a new URL hyperlink detector.
47 *
48 * @param textViewer the text viewer in which to detect the hyperlink
49 * @deprecated As of 3.2, replaced by {@link URLHyperlinkDetector}
50 */
51 public URLHyperlinkDetector(ITextViewer textViewer) {
52 }
53
54 /*
55 * @see dwtx.jface.text.hyperlink.IHyperlinkDetector#detectHyperlinks(dwtx.jface.text.ITextViewer, dwtx.jface.text.IRegion, bool)
56 */
57 public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, bool canShowMultipleHyperlinks) {
58 if (region is null || textViewer is null)
59 return null;
60
61 IDocument document= textViewer.getDocument();
62
63 int offset= region.getOffset();
64
65 String urlString= null;
66 if (document is null)
67 return null;
68
69 IRegion lineInfo;
70 String line;
71 try {
72 lineInfo= document.getLineInformationOfOffset(offset);
73 line= document.get(lineInfo.getOffset(), lineInfo.getLength());
74 } catch (BadLocationException ex) {
75 return null;
76 }
77
78 int offsetInLine= offset - lineInfo.getOffset();
79
80 bool startDoubleQuote= false;
81 int urlOffsetInLine= 0;
82 int urlLength= 0;
83
84 int urlSeparatorOffset= line.indexOf("://"); //$NON-NLS-1$
85 while (urlSeparatorOffset >= 0) {
86
87 // URL protocol (left to "://")
88 urlOffsetInLine= urlSeparatorOffset;
89 char ch;
90 do {
91 urlOffsetInLine--;
92 ch= ' ';
93 if (urlOffsetInLine > -1)
94 ch= line.charAt(urlOffsetInLine);
95 startDoubleQuote= ch is '"';
96 } while (Character.isUnicodeIdentifierStart(ch));
97 urlOffsetInLine++;
98
99 // Right to "://"
100 StringTokenizer tokenizer= new StringTokenizer(line.substring(urlSeparatorOffset + 3), " \t\n\r\f<>", false); //$NON-NLS-1$
101 if (!tokenizer.hasMoreTokens())
102 return null;
103
104 urlLength= tokenizer.nextToken().length() + 3 + urlSeparatorOffset - urlOffsetInLine;
105 if (offsetInLine >= urlOffsetInLine && offsetInLine <= urlOffsetInLine + urlLength)
106 break;
107
108 urlSeparatorOffset= line.indexOf("://", urlSeparatorOffset + 1); //$NON-NLS-1$
109 }
110
111 if (urlSeparatorOffset < 0)
112 return null;
113
114 if (startDoubleQuote) {
115 int endOffset= -1;
116 int nextDoubleQuote= line.indexOf('"', urlOffsetInLine);
117 int nextWhitespace= line.indexOf(' ', urlOffsetInLine);
118 if (nextDoubleQuote !is -1 && nextWhitespace !is -1)
119 endOffset= Math.min(nextDoubleQuote, nextWhitespace);
120 else if (nextDoubleQuote !is -1)
121 endOffset= nextDoubleQuote;
122 else if (nextWhitespace !is -1)
123 endOffset= nextWhitespace;
124 if (endOffset !is -1)
125 urlLength= endOffset - urlOffsetInLine;
126 }
127
128 // Set and validate URL string
129 try {
130 urlString= line.substring(urlOffsetInLine, urlOffsetInLine + urlLength);
131 new URL(urlString);
132 } catch (MalformedURLException ex) {
133 urlString= null;
134 return null;
135 }
136
137 IRegion urlRegion= new Region(lineInfo.getOffset() + urlOffsetInLine, urlLength);
138 return new IHyperlink[] {new URLHyperlink(urlRegion, urlString)};
139 }
140
141 }