// java2html.java //
// An example file distributed with com.stevesoft.pat // and com.stevesoft.pat.apps //
// This software comes without express or implied warranty. // No claim is made about the suitability of this software for // any purpose and neither we nor SteveSoft shall be liable for // damages suffered by the user of this software. package com.stevesoft.pat.apps; import com.stevesoft.pat.*; import java.io.*; /** java2html makes your code an in-color web page -- to use it just type
java com.stevesoft.pat.apps.java2html file_patterns
The program creates files with ".html" appended to them
that are suitable for viewing with web browsers.
The file names provided to java2html will be interpreted as either source or destination files depending on the name (anything ending in ".html" is a destination file, ".nocolor_html", ".nocolor_asp", ".java", ".jas" are source files).
Normally, java2html will not overwrite a file if the file it is colorizing is newer than its target file. However, if the -f option is supplied java2html will not check modification time.
Click here for more info.
*/
public class java2html {
// Basic colors
static String PrimitiveColor = "ff00dd";
static String QuoteColor = "0000ff";
static String CommentColor = "000088";
static String KeywordColor = "0000aa";
static String DocumentBackgroundColor = "ffffdd";
static String java_lang_Color = "dd00ff";
// Jasmine colors
static String DirectiveColor = "aaaa00";
static String LabelColor = "ff00dd";
// build up a list of rules for color changing
static Regex DQuotes = (new Regex("\"(?:\\\\.|[^\"])*\"",
"$&"));
static Regex SQuotes = (new Regex("'(?:\\\\.|[^'])*'",
"$&"));
static Regex Comment1 = (new Regex("//.*",
"$&"));
static Regex Comment2 = (new Regex("/\\*.*?\\*/",
"$&"));
final static boolean[] readme = new boolean[1];
static Regex Comment3 = null;
static Regex PrimitiveTypes = new Regex(
"\\b(?:boolean|char|byte|short|int|long|float|double)\\b",
"$&");
static Regex Keywords = (new Regex(
"\\b(?:abstract|break|byvalue|case|cast|catch|"+
"class|const|continue|default|do|else|extends|"+
"false|final|finally|for|future|generic|goto|if|"+
"implements|import|inner|instanceof|interface|"+
"native|new|null|operator|outer|package|private|"+
"protected|public|rest|return|static|super|switch|"+
"synchronized|this|throw|throws|transient|true|try|"+
"var|volatile|while)\\b",
"$&"));
static Regex java_lang = new Regex(
"\\b(?:Boolean|Byte|Character|Class|ClassLoader|Cloneable|Compiler|"+
"Double|Float|Integer|Long|Math|Number|Object|Process|"+
"Runnable|Runtime|SecurityManager|Short|String|StringBuffer|"+
"System|Thread|ThreadGroup|Void)\\b",
"$&");
static Regex oper = new Regex("(?:[\\+\\*\\^\\$\\-\\{\\}\\[\\]"+
"\\=\\.\\(\\)\\,\\:/]|&(lt|gt|amp);)",
"$&");
static Transformer colorize = new Transformer(true);
static Replacer java_replacer = colorize.getReplacer();
static Replacer colorizer = null;
static Replacer html_replacer = null;
static Replacer pretran_html = null;
static Replacer pretran_java = null;
static Replacer pretran = null;
static boolean jasmine_enabled = true;
static void init() {
// This is a rule that can apply itself across multiple
// lines. The rule named "end_comment" only looks for
// the */ sequence, and pop's itself off the rule stack
// when it finds it.
ReplaceRule.define("endcomment",
new Regex("\\*/","*/${POP}"));
Comment3 = (new Regex("/\\*","/*${+endcomment}"));
// Jasmine stuff
Regex.define("JasmineEnabled","",new Validator() {
public int validate(String src,int begin,int end) {
return jasmine_enabled ? end : -1;
}
});
colorize.add(
"s{(??JasmineEnabled)^\\s*;\\s*>>.*}"+
"{$&
}");
colorize.add(
"s{(??JasmineEnabled)(?:^|\\s)\\s*;.*}"+
"{$&}");
colorize.add(
"s{(??JasmineEnabled)\\b(?:catch|class|end|field|"+
"implements|interface|limit|line|method|source|super|"+
"throws|var|stack|locals)\\b}{$&}");
colorize.add(
"s{(??JasmineEnabled)^\\w+:}{$&}");
// stick all replacement rules into the Transformer
colorize.add(DQuotes);
colorize.add(SQuotes);
colorize.add(Comment1);
colorize.add(Comment2);
colorize.add(Comment3);
colorize.add(PrimitiveTypes);
colorize.add(Keywords);
colorize.add(java_lang);
colorize.add(oper);
colorize.add(Regex.perlCode(
"s'\\w*(Error|Exception|Throwable)\\b'$&'"));
ReplaceRule.define("colorize",colorize);
ReplaceRule.define("jascode",new ReplaceRule() {
public void apply(StringBufferLike sb,RegRes rr) {
String s1 = rr.stringMatched(1);
if(s1 != null && s1.equals("jas"))
jasmine_enabled = true;
}
});
Regex r = new Regex("(?i)<(java|jas)code([^>]*?)>\\s*",
""+
"
${POP}"));
html_replacer = r.getReplacer();
Transformer DoPre = new Transformer(true);
DoPre.add("s'(?i)\\s*(?:jav|jas)acode>'$&$POP'");
DoPre.add("s'<'<'");
DoPre.add("s'>'>'");
DoPre.add("s'&'&'");
ReplaceRule.define("DOPRE",DoPre);
pretran_html = new Regex("(?i)${jascode}${+colorize}");
r.optimize();
colorize.add(new Regex("(?i)\\s*(?:java|jas)code>",
"");
}
File f = new File(FromFile);
if(!code_flag) {
pw.println("// Uncolored, plain source file: "+f.getName()+"");
}
String s = null;
s = trline(br.readLine());
// main loop, do all colorizing and replacing
while(s != null) {
// this does everything...
pw.print(colorizer.replaceAll(s));
s = trline(br.readLine());
}
if(!code_flag) pw.println("");
pw.close();
br.close();
} catch(Exception e) {
e.printStackTrace();
System.exit(255);
}
}
static boolean force = false;
public static void main(String[] args) {
init();
for(int i=0;i