Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'org.eclipse.help')
-rw-r--r--org.eclipse.help/plugin.properties2
-rw-r--r--org.eclipse.help/plugin.xml19
-rw-r--r--org.eclipse.help/src/org/eclipse/help/internal/DynamicContentProducer.java112
-rw-r--r--org.eclipse.help/src/org/eclipse/help/internal/xhtml/XHTMLContentDescriber.java119
4 files changed, 224 insertions, 28 deletions
diff --git a/org.eclipse.help/plugin.properties b/org.eclipse.help/plugin.properties
index 099400c46..22b0e4125 100644
--- a/org.eclipse.help/plugin.properties
+++ b/org.eclipse.help/plugin.properties
@@ -15,3 +15,5 @@ contexts_extension_point_name = Context Help
content_producer_extension_point_name = Help Content Producer
index_extension_point_name = Help Index Contributions
content_extension_point_name = Help Content Extensions
+content_type_html = HTML File
+content_type_xhtml = XHTML File
diff --git a/org.eclipse.help/plugin.xml b/org.eclipse.help/plugin.xml
index a34431554..0cdf96489 100644
--- a/org.eclipse.help/plugin.xml
+++ b/org.eclipse.help/plugin.xml
@@ -13,5 +13,24 @@
point="org.eclipse.help.contentProducer">
<contentProducer producer="org.eclipse.help.internal.DynamicContentProducer"/>
</extension>
+
+ <extension
+ point="org.eclipse.core.runtime.contentTypes">
+ <content-type
+ base-type="org.eclipse.core.runtime.text"
+ file-extensions="htm,html"
+ id="html"
+ name="%content_type_html"
+ priority="high">
+ </content-type>
+ <content-type
+ base-type="org.eclipse.core.runtime.xml"
+ describer="org.eclipse.help.internal.xhtml.XHTMLContentDescriber"
+ file-extensions="htm,html,xhtml"
+ id="xhtml"
+ name="%content_type_xhtml"
+ priority="high">
+ </content-type>
+ </extension>
</plugin>
diff --git a/org.eclipse.help/src/org/eclipse/help/internal/DynamicContentProducer.java b/org.eclipse.help/src/org/eclipse/help/internal/DynamicContentProducer.java
index 85acc5d1f..d3210f57b 100644
--- a/org.eclipse.help/src/org/eclipse/help/internal/DynamicContentProducer.java
+++ b/org.eclipse.help/src/org/eclipse/help/internal/DynamicContentProducer.java
@@ -14,6 +14,7 @@ import java.io.InputStream;
import java.util.Locale;
import org.eclipse.core.runtime.Platform;
+import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.help.IHelpContentProducer;
import org.eclipse.help.internal.util.ResourceLocator;
import org.eclipse.help.internal.xhtml.UAContentParser;
@@ -32,29 +33,74 @@ public class DynamicContentProducer implements IHelpContentProducer {
public InputStream getInputStream(String pluginID, String href, Locale locale) {
String file = href;
int qloc = href.indexOf('?');
- if (qloc != -1)
+ if (qloc != -1) {
file = href.substring(0, qloc);
- int loc = file.lastIndexOf('.');
- if (loc != -1) {
- String extension = file.substring(loc + 1).toLowerCase();
- if ("xhtml".equals(extension)) {//$NON-NLS-1$
- /*
- * Filtering can be turned off when, for example,
- * indexing documents.
- */
- boolean filter = true;
- if (qloc != -1 && qloc < href.length() - 1) {
- String query = href.substring(qloc + 1);
- filter = (query.indexOf("filter=false") == -1); //$NON-NLS-1$
- }
- return openXHTMLFromPlugin(pluginID, file, locale.toString(), filter);
+ }
+ if (isXHTML(pluginID, href, locale)) {
+ /*
+ * Filtering can be turned off when, for example,
+ * indexing documents.
+ */
+ boolean filter = true;
+ if (qloc != -1 && qloc < href.length() - 1) {
+ String query = href.substring(qloc + 1);
+ filter = (query.indexOf("filter=false") == -1); //$NON-NLS-1$
}
- // place support for other formats here
+ return openXHTMLFromPlugin(pluginID, file, locale.toString(), filter);
}
return null;
}
/**
+ * Returns whether or not the given href is pointing to an XHTML
+ * document (it can be within a .html file).
+ *
+ * @param pluginID the id of the plugin containing the document
+ * @param href the href to the document
+ * @param locale the document's locale
+ * @return whether or not the document is XHTML
+ */
+ private static boolean isXHTML(String pluginID, String href, Locale locale) {
+ String file = href;
+ int qloc = href.indexOf('?');
+ if (qloc != -1) {
+ file = href.substring(0, qloc);
+ }
+ int lastSlash = file.lastIndexOf('/');
+ String fileName;
+ if (lastSlash != -1) {
+ fileName = file.substring(lastSlash + 1);
+ }
+ else {
+ fileName = file;
+ }
+ // first open it to peek inside to see whether it's really XHTML
+ InputStream contents = openXHTMLFromPluginRaw(pluginID, file, locale.toString());
+ IContentType type = null;
+ try {
+ type = Platform.getContentTypeManager().findContentTypeFor(contents, fileName);
+ }
+ catch (IOException e) {
+ HelpPlugin.logError("An error occured in DynamicContentProducer while trying to determine the content type", e); //$NON-NLS-1$
+ }
+ finally {
+ if (contents != null) {
+ try {
+ contents.close();
+ }
+ catch (IOException e) {
+ // nothing we can do
+ }
+ }
+ }
+ // if it has the right content type it's XHTML
+ if (type != null) {
+ return "org.eclipse.help.xhtml".equals(type.getId()); //$NON-NLS-1$
+ }
+ return false;
+ }
+
+ /**
* Opens an input stream to an xhtml file contained in a plugin or in a doc.zip
* in the plugin. This includes includes OS, WS and NL lookup.
*
@@ -69,7 +115,27 @@ public class DynamicContentProducer implements IHelpContentProducer {
*
* @return an InputStream to the file or <code>null</code> if the file wasn't found
*/
- private InputStream openXHTMLFromPlugin(String pluginID, String file, String locale, boolean filter) {
+ private static InputStream openXHTMLFromPlugin(String pluginID, String file, String locale, boolean filter) {
+ InputStream inputStream = openXHTMLFromPluginRaw(pluginID, file, locale);
+ if (inputStream != null) {
+ UAContentParser parser = new UAContentParser(inputStream);
+ Document dom = parser.getDocument();
+ XHTMLSupport support = new XHTMLSupport(pluginID, file, dom, locale);
+ dom = support.processDOM(filter);
+ try {
+ inputStream.close();
+ } catch (IOException e) {
+ }
+ return UATransformManager.getAsInputStream(dom);
+ }
+ return null;
+ }
+
+ /**
+ * Same as openXHTMLFromPlugin() but does not do any processing of the document
+ * (it is opened raw).
+ */
+ private static InputStream openXHTMLFromPluginRaw(String pluginID, String file, String locale) {
Bundle bundle = Platform.getBundle(pluginID);
if (bundle != null) {
// look in the doc.zip and in the plugin to find the xhtml file.
@@ -78,17 +144,7 @@ public class DynamicContentProducer implements IHelpContentProducer {
if (inputStream == null) {
inputStream = ResourceLocator.openFromPlugin(bundle, file, locale);
}
- if (inputStream != null) {
- UAContentParser parser = new UAContentParser(inputStream);
- Document dom = parser.getDocument();
- XHTMLSupport support = new XHTMLSupport(pluginID, file, dom, locale);
- dom = support.processDOM(filter);
- try {
- inputStream.close();
- } catch (IOException e) {
- }
- return UATransformManager.getAsInputStream(dom);
- }
+ return inputStream;
}
return null;
}
diff --git a/org.eclipse.help/src/org/eclipse/help/internal/xhtml/XHTMLContentDescriber.java b/org.eclipse.help/src/org/eclipse/help/internal/xhtml/XHTMLContentDescriber.java
new file mode 100644
index 000000000..b8d16197f
--- /dev/null
+++ b/org.eclipse.help/src/org/eclipse/help/internal/xhtml/XHTMLContentDescriber.java
@@ -0,0 +1,119 @@
+/*******************************************************************************
+ * Copyright (c) 2006 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.help.internal.xhtml;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Hashtable;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.QualifiedName;
+import org.eclipse.core.runtime.content.IContentDescriber;
+import org.eclipse.core.runtime.content.IContentDescription;
+import org.eclipse.core.runtime.content.XMLRootElementContentDescriber;
+
+/**
+ * A content describer for XHTML.
+ */
+public class XHTMLContentDescriber implements IContentDescriber {
+
+ private static final String PROPERTY_DTD = "dtd"; //$NON-NLS-1$
+
+ // XHTML has 3 DTDs, so we have to try each one.
+ private static final String DTD_STRICT = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"; //$NON-NLS-1$
+ private static final String DTD_TRANSITIONAL = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"; //$NON-NLS-1$
+ private static final String DTD_FRAMESET = "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"; //$NON-NLS-1$
+
+ private static final int BUFFER_SIZE = 8192;
+
+ private XMLRootElementContentDescriber describerStrict = new XMLRootElementContentDescriber();
+ private XMLRootElementContentDescriber describerTransitional = new XMLRootElementContentDescriber();
+ private XMLRootElementContentDescriber describerFrameset = new XMLRootElementContentDescriber();
+
+ /**
+ * Constructs a new XHTMLContentDescriber. Initializes the three
+ * delegates with their respective DTDs.
+ */
+ public XHTMLContentDescriber() {
+ try {
+ describerStrict.setInitializationData(null, null, getParameter(PROPERTY_DTD, DTD_STRICT));
+ }
+ catch (CoreException e) {
+ // not much we can do here
+ }
+
+ try {
+ describerTransitional.setInitializationData(null, null, getParameter(PROPERTY_DTD, DTD_TRANSITIONAL));
+ }
+ catch (CoreException e) {
+ // not much we can do here
+ }
+
+ try {
+ describerFrameset.setInitializationData(null, null, getParameter(PROPERTY_DTD, DTD_FRAMESET));
+ }
+ catch (CoreException e) {
+ // not much we can do here
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.core.runtime.content.IContentDescriber#describe(java.io.InputStream, org.eclipse.core.runtime.content.IContentDescription)
+ */
+ public int describe(InputStream contents, IContentDescription description) throws IOException {
+ /*
+ * Load the first BUFFER_SIZE bytes, then pass that to each delegate.
+ * If any one recognizes their DTD, return VALID.
+ */
+ try {
+ byte[] buffer = new byte[BUFFER_SIZE];
+ contents.read(buffer);
+ contents.close();
+
+ ByteArrayInputStream in = new ByteArrayInputStream(buffer);
+ if (describerTransitional.describe(in, description) == VALID) {
+ return VALID;
+ }
+ in = new ByteArrayInputStream(buffer);
+ if (describerStrict.describe(in, description) == VALID) {
+ return VALID;
+ }
+ in = new ByteArrayInputStream(buffer);
+ return describerFrameset.describe(in, description);
+ }
+ catch (Exception e) {
+ return INDETERMINATE;
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.core.runtime.content.IContentDescriber#getSupportedOptions()
+ */
+ public QualifiedName[] getSupportedOptions() {
+ return new QualifiedName[0];
+ }
+
+ /**
+ * Creates a new parameter suitable for passing into the
+ * XMLRootElementContentDescriber. The parameters have to be
+ * in the form of a Hashtable.
+ *
+ * @param name parameter name
+ * @param value parameter value
+ * @return the parameter, in Hashtable form
+ */
+ private static Hashtable getParameter(String name, String value) {
+ Hashtable hash = new Hashtable();
+ hash.put(name, value);
+ return hash;
+ }
+}

Back to the top