📄 pluginmanager.java
字号:
return msg; } // // Create... // try { plugin = pluginInfo.newPluginInstance(); } catch( InstantiationException e ) { throw new PluginException( MessageFormat.format( rb.getString( "plugin.error.cannotinstantiate" ), args ), e ); } catch( IllegalAccessException e ) { throw new PluginException( MessageFormat.format( rb.getString( "plugin.error.notallowed" ), args ), e ); } catch( Exception e ) { throw new PluginException( MessageFormat.format( rb.getString( "plugin.error.instantationfailed" ), args), e ); } // // ...and launch. // try { return plugin.execute( context, params ); } catch( PluginException e ) { if( debug ) { return stackTrace( params, e ); } // Just pass this exception onward. throw (PluginException) e.fillInStackTrace(); } catch( Throwable t ) { // But all others get captured here. log.info( "Plugin failed while executing:", t ); if( debug ) { return stackTrace( params, t ); } throw new PluginException( rb.getString( "plugin.error.failed" ), t ); } } catch( ClassNotFoundException e ) { throw new PluginException( MessageFormat.format( rb.getString( "plugin.error.couldnotfind" ), args ), e ); } catch( ClassCastException e ) { throw new PluginException( MessageFormat.format( rb.getString( "plugin.error.notawikiplugin" ), args ), e ); } } /** * Parses plugin arguments. Handles quotes and all other kewl stuff. * * <h3>Special parameters</h3> * The plugin body is put into a special parameter defined by {@link #PARAM_BODY}; * the plugin's command line into a parameter defined by {@link #PARAM_CMDLINE}; * and the bounds of the plugin within the wiki page text by a parameter defined * by {@link #PARAM_BOUNDS}, whose value is stored as a two-element int[] array, * i.e., <tt>[start,end]</tt>. * * @param argstring The argument string to the plugin. This is * typically a list of key-value pairs, using "'" to escape * spaces in strings, followed by an empty line and then the * plugin body. In case the parameter is null, will return an * empty parameter list. * * @return A parsed list of parameters. * * @throws IOException If the parsing fails. */ public Map parseArgs( String argstring ) throws IOException { HashMap<String, Object> arglist = new HashMap<String, Object>(); // // Protection against funny users. // if( argstring == null ) return arglist; arglist.put( PARAM_CMDLINE, argstring ); StringReader in = new StringReader(argstring); StreamTokenizer tok = new StreamTokenizer(in); int type; String param = null; String value = null; tok.eolIsSignificant( true ); boolean potentialEmptyLine = false; boolean quit = false; while( !quit ) { String s; type = tok.nextToken(); switch( type ) { case StreamTokenizer.TT_EOF: quit = true; s = null; break; case StreamTokenizer.TT_WORD: s = tok.sval; potentialEmptyLine = false; break; case StreamTokenizer.TT_EOL: quit = potentialEmptyLine; potentialEmptyLine = true; s = null; break; case StreamTokenizer.TT_NUMBER: s = Integer.toString( (int) tok.nval ); potentialEmptyLine = false; break; case '\'': s = tok.sval; break; default: s = null; } // // Assume that alternate words on the line are // parameter and value, respectively. // if( s != null ) { if( param == null ) { param = s; } else { value = s; arglist.put( param, value ); // log.debug("ARG: "+param+"="+value); param = null; } } } // // Now, we'll check the body. // if( potentialEmptyLine ) { StringWriter out = new StringWriter(); FileUtil.copyContents( in, out ); String bodyContent = out.toString(); if( bodyContent != null ) { arglist.put( PARAM_BODY, bodyContent ); } } return arglist; } /** * Parses a plugin. Plugin commands are of the form: * [{INSERT myplugin WHERE param1=value1, param2=value2}] * myplugin may either be a class name or a plugin alias. * <P> * This is the main entry point that is used. * * @param context The current WikiContext. * @param commandline The full command line, including plugin * name, parameters and body. * * @return HTML as returned by the plugin, or possibly an error * message. * * @throws PluginException From the plugin itself, it propagates, waah! */ public String execute( WikiContext context, String commandline ) throws PluginException { if( !m_pluginsEnabled ) return ""; ResourceBundle rb = context.getBundle(WikiPlugin.CORE_PLUGINS_RESOURCEBUNDLE); Object[] obArgs = { commandline }; PatternMatcher matcher = new Perl5Matcher(); try { if( matcher.contains( commandline, m_pluginPattern ) ) { MatchResult res = matcher.getMatch(); String plugin = res.group(2); String args = commandline.substring(res.endOffset(0), commandline.length() - (commandline.charAt(commandline.length()-1) == '}' ? 1 : 0 ) ); Map arglist = parseArgs( args ); return execute( context, plugin, arglist ); } } catch( NoSuchElementException e ) { String msg = "Missing parameter in plugin definition: "+commandline; log.warn( msg, e ); throw new PluginException( MessageFormat.format( rb.getString( "plugin.error.missingparameter" ), obArgs ) ); } catch( IOException e ) { String msg = "Zyrf. Problems with parsing arguments: "+commandline; log.warn( msg, e ); throw new PluginException( MessageFormat.format( rb.getString( "plugin.error.parsingarguments" ), obArgs ) ); } // FIXME: We could either return an empty string "", or // the original line. If we want unsuccessful requests // to be invisible, then we should return an empty string. return commandline; } /** * Parses a plugin invocation and returns a DOM element. * * @param context The WikiContext * @param commandline The line to parse * @param pos The position in the stream parsing. * @return A DOM element * @throws PluginException If plugin invocation is faulty */ @SuppressWarnings("unchecked") public PluginContent parsePluginLine( WikiContext context, String commandline, int pos ) throws PluginException { PatternMatcher matcher = new Perl5Matcher(); try { if( matcher.contains( commandline, m_pluginPattern ) ) { MatchResult res = matcher.getMatch(); String plugin = res.group(2); String args = commandline.substring(res.endOffset(0), commandline.length() - (commandline.charAt(commandline.length()-1) == '}' ? 1 : 0 ) ); Map<String, Object> arglist = parseArgs( args ); // set wikitext bounds of plugin as '_bounds' parameter, e.g., [345,396] if ( pos != -1 ) { int end = pos + commandline.length() + 2; int[] bounds = new int[] { pos, end }; arglist.put( PARAM_BOUNDS, bounds ); } PluginContent result = new PluginContent( plugin, arglist ); return result; } } catch( ClassCastException e ) { log.error( "Invalid type offered in parsing plugin arguments.", e ); throw new InternalWikiException("Oops, someone offered !String!"); } catch( NoSuchElementException e ) { String msg = "Missing parameter in plugin definition: "+commandline; log.warn( msg, e ); throw new PluginException( msg ); } catch( IOException e ) { String msg = "Zyrf. Problems with parsing arguments: "+commandline; log.warn( msg, e ); throw new PluginException( msg ); } return null; } /** * Register a plugin. */ private void registerPlugin(WikiPluginInfo pluginClass) { String name; // Registrar the plugin with the className without the package-part name = pluginClass.getName(); if(name != null) { log.debug("Registering plugin [name]: " + name); m_pluginClassMap.put(name, pluginClass); } // Registrar the plugin with a short convenient name. name = pluginClass.getAlias(); if(name != null) { log.debug("Registering plugin [shortName]: " + name); m_pluginClassMap.put(name, pluginClass); } // Registrar the plugin with the className with the package-part name = pluginClass.getClassName(); if(name != null) { log.debug("Registering plugin [className]: " + name); m_pluginClassMap.put(name, pluginClass); } pluginClass.initializePlugin( m_engine ); } private void registerPlugins() { log.info( "Registering plugins" ); SAXBuilder builder = new SAXBuilder(); try { // // Register all plugins which have created a resource containing its properties. // // Get all resources of all plugins. // Enumeration resources = getClass().getClassLoader().getResources( PLUGIN_RESOURCE_LOCATION );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -