KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > xhtml > DynamicXHTMLProcessor


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.xhtml;
12
13 import java.io.BufferedInputStream JavaDoc;
14 import java.io.ByteArrayInputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.InputStream JavaDoc;
17
18 import javax.xml.parsers.ParserConfigurationException JavaDoc;
19 import javax.xml.transform.TransformerConfigurationException JavaDoc;
20 import javax.xml.transform.TransformerException JavaDoc;
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 JavaDoc;
32
33 /*
34  * Performs any needed XHTML processing on the given input stream. If the input
35  * is not XHTML, simply forwards the stream.
36  */

37 public class DynamicXHTMLProcessor {
38
39     private static IContentDescriber xhtmlDescriber;
40     private static XMLProcessor xmlProcessor;
41     private static XMLProcessor xmlProcessorNoFilter;
42
43     /*
44      * Performs any needed processing. Does nothing if not XHTML.
45      */

46     public static InputStream JavaDoc process(String JavaDoc href, InputStream JavaDoc in, String JavaDoc locale, boolean filter) throws IOException JavaDoc, SAXException JavaDoc, ParserConfigurationException JavaDoc, TransformerConfigurationException JavaDoc, TransformerException JavaDoc {
47         BufferedInputStream JavaDoc buf = new BufferedInputStream JavaDoc(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 JavaDoc(buffer));
54         if (isXHTML) {
55             String JavaDoc charset = HTMLDocParser.getCharsetFromHTML(new ByteArrayInputStream JavaDoc(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     /*
82      * Returns whether or not the given input stream is XHTML content.
83      */

84     private static synchronized boolean isXHTML(InputStream JavaDoc 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 JavaDoc e) {
93             }
94         }
95         return false;
96     }
97 }
98
Popular Tags