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.intro.impl.model;
011:
012: import java.io.StringReader;
013: import java.net.URL;
014: import java.util.HashSet;
015: import java.util.Set;
016:
017: import org.apache.lucene.document.Document;
018: import org.apache.lucene.document.Field;
019: import org.eclipse.core.runtime.IConfigurationElement;
020: import org.eclipse.core.runtime.IProduct;
021: import org.eclipse.core.runtime.IStatus;
022: import org.eclipse.core.runtime.Platform;
023: import org.eclipse.core.runtime.Status;
024: import org.eclipse.help.search.ISearchIndex;
025: import org.eclipse.help.search.LuceneSearchParticipant;
026: import org.eclipse.ui.PlatformUI;
027: import org.eclipse.ui.internal.intro.impl.IntroPlugin;
028: import org.eclipse.ui.internal.intro.impl.model.loader.ExtensionPointManager;
029: import org.eclipse.ui.intro.IIntroManager;
030: import org.eclipse.ui.intro.IIntroPart;
031: import org.eclipse.ui.intro.config.IIntroURL;
032: import org.eclipse.ui.intro.config.IntroURLFactory;
033: import org.osgi.framework.Bundle;
034:
035: /**
036: * An implementation of the Lucene search participant that adds Welcome content into the local help
037: * index so that it can be searched.
038: *
039: */
040:
041: public class IntroSearchParticipant extends LuceneSearchParticipant {
042:
043: private IntroModelRoot model;
044:
045: private class TitleAndSummary {
046: String title;
047: String summary;
048: }
049:
050: /*
051: * (non-Javadoc)
052: *
053: * @see org.eclipse.help.search.LuceneSearchParticipant#getContributingPlugins()
054: */
055: public Set getContributingPlugins() {
056: IConfigurationElement[] elements = Platform
057: .getExtensionRegistry().getConfigurationElementsFor(
058: "org.eclipse.ui.intro.config"); //$NON-NLS-1$
059: HashSet set = new HashSet();
060: for (int i = 0; i < elements.length; i++) {
061: IConfigurationElement element = elements[i];
062: if (!element.getName().equals("config")) //$NON-NLS-1$
063: continue;
064: set.add(element.getContributor().getName());
065: }
066: elements = Platform.getExtensionRegistry()
067: .getConfigurationElementsFor(
068: "org.eclipse.ui.intro.configExtension"); //$NON-NLS-1$
069: for (int i = 0; i < elements.length; i++) {
070: IConfigurationElement element = elements[i];
071: if (!element.getName().equals("configExtension")) //$NON-NLS-1$
072: continue;
073: set.add(element.getContributor().getName());
074: }
075: return set;
076: }
077:
078: /*
079: * (non-Javadoc)
080: *
081: * @see org.eclipse.help.search.LuceneSearchParticipant#getAllDocuments(java.lang.String)
082: */
083: public Set getAllDocuments(String locale) {
084: HashSet set = new HashSet();
085: IProduct product = Platform.getProduct();
086: if (product == null) {
087: return set;
088: }
089: String productId = product.getId();
090: IConfigurationElement[] elements = Platform
091: .getExtensionRegistry().getConfigurationElementsFor(
092: "org.eclipse.ui.intro"); //$NON-NLS-1$
093: String targetIntroId = null;
094: for (int i = 0; i < elements.length; i++) {
095: IConfigurationElement element = elements[i];
096: if (element.getName().equals("introProductBinding")) { //$NON-NLS-1$
097: String pid = element.getAttribute("productId"); //$NON-NLS-1$
098: String iid = element.getAttribute("introId"); //$NON-NLS-1$
099: if (productId.equals(pid)) {
100: targetIntroId = iid;
101: break;
102: }
103: }
104: }
105: if (targetIntroId == null)
106: return set;
107: elements = Platform.getExtensionRegistry()
108: .getConfigurationElementsFor(
109: "org.eclipse.ui.intro.config"); //$NON-NLS-1$
110: IConfigurationElement config = null;
111: for (int i = 0; i < elements.length; i++) {
112: IConfigurationElement element = elements[i];
113: if (element.getName().equals("config")) { //$NON-NLS-1$
114: String iid = element.getAttribute("introId"); //$NON-NLS-1$
115: if (targetIntroId.equals(iid)) {
116: config = element;
117: break;
118: }
119: }
120: }
121: if (config == null)
122: return set;
123: String configId = config.getAttribute("id"); //$NON-NLS-1$
124: ExtensionPointManager extensionPointManager = IntroPlugin
125: .getDefault().getExtensionPointManager();
126: model = extensionPointManager.getModel(configId);
127: if (model != null && model.hasValidConfig())
128: loadFromModel(model, set, locale);
129: return set;
130: }
131:
132: private void loadFromModel(IntroModelRoot model, HashSet set,
133: String locale) {
134: IntroPage[] pages = model.getPages();
135: for (int i = 0; i < pages.length; i++) {
136: IntroPage page = pages[i];
137: Bundle bundle = page.getBundle();
138: String bundleId = bundle.getSymbolicName();
139: String content = page.getRawContent();
140: String pageId = page.getId();
141: String href;
142: if (content != null)
143: href = resolveVariables(bundleId, content, locale);
144: else
145: href = pageId;
146: set.add("/" + bundleId + "/" + href + "?id=" + pageId); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
147: }
148: }
149:
150: /*
151: * (non-Javadoc)
152: *
153: * @see org.eclipse.help.search.LuceneSearchParticipant#addDocument(java.lang.String,
154: * java.lang.String, java.net.URL, java.lang.String, java.lang.String,
155: * org.apache.lucene.document.Document)
156: */
157: public IStatus addDocument(ISearchIndex index, String pluginId,
158: String name, URL url, String id, Document doc) {
159: if (model == null)
160: return Status.CANCEL_STATUS;
161: IntroPage page = getPage(id);
162: if (page == null)
163: return Status.CANCEL_STATUS;
164: return addPage(index, pluginId, name, url, page, doc);
165: }
166:
167: private IntroPage getPage(String id) {
168: IntroPage[] pages = model.getPages();
169: for (int i = 0; i < pages.length; i++) {
170: if (pages[i].getId().equals(id))
171: return pages[i];
172: }
173: return null;
174: }
175:
176: private IStatus addPage(ISearchIndex index, String pluginId,
177: String name, URL url, IntroPage page, Document doc) {
178: AbstractIntroElement[] children = page.getChildren();
179: if (children.length > 0) {
180: StringBuffer buf = new StringBuffer();
181: TitleAndSummary titleSummary = new TitleAndSummary();
182: addChildren(children, buf, doc, titleSummary);
183: String contents = buf.toString();
184: if (titleSummary.title != null) {
185: addTitle(titleSummary.title, doc);
186: }
187: if (titleSummary.summary != null) {
188: doc
189: .add(new Field(
190: "summary", titleSummary.summary, Field.Store.YES, Field.Index.NO)); //$NON-NLS-1$
191: }
192: doc.add(new Field("contents", new StringReader(contents))); //$NON-NLS-1$
193: doc.add(new Field(
194: "exact_contents", new StringReader(contents))); //$NON-NLS-1$
195: return Status.OK_STATUS;
196: }
197: // delegate to the help system
198: return index
199: .addDocument(pluginId, name, url, page.getId(), doc);
200: }
201:
202: private void addChildren(AbstractIntroElement[] children,
203: StringBuffer buf, Document doc, TitleAndSummary titleSummary) {
204: for (int i = 0; i < children.length; i++) {
205: AbstractIntroElement child = children[i];
206: if (child instanceof IntroLink) {
207: String text = ((IntroLink) child).getLabel();
208: appendNewText(buf, text);
209: } else if (child instanceof IntroText) {
210: IntroText childIntroText = (IntroText) child;
211: appendNewText(buf, childIntroText.getText());
212: String childId = childIntroText.getId();
213: String title = null;
214: if ("page-title".equals(childId)) { //$NON-NLS-1$
215: title = childIntroText.getText();
216: } else if (child instanceof IntroPageTitle) {
217: title = ((IntroPageTitle) child).getTitle();
218: }
219: if (title != null) {
220: titleSummary.title = title;
221: }
222: if ("page-description".equals(childId)) { //$NON-NLS-1$
223: titleSummary.summary = childIntroText.getText();
224: }
225: }
226: if (child instanceof AbstractIntroContainer) {
227: AbstractIntroContainer container = (AbstractIntroContainer) child;
228: if (!"navigation-links".equals(container.getId())) { //$NON-NLS-1$
229: AbstractIntroElement[] cc = container.getChildren();
230: addChildren(cc, buf, doc, titleSummary);
231: }
232: }
233: }
234: }
235:
236: private void appendNewText(StringBuffer buf, String text) {
237: if (text == null)
238: return;
239: if (buf.length() > 0)
240: buf.append(" "); //$NON-NLS-1$
241: buf.append(text);
242: }
243:
244: /*
245: * (non-Javadoc)
246: *
247: * @see org.eclipse.help.search.LuceneSearchParticipant#clear()
248: */
249: public void clear() {
250: model = null;
251: }
252:
253: /*
254: * (non-Javadoc)
255: *
256: * @see org.eclipse.help.search.LuceneSearchParticipant#open(java.lang.String)
257: */
258: public boolean open(String id) {
259: IIntroManager introManager = PlatformUI.getWorkbench()
260: .getIntroManager();
261: IIntroPart intro = introManager.showIntro(PlatformUI
262: .getWorkbench().getActiveWorkbenchWindow(), false);
263: if (intro == null)
264: return false;
265: IIntroURL url = IntroURLFactory
266: .createIntroURL("http://org.eclipse.ui.intro/showPage?id=" + id); //$NON-NLS-1$
267: return url.execute();
268: }
269: }
|