1 9 10 package org.eclipse.help.internal; 11 12 import java.io.IOException ; 13 import java.io.InputStream ; 14 import java.util.Locale ; 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 ; 26 27 31 32 public class DynamicContentProducer implements IHelpContentProducer { 33 34 private IContentDescriber xhtmlDescriber; 35 36 public InputStream getInputStream(String pluginID, String href, Locale locale) { 37 if (isXHTML(pluginID, href, locale)) { 38 String file = href; 39 int qloc = href.indexOf('?'); 40 if (qloc != -1) { 41 file = href.substring(0, qloc); 42 } 43 44 48 boolean filter = true; 49 if (qloc != -1 && qloc < href.length() - 1) { 50 String query = href.substring(qloc + 1); 51 filter = (query.indexOf("filter=false") == -1); } 53 return openXHTMLFromPlugin(pluginID, file, locale.toString(), filter); 54 } 55 return null; 56 } 57 58 67 private boolean isXHTML(String pluginID, String href, Locale locale) { 68 String 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 InputStream 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 e) { 85 HelpPlugin.logError("An error occured in DynamicContentProducer while trying to determine the content type", e); } 87 finally { 88 if (in != null) { 89 try { 90 in.close(); 91 } 92 catch (IOException e) { 93 } 95 } 96 } 97 return false; 98 } 99 100 115 private static InputStream openXHTMLFromPlugin(String pluginID, String file, String locale, boolean filter) { 116 InputStream inputStream = openXHTMLFromPluginRaw(pluginID, file, locale); 117 if (inputStream != null) { 118 UAContentParser parser = new UAContentParser(inputStream); 119 Document 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 e) { 125 } 126 return UATransformManager.getAsInputStream(dom); 127 } 128 return null; 129 } 130 131 135 private static InputStream openXHTMLFromPluginRaw(String pluginID, String file, String locale) { 136 Bundle bundle = Platform.getBundle(pluginID); 137 if (bundle != null) { 138 InputStream inputStream = ResourceLocator.openFromZip(bundle, "doc.zip", 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 |