1 11 package org.eclipse.help.internal.xhtml; 12 13 import java.io.BufferedInputStream ; 14 import java.io.ByteArrayInputStream ; 15 import java.io.IOException ; 16 import java.io.InputStream ; 17 18 import javax.xml.parsers.ParserConfigurationException ; 19 import javax.xml.transform.TransformerConfigurationException ; 20 import javax.xml.transform.TransformerException ; 21 22 import org.eclipse.core.runtime.content.IContentDescriber; 23 import org.eclipse.help.internal.base.HelpEvaluationContext; 24 import org.eclipse.help.internal.dynamic.DocumentReader; 25 import org.eclipse.help.internal.dynamic.ExtensionHandler; 26 import org.eclipse.help.internal.dynamic.FilterHandler; 27 import org.eclipse.help.internal.dynamic.IncludeHandler; 28 import org.eclipse.help.internal.dynamic.ProcessorHandler; 29 import org.eclipse.help.internal.dynamic.XMLProcessor; 30 import org.eclipse.help.internal.search.HTMLDocParser; 31 import org.xml.sax.SAXException ; 32 33 37 public class DynamicXHTMLProcessor { 38 39 private static IContentDescriber xhtmlDescriber; 40 private static XMLProcessor xmlProcessor; 41 private static XMLProcessor xmlProcessorNoFilter; 42 43 46 public static InputStream process(String href, InputStream in, String locale, boolean filter) throws IOException , SAXException , ParserConfigurationException , TransformerConfigurationException , TransformerException { 47 BufferedInputStream buf = new BufferedInputStream (in, XHTMLContentDescriber.BUFFER_SIZE); 48 int bufferSize = Math.max(XHTMLContentDescriber.BUFFER_SIZE, HTMLDocParser.MAX_OFFSET); 49 byte[] buffer = new byte[bufferSize]; 50 buf.mark(Math.max(XHTMLContentDescriber.BUFFER_SIZE, HTMLDocParser.MAX_OFFSET)); 51 buf.read(buffer); 52 buf.reset(); 53 boolean isXHTML = isXHTML(new ByteArrayInputStream (buffer)); 54 if (isXHTML) { 55 String charset = HTMLDocParser.getCharsetFromHTML(new ByteArrayInputStream (buffer)); 56 if (filter) { 57 if (xmlProcessor == null) { 58 DocumentReader reader = new DocumentReader(); 59 xmlProcessor = new XMLProcessor(new ProcessorHandler[] { 60 new IncludeHandler(reader, locale), 61 new ExtensionHandler(reader, locale), 62 new XHTMLCharsetHandler(), 63 new FilterHandler(HelpEvaluationContext.getContext()) 64 }); 65 } 66 return xmlProcessor.process(buf, href, charset); 67 } 68 if (xmlProcessorNoFilter == null) { 69 DocumentReader reader = new DocumentReader(); 70 xmlProcessorNoFilter = new XMLProcessor(new ProcessorHandler[] { 71 new IncludeHandler(reader, locale), 72 new ExtensionHandler(reader, locale), 73 new XHTMLCharsetHandler() 74 }); 75 } 76 return xmlProcessorNoFilter.process(buf, href, charset); 77 } 78 return buf; 79 } 80 81 84 private static synchronized boolean isXHTML(InputStream in) { 85 if (xhtmlDescriber == null) { 86 xhtmlDescriber = new XHTMLContentDescriber(); 87 } 88 if (in != null) { 89 try { 90 return (xhtmlDescriber.describe(in, null) == IContentDescriber.VALID); 91 } 92 catch (IOException e) { 93 } 94 } 95 return false; 96 } 97 } 98 | Popular Tags |