📄 versionfilter.java
字号:
} else { s += buf[offset + i]; } } } // Emit the data, along with any whitespace we've accumulated, // then clear the whitespace buffer. emit(wsBuf + s, indentLevel); wsBuf = ""; } } // Check to see if character is a whitespace char protected boolean white(char c) { if (c == ' ') { return true; } if (c == '\n') { return true; } if (c == '\t') { return true; } return false; } // Trim ending blanks from a string protected String rtrim(String s) { while (s.endsWith(" ")) { s = s.substring(0, s.length() - 1); } return s; } public void ignorableWhitespace(char[] buf, int offset, int len) throws SAXException { // If it exists, ignore it! } // WHAT WE NEED TO DO: // * BUFFER THE LAST CHARACTER STRING WE SAW // * WHEN WE SEE A PROCESSSING INSTRUCTION, REMOVE THE // TRAILING BLANKS FROM THAT STRING. // * FLUSH THE BUFFER WHEN: // --we see another character string // --we see any other element // --we come to the end of the document public void processingInstruction(String target, String data) throws SAXException { // Remove whitespace immediately ahead of the processing instruction. int ix = wsBuf.lastIndexOf('\n'); if (ix >= 0) { wsBuf = wsBuf.substring(0, ix + 1); } //System.out.print("\nPI: " + target + " " + data); // Tell character handler to kill the NL after the PI. pi = true; // Remove rightmost blanks from previous character string // stored in lastBuf. lastBuf = rtrim(lastBuf); // Process version control instructions if (target.equals("version")) { // OUTPUT TARGET VERSION# if (data.equals("#")) { // If we're in a delete-section or a section added for a later // revision, ignore this instruction. if (!echo) { return; } emit("" + targetString, indentLevel); return; } // END-SECTION TAG if (data.endsWith("end add") || data.endsWith("end del")) { if (!echo && (piLevel == resumeLevel)) { echo = true; } // If we're at the end of highlight-section, turn off highlighting if (highlight && data.endsWith("end add") && (piLevel == noBoldLevel)) { highlight = false; endNew(); } else if (strikeout && data.endsWith("end del") && (piLevel == noStrikeLevel)) { strikeout = false; endOld(); } piLevel--; return; } // START-SECTION TAG if (data.endsWith("add") || data.endsWith("del")) { piLevel++; // If we're in a delete-section or a section added for a later // revision, ignore everything until we close that section. if (!echo) { return; } // Figure out the version currVersion = Integer.parseInt(data.substring(0, data.length() - 4)); // Determine whether to echo this element echo = true; if ((data.endsWith("add") && (currVersion > targetVersion)) || (chg && data.endsWith("del") && (currVersion < targetVersion)) || (!chg && data.endsWith("del") && (currVersion <= targetVersion))) { echo = false; resumeLevel = piLevel; return; } // Determine whether to highlight or strikeout succeeding elements if (chg) { if (data.endsWith("add") && (currVersion == targetVersion)) { highlight = true; noBoldLevel = piLevel; startNew(); return; } else if (data.endsWith("del") && (currVersion == targetVersion)) { strikeout = true; noStrikeLevel = piLevel; startOld(); return; } } } return; } // Output other processing instructions emit("<?" + target + " " + data + "?>", indentLevel + 1); nl(); indentLevel--; // act like an end tag seen } public void skippedEntity(String name) throws SAXException { // Ignore it } public void startPrefixMapping(String prefix, String url) throws SAXException { // Ignore it } public void endPrefixMapping(String prefix) throws SAXException { // Ignore it } //----------------------------------------------------------- // Lexical Event Listener Methods //----------------------------------------------------------- public void comment(char[] ch, int start, int length) throws SAXException { // If we're in a delete-section or a section added for a later // revision, ignore everything until we close that section. if (!echo) { return; } // Root-element tag is only included if we are producing // pretty-print output. In that case, echo comments. if (!root) { return; } // Echo Comments String text = new String(ch, start, length); emit("<!-- " + text + " -->", indentLevel + 1); nl(); indentLevel--; // act like an end tag seen } public void startCDATA() throws SAXException { // Nada } public void endCDATA() throws SAXException { // Nada } public void startEntity(java.lang.String name) throws SAXException { // Nada } public void endEntity(java.lang.String name) throws SAXException { // Nada } public void startDTD(String name, String publicId, String systemId) throws SAXException { // Ignore it } public void endDTD() throws SAXException { // Ignore it } void startNew() throws SAXException { printBuf(); // flush buffer startString = START_NEW; endString = END_NEW; } void startOld() throws SAXException { printBuf(); // flush buffer startString = START_OLD; endString = END_OLD; } void endNew() throws SAXException { printBuf(); // flush buffer startString = ""; endString = ""; } void endOld() throws SAXException { printBuf(); // flush buffer startString = ""; endString = ""; } // Wrap I/O exceptions in SAX exceptions, to // suit handler signature requirements private void emit(String s, int level) throws SAXException { if (level != indentLevel) { //|| (lastBuf.indexOf('\n') != -1) if (lastBuf != "") { nl(); // flush buffer } indentLevel = level; } lastBuf += s; } // Flush the buffer and start a new line private void nl() throws SAXException { try { // Check last non-space character. If it's a newline, // don't write a second line end. It's already got one. int i = lastBuf.length() - 1; while (i >= 0) { if ((lastBuf.charAt(i) != ' ') && (lastBuf.charAt(i) != '\t')) { break; } i--; } boolean writeNL = true; if ((i > 0) && (lastBuf.charAt(i) == '\n')) { writeNL = false; } printBuf(); if (writeNL) { out.write(lineEnd); } } catch (IOException e) { throw new SAXException("I/O error", e); } } // Add end-string tags before NLs in the existing buffer // and put startString tags after any spaces on the next // line. private void printBuf() throws SAXException { if (lastBuf.equals("")) { return; } int i = 0; int lastInsertPosition = -1; StringBuffer sb = new StringBuffer(lastBuf); if (root) { // Root-element tag is only included if we are producing // pretty-print output. In that case, do indentation. for (int j = 0; j < indentLevel; j++) { // Indent the line sb.insert(i, indentString); i += indentString.length(); } } while (white(sb.charAt(i))) { i++; if (i >= sb.length()) { break; } } if (i < sb.length()) { // Not all whitespace sb.insert(i, startString); i += (startString.length() + 1); }LOOP: while (i < sb.length()) { if (sb.charAt(i) == '\n') { sb.insert(i, endString); i += endString.length(); lastInsertPosition = i; i++; if (i >= sb.length()) { break LOOP; } if (root) { // Root-element tag is only included if we are producing // pretty-print output. In that case, do indentation. for (int j = 0; j < indentLevel; j++) { // Indent the line sb.insert(i, indentString); i += indentString.length(); } } while (white(sb.charAt(i))) { // Move past whitespace, including multiple NLs i++; if (i >= sb.length()) { break LOOP; } } sb.insert(i, startString); i += startString.length(); lastInsertPosition = i; } i++; } // If there are characters after the final insert, // we were called by nl(). Make sure there is a // tag-terminator after the last non-white character. for (i = sb.length() - 1; i >= lastInsertPosition; i--) { if (!white(sb.charAt(i))) { if ((i + 1) == sb.length()) { sb.append(endString); } else { sb.insert(i + 1, endString); } break; } } try { out.write(sb.toString()); out.flush(); } catch (IOException e) { throw new SAXException("I/O error", e); } lastBuf = ""; } //printBuf} //VersionFilter
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -