You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@click.apache.org by sa...@apache.org on 2009/03/04 23:39:45 UTC
svn commit: r750202 -
/incubator/click/trunk/click/examples/src/org/apache/click/examples/page/SourceViewer.java
Author: sabob
Date: Wed Mar 4 22:39:45 2009
New Revision: 750202
URL: http://svn.apache.org/viewvc?rev=750202&view=rev
Log:
improved sourceviewer to highlight Java single and multiline comments
Modified:
incubator/click/trunk/click/examples/src/org/apache/click/examples/page/SourceViewer.java
Modified: incubator/click/trunk/click/examples/src/org/apache/click/examples/page/SourceViewer.java
URL: http://svn.apache.org/viewvc/incubator/click/trunk/click/examples/src/org/apache/click/examples/page/SourceViewer.java?rev=750202&r1=750201&r2=750202&view=diff
==============================================================================
--- incubator/click/trunk/click/examples/src/org/apache/click/examples/page/SourceViewer.java (original)
+++ incubator/click/trunk/click/examples/src/org/apache/click/examples/page/SourceViewer.java Wed Mar 4 22:39:45 2009
@@ -74,6 +74,8 @@
private boolean isJava = false;
+ private boolean javaMultilineComment = false;
+
private boolean isXml = false;
private boolean isHtml = false;
@@ -217,9 +219,25 @@
if (isJava) {
line = ClickUtils.escapeHtml(line);
- for (int i = 0; i < JAVA_KEYWORDS.length; i++) {
- String keyword = JAVA_KEYWORDS[i];
- line = renderJavaKeywords(line, keyword);
+ // Check if line is part of multiline comment
+ if (isJavaMultilineComment(line)) {
+ line = renderJavaComment(line);
+ } else {
+ // Check if line contains singleline comment
+ String comment = "";
+ if (hasJavaComment(line)) {
+ comment = getJavaComment(line);
+ comment = renderJavaComment(comment);
+ line = removeJavaComment(line);
+ }
+
+ if (StringUtils.isNotBlank(line)) {
+ for (int i = 0; i < JAVA_KEYWORDS.length; i++) {
+ String keyword = JAVA_KEYWORDS[i];
+ line = renderJavaKeywords(line, keyword);
+ }
+ }
+ line = line + comment;
}
} else if (isHtml) {
@@ -272,6 +290,43 @@
return line;
}
+ private boolean hasJavaComment(String line) {
+ return line.indexOf("//") != -1;
+ }
+
+ private String removeJavaComment(String line) {
+ int lineCommentStart = line.indexOf("//");
+ if (lineCommentStart != -1) {
+ line = line.substring(0, lineCommentStart);
+ }
+ return line;
+ }
+
+ private String getJavaComment(String line) {
+ int lineCommentStart = line.indexOf("//");
+ if (lineCommentStart != -1) {
+ return line.substring(lineCommentStart);
+ }
+ return "";
+ }
+
+ private boolean isJavaMultilineComment(String line) {
+ boolean isComment = false;
+ line = line.trim();
+ if (line.startsWith("/*")) {
+ isComment = true;
+ javaMultilineComment = true;
+ }
+ if (line.endsWith("*/")) {
+ isComment = true;
+ javaMultilineComment = false;
+ }
+ if (javaMultilineComment && line.startsWith("*")) {
+ isComment = true;
+ }
+ return isComment;
+ }
+
private String renderVelocityKeywords(String line, String token) {
String markupToken = renderVelocityToken(token);
@@ -347,4 +402,8 @@
private String renderJavaToken(String token) {
return "<font color=\"#7f0055\"><b>" + token + "</b></font>";
}
+
+ private String renderJavaComment(String comment) {
+ return "<font color=\"#3F7F5F\">" + comment + "</font>";
+ }
}