[jboss-cvs] JBossAS SVN: r81012 - in trunk/docbook-support/highlight/src/main/java/org/jboss/highlight: renderer and 1 other directory.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Fri Nov 14 07:23:25 EST 2008
Author: dimitris at jboss.org
Date: 2008-11-14 07:23:25 -0500 (Fri, 14 Nov 2008)
New Revision: 81012
Modified:
trunk/docbook-support/highlight/src/main/java/org/jboss/highlight/XhtmlRendererFactory.java
trunk/docbook-support/highlight/src/main/java/org/jboss/highlight/renderer/FORenderer.java
trunk/docbook-support/highlight/src/main/java/org/jboss/highlight/renderer/JavaXhtmlRenderer.java
Log:
JBAS-3916, use the right copyright header
Modified: trunk/docbook-support/highlight/src/main/java/org/jboss/highlight/XhtmlRendererFactory.java
===================================================================
--- trunk/docbook-support/highlight/src/main/java/org/jboss/highlight/XhtmlRendererFactory.java 2008-11-14 12:22:38 UTC (rev 81011)
+++ trunk/docbook-support/highlight/src/main/java/org/jboss/highlight/XhtmlRendererFactory.java 2008-11-14 12:23:25 UTC (rev 81012)
@@ -1,94 +1,94 @@
-/**
- * License Agreement.
- *
- * JBoss RichFaces 3.0 - Ajax4jsf Component Library
- *
- * Copyright (C) 2007 Exadel, Inc.
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License version 2.1 as published by the Free Software Foundation.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-package org.jboss.highlight;
-
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Properties;
-
-import com.uwyn.jhighlight.renderer.Renderer;
-
-/**
- * @author Maksim Kaszynski
- * @author Mark Newton (mark.newton at jboss.org)
- *
- */
-public class XhtmlRendererFactory {
-
- private static XhtmlRendererFactory instance;
- public static final String fileName = "renderers.properties";
- private Map<Object, Object> classNames = new HashMap<Object, Object>();
-
- public static final XhtmlRendererFactory instance() {
- synchronized(XhtmlRendererFactory.class) {
- if (instance == null) {
- instance = new XhtmlRendererFactory();
- }
- }
-
- return instance;
- }
-
- public XhtmlRendererFactory() {
- InputStream resourceAsStream =
- getClass().getResourceAsStream(fileName);
- try {
- Properties props = new Properties();
- props.load(resourceAsStream);
- classNames.putAll(props);
- resourceAsStream.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- }
-
- public Renderer getRenderer(String type) {
-
- // Look first for our own renderer, followed by the richfaces renderer
- Renderer renderer = null;
- Object object = classNames.get(type.toLowerCase());
- if (object != null) {
- String className = object.toString();
-
- try {
- Class<?> class1 = Class.forName(className);
- Object newInstance = class1.newInstance();
- if (newInstance instanceof Renderer) {
- return (Renderer) newInstance;
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- } else {
- renderer = org.richfaces.highlight.XhtmlRendererFactory.instance().getRenderer(type);
- }
-
- return renderer;
- }
-}
+/**
+ * License Agreement.
+ *
+ * JBoss RichFaces 3.0 - Ajax4jsf Component Library
+ *
+ * Copyright (C) 2007 Exadel, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+package org.jboss.highlight;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import com.uwyn.jhighlight.renderer.Renderer;
+
+/**
+ * @author Maksim Kaszynski
+ * @author Mark Newton (mark.newton at jboss.org)
+ *
+ */
+public class XhtmlRendererFactory {
+
+ private static XhtmlRendererFactory instance;
+ public static final String fileName = "renderers.properties";
+ private Map<Object, Object> classNames = new HashMap<Object, Object>();
+
+ public static final XhtmlRendererFactory instance() {
+ synchronized(XhtmlRendererFactory.class) {
+ if (instance == null) {
+ instance = new XhtmlRendererFactory();
+ }
+ }
+
+ return instance;
+ }
+
+ public XhtmlRendererFactory() {
+ InputStream resourceAsStream =
+ getClass().getResourceAsStream(fileName);
+ try {
+ Properties props = new Properties();
+ props.load(resourceAsStream);
+ classNames.putAll(props);
+ resourceAsStream.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ }
+
+ public Renderer getRenderer(String type) {
+
+ // Look first for our own renderer, followed by the richfaces renderer
+ Renderer renderer = null;
+ Object object = classNames.get(type.toLowerCase());
+ if (object != null) {
+ String className = object.toString();
+
+ try {
+ Class<?> class1 = Class.forName(className);
+ Object newInstance = class1.newInstance();
+ if (newInstance instanceof Renderer) {
+ return (Renderer) newInstance;
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ } else {
+ renderer = org.richfaces.highlight.XhtmlRendererFactory.instance().getRenderer(type);
+ }
+
+ return renderer;
+ }
+}
Modified: trunk/docbook-support/highlight/src/main/java/org/jboss/highlight/renderer/FORenderer.java
===================================================================
--- trunk/docbook-support/highlight/src/main/java/org/jboss/highlight/renderer/FORenderer.java 2008-11-14 12:22:38 UTC (rev 81011)
+++ trunk/docbook-support/highlight/src/main/java/org/jboss/highlight/renderer/FORenderer.java 2008-11-14 12:23:25 UTC (rev 81012)
@@ -1,210 +1,210 @@
-/*
- * Copyright 2004-2006 Geert Bevin <gbevin[remove] at uwyn dot com>
- * 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.*;
-
-import com.uwyn.jhighlight.JHighlightVersion;
-import com.uwyn.jhighlight.highlighter.ExplicitStateHighlighter;
-import com.uwyn.jhighlight.tools.ExceptionUtils;
-import com.uwyn.jhighlight.tools.StringUtils;
-import com.uwyn.jhighlight.highlighter.JavaHighlighter;
-import com.uwyn.jhighlight.highlighter.XmlHighlighter;
-import java.net.URL;
-import java.net.URLConnection;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.Properties;
-import java.util.logging.Logger;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.HashMap;
-
-/**
- * Parses the contents of programlisting elements to allow FO syntax
- * highlighting.
- *
- * @author Geert Bevin (gbevin[remove] at uwyn dot com)
- * @author Mark Newton (mark.newton at jboss.org)
- * @version $Revision$
- * @since 1.0
- */
-public class FORenderer
-{
- private List<String> styles = new ArrayList<String>();
- private List<String> tokens = new ArrayList<String>();
-
- /** We assume that this class will get called multiple times by Saxon */
- private static int caller = 0;
-
- /** Store the lists for each caller so that we can access them from the iterator template */
- private static Map<Integer, List<String>> allStyles = new HashMap<Integer, List<String>>();
- private static Map<Integer, List<String>> allTokens = new HashMap<Integer, List<String>>();
-
- public static int getNoOfTokens(int caller) {
- return allTokens.get(caller).size();
- }
-
- public static String getStyle(int caller, int index) {
- List<String> styles = allStyles.get(caller);
-
- if (styles.size() > 0) {
- return styles.get(index);
- }
- return "";
- }
-
- public static String getToken(int caller, int index) {
- List<String> tokens = allTokens.get(caller);
-
- if (tokens.size() > 0) {
- return tokens.get(index);
- }
- return "";
- }
-
- public static boolean isParsable(String role) {
- if (role.equals("JAVA") || role.equals("XML")) {
- return true;
- } else {
- return false;
- }
- }
-
- /**
- * Parse the text into tokens and store in a list.
- * Also store a corresponding list of styles for each token.
- */
- public int parseText(String role, String text, String encoding) throws IOException {
-
-// System.out.println("Text: " + text);
- styles.clear();
- tokens.clear();
-
- InputStream in = new StringBufferInputStream(text);
-
- ExplicitStateHighlighter highlighter = null;
-
- if (role.equals("JAVA")) {
- highlighter = new JavaHighlighter();
- ((JavaHighlighter) highlighter).ASSERT_IS_KEYWORD = true;
- } else if (role.equals("XML")) {
- highlighter = new XmlHighlighter();
- } else {
- return 0;
- }
-
- Reader isr;
- if (encoding == null) {
- isr = new InputStreamReader(in);
- }
- else {
- isr = new InputStreamReader(in, encoding);
- }
-
- BufferedReader r = new BufferedReader(isr);
-
- // Read all the text into a buffer so that we capture newline characters
- StringBuffer buf = new StringBuffer();
- int c = 0;
- while ((c = r.read()) != -1) {
- buf.append((char) c);
- }
-
- // Split the text into separate lines incase it includes newline characters
- String allLines = buf.toString();
- String[] lines = allLines.split("\n");
-// for (int jj=0; jj < lines.length; jj++) {
-// System.out.println("Line: " + lines[jj]);
-// }
-
- for (int i=0; i < lines.length; i ++) {
- String line = lines[i];
- if ((i != lines.length -1) || (i == lines.length - 1 && allLines.endsWith("\n"))) {
- line += "\n"; // Put back the newline if needed
- }
-
- line = StringUtils.convertTabsToSpaces(line, 4);
-
- // should be optimized by reusing a custom LineReader class
- Reader lineReader = new StringReader(line);
- highlighter.setReader(lineReader);
- int index = 0;
- while (index < line.length())
- {
- int style = highlighter.getNextToken();
- int length = highlighter.getTokenLength();
- String token = line.substring(index, index + length);
-
- styles.add(getCssClass(role, style));
- tokens.add(token);
-
- index += length;
- }
- }
-
- // Store the lists so that we can retrieve them later
- allStyles.put(caller, styles);
- allTokens.put(caller, tokens);
-
- return caller++;
- }
-
- protected String getCssClass(String role, int style)
- {
- if (role.equals("JAVA")) {
- 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";
- }
- } else if (role.equals("XML")) {
- switch (style)
- {
- case XmlHighlighter.PLAIN_STYLE:
- return "xml_plain";
- case XmlHighlighter.CHAR_DATA:
- return "xml_char_data";
- case XmlHighlighter.TAG_SYMBOLS:
- return "xml_tag_symbols";
- case XmlHighlighter.COMMENT:
- return "xml_comment";
- case XmlHighlighter.ATTRIBUTE_VALUE:
- return "xml_attribute_value";
- case XmlHighlighter.ATTRIBUTE_NAME:
- return "xml_attribute_name";
- case XmlHighlighter.PROCESSING_INSTRUCTION:
- return "xml_processing_instruction";
- case XmlHighlighter.TAG_NAME:
- return "xml_tag_name";
- case XmlHighlighter.RIFE_TAG:
- return "xml_rife_tag";
- case XmlHighlighter.RIFE_NAME:
- return "xml_rife_name";
- }
- }
-
- return null;
- }
-}
+/*
+ * Copyright 2004-2006 Geert Bevin <gbevin[remove] at uwyn dot com>
+ * 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.*;
+
+import com.uwyn.jhighlight.JHighlightVersion;
+import com.uwyn.jhighlight.highlighter.ExplicitStateHighlighter;
+import com.uwyn.jhighlight.tools.ExceptionUtils;
+import com.uwyn.jhighlight.tools.StringUtils;
+import com.uwyn.jhighlight.highlighter.JavaHighlighter;
+import com.uwyn.jhighlight.highlighter.XmlHighlighter;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Properties;
+import java.util.logging.Logger;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.HashMap;
+
+/**
+ * Parses the contents of programlisting elements to allow FO syntax
+ * highlighting.
+ *
+ * @author Geert Bevin (gbevin[remove] at uwyn dot com)
+ * @author Mark Newton (mark.newton at jboss.org)
+ * @version $Revision$
+ * @since 1.0
+ */
+public class FORenderer
+{
+ private List<String> styles = new ArrayList<String>();
+ private List<String> tokens = new ArrayList<String>();
+
+ /** We assume that this class will get called multiple times by Saxon */
+ private static int caller = 0;
+
+ /** Store the lists for each caller so that we can access them from the iterator template */
+ private static Map<Integer, List<String>> allStyles = new HashMap<Integer, List<String>>();
+ private static Map<Integer, List<String>> allTokens = new HashMap<Integer, List<String>>();
+
+ public static int getNoOfTokens(int caller) {
+ return allTokens.get(caller).size();
+ }
+
+ public static String getStyle(int caller, int index) {
+ List<String> styles = allStyles.get(caller);
+
+ if (styles.size() > 0) {
+ return styles.get(index);
+ }
+ return "";
+ }
+
+ public static String getToken(int caller, int index) {
+ List<String> tokens = allTokens.get(caller);
+
+ if (tokens.size() > 0) {
+ return tokens.get(index);
+ }
+ return "";
+ }
+
+ public static boolean isParsable(String role) {
+ if (role.equals("JAVA") || role.equals("XML")) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * Parse the text into tokens and store in a list.
+ * Also store a corresponding list of styles for each token.
+ */
+ public int parseText(String role, String text, String encoding) throws IOException {
+
+// System.out.println("Text: " + text);
+ styles.clear();
+ tokens.clear();
+
+ InputStream in = new StringBufferInputStream(text);
+
+ ExplicitStateHighlighter highlighter = null;
+
+ if (role.equals("JAVA")) {
+ highlighter = new JavaHighlighter();
+ ((JavaHighlighter) highlighter).ASSERT_IS_KEYWORD = true;
+ } else if (role.equals("XML")) {
+ highlighter = new XmlHighlighter();
+ } else {
+ return 0;
+ }
+
+ Reader isr;
+ if (encoding == null) {
+ isr = new InputStreamReader(in);
+ }
+ else {
+ isr = new InputStreamReader(in, encoding);
+ }
+
+ BufferedReader r = new BufferedReader(isr);
+
+ // Read all the text into a buffer so that we capture newline characters
+ StringBuffer buf = new StringBuffer();
+ int c = 0;
+ while ((c = r.read()) != -1) {
+ buf.append((char) c);
+ }
+
+ // Split the text into separate lines incase it includes newline characters
+ String allLines = buf.toString();
+ String[] lines = allLines.split("\n");
+// for (int jj=0; jj < lines.length; jj++) {
+// System.out.println("Line: " + lines[jj]);
+// }
+
+ for (int i=0; i < lines.length; i ++) {
+ String line = lines[i];
+ if ((i != lines.length -1) || (i == lines.length - 1 && allLines.endsWith("\n"))) {
+ line += "\n"; // Put back the newline if needed
+ }
+
+ line = StringUtils.convertTabsToSpaces(line, 4);
+
+ // should be optimized by reusing a custom LineReader class
+ Reader lineReader = new StringReader(line);
+ highlighter.setReader(lineReader);
+ int index = 0;
+ while (index < line.length())
+ {
+ int style = highlighter.getNextToken();
+ int length = highlighter.getTokenLength();
+ String token = line.substring(index, index + length);
+
+ styles.add(getCssClass(role, style));
+ tokens.add(token);
+
+ index += length;
+ }
+ }
+
+ // Store the lists so that we can retrieve them later
+ allStyles.put(caller, styles);
+ allTokens.put(caller, tokens);
+
+ return caller++;
+ }
+
+ protected String getCssClass(String role, int style)
+ {
+ if (role.equals("JAVA")) {
+ 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";
+ }
+ } else if (role.equals("XML")) {
+ switch (style)
+ {
+ case XmlHighlighter.PLAIN_STYLE:
+ return "xml_plain";
+ case XmlHighlighter.CHAR_DATA:
+ return "xml_char_data";
+ case XmlHighlighter.TAG_SYMBOLS:
+ return "xml_tag_symbols";
+ case XmlHighlighter.COMMENT:
+ return "xml_comment";
+ case XmlHighlighter.ATTRIBUTE_VALUE:
+ return "xml_attribute_value";
+ case XmlHighlighter.ATTRIBUTE_NAME:
+ return "xml_attribute_name";
+ case XmlHighlighter.PROCESSING_INSTRUCTION:
+ return "xml_processing_instruction";
+ case XmlHighlighter.TAG_NAME:
+ return "xml_tag_name";
+ case XmlHighlighter.RIFE_TAG:
+ return "xml_rife_tag";
+ case XmlHighlighter.RIFE_NAME:
+ return "xml_rife_name";
+ }
+ }
+
+ return null;
+ }
+}
Modified: trunk/docbook-support/highlight/src/main/java/org/jboss/highlight/renderer/JavaXhtmlRenderer.java
===================================================================
--- trunk/docbook-support/highlight/src/main/java/org/jboss/highlight/renderer/JavaXhtmlRenderer.java 2008-11-14 12:22:38 UTC (rev 81011)
+++ trunk/docbook-support/highlight/src/main/java/org/jboss/highlight/renderer/JavaXhtmlRenderer.java 2008-11-14 12:23:25 UTC (rev 81012)
@@ -1,261 +1,261 @@
-/*
- * Copyright 2004-2006 Geert Bevin <gbevin[remove] at uwyn dot com>
- * 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.*;
-
-import com.uwyn.jhighlight.highlighter.ExplicitStateHighlighter;
-import com.uwyn.jhighlight.highlighter.JavaHighlighter;
-import com.uwyn.jhighlight.renderer.XhtmlRenderer;
-import java.util.HashMap;
-import java.util.Map;
-import com.uwyn.jhighlight.tools.StringUtils;
-
-/**
- * Generates highlighted syntax in XHTML from Java source.
- *
- * @author Geert Bevin (gbevin[remove] at uwyn dot com)
- * @author Mark Newton (mark.newton at jboss.org)
- * @version $Revision$
- * @since 1.0
- */
-public class JavaXhtmlRenderer extends XhtmlRenderer
-{
- private int lineNo = 0;
-
- /**
- * Transforms source code that's provided through an
- * <code>InputStream</code> to highlighted syntax in XHTML and writes it
- * back to an <code>OutputStream</code>.
- * <p>If the highlighting has to become a fragment, no CSS styles will be
- * generated.
- * <p>For complete documents, there's a collection of default styles that
- * will be included. It's possible to override these by changing the
- * provided <code>jhighlight.properties</code> 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 <code>true</code> if the generated XHTML should be a
- * fragment; or <code>false</code> 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
- {
- ExplicitStateHighlighter highlighter = getHighlighter();
-
- 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(getXhtmlHeaderFragment(name));
- }
- else
- {
- w.write(getXhtmlHeader(name));
- }
-
- String line;
- String token;
- int length;
- int style;
- String css_class;
- int previous_style = 0;
- boolean newline = false;
-
- StringBuffer buf = new StringBuffer();
- int c = 0;
- while ((c = r.read()) != -1) {
- buf.append((char) c);
- }
-
- String allLines = buf.toString();
- String[] lines = allLines.split("\n");
-// for (int jj=0; jj < lines.length; jj++) {
-// System.out.println("Line: " + lines[jj]);
-// }
-
- // We get a new instance of this class each time we parse a programlisting so we can
- // can use an instance variable to detect if it's the first time we've been called.
- // This will allow us to put <br/> before each line so that the callout extension will work correctly.
-
- for (int i=0; i < lines.length; i ++) {
- line = lines[i];
- lineNo++;
-
- line = StringUtils.convertTabsToSpaces(line, 4);
-
- // should be optimized by reusing a custom LineReader class
- Reader lineReader = new StringReader(line);
- highlighter.setReader(lineReader);
- int index = 0;
- while (index < line.length())
- {
- style = highlighter.getNextToken();
- length = highlighter.getTokenLength();
- token = line.substring(index, index + length);
-
- if (style != previous_style || newline) // assume we have a new style if there is a newline
- {
- css_class = getCssClass(style);
-
- if (css_class != null)
- {
- if (previous_style != 0 && !newline) // each token will potentially have a different style
- {
- w.write("</span>");
- }
-
- // Write the start tag for the <span> element representing the style
- if (lineNo == 1) {
- w.write("<!-- <br/> --><span class=\"" + css_class + "\">"); // the 1st line doesn't have a linebreak
- } else if (!newline) {
- w.write("<span class=\"" + css_class + "\">"); // we're in the middle of a line
- }
- else if (newline && lineNo != 1) {
- w.write("<!-- --><br/><span class=\"" + css_class + "\">"); // we're at the start of a new line
- }
-
- previous_style = style;
- }
- }
- newline = false; // if we've just started processing a new line we need to know
- w.write(StringUtils.replace(StringUtils.encodeHtml(StringUtils.replace(token, "\n", "")), " ", " "));
-
- index += length;
- }
-
- // Write a newline character if we're not on the last line or we are and there was a newline at the end anyway
- if ((i != lines.length -1) || (i == lines.length - 1 && allLines.endsWith("\n"))) {
- w.write("</span>\n");
- } else {
- w.write("</span>");
- }
- newline = true;
- }
-
- if (!fragment) w.write(getXhtmlFooter());
-
- w.flush();
- w.close();
- }
-
- public final static HashMap DEFAULT_CSS = new HashMap() {{
- 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();
- highlighter.ASSERT_IS_KEYWORD = true;
-
- return highlighter;
- }
-}
+/*
+ * Copyright 2004-2006 Geert Bevin <gbevin[remove] at uwyn dot com>
+ * 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.*;
+
+import com.uwyn.jhighlight.highlighter.ExplicitStateHighlighter;
+import com.uwyn.jhighlight.highlighter.JavaHighlighter;
+import com.uwyn.jhighlight.renderer.XhtmlRenderer;
+import java.util.HashMap;
+import java.util.Map;
+import com.uwyn.jhighlight.tools.StringUtils;
+
+/**
+ * Generates highlighted syntax in XHTML from Java source.
+ *
+ * @author Geert Bevin (gbevin[remove] at uwyn dot com)
+ * @author Mark Newton (mark.newton at jboss.org)
+ * @version $Revision$
+ * @since 1.0
+ */
+public class JavaXhtmlRenderer extends XhtmlRenderer
+{
+ private int lineNo = 0;
+
+ /**
+ * Transforms source code that's provided through an
+ * <code>InputStream</code> to highlighted syntax in XHTML and writes it
+ * back to an <code>OutputStream</code>.
+ * <p>If the highlighting has to become a fragment, no CSS styles will be
+ * generated.
+ * <p>For complete documents, there's a collection of default styles that
+ * will be included. It's possible to override these by changing the
+ * provided <code>jhighlight.properties</code> 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 <code>true</code> if the generated XHTML should be a
+ * fragment; or <code>false</code> 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
+ {
+ ExplicitStateHighlighter highlighter = getHighlighter();
+
+ 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(getXhtmlHeaderFragment(name));
+ }
+ else
+ {
+ w.write(getXhtmlHeader(name));
+ }
+
+ String line;
+ String token;
+ int length;
+ int style;
+ String css_class;
+ int previous_style = 0;
+ boolean newline = false;
+
+ StringBuffer buf = new StringBuffer();
+ int c = 0;
+ while ((c = r.read()) != -1) {
+ buf.append((char) c);
+ }
+
+ String allLines = buf.toString();
+ String[] lines = allLines.split("\n");
+// for (int jj=0; jj < lines.length; jj++) {
+// System.out.println("Line: " + lines[jj]);
+// }
+
+ // We get a new instance of this class each time we parse a programlisting so we can
+ // can use an instance variable to detect if it's the first time we've been called.
+ // This will allow us to put <br/> before each line so that the callout extension will work correctly.
+
+ for (int i=0; i < lines.length; i ++) {
+ line = lines[i];
+ lineNo++;
+
+ line = StringUtils.convertTabsToSpaces(line, 4);
+
+ // should be optimized by reusing a custom LineReader class
+ Reader lineReader = new StringReader(line);
+ highlighter.setReader(lineReader);
+ int index = 0;
+ while (index < line.length())
+ {
+ style = highlighter.getNextToken();
+ length = highlighter.getTokenLength();
+ token = line.substring(index, index + length);
+
+ if (style != previous_style || newline) // assume we have a new style if there is a newline
+ {
+ css_class = getCssClass(style);
+
+ if (css_class != null)
+ {
+ if (previous_style != 0 && !newline) // each token will potentially have a different style
+ {
+ w.write("</span>");
+ }
+
+ // Write the start tag for the <span> element representing the style
+ if (lineNo == 1) {
+ w.write("<!-- <br/> --><span class=\"" + css_class + "\">"); // the 1st line doesn't have a linebreak
+ } else if (!newline) {
+ w.write("<span class=\"" + css_class + "\">"); // we're in the middle of a line
+ }
+ else if (newline && lineNo != 1) {
+ w.write("<!-- --><br/><span class=\"" + css_class + "\">"); // we're at the start of a new line
+ }
+
+ previous_style = style;
+ }
+ }
+ newline = false; // if we've just started processing a new line we need to know
+ w.write(StringUtils.replace(StringUtils.encodeHtml(StringUtils.replace(token, "\n", "")), " ", " "));
+
+ index += length;
+ }
+
+ // Write a newline character if we're not on the last line or we are and there was a newline at the end anyway
+ if ((i != lines.length -1) || (i == lines.length - 1 && allLines.endsWith("\n"))) {
+ w.write("</span>\n");
+ } else {
+ w.write("</span>");
+ }
+ newline = true;
+ }
+
+ if (!fragment) w.write(getXhtmlFooter());
+
+ w.flush();
+ w.close();
+ }
+
+ public final static HashMap DEFAULT_CSS = new HashMap() {{
+ 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();
+ highlighter.ASSERT_IS_KEYWORD = true;
+
+ return highlighter;
+ }
+}
More information about the jboss-cvs-commits
mailing list