📄 java2xhtml.java
字号:
String javadocTagArray[]= { "see", "author", "version", "param", "return", "exception", "deprecated", "throws", "link", "since", "serial", "serialField","serialData", "beaninfo" }; javadocTagCollection = new HashSet(Arrays.asList(javadocTagArray)); } public Java2xhtml() { } // create the various keyword collections // parse the html options file Java2xhtml(String propertiesFileName, File sourceFilePath, String htmlFileName) { // get html properties (use defaults if necessary) File propertiesFilePath = new File (propertiesFileName); if (propertiesFilePath.exists()) { // html properies file exist try parsing it try { InputStream propertiesFile = new FileInputStream(propertiesFileName); Properties htmlProperties = new Properties(); htmlProperties.load(propertiesFile); propertiesFile.close(); setProperties(htmlProperties); } catch (IOException exception) { System.out.println(exception); } } if (sourceFilePath.isFile()) { // process the file processFile(sourceFilePath, htmlFileName); } else if (sourceFilePath.isDirectory()) { // process a directory File [] sourceFilePathArray = sourceFilePath.listFiles(); for (int i = 0; i < sourceFilePathArray.length; i++) { if (((sourceFilePathArray[i]).getName()).endsWith(".java")) { // process each file that ends in .java // create a unique default html file name, // bubba.java -> bubba_java.html htmlFileName = ((sourceFilePathArray[i]).getName()).replace( '.', '_') + ".html"; processFile(sourceFilePathArray[i], htmlFileName); } } } } public void setProperties(Properties htmlProperties) { hasLegend = Boolean.valueOf(htmlProperties.getProperty("hasLegend", "false")).booleanValue(); extraIndentation = Integer.parseInt(htmlProperties.getProperty("extraIndentation", "0")); tabSize = Integer.parseInt(htmlProperties.getProperty("tabSize", "4")); hasLineNumbers = Boolean.valueOf(htmlProperties.getProperty("hasLineNumbers", "false")).booleanValue(); lineModulus = Integer.parseInt(htmlProperties.getProperty("lineModulus", "5")); hasLineModulusDrawnLines = Boolean.valueOf(htmlProperties.getProperty("hasLineModulusDrawnLines", "false")).booleanValue(); hasLineModulusCodeBlocks = Boolean.valueOf(htmlProperties.getProperty("hasLineModulusCodeBlocks", "false")).booleanValue(); hasFooter = Boolean.valueOf(htmlProperties.getProperty("hasFooter", "false")).booleanValue(); hasFooterIcons = Boolean.valueOf(htmlProperties.getProperty("hasFooterIcons", "false")).booleanValue(); hasFooterDate = Boolean.valueOf(htmlProperties.getProperty("hasFooterDate", "true")).booleanValue(); isXHTML_1_1 = Boolean.valueOf(htmlProperties.getProperty("isXHTML_1_1", "true")).booleanValue(); isCodeSnippet = Boolean.valueOf(htmlProperties.getProperty("isCodeSnippet", "false")).booleanValue(); hasTitle = Boolean.valueOf(htmlProperties.getProperty("hasTitle", "false")).booleanValue(); hasAllBoldSourceCode = Boolean.valueOf(htmlProperties.getProperty("hasAllBoldSourceCode", "false")).booleanValue(); hasInternalStyleSheet = Boolean.valueOf(htmlProperties.getProperty("hasInternalStyleSheet", "true")).booleanValue(); hasExternalStyleSheet = Boolean.valueOf(htmlProperties.getProperty("hasExternalStyleSheet", "true")).booleanValue(); externalStyleSheetName = htmlProperties.getProperty("externalStyleSheetName", "style.css"); } // read the file and put it into a stringbuffer void processFile(File sourceFilePath, String htmlFileName) { // open the file, copy it to a Stringbuffer , process into an // HTMLified String and convert result into an HTML file try { BufferedReader sourceReader = new BufferedReader(new FileReader(sourceFilePath)); StringBuffer bufferIn = new StringBuffer(); int readInInt = 0; char presentChar = 0; // copy file into a Stringbuffer while (readInInt != -1) // -1 value means end of stream/file { // put the file into a Stringbuffer readInInt= sourceReader.read(); presentChar = ((readInInt >= 0) ? (char) readInInt : 0); bufferIn.append(presentChar); } sourceReader.close(); BufferedWriter tempBufferedWriter = new BufferedWriter(new FileWriter(htmlFileName)); tempBufferedWriter.write(makeHTML(bufferIn, sourceFilePath.getName())); tempBufferedWriter.close(); System.out.println(sourceFilePath.getName() + " --> " + htmlFileName); } catch (IOException exception) { System.out.println(exception); } } // constant 'States' java source code can be in public final static class State { public final static State TEXT = new State(); public final static State IMPORT_NAME = new State(); public final static State PARAM_VARIABLE = new State(); public final static State JAVADOC = new State(); public final static State PACKAGE_NAME = new State(); public final static State DOUBLE_QUOTE = new State(); public final static State SINGLE_QUOTE = new State(); public final static State TRADITIONAL_COMMENT = new State(); public final static State LINE_COMMENT = new State(); // empty constructor private State() { // empty body } } // Convert java source code StringBufffer into colorized (and tab spaced) // HTML String . // Assumes that Java naming convention is used // Uses a very basic state machine design. public String makeHTML(StringBuffer bufferIn, String sourceFileName) { int codeLineNumber = 0; boolean isNewLine = true; boolean isNewBlock = true; int identifierLength = 0; int qualifiedIdentifierLength = 0; int presentIndex = -1; int spaceLength = 0; int saveIndex = 0; char presentChar = 0; State presentState = State.TEXT; StringBuffer bufferOut = new StringBuffer(8192); if (!isCodeSnippet) { bufferOut.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"); if (isXHTML_1_1) { bufferOut.append("<!DOCTYPE html PUBLIC " + "\"-//W3C//DTD XHTML 1.1//EN\"\r\n"); bufferOut.append(" \"http://www.w3.org/TR/xhtml11/DTD/" + "xhtml11.dtd\">\r\n"); bufferOut.append("<html xmlns=\"http://www.w3.org/1999/xhtml\""+ " xml:lang=\"en\">\r\n"); } else { bufferOut.append("<!DOCTYPE html PUBLIC " + "\"-//W3C//DTD XHTML 1.0 Strict//EN\"\r\n"); bufferOut.append(" \"http://www.w3.org/TR/xhtml1/DTD/" + "xhtml1-strict.dtd\">\r\n"); bufferOut.append("<html xmlns=\"http://www.w3.org/1999/xhtml\""+ " xml:lang=\"en\" lang=\"en\">\r\n"); } bufferOut.append(" <head>\r\n"); bufferOut.append(" <title>\r\n"); bufferOut.append(" " + sourceFileName + "\r\n"); bufferOut.append(" </title>\r\n"); bufferOut.append(" <meta name=\"generator\"\r\n"); bufferOut.append(" content=\"Java2xhtml 0.9\" />\r\n"); if (hasInternalStyleSheet) { bufferOut.append(" <style type=\"text/css\">\r\n"); bufferOut.append(" <!-- /* <![CDATA[ */\r\n"); bufferOut.append(" ." + sourceCodeStyle + "\r\n"); bufferOut.append(" {\r\n"); bufferOut.append(" color: #000000;\r\n"); bufferOut.append(" background-color: #FFFFFF;\r\n"); if (hasAllBoldSourceCode) { bufferOut.append(" font-weight: bold;\r\n"); } bufferOut.append(" }\r\n"); bufferOut.append(" ." + lineNumberStyle + "\r\n"); bufferOut.append(" {\r\n"); bufferOut.append(" font-weight: normal;\r\n"); bufferOut.append(" color: #000000;\r\n"); bufferOut.append(" background-color: transparent;\r\n"); bufferOut.append(" }\r\n"); if (lineModulus > 0) { bufferOut.append(" ." + modulusLineNumberStyle + "\r\n"); bufferOut.append(" {\r\n"); bufferOut.append(" font-weight: bold;\r\n"); bufferOut.append(" color: #000000;\r\n"); bufferOut.append(" background-color: "); bufferOut.append("transparent;\r\n"); bufferOut.append(" }\r\n"); if (hasLineModulusDrawnLines) { bufferOut.append(" .modulusLineStyle\r\n"); bufferOut.append(" {\r\n"); bufferOut.append(" text-decoration: "); bufferOut.append("line-through;\r\n"); bufferOut.append(" color: #000000;\r\n"); bufferOut.append(" background-color: "); bufferOut.append("transparent;\r\n"); bufferOut.append(" }\r\n"); } if (hasLineModulusCodeBlocks) { bufferOut.append(" .modulusBlockPREStyle\r\n"); bufferOut.append(" {\r\n"); bufferOut.append(" margin: 0em\r\n"); bufferOut.append(" }\r\n"); bufferOut.append(" .modulusBlockStyle\r\n"); bufferOut.append(" {\r\n"); bufferOut.append(" color: #000000;\r\n"); bufferOut.append(" background-color: "); bufferOut.append("#CCCCCC;\r\n"); bufferOut.append(" }\r\n"); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -