All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.highlight.renderer.JavaXhtmlRenderer Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2004-2006 Geert Bevin 
 * Distributed under the terms of either:
 * - the common development and distribution license (CDDL), v1.0; or
 * - the GNU Lesser General Public License, v2.1 or later
 * $Id$
 */
package org.jboss.highlight.renderer;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import com.uwyn.jhighlight.highlighter.ExplicitStateHighlighter;
import com.uwyn.jhighlight.highlighter.JavaHighlighter;
import com.uwyn.jhighlight.renderer.XhtmlRenderer;

/**
 * Generates highlighted syntax in XHTML from Java source.
 * 
 * @author Geert Bevin (gbevin[remove] at uwyn dot com)
 * @author Mark Newton ([email protected])
 * @version $Revision$
 * @since 1.0
 */
public class JavaXhtmlRenderer extends XhtmlRenderer {

	/**
	 * Transforms source code that's provided through an
	 * InputStream to highlighted syntax in XHTML and writes it
	 * back to an OutputStream.
	 * 

* If the highlighting has to become a fragment, no CSS styles will be * generated. *

* For complete documents, there's a collection of default styles that will * be included. It's possible to override these by changing the provided * jhighlight.properties file. It's best to look at this file * in the JHighlight archive and modify the styles that are there already. * * @param name * The name of the source file. * @param in * The input stream that provides the source code that needs to * be transformed. * @param out * The output stream to which to resulting XHTML should be * written. * @param encoding * The encoding that will be used to read and write the text. * @param fragment * true if the generated XHTML should be a fragment; * or false if it should be a complete page * @see #highlight(String, String, String, boolean) * @since 1.0 */ public void highlight(String name, InputStream in, OutputStream out, String encoding, boolean fragment) throws IOException { Reader isr; Writer osw; if (null == encoding) { isr = new InputStreamReader(in); osw = new OutputStreamWriter(out); } else { isr = new InputStreamReader(in, encoding); osw = new OutputStreamWriter(out, encoding); } BufferedReader r = new BufferedReader(isr); BufferedWriter w = new BufferedWriter(osw); if (!fragment) { w.write(getXhtmlHeader(name)); } StringBuilder buf = new StringBuilder(); int c = 0; while ((c = r.read()) != -1) { buf.append((char) c); } w.write(buf.toString()); if (!fragment) w.write(getXhtmlFooter()); w.flush(); w.close(); } public final static HashMap DEFAULT_CSS = new HashMap() { /** * */ private static final long serialVersionUID = 6356314719813876066L; { put("h1", "font-family: sans-serif; " + "font-size: 16pt; " + "font-weight: bold; " + "color: rgb(0,0,0); " + "background: rgb(210,210,210); " + "border: solid 1px black; " + "padding: 5px; " + "text-align: center;"); put("code", "color: rgb(0,0,0); " + "font-family: monospace; " + "font-size: 12px; " + "white-space: nowrap;"); put(".java_plain", "color: rgb(0,0,0);"); put(".java_keyword", "color: rgb(0,0,0); " + "font-weight: bold;"); put(".java_type", "color: rgb(0,44,221);"); put(".java_operator", "color: rgb(0,124,31);"); put(".java_separator", "color: rgb(0,33,255);"); put(".java_literal", "color: rgb(188,0,0);"); put(".java_comment", "color: rgb(147,147,147); " + "background-color: rgb(247,247,247);"); put(".java_javadoc_comment", "color: rgb(147,147,147); " + "background-color: rgb(247,247,247); " + "font-style: italic;"); put(".java_javadoc_tag", "color: rgb(147,147,147); " + "background-color: rgb(247,247,247); " + "font-style: italic; " + "font-weight: bold;"); } }; protected Map getDefaultCssStyles() { return DEFAULT_CSS; } protected String getCssClass(int style) { switch (style) { case JavaHighlighter.PLAIN_STYLE: return "java_plain"; case JavaHighlighter.KEYWORD_STYLE: return "java_keyword"; case JavaHighlighter.TYPE_STYLE: return "java_type"; case JavaHighlighter.OPERATOR_STYLE: return "java_operator"; case JavaHighlighter.SEPARATOR_STYLE: return "java_separator"; case JavaHighlighter.LITERAL_STYLE: return "java_literal"; case JavaHighlighter.JAVA_COMMENT_STYLE: return "java_comment"; case JavaHighlighter.JAVADOC_COMMENT_STYLE: return "java_javadoc_comment"; case JavaHighlighter.JAVADOC_TAG_STYLE: return "java_javadoc_tag"; } return null; } protected ExplicitStateHighlighter getHighlighter() { JavaHighlighter highlighter = new JavaHighlighter(); JavaHighlighter.ASSERT_IS_KEYWORD = true; return highlighter; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy