📄 translatorreader.java
字号:
*/ public static String cleanLink( String link ) { StringBuffer clean = new StringBuffer(); if( link == null ) return null; // // Compress away all whitespace and capitalize // all words in between. // StringTokenizer st = new StringTokenizer( link, " -" ); while( st.hasMoreTokens() ) { StringBuffer component = new StringBuffer(st.nextToken()); component.setCharAt(0, Character.toUpperCase( component.charAt(0) ) ); // // We must do this, because otherwise compiling on JDK 1.4 causes // a downwards incompatibility to JDK 1.3. // clean.append( component.toString() ); } // // Remove non-alphanumeric characters that should not // be put inside WikiNames. Note that all valid // Unicode letters are considered okay for WikiNames. // It is the problem of the WikiPageProvider to take // care of actually storing that information. // for( int i = 0; i < clean.length(); i++ ) { if( !(Character.isLetterOrDigit(clean.charAt(i)) || clean.charAt(i) == '_' || clean.charAt(i) == '.') ) { clean.deleteCharAt(i); --i; // We just shortened this buffer. } } return clean.toString(); } /** * Figures out if a link is an off-site link. This recognizes * the most common protocols by checking how it starts. */ private boolean isExternalLink( String link ) { return link.startsWith("http:") || link.startsWith("ftp:") || link.startsWith("https:") || link.startsWith("mailto:") || link.startsWith("news:") || link.startsWith("file:"); } /** * Matches the given link to the list of image name patterns * to determine whether it should be treated as an inline image * or not. */ private boolean isImageLink( String link ) { for( Iterator i = m_inlineImagePatterns.iterator(); i.hasNext(); ) { if( m_inlineMatcher.matches( link, (Pattern) i.next() ) ) return true; } return false; } /** * Returns true, if the argument contains a number, otherwise false. * In a quick test this is roughly the same speed as Integer.parseInt() * if the argument is a number, and roughly ten times the speed, if * the argument is NOT a number. */ private boolean isNumber( String s ) { if( s == null ) return false; if( s.length() > 1 && s.charAt(0) == '-' ) s = s.substring(1); for( int i = 0; i < s.length(); i++ ) { if( !Character.isDigit(s.charAt(i)) ) return false; } return true; } /** * Checks for the existence of a traditional style CamelCase link. * <P> * We separate all white-space -separated words, and feed it to this * routine to find if there are any possible camelcase links. * For example, if "word" is "__HyperLink__" we return "HyperLink". * * @param word A phrase to search in. * @return The match within the phrase. Returns null, if no CamelCase * hyperlink exists within this phrase. */ private String checkForCamelCaseLink( String word ) { PatternMatcherInput input; input = new PatternMatcherInput( word ); if( m_matcher.contains( input, m_camelCasePtrn ) ) { MatchResult res = m_matcher.getMatch(); int start = res.beginOffset(2); int end = res.endOffset(2); String link = res.group(2); String matchedLink; if( res.group(1) != null ) { if( res.group(1).equals("~") || res.group(1).indexOf('[') != -1 ) { // Delete the (~) from beginning. // We'll make '~' the generic kill-processing-character from // now on. return null; } } return link; } // if match return null; } /** * When given a link to a WikiName, we just return * a proper HTML link for it. The local link mutator * chain is also called. */ private String makeCamelCaseLink( String wikiname ) { String matchedLink; String link; callMutatorChain( m_localLinkMutatorChain, wikiname ); if( (matchedLink = linkExists( wikiname )) != null ) { link = makeLink( READ, matchedLink, wikiname ); } else { link = makeLink( EDIT, wikiname, wikiname ); } return link; } /** * Image links are handled differently: * 1. If the text is a WikiName of an existing page, * it gets linked. * 2. If the text is an external link, then it is inlined. * 3. Otherwise it becomes an ALT text. * * @param reallink The link to the image. * @param link Link text portion, may be a link to somewhere else. * @param hasLinkText If true, then the defined link had a link text available. * This means that the link text may be a link to a wiki page, * or an external resource. */ private String handleImageLink( String reallink, String link, boolean hasLinkText ) { String possiblePage = cleanLink( link ); String matchedLink; String res = ""; if( isExternalLink( link ) && hasLinkText ) { res = makeLink( IMAGELINK, reallink, link ); } else if( (matchedLink = linkExists( possiblePage )) != null && hasLinkText ) { // System.out.println("Orig="+link+", Matched: "+matchedLink); callMutatorChain( m_localLinkMutatorChain, possiblePage ); res = makeLink( IMAGEWIKILINK, reallink, link ); } else { res = makeLink( IMAGE, reallink, link ); } return res; } /** * If outlink images are turned on, returns a link to the outward * linking image. */ private final String outlinkImage() { if( m_useOutlinkImage ) { return "<img class=\"outlink\" src=\""+m_engine.getBaseURL()+"images/out.png\" alt=\"\" />"; } return ""; } /** * Gobbles up all hyperlinks that are encased in square brackets. */ private String handleHyperlinks( String link ) { StringBuffer sb = new StringBuffer(); String reallink; int cutpoint; // // Start with plugin links. // if( PluginManager.isPluginLink( link ) ) { String included; try { included = m_engine.getPluginManager().execute( m_context, link ); } catch( PluginException e ) { log.error( "Failed to insert plugin", e ); log.error( "Root cause:",e.getRootThrowable() ); included = "<FONT COLOR=\"#FF0000\">Plugin insertion failed: "+e.getMessage()+"</FONT>"; } sb.append( included ); return sb.toString(); } link = TextUtil.replaceEntities( link ); if( (cutpoint = link.indexOf('|')) != -1 ) { reallink = link.substring( cutpoint+1 ).trim(); link = link.substring( 0, cutpoint ); } else { reallink = link.trim(); } int interwikipoint = -1; // // Yes, we now have the components separated. // link = the text the link should have // reallink = the url or page name. // // In many cases these are the same. [link|reallink]. // if( VariableManager.isVariableLink( link ) ) { String value; try { value = m_engine.getVariableManager().parseAndGetValue( m_context, link ); } catch( NoSuchVariableException e ) { value = "<FONT COLOR=\"#FF0000\">"+e.getMessage()+"</FONT>"; } catch( IllegalArgumentException e ) { value = "<FONT COLOR=\"#FF0000\">"+e.getMessage()+"</FONT>"; } sb.append( value ); } else if( isExternalLink( reallink ) ) { // It's an external link, out of this Wiki callMutatorChain( m_externalLinkMutatorChain, reallink ); if( isImageLink( reallink ) ) { sb.append( handleImageLink( reallink, link, (cutpoint != -1) ) ); } else { sb.append( makeLink( EXTERNAL, reallink, link ) ); sb.append( outlinkImage() ); } } else if( (interwikipoint = reallink.indexOf(":")) != -1 ) { // It's an interwiki link // InterWiki links also get added to external link chain // after the links have been resolved. // FIXME: There is an interesting issue here: We probably should // URLEncode the wikiPage, but we can't since some of the // Wikis use slashes (/), which won't survive URLEncoding. // Besides, we don't know which character set the other Wiki // is using, so you'll have to write the entire name as it appears // in the URL. Bugger. String extWiki = reallink.substring( 0, interwikipoint ); String wikiPage = reallink.substring( interwikipoint+1 ); String urlReference = m_engine.getInterWikiURL( extWiki ); if( urlReference != null ) { urlReference = TextUtil.replaceString( urlReference, "%s", wikiPage ); callMutatorChain( m_externalLinkMutatorChain, urlReference ); sb.append( makeLink( INTERWIKI, urlReference, link ) ); if( isExternalLink(urlReference) ) { sb.append( outlinkImage() ); } } else { sb.append( link+" <FONT COLOR=\"#FF0000\">(No InterWiki reference defined in properties for Wiki called '"+extWiki+"'!)</FONT>"); } } else if( reallink.startsWith("#") ) { // It defines a local footnote sb.append( makeLink( LOCAL, reallink, link ) ); } else if( isNumber( reallink ) ) { // It defines a reference to a local footnote sb.append( makeLink( LOCALREF, reallink, link ) ); } else { // // Internal wiki link, but is it an attachment link? // String attachment = findAttachment( reallink ); if( attachment != null ) { callMutatorChain( m_attachmentLinkMutatorChain, attachment ); if( isImageLink( reallink ) ) { attachment = m_engine.getAttachmentURL(attachment); sb.append( handleImageLink( attachment, link, (cutpoint != -1) ) ); } else { sb.append( makeLink( ATTACHMENT, attachment, link ) ); } } else { // It's an internal Wiki link reallink = cleanLink( reallink ); callMutatorChain( m_localLinkMutatorChain, reallink ); String matchedLink; if( (matchedLink = linkExists( reallink )) != null ) { sb.append( makeLink( READ, matchedLink, link ) ); } else { sb.append( makeLink( EDIT, reallink, link ) ); } } } return sb.toString(); } private String findAttachment( String link ) { AttachmentManager mgr = m_engine.getAttachmentManager(); WikiPage currentPage = m_context.getPage(); Attachment att = null; /* System.out.println("Finding attachment of page "+currentPage.getName()); System.out.println("With name "+link); */ try { att = mgr.getAttachmentInfo( m_context, link ); } catch( ProviderException e ) { log.warn("Finding attachments failed: ",e); return null; } if( att != null ) { return att.getName(); } else if( link.indexOf('/') != -1 ) { return link; } return null; } /** * Closes all annoying lists and things that the user might've * left open. */ private String closeAll() { StringBuffer buf = new StringBuffer(); if( m_isbold ) { buf.append("</B>"); m_isbold = false; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -