KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > text > hyperlink > URLHyperlinkDetector


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  *******************************************************************************/

12 package org.eclipse.jface.text.hyperlink;
13
14 import java.net.MalformedURLException JavaDoc;
15 import java.net.URL JavaDoc;
16 import java.util.StringTokenizer JavaDoc;
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 /**
26  * URL hyperlink detector.
27  *
28  * @since 3.1
29  */

30 public class URLHyperlinkDetector extends AbstractHyperlinkDetector {
31
32
33     /**
34      * Creates a new URL hyperlink detector.
35      *
36      * @since 3.2
37      */

38     public URLHyperlinkDetector() {
39     }
40     
41     /**
42      * Creates a new URL hyperlink detector.
43      *
44      * @param textViewer the text viewer in which to detect the hyperlink
45      * @deprecated As of 3.2, replaced by {@link URLHyperlinkDetector}
46      */

47     public URLHyperlinkDetector(ITextViewer textViewer) {
48     }
49
50     /*
51      * @see org.eclipse.jface.text.hyperlink.IHyperlinkDetector#detectHyperlinks(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion, boolean)
52      */

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 JavaDoc urlString= null;
62         if (document == null)
63             return null;
64
65         IRegion lineInfo;
66         String JavaDoc 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("://"); //$NON-NLS-1$
81
while (urlSeparatorOffset >= 0) {
82
83             // URL protocol (left to "://")
84
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             // Right to "://"
96
StringTokenizer JavaDoc tokenizer= new StringTokenizer JavaDoc(line.substring(urlSeparatorOffset + 3), " \t\n\r\f<>", false); //$NON-NLS-1$
97
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); //$NON-NLS-1$
105
}
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         // Set and validate URL string
125
try {
126             urlString= line.substring(urlOffsetInLine, urlOffsetInLine + urlLength);
127             new URL JavaDoc(urlString);
128         } catch (MalformedURLException JavaDoc 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