1 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 22 public class XMLContentAssistText { 23 24 private String fText; 25 26 private int fStartOffset; 27 28 private XMLContentAssistText(String text, int startOffset) { 29 fText = text; 30 fStartOffset = startOffset; 31 } 32 33 39 public static XMLContentAssistText parse(int offset, IDocument document) { 40 boolean writeCAText = true; 41 int lastCATextOffset = -1; 42 StringBuffer buffer = new StringBuffer (); 43 int endOffset = offset - 1; 44 char currentChar; 45 46 if (offset <= 0) { 47 return null; 48 } 49 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 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 buffer.append(currentChar); 75 lastCATextOffset = i; 78 } 79 } else if (Character.isWhitespace(currentChar)) { 80 writeCAText = false; 86 } else if (currentChar == '>') { 87 if (buffer.length() > 0) { 90 return new XMLContentAssistText( 91 buffer.reverse().toString(), 92 lastCATextOffset); 93 } 94 return null; 95 } else { 96 return null; 99 } 100 } 101 return null; 103 } 104 105 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 125 public String getText() { 126 return fText; 127 } 128 129 132 public int getStartOffset() { 133 return fStartOffset; 134 } 135 136 139 public String toString() { 140 return "Start Offset: " + fStartOffset + " Text: |" + fText + "|\n"; } 142 } 143 | Popular Tags |