📄 translatorreader.java
字号:
/* JSPWiki - a JSP-based WikiWiki clone. Copyright (C) 2001-2005 Janne Jalkanen (Janne.Jalkanen@iki.fi) This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package com.ecyrd.jspwiki;import java.io.*;import java.util.*;import org.apache.log4j.Logger;import org.apache.oro.text.*;import org.apache.oro.text.regex.*;import com.ecyrd.jspwiki.plugin.PluginManager;import com.ecyrd.jspwiki.plugin.PluginException;import com.ecyrd.jspwiki.attachment.AttachmentManager;import com.ecyrd.jspwiki.attachment.Attachment;import com.ecyrd.jspwiki.providers.ProviderException;import com.ecyrd.jspwiki.acl.AccessControlList;import com.ecyrd.jspwiki.auth.modules.PageAuthorizer;import com.ecyrd.jspwiki.auth.WikiSecurityException;import com.ecyrd.jspwiki.auth.UserManager;/** * Handles conversion from Wiki format into fully featured HTML. * This is where all the magic happens. It is CRITICAL that this * class is tested, or all Wikis might die horribly. * <P> * The output of the HTML has not yet been validated against * the HTML DTD. However, it is very simple. * * @author Janne Jalkanen */public class TranslatorReader extends Reader{ public static final int READ = 0; public static final int EDIT = 1; private static final int EMPTY = 2; // Empty message private static final int LOCAL = 3; private static final int LOCALREF = 4; private static final int IMAGE = 5; private static final int EXTERNAL = 6; private static final int INTERWIKI = 7; private static final int IMAGELINK = 8; private static final int IMAGEWIKILINK = 9; public static final int ATTACHMENT = 10; private static final int ATTACHMENTIMAGE = 11; /** Lists all punctuation characters allowed in WikiMarkup. These will not be cleaned away. */ private static final String PUNCTUATION_CHARS_ALLOWED = "._"; /** Allow this many characters to be pushed back in the stream. In effect, this limits the size of a single heading line. */ private static final int PUSHBACK_BUFFER_SIZE = 10*1024; private PushbackReader m_in; private StringReader m_data = new StringReader(""); private static Logger log = Logger.getLogger( TranslatorReader.class ); //private boolean m_iscode = false; private boolean m_isbold = false; private boolean m_isitalic = false; private boolean m_isTypedText = false; private boolean m_istable = false; private boolean m_isPre = false; private boolean m_isEscaping = false; private boolean m_isdefinition = false; /** Contains style information, in multiple forms. */ private Stack m_styleStack = new Stack(); // general list handling private int m_genlistlevel = 0; private StringBuffer m_genlistBulletBuffer = new StringBuffer(); // stores the # and * pattern private boolean m_allowPHPWikiStyleLists = true; private boolean m_isOpenParagraph = false; /** Tag that gets closed at EOL. */ private String m_closeTag = null; private WikiEngine m_engine; private WikiContext m_context; /** Optionally stores internal wikilinks */ private ArrayList m_localLinkMutatorChain = new ArrayList(); private ArrayList m_externalLinkMutatorChain = new ArrayList(); private ArrayList m_attachmentLinkMutatorChain = new ArrayList(); private ArrayList m_headingListenerChain = new ArrayList(); /** Keeps image regexp Patterns */ private ArrayList m_inlineImagePatterns; private PatternMatcher m_inlineMatcher = new Perl5Matcher(); private ArrayList m_linkMutators = new ArrayList(); /** * This property defines the inline image pattern. It's current value * is jspwiki.translatorReader.inlinePattern */ public static final String PROP_INLINEIMAGEPTRN = "jspwiki.translatorReader.inlinePattern"; /** If true, consider CamelCase hyperlinks as well. */ public static final String PROP_CAMELCASELINKS = "jspwiki.translatorReader.camelCaseLinks"; /** If true, all hyperlinks are translated as well, regardless whether they are surrounded by brackets. */ public static final String PROP_PLAINURIS = "jspwiki.translatorReader.plainUris"; /** If true, all outward links (external links) have a small link image appended. */ public static final String PROP_USEOUTLINKIMAGE = "jspwiki.translatorReader.useOutlinkImage"; /** If set to "true", allows using raw HTML within Wiki text. Be warned, this is a VERY dangerous option to set - never turn this on in a publicly allowable Wiki, unless you are absolutely certain of what you're doing. */ public static final String PROP_ALLOWHTML = "jspwiki.translatorReader.allowHTML"; /** If set to "true", all external links are tagged with 'rel="nofollow"' */ public static final String PROP_USERELNOFOLLOW = "jspwiki.translatorReader.useRelNofollow"; /** If set to "true", enables plugins during parsing */ public static final String PROP_RUNPLUGINS = "jspwiki.translatorReader.runPlugins"; /** If true, then considers CamelCase links as well. */ private boolean m_camelCaseLinks = false; /** If true, consider URIs that have no brackets as well. */ // FIXME: Currently reserved, but not used. private boolean m_plainUris = false; /** If true, all outward links use a small link image. */ private boolean m_useOutlinkImage = true; /** If true, allows raw HTML. */ private boolean m_allowHTML = false; /** If true, executes plugins; otherwise ignores them. */ private boolean m_enablePlugins = true; private boolean m_useRelNofollow = false; private boolean m_inlineImages = true; private PatternMatcher m_matcher = new Perl5Matcher(); private PatternCompiler m_compiler = new Perl5Compiler(); private Pattern m_camelCasePtrn; private TextRenderer m_renderer; /** * The default inlining pattern. Currently "*.png" */ public static final String DEFAULT_INLINEPATTERN = "*.png"; /** * These characters constitute word separators when trying * to find CamelCase links. */ private static final String WORD_SEPARATORS = ",.|;+=&()"; protected static final int BOLD = 0; protected static final int ITALIC = 1; protected static final int TYPED = 2; /** * This list contains all IANA registered URI protocol * types as of September 2004 + a few well-known extra types. * * JSPWiki recognises all of them as external links. */ static final String[] c_externalLinks = { "http:", "ftp:", "https:", "mailto:", "news:", "file:", "rtsp:", "mms:", "ldap:", "gopher:", "nntp:", "telnet:", "wais:", "prospero:", "z39.50s", "z39.50r", "vemmi:", "imap:", "nfs:", "acap:", "tip:", "pop:", "dav:", "opaquelocktoken:", "sip:", "sips:", "tel:", "fax:", "modem:", "soap.beep:", "soap.beeps", "xmlrpc.beep", "xmlrpc.beeps", "urn:", "go:", "h323:", "ipp:", "tftp:", "mupdate:", "pres:", "im:", "mtqp", "smb:" }; /** * Creates a TranslatorReader using the default HTML renderer. */ public TranslatorReader( WikiContext context, Reader in ) { initialize( context, in, new HTMLRenderer() ); } public TranslatorReader( WikiContext context, Reader in, TextRenderer renderer ) { initialize( context, in, renderer ); } /** * Replaces the current input character stream with a new one. * @param in New source for input. If null, this method does nothing. * @return the old stream */ public Reader setInputReader( Reader in ) { Reader old = m_in; if( in != null ) { m_in = new PushbackReader( new BufferedReader( in ), PUSHBACK_BUFFER_SIZE ); } return old; } /** * @param engine The WikiEngine this reader is attached to. Is * used to figure out of a page exits. */ // FIXME: TranslatorReaders should be pooled for better performance. private void initialize( WikiContext context, Reader in, TextRenderer renderer ) { PatternCompiler compiler = new GlobCompiler(); ArrayList compiledpatterns = new ArrayList(); m_engine = context.getEngine(); m_context = context; m_renderer = renderer; setInputReader( in ); Collection ptrns = getImagePatterns( m_engine ); // // Make them into Regexp Patterns. Unknown patterns // are ignored. // for( Iterator i = ptrns.iterator(); i.hasNext(); ) { try { compiledpatterns.add( compiler.compile( (String)i.next() ) ); } catch( MalformedPatternException e ) { log.error("Malformed pattern in properties: ", e ); } } m_inlineImagePatterns = compiledpatterns; try { m_camelCasePtrn = m_compiler.compile( "^([[:^alnum:]]*)([[:upper:]]+[[:lower:]]+[[:upper:]]+[[:alnum:]]*)[[:^alnum:]]*$" ); } catch( MalformedPatternException e ) { log.fatal("Internal error: Someone put in a faulty pattern.",e); throw new InternalWikiException("Faulty camelcasepattern in TranslatorReader"); } // // Set the properties. // Properties props = m_engine.getWikiProperties(); String cclinks = (String)m_context.getPage().getAttribute( PROP_CAMELCASELINKS ); if( cclinks != null ) { m_camelCaseLinks = TextUtil.isPositive( cclinks ); } else { m_camelCaseLinks = TextUtil.getBooleanProperty( props, PROP_CAMELCASELINKS, m_camelCaseLinks ); } m_plainUris = TextUtil.getBooleanProperty( props, PROP_PLAINURIS, m_plainUris ); m_useOutlinkImage = TextUtil.getBooleanProperty( props, PROP_USEOUTLINKIMAGE, m_useOutlinkImage ); m_allowHTML = TextUtil.getBooleanProperty( props, PROP_ALLOWHTML, m_allowHTML ); m_useRelNofollow = TextUtil.getBooleanProperty( props, PROP_USERELNOFOLLOW, m_useRelNofollow ); String runplugins = m_engine.getVariable( m_context, PROP_RUNPLUGINS ); if( runplugins != null ) enablePlugins( TextUtil.isPositive(runplugins)); if( m_engine.getUserManager() == null || m_engine.getUserManager().getAuthenticator() == null ) { disableAccessRules(); } m_context.getPage().setHasMetadata(); } /** * Sets the currently used renderer. This method is protected because * we only want to use it internally for now. The renderer interface * is not yet set to stone, so it's not expected that third parties * would use this. */ protected void setRenderer( TextRenderer renderer ) { m_renderer = renderer; } /** * Adds a hook for processing link texts. This hook is called * when the link text is written into the output stream, and * you may use it to modify the text. It does not affect the * actual link, only the user-visible text. * * @param mutator The hook to call. Null is safe. */ public void addLinkTransmutator( StringTransmutator mutator ) { if( mutator != null ) { m_linkMutators.add( mutator ); } } /** * Adds a hook for processing local links. The engine * transforms both non-existing and existing page links. * * @param mutator The hook to call. Null is safe. */ public void addLocalLinkHook( StringTransmutator mutator ) { if( mutator != null ) { m_localLinkMutatorChain.add( mutator ); } } /** * Adds a hook for processing external links. This includes * all http:// ftp://, etc. links, including inlined images. * * @param mutator The hook to call. Null is safe. */ public void addExternalLinkHook( StringTransmutator mutator ) { if( mutator != null ) { m_externalLinkMutatorChain.add( mutator ); } } /** * Adds a hook for processing attachment links. * * @param mutator The hook to call. Null is safe. */ public void addAttachmentLinkHook( StringTransmutator mutator ) { if( mutator != null ) { m_attachmentLinkMutatorChain.add( mutator ); } } public void addHeadingListener( HeadingListener listener ) { if( listener != null ) { m_headingListenerChain.add( listener ); } } private boolean m_parseAccessRules = true; public void disableAccessRules() { m_parseAccessRules = false; } /** * Can be used to turn on plugin execution on a translator-reader basis */ public void enablePlugins( boolean toggle ) { m_enablePlugins = toggle; } /** * Use this to turn on or off image inlining. * @param toggle If true, images are inlined (as per set in jspwiki.properties) * If false, then images won't be inlined; instead, they will be * treated as standard hyperlinks. * @since 2.2.9 */ public void enableImageInlining( boolean toggle ) { m_inlineImages = toggle; } /** * Figure out which image suffixes should be inlined. * @return Collection of Strings with patterns. */ protected static Collection getImagePatterns( WikiEngine engine ) { Properties props = engine.getWikiProperties();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -