[jboss-cvs] JBossAS SVN: r71761 - in trunk/docbook-support: highlight and 11 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Mon Apr 7 09:36:33 EDT 2008


Author: newtonm
Date: 2008-04-07 09:36:33 -0400 (Mon, 07 Apr 2008)
New Revision: 71761

Added:
   trunk/docbook-support/highlight/
   trunk/docbook-support/highlight/pom.xml
   trunk/docbook-support/highlight/src/
   trunk/docbook-support/highlight/src/main/
   trunk/docbook-support/highlight/src/main/java/
   trunk/docbook-support/highlight/src/main/java/org/
   trunk/docbook-support/highlight/src/main/java/org/jboss/
   trunk/docbook-support/highlight/src/main/java/org/jboss/highlight/
   trunk/docbook-support/highlight/src/main/java/org/jboss/highlight/XhtmlRendererFactory.java
   trunk/docbook-support/highlight/src/main/java/org/jboss/highlight/renderer/
   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
   trunk/docbook-support/highlight/src/main/resources/
   trunk/docbook-support/highlight/src/main/resources/org/
   trunk/docbook-support/highlight/src/main/resources/org/jboss/
   trunk/docbook-support/highlight/src/main/resources/org/jboss/highlight/
   trunk/docbook-support/highlight/src/main/resources/org/jboss/highlight/renderers.properties
Log:
Added highlight project.

Added: trunk/docbook-support/highlight/pom.xml
===================================================================
--- trunk/docbook-support/highlight/pom.xml	                        (rev 0)
+++ trunk/docbook-support/highlight/pom.xml	2008-04-07 13:36:33 UTC (rev 71761)
@@ -0,0 +1,64 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.jboss.docs</groupId>
+  <artifactId>highlight</artifactId>
+  <packaging>jar</packaging>
+  <version>1.0-SNAPSHOT</version>
+
+  <name>highlight</name>
+  <url>http://maven.apache.org</url>
+  <dependencies>
+           <dependency>
+            <groupId>com.uwyn</groupId>
+            <artifactId>jhighlight</artifactId>
+            <version>1.0</version>
+            <scope>compile</scope>
+           </dependency>
+                    <dependency>
+                        <groupId>org.richfaces.docs</groupId>
+                        <artifactId>highlight</artifactId>
+                        <version>3.1.4.GA</version>
+                    </dependency>
+  </dependencies>
+
+      <build>
+         <pluginManagement>
+            <plugins>
+               <plugin>
+                  <groupId>org.apache.maven.plugins</groupId>
+                  <artifactId>maven-compiler-plugin</artifactId>
+                  <version>2.0</version>
+                  <configuration>
+                     <source>1.5</source>
+                     <target>1.5</target>
+                  </configuration>
+               </plugin>
+            </plugins>
+	</pluginManagement>
+        <extensions>
+            <extension>
+               <groupId>org.apache.maven.wagon</groupId>
+               <artifactId>wagon-webdav</artifactId>
+               <version>1.0-beta-2</version>
+            </extension>
+         </extensions>
+      </build>
+
+    <distributionManagement>
+        <repository>
+            <!-- Copy the dist to the local checkout of the JBoss maven2 repo ${maven.repository.root} -->
+            <!-- It is anticipated that ${maven.repository.root} be set in user's settings.xml -->
+            <!-- todo : replace this with direct svn access once the svnkit providers are available -->
+            <id>repository.jboss.org</id>
+            <url>file://${maven.repository.root}</url>
+        </repository>
+        <snapshotRepository>
+            <id>snapshots.jboss.org</id>
+            <name>JBoss Snapshot Repository</name>
+            <url>dav:https://snapshots.jboss.org/maven2</url>
+        </snapshotRepository>
+    </distributionManagement>
+
+</project>

Added: trunk/docbook-support/highlight/src/main/java/org/jboss/highlight/XhtmlRendererFactory.java
===================================================================
--- trunk/docbook-support/highlight/src/main/java/org/jboss/highlight/XhtmlRendererFactory.java	                        (rev 0)
+++ trunk/docbook-support/highlight/src/main/java/org/jboss/highlight/XhtmlRendererFactory.java	2008-04-07 13:36:33 UTC (rev 71761)
@@ -0,0 +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;
+	}
+}

Added: 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	                        (rev 0)
+++ trunk/docbook-support/highlight/src/main/java/org/jboss/highlight/renderer/FORenderer.java	2008-04-07 13:36:33 UTC (rev 71761)
@@ -0,0 +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;
+	}
+}

Added: 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	                        (rev 0)
+++ trunk/docbook-support/highlight/src/main/java/org/jboss/highlight/renderer/JavaXhtmlRenderer.java	2008-04-07 13:36:33 UTC (rev 71761)
@@ -0,0 +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", "")), " ", "&nbsp;"));
+				
+				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;
+	}
+}

Added: trunk/docbook-support/highlight/src/main/resources/org/jboss/highlight/renderers.properties
===================================================================
--- trunk/docbook-support/highlight/src/main/resources/org/jboss/highlight/renderers.properties	                        (rev 0)
+++ trunk/docbook-support/highlight/src/main/resources/org/jboss/highlight/renderers.properties	2008-04-07 13:36:33 UTC (rev 71761)
@@ -0,0 +1 @@
+java=org.jboss.highlight.renderer.JavaXhtmlRenderer




More information about the jboss-cvs-commits mailing list