001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.cheatsheets;
011:
012: import java.util.HashSet;
013: import java.util.Set;
014:
015: import org.eclipse.core.runtime.IConfigurationElement;
016: import org.eclipse.core.runtime.Platform;
017: import org.eclipse.help.search.XMLSearchParticipant;
018: import org.eclipse.jface.action.Action;
019: import org.eclipse.ui.cheatsheets.OpenCheatSheetAction;
020: import org.eclipse.ui.internal.cheatsheets.composite.parser.ICompositeCheatsheetTags;
021: import org.eclipse.ui.internal.cheatsheets.data.IParserTags;
022: import org.eclipse.ui.internal.cheatsheets.registry.CheatSheetRegistryReader;
023: import org.xml.sax.Attributes;
024:
025: public class CheatsheetSearchParticipant extends XMLSearchParticipant {
026: private static final String INTRO_DESC = "cheatsheet/intro/description"; //$NON-NLS-1$
027:
028: private static final String ITEM_DESC = "cheatsheet/item/description"; //$NON-NLS-1$
029:
030: private static final String CCS_DESC = "compositeCheatsheet/taskGroup/intro"; //$NON-NLS-1$
031:
032: /**
033: * Returns all the documents that this participant knows about. This method
034: * is only used for participants that handle documents outside of the help
035: * system's TOC.
036: *
037: * @return a set of hrefs for documents managed by this participant.
038: */
039: public Set getAllDocuments(String locale) {
040: HashSet set = new HashSet();
041: IConfigurationElement[] elements = Platform
042: .getExtensionRegistry()
043: .getConfigurationElementsFor(
044: ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID
045: + '.'
046: + CheatSheetRegistryReader.CHEAT_SHEET_CONTENT);
047: for (int i = 0; i < elements.length; i++) {
048: IConfigurationElement element = elements[i];
049: if (!element.getName().equals(
050: CheatSheetRegistryReader.TAG_CHEATSHEET))
051: continue;
052: String fileName = element
053: .getAttribute(CheatSheetRegistryReader.ATT_CONTENTFILE);
054: String id = element.getAttribute("id"); //$NON-NLS-1$
055: String pluginId = element.getContributor().getName();
056: if (isExtensionValid(fileName, id, pluginId)) {
057: try {
058: fileName = resolveVariables(pluginId, fileName,
059: locale);
060: set
061: .add("/" + pluginId + "/" + fileName + "?id=" + id); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
062: } catch (Throwable t) {
063: // log and skip
064: CheatSheetPlugin
065: .logError(
066: "Error parsing cheat sheet extension from plug-in " + pluginId + ", id " + id + ", file " + fileName, t); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
067: }
068: }
069: }
070: return set;
071: }
072:
073: public Set getContributingPlugins() {
074: IConfigurationElement[] elements = Platform
075: .getExtensionRegistry()
076: .getConfigurationElementsFor(
077: ICheatSheetResource.CHEAT_SHEET_PLUGIN_ID
078: + '.'
079: + CheatSheetRegistryReader.CHEAT_SHEET_CONTENT);
080: HashSet set = new HashSet();
081: for (int i = 0; i < elements.length; i++) {
082: IConfigurationElement element = elements[i];
083: if (element.getName().equals(
084: CheatSheetRegistryReader.TAG_CHEATSHEET)) {
085: set.add(element.getContributor().getName());
086: }
087: }
088: return set;
089: }
090:
091: protected void handleStartElement(String name,
092: Attributes attributes, IParsedXMLContent data) {
093: if (name.equals(IParserTags.CHEATSHEET)) {
094: data.setTitle(attributes.getValue(IParserTags.TITLE));
095: data.addText(attributes.getValue(IParserTags.TITLE));
096: } else if (name
097: .equals(ICompositeCheatsheetTags.COMPOSITE_CHEATSHEET)) {
098: data.addText(attributes
099: .getValue(ICompositeCheatsheetTags.NAME));
100: data.setTitle(attributes
101: .getValue(ICompositeCheatsheetTags.NAME));
102: } else if (name.equals(IParserTags.ITEM)) {
103: data.addText(attributes.getValue(IParserTags.TITLE));
104: } else if (name.equals(IParserTags.SUBITEM)) {
105: data.addText(attributes.getValue(IParserTags.LABEL));
106: } else if (name.equals(ICompositeCheatsheetTags.TASK)
107: || name.equals(ICompositeCheatsheetTags.TASK_GROUP)) {
108: data.addText(attributes
109: .getValue(ICompositeCheatsheetTags.NAME));
110: }
111: }
112:
113: protected void handleEndElement(String name, IParsedXMLContent data) {
114: }
115:
116: protected void handleText(String text, IParsedXMLContent data) {
117: String stackPath = getElementStackPath();
118: String top = getTopElement();
119: if (IParserTags.INTRO.equals(top)) {
120: data.addText(text);
121: if (stackPath.equalsIgnoreCase(CCS_DESC)) {
122: data.addToSummary(text);
123: }
124: } else if (IParserTags.ON_COMPLETION.equals(top)) {
125: data.addText(text);
126: } else if (stackPath.equalsIgnoreCase(INTRO_DESC)) {
127: data.addText(text);
128: data.addToSummary(text);
129: return;
130: } else if (stackPath.equalsIgnoreCase(ITEM_DESC)) {
131: data.addText(text);
132: return;
133: }
134: }
135:
136: public boolean open(String id) {
137: Action openAction = new OpenCheatSheetAction(id);
138: openAction.run();
139: return true;
140: }
141:
142: private static boolean isExtensionValid(String fileName, String id,
143: String pluginId) {
144: if (fileName.indexOf('\\') != -1) {
145: CheatSheetPlugin
146: .logError(
147: "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); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
148: return false;
149: }
150: return true;
151: }
152: }
|