KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > base > remote > RemoteIndexProvider


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.help.internal.base.remote;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.net.URL JavaDoc;
16
17 import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener;
18 import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent;
19 import org.eclipse.help.AbstractIndexProvider;
20 import org.eclipse.help.IIndexContribution;
21 import org.eclipse.help.internal.base.HelpBasePlugin;
22
23 /*
24  * Provides the TOC data that is located on the remote infocenter, if the system
25  * is configured for remote help. If not, returns no contributions.
26  */

27 public class RemoteIndexProvider extends AbstractIndexProvider {
28
29     private static final String JavaDoc PATH_INDEX = "/index"; //$NON-NLS-1$
30
private static final String JavaDoc PARAM_LANG = "lang"; //$NON-NLS-1$
31

32     /*
33      * Constructs a new remote index provider, which listens for remote
34      * help preference changes.
35      */

36     public RemoteIndexProvider() {
37         RemoteHelp.addPreferenceChangeListener(new IPreferenceChangeListener() {
38             public void preferenceChange(PreferenceChangeEvent event) {
39                 contentChanged();
40             }
41         });
42     }
43     
44     /* (non-Javadoc)
45      * @see org.eclipse.help.AbstractIndexProvider#getIndexContributions(String)
46      */

47     public IIndexContribution[] getIndexContributions(String JavaDoc locale) {
48         if (RemoteHelp.isEnabled()) {
49             InputStream JavaDoc in = null;
50             try {
51                 URL JavaDoc url = RemoteHelp.getURL(PATH_INDEX + '?' + PARAM_LANG + '=' + locale);
52                 in = url.openStream();
53                 RemoteIndexParser parser = new RemoteIndexParser();
54                 return parser.parse(in);
55             }
56             catch (IOException JavaDoc e) {
57                 String JavaDoc msg = "I/O error while trying to contact the remote help server"; //$NON-NLS-1$
58
HelpBasePlugin.logError(msg, e);
59             }
60             catch (Throwable JavaDoc t) {
61                 String JavaDoc msg = "Internal error while reading index contents from remote server"; //$NON-NLS-1$
62
HelpBasePlugin.logError(msg, t);
63             }
64             finally {
65                 if (in != null) {
66                     try {
67                         in.close();
68                     }
69                     catch (IOException JavaDoc e) {
70                         // nothing more we can do
71
}
72                 }
73             }
74         }
75         return new IIndexContribution[0];
76     }
77 }
78
Popular Tags