KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > DynamicContentProducer


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

9
10 package org.eclipse.help.internal;
11
12 import java.io.IOException JavaDoc;
13 import java.io.InputStream JavaDoc;
14 import java.util.Locale JavaDoc;
15
16 import org.eclipse.core.runtime.Platform;
17 import org.eclipse.core.runtime.content.IContentDescriber;
18 import org.eclipse.help.IHelpContentProducer;
19 import org.eclipse.help.internal.util.ResourceLocator;
20 import org.eclipse.help.internal.xhtml.UAContentParser;
21 import org.eclipse.help.internal.xhtml.UATransformManager;
22 import org.eclipse.help.internal.xhtml.XHTMLContentDescriber;
23 import org.eclipse.help.internal.xhtml.XHTMLSupport;
24 import org.osgi.framework.Bundle;
25 import org.w3c.dom.Document JavaDoc;
26
27 /**
28  * The entry for all the dynamic format support. Depending on the file extension, this content
29  * producer delegates to content producers that can handle one format.
30  */

31
32 public class DynamicContentProducer implements IHelpContentProducer {
33
34     private IContentDescriber xhtmlDescriber;
35
36     public InputStream JavaDoc getInputStream(String JavaDoc pluginID, String JavaDoc href, Locale JavaDoc locale) {
37         if (isXHTML(pluginID, href, locale)) {
38             String JavaDoc file = href;
39             int qloc = href.indexOf('?');
40             if (qloc != -1) {
41                 file = href.substring(0, qloc);
42             }
43
44             /*
45              * Filtering can be turned off when, for example,
46              * indexing documents.
47              */

48             boolean filter = true;
49             if (qloc != -1 && qloc < href.length() - 1) {
50                 String JavaDoc query = href.substring(qloc + 1);
51                 filter = (query.indexOf("filter=false") == -1); //$NON-NLS-1$
52
}
53             return openXHTMLFromPlugin(pluginID, file, locale.toString(), filter);
54         }
55         return null;
56     }
57
58     /**
59      * Returns whether or not the given href is pointing to an XHTML
60      * document (it can be within a .html file).
61      *
62      * @param pluginID the id of the plugin containing the document
63      * @param href the href to the document
64      * @param locale the document's locale
65      * @return whether or not the document is XHTML
66      */

67     private boolean isXHTML(String JavaDoc pluginID, String JavaDoc href, Locale JavaDoc locale) {
68         String JavaDoc file = href;
69         int qloc = href.indexOf('?');
70         if (qloc != -1) {
71             file = href.substring(0, qloc);
72         }
73         if (xhtmlDescriber == null) {
74             xhtmlDescriber = new XHTMLContentDescriber();
75         }
76         // first open it to peek inside to see whether it's really XHTML
77
InputStream JavaDoc in = null;
78         try {
79             in = openXHTMLFromPluginRaw(pluginID, file, locale.toString());
80             if (in != null) {
81                 return (xhtmlDescriber.describe(in, null) == IContentDescriber.VALID);
82             }
83         }
84         catch (Exception JavaDoc e) {
85             HelpPlugin.logError("An error occured in DynamicContentProducer while trying to determine the content type", e); //$NON-NLS-1$
86
}
87         finally {
88             if (in != null) {
89                 try {
90                     in.close();
91                 }
92                 catch (IOException JavaDoc e) {
93                     // nothing we can do
94
}
95             }
96         }
97         return false;
98     }
99     
100     /**
101      * Opens an input stream to an xhtml file contained in a plugin or in a doc.zip
102      * in the plugin. This includes includes OS, WS and NL lookup.
103      *
104      * @param pluginDesc
105      * the plugin description of the plugin that contains the file you are trying to find
106      * @param file
107      * the relative path of the file to find
108      * @param locale
109      * the locale used as an override or <code>null</code> to use the default locale
110      * @param filter
111      * whether or not the content should be filtered
112      *
113      * @return an InputStream to the file or <code>null</code> if the file wasn't found
114      */

115     private static InputStream JavaDoc openXHTMLFromPlugin(String JavaDoc pluginID, String JavaDoc file, String JavaDoc locale, boolean filter) {
116         InputStream JavaDoc inputStream = openXHTMLFromPluginRaw(pluginID, file, locale);
117         if (inputStream != null) {
118             UAContentParser parser = new UAContentParser(inputStream);
119             Document JavaDoc dom = parser.getDocument();
120             XHTMLSupport support = new XHTMLSupport(pluginID, file, dom, locale);
121             dom = support.processDOM(filter);
122             try {
123                 inputStream.close();
124             } catch (IOException JavaDoc e) {
125             }
126             return UATransformManager.getAsInputStream(dom);
127         }
128         return null;
129     }
130
131     /**
132      * Same as openXHTMLFromPlugin() but does not do any processing of the document
133      * (it is opened raw).
134      */

135     private static InputStream JavaDoc openXHTMLFromPluginRaw(String JavaDoc pluginID, String JavaDoc file, String JavaDoc locale) {
136         Bundle bundle = Platform.getBundle(pluginID);
137         if (bundle != null) {
138             // look in the doc.zip and in the plugin to find the xhtml file.
139
InputStream JavaDoc inputStream = ResourceLocator.openFromZip(bundle, "doc.zip", //$NON-NLS-1$
140
file, locale);
141             if (inputStream == null) {
142                 inputStream = ResourceLocator.openFromPlugin(bundle, file, locale);
143             }
144             return inputStream;
145         }
146         return null;
147     }
148 }
149
Popular Tags