|
La classe ci-jointe vous permettra de mettre en forme dans un fichier HTML le code source de vos fichiers Java, en coloriant les mots clés, chaînes de caractères et commentaires.
package com.supinfo.labs.sun.utils;
import java.io.*;
/**
* @author FrŽdŽric Chuong
* @version 0.2
*/
public class SourceFormatter {
public static final int STATE_NONE = 0;
public static final int STATE_STRING = 1;
public static final int STATE_CHAR = 2;
public static final int STATE_SIMPLE_COMMENT = 3;
public static final int STATE_COMPLEX_COMMENT = 4;
private StringBuffer formattedSource;
private String[] keywords = { "abstract", "default", "if", "private",
"this", "boolean", "do", "implements", "protected", "throw",
"break", "double", "import", "public", "throws", "byte", "else",
"instanceof", "return", "transient", "case", "extends", "int",
"short", "try", "catch", "final", "interface", "static", "void",
"char", "finally", "long", "strictfp", "volatile", "class",
"float", "native", "super", "while", "const", "for", "new",
"switch", "continue", "goto", "package", "synchronized", "null",
"false", "true" };
/**
* Parses the source code
*
* @param source Source code to parse
*/
public void parse(String source) {
formattedSource = new StringBuffer();
StringBuffer buffer = new StringBuffer();
int state = STATE_NONE;
char c;
boolean escapedChar = false;
for (int i = 0; i < source.length(); i++) {
c = source.charAt(i);
try {
if (c == ’\r’ && source.charAt(i + 1) == ’\n’) {
continue;
}
} catch (IndexOutOfBoundsException e) {
}
switch (state) {
case STATE_NONE:
if (Character.isLetter(c)) {
buffer.append(c);
} else {
if (buffer.length() > 0) {
// test whether buffer is a Java keyword
if (isKeyword(buffer)) {
formattedSource
.append(""
+ buffer + "");
buffer = new StringBuffer();
}
}
if (c == ’"’) {
state = STATE_STRING;
buffer.append(c);
} else if (c == ’\’’) {
state = STATE_CHAR;
buffer.append(c);
} else if (c == ’/’) {
if (buffer.length() == 1 && buffer.charAt(0) == ’/’) {
state = STATE_SIMPLE_COMMENT;
}
buffer.append(c);
} else if (buffer.length() == 1 && buffer.charAt(0) == ’/’
&& c == ’*’) {
state = STATE_COMPLEX_COMMENT;
buffer.append(c);
} else {
formattedSource.append(escape(buffer));
formattedSource.append(escape(c));
buffer = new StringBuffer();
}
}
break;
case STATE_STRING:
buffer.append(c);
if (c == ’"’ && !escapedChar) {
state = STATE_NONE;
formattedSource.append(""
+ escape(buffer) + "");
buffer = new StringBuffer();
break;
}
if (!escapedChar && c == ’\\’) {
escapedChar = true;
} else if (escapedChar) {
escapedChar = false;
}
break;
case STATE_CHAR:
buffer.append(c);
if (c == ’\’’ && !escapedChar) {
state = STATE_NONE;
formattedSource.append(""
+ escape(buffer) + "");
buffer = new StringBuffer();
break;
}
if (!escapedChar && c == ’\\’) {
escapedChar = true;
} else if (escapedChar) {
escapedChar = false;
}
break;
case STATE_SIMPLE_COMMENT:
if (c == ’\n’) {
state = STATE_NONE;
formattedSource.append("");
formattedSource.append(c);
buffer = new StringBuffer();
} else {
buffer.append(c);
}
break;
case STATE_COMPLEX_COMMENT:
buffer.append(c);
try {
if (c == ’/’ && buffer.charAt(buffer.length() - 2) == ’*’) {
state = STATE_NONE;
formattedSource.append("");
buffer = new StringBuffer();
}
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
}
break;
}
}
switch (state) {
case STATE_CHAR:
case STATE_STRING:
formattedSource.append(""
+ escape(buffer) + "");
break;
case STATE_SIMPLE_COMMENT:
case STATE_COMPLEX_COMMENT:
formattedSource.append("");
break;
case STATE_NONE:
formattedSource.append(escape(buffer));
break;
}
buffer = null;
}
/**
* Transforms special-meaning HTML characters from the specified text
*
* @param text Text to transform
* @return Escaped text
*/
public String escape(String text) {
String tmp = new String(text);
tmp = tmp.replaceAll("&", "&");
tmp = tmp.replaceAll("<", "<");
tmp = tmp.replaceAll(">", ">");
tmp = tmp.replaceAll("\"", """);
return tmp;
}
/**
* Transforms special-meaning HTML characters from the specified text
*
* @param text Text to transform
* @return Escaped text
*/
public String escape(StringBuffer text) {
return escape(text.toString());
}
/**
* Transforms special-meaning HTML character from the specified character
*
* @param c Character to transform
* @return Escaped text
*/
public String escape(char c) {
return escape(Character.toString(c));
}
/**
* Tests wheter the specified word is a Java keyword
*
* @param word Word to test
* @return
*/
public boolean isKeyword(String word) {
// TODO: Use a Collection to get a faster search
for (int i = 0; i < keywords.length; i++) {
if (keywords[i].compareTo(word) == 0) {
return true;
}
}
return false;
}
/**
* Tests wheter the specified word is a Java keyword
*
* @param word Word to test
* @return
*/
public boolean isKeyword(StringBuffer word) {
return isKeyword(word.toString());
}
/**
* Opens and parses a file
*
* @param filename File to parse
*/
public void parseFile(String filename) {
try {
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
int c;
StringBuffer buffer = new StringBuffer();
while ((c = br.read()) != -1) {
buffer.append((char) c);
}
br.close();
fr.close();
parse(buffer.toString());
} catch (IOException e) {
}
}
/**
* Returns the HTML formatted text
*
* @return The HTML formatted text
*/
public String getText() {
return formattedSource.toString();
}
/**
* Returns the CSS styles used
*
* @return CSS styles
*/
public static String getCSS() {
return ".sourceString { color: #0000FF; }"
+ ".sourceKeyword { color: #660066; font-weight: bold; }"
+ ".sourceComment { color: #006600; }";
}
}
|