KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > contentassist > XMLContentAssistText


1 /*******************************************************************************
2  * Copyright (c) 2006 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  *******************************************************************************/

11
12 package org.eclipse.pde.internal.ui.editor.contentassist;
13
14 import org.eclipse.jface.text.BadLocationException;
15 import org.eclipse.jface.text.IDocument;
16
17 /**
18  * Content assist text is any contiguous segment of text that can be
19  * construed as the beginning of a element document node prior to invoking
20  * content assist at the end of it.
21  */

22 public class XMLContentAssistText {
23     
24     private String JavaDoc fText;
25     
26     private int fStartOffset;
27     
28     private XMLContentAssistText(String JavaDoc text, int startOffset) {
29         fText = text;
30         fStartOffset = startOffset;
31     }
32     
33     /**
34      * Parses document for content assist text.
35      * @param offset The document offset to start scanning backward from
36      * @param document The document
37      * @return new content assist text if found; otherwise, returns null.
38      */

39     public static XMLContentAssistText parse(int offset, IDocument document) {
40         boolean writeCAText = true;
41         int lastCATextOffset = -1;
42         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
43         int endOffset = offset - 1;
44         char currentChar;
45         
46         if (offset <= 0) {
47             return null;
48         }
49         // Performance enhancement
50
// Ensure the first character is valid content assist text
51
try {
52             currentChar = document.getChar(endOffset);
53         } catch (BadLocationException e) {
54             return null;
55         }
56         if (isContentAssistText(currentChar)) {
57             buffer.append(currentChar);
58             lastCATextOffset = endOffset;
59         } else {
60             return null;
61         }
62         // Scan backwards from specified offset until we find a right angle
63
// bracket
64
for (int i = endOffset - 1; i > 0; i--) {
65             try {
66                 currentChar = document.getChar(i);
67             } catch (BadLocationException e) {
68                 return null;
69             }
70             if (isContentAssistText(currentChar)) {
71                 if (writeCAText) {
72                     // Accumulate the contiguous segment of content assist
73
// text
74
buffer.append(currentChar);
75                     // Track the start offset of the contiguous segment of
76
// content assist text
77
lastCATextOffset = i;
78                 }
79             } else if (Character.isWhitespace(currentChar)) {
80                 // We found whitespace. This represents the contiguous text
81
// boundary. Do not write anything else to the buffer.
82
// Continue scanning backwards to make sure we find a right
83
// angle bracket to validate what we have in the buffer is
84
// indeed valid content assist text
85
writeCAText = false;
86             } else if (currentChar == '>') {
87                 // We found the right angle bracket, if there is anything in
88
// the buffer it is valid content assist text
89
if (buffer.length() > 0) {
90                     return new XMLContentAssistText(
91                             buffer.reverse().toString(),
92                             lastCATextOffset);
93                 }
94                 return null;
95             } else {
96                 // We found an invalid content assist character
97
// Anything we have in the buffer is garbage
98
return null;
99             }
100         }
101         // We should never reach here
102
return null;
103     }
104     
105     /**
106      * Determines whether a character is a valid XML element name character
107      * @param c A character
108      * @return True if the character is valid content assist text; Otherwise,
109      * returns false.
110      */

111     private static boolean isContentAssistText(char c) {
112         if ((Character.isLetterOrDigit(c)) ||
113                 (c == '.') ||
114                 (c == '-') ||
115                 (c == '_') ||
116                 (c == ':')) {
117             return true;
118         }
119         return false;
120     }
121     
122     /**
123      * @return the fText
124      */

125     public String JavaDoc getText() {
126         return fText;
127     }
128     
129     /**
130      * @return the fStartOffset
131      */

132     public int getStartOffset() {
133         return fStartOffset;
134     }
135     
136     /* (non-Javadoc)
137      * @see java.lang.Object#toString()
138      */

139     public String JavaDoc toString() {
140         return "Start Offset: " + fStartOffset + " Text: |" + fText + "|\n"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
141
}
142 }
143
Popular Tags