1 11 12 package org.eclipse.jdt.internal.ui.propertiesfileeditor; 13 14 import java.io.ByteArrayInputStream ; 15 import java.io.IOException ; 16 import java.text.StringCharacterIterator ; 17 import java.util.Properties ; 18 19 import org.eclipse.jface.text.BadLocationException; 20 import org.eclipse.jface.text.BadPartitioningException; 21 import org.eclipse.jface.text.IDocument; 22 import org.eclipse.jface.text.IDocumentExtension3; 23 import org.eclipse.jface.text.IRegion; 24 import org.eclipse.jface.text.ITextViewer; 25 import org.eclipse.jface.text.ITypedRegion; 26 import org.eclipse.jface.text.Region; 27 import org.eclipse.jface.text.hyperlink.AbstractHyperlinkDetector; 28 import org.eclipse.jface.text.hyperlink.IHyperlink; 29 30 import org.eclipse.ui.IEditorSite; 31 import org.eclipse.ui.IFileEditorInput; 32 import org.eclipse.ui.IStorageEditorInput; 33 import org.eclipse.ui.texteditor.ITextEditor; 34 35 36 46 public class PropertyKeyHyperlinkDetector extends AbstractHyperlinkDetector { 47 48 51 public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) { 52 ITextEditor textEditor= (ITextEditor)getAdapter(ITextEditor.class); 53 if (region == null || textEditor == null || canShowMultipleHyperlinks) 54 return null; 55 56 IEditorSite site= textEditor.getEditorSite(); 57 if (site == null) 58 return null; 59 60 int offset= region.getOffset(); 61 if (!checkEnabled(textEditor, offset)) 62 return null; 63 64 ITypedRegion partition= null; 65 try { 66 IStorageEditorInput storageEditorInput= (IStorageEditorInput)textEditor.getEditorInput(); 67 IDocument document= textEditor.getDocumentProvider().getDocument(storageEditorInput); 68 if (document instanceof IDocumentExtension3) 69 partition= ((IDocumentExtension3)document).getPartition(IPropertiesFilePartitions.PROPERTIES_FILE_PARTITIONING, offset, false); 70 71 if (partition == null || !IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType())) { 73 return null; 74 } 75 76 if (offset + region.getLength() > partition.getOffset() + partition.getLength()) { 78 return null; 79 } 80 81 String key= document.get(partition.getOffset(), partition.getLength()); 83 84 String realKey= key.trim(); 85 int delta= key.indexOf(realKey); 86 87 String unicodeKey= getUnicodeString(realKey); 88 Properties properties= new Properties (); 90 properties.load(new ByteArrayInputStream (document.get().getBytes())); 91 if (properties.getProperty(unicodeKey) == null) { 92 return null; 93 } 94 95 return new PropertyKeyHyperlink[] {new PropertyKeyHyperlink(new Region(partition.getOffset() + delta, realKey.length()), realKey, textEditor)}; 96 97 } catch (BadLocationException ex) { 98 return null; 99 } catch (BadPartitioningException ex) { 100 return null; 101 } catch (IOException ex) { 102 return null; 103 } 104 } 105 106 private String getUnicodeString(String key) { 107 StringCharacterIterator iter= new StringCharacterIterator (key); 108 StringBuffer result= new StringBuffer (); 109 while (iter.getIndex() < iter.getEndIndex()) { 110 char c= iter.current(); 111 if (c == '\\') { 112 iter.next(); 113 c= iter.current(); 114 if (c == 'u') { 115 StringBuffer unicode= new StringBuffer (); 116 unicode.append(iter.next()); 117 unicode.append(iter.next()); 118 unicode.append(iter.next()); 119 unicode.append(iter.next()); 120 c= (char)Integer.parseInt(unicode.toString(), 16); 121 } 122 } 123 result.append(c); 124 iter.next(); 125 } 126 return result.toString(); 127 } 128 129 private boolean checkEnabled(ITextEditor textEditor, int offset) { 130 if (offset < 0) 131 return false; 132 133 return textEditor.getEditorInput() instanceof IFileEditorInput; 135 } 136 } 137 | Popular Tags |