1 11 package org.eclipse.ui.internal.cheatsheets; 12 13 import java.util.HashSet ; 14 import java.util.Set ; 15 16 import org.eclipse.core.runtime.IConfigurationElement; 17 import org.eclipse.core.runtime.Platform; 18 import org.eclipse.help.search.XMLSearchParticipant; 19 import org.eclipse.jface.action.Action; 20 import org.eclipse.ui.cheatsheets.OpenCheatSheetAction; 21 import org.eclipse.ui.internal.cheatsheets.composite.parser.ICompositeCheatsheetTags; 22 import org.eclipse.ui.internal.cheatsheets.data.IParserTags; 23 import org.eclipse.ui.internal.cheatsheets.registry.CheatSheetRegistryReader; 24 import org.xml.sax.Attributes ; 25 26 public class CheatsheetSearchParticipant extends XMLSearchParticipant { 27 private static final String INTRO_DESC = "cheatsheet/intro/description"; 29 private static final String ITEM_DESC = "cheatsheet/item/description"; 31 private static final String CCS_DESC = "compositeCheatsheet/taskGroup/intro"; 33 40 public Set getAllDocuments(String locale) { 41 HashSet set = new HashSet (); 42 IConfigurationElement[] elements = Platform.getExtensionRegistry() 43 .getConfigurationElementsFor( 44 ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID + '.' 45 + CheatSheetRegistryReader.CHEAT_SHEET_CONTENT); 46 for (int i = 0; i < elements.length; i++) { 47 IConfigurationElement element = elements[i]; 48 if (!element.getName().equals(CheatSheetRegistryReader.TAG_CHEATSHEET)) 49 continue; 50 String fileName = element.getAttribute(CheatSheetRegistryReader.ATT_CONTENTFILE); 51 String id = element.getAttribute("id"); String pluginId = element.getContributor().getName(); 53 if (isExtensionValid(fileName, id, pluginId)) { 54 try { 55 fileName = resolveVariables(pluginId, fileName, locale); 56 set.add("/" + pluginId + "/" + fileName + "?id=" + id); } 58 catch (Throwable t) { 59 CheatSheetPlugin.logError("Error parsing cheat sheet extension from plug-in " + pluginId + ", id " + id + ", file " + fileName, t); } 62 } 63 } 64 return set; 65 } 66 67 public Set getContributingPlugins() { 68 IConfigurationElement[] elements = Platform.getExtensionRegistry() 69 .getConfigurationElementsFor( 70 ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID + '.' 71 + CheatSheetRegistryReader.CHEAT_SHEET_CONTENT); 72 HashSet set = new HashSet (); 73 for (int i = 0; i < elements.length; i++) { 74 IConfigurationElement element = elements[i]; 75 if (element.getName().equals(CheatSheetRegistryReader.TAG_CHEATSHEET)) { 76 set.add(element.getContributor().getName()); 77 } 78 } 79 return set; 80 } 81 82 protected void handleStartElement(String name, Attributes attributes, 83 IParsedXMLContent data) { 84 if (name.equals(IParserTags.CHEATSHEET)) { 85 data.setTitle(attributes.getValue(IParserTags.TITLE)); 86 data.addText(attributes.getValue(IParserTags.TITLE)); 87 } else if (name.equals(ICompositeCheatsheetTags.COMPOSITE_CHEATSHEET)) { 88 data.addText(attributes.getValue(ICompositeCheatsheetTags.NAME)); 89 data.setTitle(attributes.getValue(ICompositeCheatsheetTags.NAME)); 90 } else if (name.equals(IParserTags.ITEM)) { 91 data.addText(attributes.getValue(IParserTags.TITLE)); 92 } else if (name.equals(IParserTags.SUBITEM)) { 93 data.addText(attributes.getValue(IParserTags.LABEL)); 94 } else if (name.equals(ICompositeCheatsheetTags.TASK ) 95 || name.equals(ICompositeCheatsheetTags.TASK_GROUP)) { 96 data.addText(attributes.getValue(ICompositeCheatsheetTags.NAME)); 97 } 98 } 99 100 protected void handleEndElement(String name, IParsedXMLContent data) { 101 } 102 103 protected void handleText(String text, IParsedXMLContent data) { 104 String stackPath = getElementStackPath(); 105 String top = getTopElement(); 106 if (IParserTags.INTRO.equals(top)) { 107 data.addText(text); 108 if (stackPath.equalsIgnoreCase(CCS_DESC)) { 109 data.addToSummary(text); 110 } 111 } else if (IParserTags.ON_COMPLETION.equals(top)) { 112 data.addText(text); 113 } else if (stackPath.equalsIgnoreCase(INTRO_DESC)) { 114 data.addText(text); 115 data.addToSummary(text); 116 return; 117 } else if (stackPath.equalsIgnoreCase(ITEM_DESC)) { 118 data.addText(text); 119 return; 120 } 121 } 122 123 public boolean open(String id) { 124 Action openAction = new OpenCheatSheetAction(id); 125 openAction.run(); 126 return true; 127 } 128 129 private static boolean isExtensionValid(String fileName, String id, String pluginId) { 130 if (fileName.indexOf('\\') != -1) { 131 CheatSheetPlugin.logError("Error in cheat sheet extension id " + id + " from plug-in " + pluginId + ": path should not contain back-slashes (\\): " + fileName + ". This cheat sheet will not be indexed for searching.", null); return false; 133 } 134 return true; 135 } 136 } 137 | Popular Tags |