*** empty log message ***
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / php / HTMLWordExtractor.java
1 /**********************************************************************
2 Copyright (c) 2000, 2002 IBM Corp. and others.
3 All rights reserved. This program and the accompanying materials
4 are made available under the terms of the Common Public License v1.0
5 which accompanies this distribution, and is available at
6 http://www.eclipse.org/legal/cpl-v10.html
7
8 Contributors:
9     IBM Corporation - Initial implementation
10     Klaus Hartlage - www.eclipseproject.de
11 **********************************************************************/
12 package net.sourceforge.phpeclipse.phpeditor.php;
13
14 import org.eclipse.jface.text.BadLocationException;
15 import org.eclipse.jface.text.IDocument;
16 import org.eclipse.swt.graphics.Point;
17
18 /**
19  * Detects HTML words in documents.
20  */
21 public class HTMLWordExtractor {
22
23   /**
24    * Find the location of the word at offset in document.
25    * @returns Point - x is the start position, y is the end position.
26    *    Return null if it is not found.
27    * @param document the document being searched.
28    * @param offset - the position to start searching from.
29    */
30   public static Point findWord(IDocument document, int offset) {
31
32     int start = -1;
33     int end = -1;
34
35     try {
36
37       int position = offset;
38       char character = ' ';
39
40       while (position >= 0) {
41         character = document.getChar(position);
42         if (!Character.isJavaIdentifierPart(character))
43           break;
44         --position;
45       }
46       if ((position > 0) && (character == '<')) {
47         --position;
48       }
49       if ((position > 1) && (character == '/')) {
50         character = document.getChar(position - 1);
51         if (character == '<') {
52           --position;
53           --position;
54         }
55       }
56       if (position==offset) {
57         return null; 
58       }
59
60       start = position;
61
62       position = offset;
63       int length = document.getLength();
64       character = ' ';
65
66       while (position < length) {
67         character = document.getChar(position);
68         if (!Character.isJavaIdentifierPart(character))
69           break;
70         ++position;
71       }
72       if ((position < length) && (character == '>')) {
73         ++position;
74       }
75       start++;
76       end = position;
77
78       if (end > start)
79         return new Point(start, end - start);
80
81     } catch (BadLocationException x) {
82     }
83
84     return null;
85   }
86 }