📄 codeviewer.java
字号:
//of a multi-line comment is in this line. else if ((index = line.indexOf("/*")) > -1 && !isInsideString(line,index)) { inMultiLineComment = true; //Return result of other filters + everything after the start //of the multiline comment. We need to pass the through the //to the multiLineComment filter again in case the comment ends //on the same line. buf.append(inlineCommentFilter(line.substring(0,index))); buf.append(commentStart).append("/*"); buf.append(multiLineCommentFilter(line.substring(index+2))); return buf.toString(); } //Otherwise, no useful multi-line comment information was found so //pass the line down to the next filter for processesing. else { return inlineCommentFilter(line); } } /* * Filter inline comments from a line and formats them properly. */ private String inlineCommentFilter(String line) { if (line == null || line.equals("")) { return ""; } StringBuffer buf = new StringBuffer(); int index; if ((index = line.indexOf("//")) > -1 && !isInsideString(line,index)) { buf.append(stringFilter(line.substring(0,index))); buf.append(commentStart); buf.append(line.substring(index)); buf.append(commentEnd); } else { buf.append(stringFilter(line)); } return buf.toString(); } /* * Filters strings from a line of text and formats them properly. */ private String stringFilter(String line) { if (line == null || line.equals("")) { return ""; } StringBuffer buf = new StringBuffer(); if (line.indexOf("\"") <= -1) { return keywordFilter(line); } int start = 0; int startStringIndex = -1; int endStringIndex = -1; int tempIndex; //Keep moving through String characters until we want to stop... while ((tempIndex = line.indexOf("\"")) > -1) { //We found the beginning of a string if (startStringIndex == -1) { startStringIndex = 0; buf.append( stringFilter(line.substring(start,tempIndex)) ); buf.append(stringStart).append("\""); line = line.substring(tempIndex+1); } //Must be at the end else { startStringIndex = -1; endStringIndex = tempIndex; buf.append(line.substring(0,endStringIndex+1)); buf.append(stringEnd); line = line.substring(endStringIndex+1); } } buf.append( keywordFilter(line) ); return buf.toString(); } /* * Filters keywords from a line of text and formats them properly. */ private String keywordFilter( String line ) { if( line == null || line.equals("") ) { return ""; } StringBuffer buf = new StringBuffer(); //HashMap usedReservedWords = new HashMap(); // >= Java2 only (not thread-safe) Hashtable usedReservedWords = new Hashtable(); // < Java2 (thread-safe) int i=0, startAt=0; char ch; StringBuffer temp = new StringBuffer(); while( i < line.length() ) { temp.setLength(0); ch = line.charAt(i); startAt = i; // 65-90, uppercase letters // 97-122, lowercase letters while( i<line.length() && ( ( ch >= 65 && ch <= 90 ) || ( ch >= 97 && ch <= 122 ) ) ) { temp.append(ch); i++; if( i < line.length() ) { ch = line.charAt(i); } } String tempString = temp.toString(); if( reservedWords.containsKey(tempString) && !usedReservedWords.containsKey(tempString)) { usedReservedWords.put(tempString,tempString); line = replace( line, tempString, (reservedWordStart+tempString+reservedWordEnd) ); i += (reservedWordStart.length() + reservedWordEnd.length()); } else { i++; } } buf.append(line); return buf.toString(); } /** * Replaces all instances of oldString with newString in line. */ public static final String replace( String line, String oldString, String newString ) { int i=0; if ( ( i=line.indexOf( oldString, i ) ) >= 0 ) { char [] line2 = line.toCharArray(); char [] newString2 = newString.toCharArray(); int oLength = oldString.length(); StringBuffer buf = new StringBuffer(line2.length); buf.append(line2, 0, i).append(newString2); i += oLength; int j = i; while( ( i=line.indexOf( oldString, i ) ) > 0 ) { buf.append(line2, j, i-j).append(newString2); i += oLength; j = i; } buf.append(line2, j, line2.length - j); return buf.toString(); } return line; } /* * Checks to see if some position in a line is between String start and * ending characters. Not yet used in code or fully working :) */ private boolean isInsideString(String line, int position) { if (line.indexOf("\"") < 0) { return false; } int index; String left = line.substring(0,position); String right = line.substring(position); int leftCount = 0; int rightCount = 0; while ((index = left.indexOf("\"")) > -1) { leftCount ++; left = left.substring(index+1); } while ((index = right.indexOf("\"")) > -1) { rightCount ++; right = right.substring(index+1); } if (rightCount % 2 != 0 && leftCount % 2 != 0) { return true; } else { return false; } } /* * Load Hashtable (or HashMap) with Java reserved words. Improved list * in version 1.1 taken directly from Java language spec. */ private static void loadKeywords() { reservedWords.put( "abstract", "abstract" ); reservedWords.put( "boolean", "boolean" ); reservedWords.put( "break", "break" ); reservedWords.put( "byte", "byte" ); reservedWords.put( "case", "case" ); reservedWords.put( "catch", "catch" ); reservedWords.put( "char", "char" ); reservedWords.put( "class", "class" ); reservedWords.put( "const", "const" ); reservedWords.put( "continue", "continue" ); reservedWords.put( "default", "default" ); reservedWords.put( "do", "do" ); reservedWords.put( "double", "double" ); reservedWords.put( "else", "else" ); reservedWords.put( "extends", "extends" ); reservedWords.put( "final", "final" ); reservedWords.put( "finally", "finally" ); reservedWords.put( "float", "float" ); reservedWords.put( "for", "for" ); reservedWords.put( "goto", "goto" ); reservedWords.put( "if", "if" ); reservedWords.put( "implements", "implements" ); reservedWords.put( "import", "import" ); reservedWords.put( "instanceof", "instanceof" ); reservedWords.put( "int", "int" ); reservedWords.put( "interface", "interface" ); reservedWords.put( "long", "long" ); reservedWords.put( "native", "native" ); reservedWords.put( "new", "new" ); reservedWords.put( "package", "package" ); reservedWords.put( "private", "private" ); reservedWords.put( "protected", "protected" ); reservedWords.put( "public", "public" ); reservedWords.put( "return", "return" ); reservedWords.put( "short", "short" ); reservedWords.put( "static", "static" ); reservedWords.put( "strictfp", "strictfp" ); reservedWords.put( "super", "super" ); reservedWords.put( "switch", "switch" ); reservedWords.put( "synchronized", "synchronized" ); reservedWords.put( "this", "this" ); reservedWords.put( "throw", "throw" ); reservedWords.put( "throws", "throws" ); reservedWords.put( "transient", "transient" ); reservedWords.put( "try", "try" ); reservedWords.put( "void", "void" ); reservedWords.put( "volatile", "volatile" ); reservedWords.put( "while", "while" ); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -