KickJava   Java API By Example, From Geeks To Geeks.

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


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.AbstractTocProvider;
20 import org.eclipse.help.ITocContribution;
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 RemoteTocProvider extends AbstractTocProvider {
28
29     private static final String JavaDoc PATH_TOC = "/toc"; //$NON-NLS-1$
30
private static final String JavaDoc PARAM_LANG = "lang"; //$NON-NLS-1$
31

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

36     public RemoteTocProvider() {
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.AbstractTocProvider#getTocContributions(java.lang.String)
46      */

47     public ITocContribution[] getTocContributions(String JavaDoc locale) {
48         if (RemoteHelp.isEnabled()) {
49             InputStream JavaDoc in = null;
50             try {
51                 URL JavaDoc url = RemoteHelp.getURL(PATH_TOC+ '?' + PARAM_LANG + '=' + locale);
52                 in = url.openStream();
53                 RemoteTocParser parser = new RemoteTocParser();
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                 RemoteHelp.setError(e);
60             }
61             catch (Throwable JavaDoc t) {
62                 String JavaDoc msg = "Internal error while reading TOC contents from remote server"; //$NON-NLS-1$
63
HelpBasePlugin.logError(msg, t);
64                 RemoteHelp.setError(t);
65             }
66             finally {
67                 if (in != null) {
68                     try {
69                         in.close();
70                     }
71                     catch (IOException JavaDoc e) {
72                         // nothing more we can do
73
}
74                 }
75             }
76         }
77         return new ITocContribution[0];
78     }
79 }
80
Popular Tags