translatorreader.java
来自「JSPWiki,100%Java开发的一套完整WIKI程序」· Java 代码 · 共 2,275 行 · 第 1/5 页
JAVA
2,275 行
while( (ch = nextToken()) == '-' ); pushBack(ch); return m_renderer.makeRuler(); } pushBack( ch3 ); } pushBack( ch2 ); } pushBack( ch ); return "-"; } /** * This method peeks ahead in the stream until EOL and returns the result. * It will keep the buffers untouched. * * @return The string from the current position to the end of line. */ // FIXME: Always returns an empty line, even if the stream is full. private String peekAheadLine() throws IOException { String s = readUntilEOL().toString(); pushBack( s ); return s; } private String handleHeading() throws IOException { StringBuffer buf = new StringBuffer(); int ch = nextToken(); Heading hd = new Heading(); if( ch == '!' ) { int ch2 = nextToken(); if( ch2 == '!' ) { String title = peekAheadLine(); buf.append( m_renderer.makeHeading( Heading.HEADING_LARGE, title, hd) ); } else { pushBack( ch2 ); String title = peekAheadLine(); buf.append( m_renderer.makeHeading( Heading.HEADING_MEDIUM, title, hd ) ); } } else { pushBack( ch ); String title = peekAheadLine(); buf.append( m_renderer.makeHeading( Heading.HEADING_SMALL, title, hd ) ); } callHeadingListenerChain( hd ); return buf.toString(); } /** * Reads the stream until the next EOL or EOF. Note that it will also read the * EOL from the stream. */ private StringBuffer readUntilEOL() throws IOException { int ch; StringBuffer buf = new StringBuffer(); while( true ) { ch = nextToken(); if( ch == -1 ) break; buf.append( (char) ch ); if( ch == '\n' ) break; } return buf; } /** * Like original handleOrderedList() and handleUnorderedList() * however handles both ordered ('#') and unordered ('*') mixed together. */ // FIXME: Refactor this; it's a bit messy. private String handleGeneralList() throws IOException { String cStrShortName = "handleGeneralList()"; //put log messages in some context StringBuffer buf = new StringBuffer(); if( m_genlistlevel > 0 ) { buf.append(m_renderer.closeListItem()); } String strBullets = readWhile( "*#" ); String strBulletsRaw = strBullets; // to know what was original before phpwiki style substitution int numBullets = strBullets.length(); // override the beginning portion of bullet pattern to be like the previous // to simulate PHPWiki style lists if(m_allowPHPWikiStyleLists) { // only substitute if different if(!( strBullets.substring(0,Math.min(numBullets,m_genlistlevel)).equals (m_genlistBulletBuffer.substring(0,Math.min(numBullets,m_genlistlevel)) ) ) ) { if(numBullets <= m_genlistlevel) { // Substitute all but the last character (keep the expressed bullet preference) strBullets = (numBullets > 1 ? m_genlistBulletBuffer.substring(0, numBullets-1) : "") + strBullets.substring(numBullets-1, numBullets); } else { strBullets = m_genlistBulletBuffer + strBullets.substring(m_genlistlevel, numBullets); } } } if( strBullets.substring(0,Math.min(numBullets,m_genlistlevel)).equals (m_genlistBulletBuffer.substring(0,Math.min(numBullets,m_genlistlevel)) ) ) { int chBullet; if( numBullets > m_genlistlevel ) { for( ; m_genlistlevel < numBullets; m_genlistlevel++ ) { // bullets are growing, get from new bullet list buf.append( m_renderer.openList(strBullets.charAt(m_genlistlevel)) ); } } else if( numBullets < m_genlistlevel ) { for( ; m_genlistlevel > numBullets; m_genlistlevel-- ) { // bullets are shrinking, get from old bullet list buf.append( m_renderer.closeList(m_genlistBulletBuffer.charAt(m_genlistlevel - 1)) ); } } } else { char chBullet; int numEqualBullets; int numCheckBullets; // the pattern has changed, unwind and restart // find out how much is the same numEqualBullets = 0; numCheckBullets = Math.min(numBullets,m_genlistlevel); while( numEqualBullets < numCheckBullets ) { // if the bullets are equal so far, keep going if( strBullets.charAt(numEqualBullets) == m_genlistBulletBuffer.charAt(numEqualBullets)) numEqualBullets++; // otherwise giveup, we have found how many are equal else break; } //unwind for( ; m_genlistlevel > numEqualBullets; m_genlistlevel-- ) { buf.append( m_renderer.closeList( m_genlistBulletBuffer.charAt(m_genlistlevel - 1) ) ); } //rewind for(int i = numEqualBullets; i < numBullets; i++) { buf.append( m_renderer.openList( strBullets.charAt(i) ) ); } m_genlistlevel = numBullets; } buf.append( m_renderer.openListItem() ); // work done, remember the new bullet list (in place of old one) m_genlistBulletBuffer.setLength(0); m_genlistBulletBuffer.append(strBullets); return buf.toString(); } private String unwindGeneralList() { // String cStrShortName = "unwindGeneralList()"; StringBuffer buf = new StringBuffer(); int bulletCh; if( m_genlistlevel > 0 ) { buf.append(m_renderer.closeListItem()); } //unwind for( ; m_genlistlevel > 0; m_genlistlevel-- ) { buf.append( m_renderer.closeList( m_genlistBulletBuffer.charAt(m_genlistlevel - 1) ) ); } m_genlistBulletBuffer.setLength(0); return buf.toString(); } private String handleDefinitionList() throws IOException { if( !m_isdefinition ) { m_isdefinition = true; m_closeTag = m_renderer.closeDefinitionItem()+m_renderer.closeDefinitionList(); return m_renderer.openDefinitionList()+m_renderer.openDefinitionTitle(); } return ";"; } private String handleOpenbracket() throws IOException { StringBuffer sb = new StringBuffer(); int ch; boolean isPlugin = false; while( (ch = nextToken()) == '[' ) { sb.append( (char)ch ); } if( ch == '{' ) { isPlugin = true; } pushBack( ch ); if( sb.length() > 0 ) { return sb.toString(); } // // Find end of hyperlink // ch = nextToken(); while( ch != -1 ) { if( ch == ']' && (!isPlugin || sb.charAt( sb.length()-1 ) == '}' ) ) { break; } sb.append( (char) ch ); ch = nextToken(); } if( ch == -1 ) { log.debug("Warning: unterminated link detected!"); return sb.toString(); } return handleHyperlinks( sb.toString() ); } /** * Reads the stream until it meets one of the specified * ending characters, or stream end. The ending character will be left * in the stream. */ private String readUntil( String endChars ) throws IOException { StringBuffer sb = new StringBuffer(); int ch = nextToken(); while( ch != -1 ) { if( ch == '\\' ) { ch = nextToken(); if( ch == -1 ) { break; } } else { if( endChars.indexOf((char)ch) != -1 ) { pushBack( ch ); break; } } sb.append( (char) ch ); ch = nextToken(); } return sb.toString(); } /** * Reads the stream while the characters that have been specified are * in the stream, returning then the result as a String. */ private String readWhile( String endChars ) throws IOException { StringBuffer sb = new StringBuffer(); int ch = nextToken(); while( ch != -1 ) { if( ch == '\\' ) { ch = nextToken(); if( ch == -1 ) { break; } } else { if( endChars.indexOf((char)ch) == -1 ) { pushBack( ch ); break; } } sb.append( (char) ch ); ch = nextToken(); } return sb.toString(); } private String handleDiv( boolean newLine ) throws IOException { int ch = nextToken(); if( ch == '%' ) { StringBuffer sb = new StringBuffer(); String style = null; String clazz = null; ch = nextToken(); // // Style or class? // if( ch == '(' ) { style = readUntil( ")" ); nextToken(); // Pop the ) from the list, too. } else if( Character.isLetter( (char) ch ) ) { pushBack( ch ); clazz = readUntil( " \t\n\r" ); } else { // // Anything else stops. // /* if( m_isOpenParagraph ) { sb.append("</p>\n"); m_isOpenParagraph=false; } */ sb.append( m_renderer.closeDiv() ); return sb.toString(); } // sb.append( newLine ? "<div" : "<span" ); sb.append( m_renderer.openDiv( style, clazz ) );/* sb.append( "<div" ); sb.append( style != null ? " style=\""+style+"\"" : "" ); sb.append( clazz != null ? " class=\""+clazz+"\"" : "" ); sb.append( ">" );*/ return sb.toString(); } pushBack(ch); return "%"; } private String handleBar( boolean newLine ) throws IOException { StringBuffer sb = new StringBuffer(); if( !m_istable && !newLine ) { return "|"; } if( newLine ) { if( !m_istable ) { sb.append( m_renderer.openTable() ); m_istable = true; } sb.append( m_renderer.openTableRow() ); m_closeTag = m_renderer.closeTableItem()+m_renderer.closeTableRow(); } int ch = nextToken();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?