Search in sources :

Example 1 with PluginManager

use of org.apache.wiki.api.engine.PluginManager in project jspwiki by apache.

the class PluginContent method invoke.

/**
 * Performs plugin invocation and return its contents.
 *
 * @param context WikiContext in which the plugin is executed. Must NOT be null.
 * @return plugin contents.
 */
public String invoke(WikiContext context) {
    String result;
    Boolean wysiwygVariable = (Boolean) context.getVariable(RenderingManager.WYSIWYG_EDITOR_MODE);
    boolean wysiwygEditorMode = false;
    if (wysiwygVariable != null) {
        wysiwygEditorMode = wysiwygVariable.booleanValue();
    }
    try {
        // FIXME: The plugin name matching should not be done here, but in a per-editor resource
        if (wysiwygEditorMode && !m_pluginName.matches(EMITTABLE_PLUGINS)) {
            result = PLUGIN_START + m_pluginName + SPACE;
            // convert newlines to <br> in case the plugin has a body.
            String cmdLine = (m_params.get(CMDLINE)).replaceAll(LINEBREAK, ELEMENT_BR);
            result = result + cmdLine + PLUGIN_END;
        } else {
            Boolean b = (Boolean) context.getVariable(RenderingManager.VAR_EXECUTE_PLUGINS);
            if (b != null && !b.booleanValue()) {
                return BLANK;
            }
            WikiEngine engine = context.getEngine();
            Map<String, String> parsedParams = new HashMap<String, String>();
            // 
            for (Map.Entry<String, String> e : m_params.entrySet()) {
                String val = e.getValue();
                val = engine.getVariableManager().expandVariables(context, val);
                parsedParams.put(e.getKey(), val);
            }
            PluginManager pm = engine.getPluginManager();
            result = pm.execute(context, m_pluginName, parsedParams);
        }
    } catch (Exception e) {
        if (wysiwygEditorMode) {
            result = "";
        } else {
            // log.info("Failed to execute plugin",e);
            ResourceBundle rb = Preferences.getBundle(context, WikiPlugin.CORE_PLUGINS_RESOURCEBUNDLE);
            result = MarkupParser.makeError(MessageFormat.format(rb.getString("plugin.error.insertionfailed"), context.getRealPage().getWiki(), context.getRealPage().getName(), e.getMessage())).getText();
        }
    }
    return result;
}
Also used : PluginManager(org.apache.wiki.api.engine.PluginManager) HashMap(java.util.HashMap) ResourceBundle(java.util.ResourceBundle) WikiEngine(org.apache.wiki.WikiEngine) HashMap(java.util.HashMap) Map(java.util.Map) IOException(java.io.IOException) InternalWikiException(org.apache.wiki.InternalWikiException) PluginException(org.apache.wiki.api.exceptions.PluginException) NoSuchElementException(java.util.NoSuchElementException)

Example 2 with PluginManager

use of org.apache.wiki.api.engine.PluginManager in project jspwiki by apache.

the class PluginContent method executeParse.

/**
 * Executes the executeParse() method.
 *
 * @param context The WikiContext
 * @throws PluginException If something goes wrong.
 */
public void executeParse(WikiContext context) throws PluginException {
    PluginManager pm = context.getEngine().getPluginManager();
    if (pm.pluginsEnabled()) {
        ResourceBundle rb = Preferences.getBundle(context, WikiPlugin.CORE_PLUGINS_RESOURCEBUNDLE);
        Map<String, String> params = getParameters();
        WikiPlugin plugin = pm.newWikiPlugin(getPluginName(), rb);
        try {
            if (plugin != null && plugin instanceof ParserStagePlugin) {
                ((ParserStagePlugin) plugin).executeParser(this, context, params);
            }
        } catch (ClassCastException e) {
            throw new PluginException(MessageFormat.format(rb.getString("plugin.error.notawikiplugin"), getPluginName()), e);
        }
    }
}
Also used : PluginManager(org.apache.wiki.api.engine.PluginManager) ParserStagePlugin(org.apache.wiki.api.plugin.ParserStagePlugin) PluginException(org.apache.wiki.api.exceptions.PluginException) ResourceBundle(java.util.ResourceBundle) WikiPlugin(org.apache.wiki.api.plugin.WikiPlugin)

Example 3 with PluginManager

use of org.apache.wiki.api.engine.PluginManager in project jspwiki by apache.

the class FormOutput method execute.

/**
 * Executes the FormHandler specified in a Form 'output' plugin,
 * using entries provided in the HttpRequest as FormHandler
 * parameters.
 * <p>
 * If the parameter 'populate' was given, the WikiPlugin it names
 * is used to get default values. (It probably makes a lot of
 * sense for this to be the same plugin as the handler.)
 * Information for the populator can be given with the FormSet
 * plugin. If 'populate' is not specified, the form is not
 * displayed.
 * <p>
 * Should there be no HTTP request associated with this request,
 * the method will return immediately with an empty string.
 *
 * @param ctx {@inheritDoc}
 * @param params {@inheritDoc}
 * @return {@inheritDoc}
 */
public String execute(WikiContext ctx, Map<String, String> params) throws PluginException {
    // 
    if (ctx.getHttpRequest() == null) {
        return "";
    }
    ResourceBundle rb = Preferences.getBundle(ctx, WikiPlugin.CORE_PLUGINS_RESOURCEBUNDLE);
    // If we are NOT here due to this form being submitted, we do nothing.
    // The submitted form MUST have parameter 'formname' equal to the name
    // parameter of this Form plugin.
    String formName = params.get(PARAM_FORM);
    String submitForm = ctx.getHttpParameter(PARAM_FORMNAMEHIDDEN);
    String populator = params.get(PARAM_POPULATE);
    if (submitForm == null || formName == null || !formName.equals(submitForm)) {
        // the context, otherwise we'll just hide.
        if (populator == null || !PARAM_HANDLER.equals(populator))
            return "";
    // If population was allowed, we should first
    }
    String handler = params.get(PARAM_HANDLER);
    if (handler == null || handler.length() == 0) {
        // Need to print out an error here as this form is misconfigured
        return "<p class=\"error\">" + MessageFormat.format(rb.getString("formoutput.missingargument"), PARAM_HANDLER) + "</p>";
    }
    String sourcePage = ctx.getPage().getName();
    String submitServlet = ctx.getURL(WikiContext.VIEW, sourcePage);
    // If there is previous FormInfo available - say, from a
    // FormSet plugin - use it.
    FormInfo info = getFormInfo(ctx);
    if (info == null) {
        // Reconstruct the form info from post data
        info = new FormInfo();
        info.setName(formName);
    }
    // Force override of handler and submit.
    info.setHandler(handler);
    info.setAction(submitServlet);
    // Sift out all extra parameters, leaving only those submitted
    // in the HTML FORM.
    Map<String, String> handlerParams = FormUtil.requestToMap(ctx.getHttpRequest(), HANDLERPARAM_PREFIX);
    // Previous submission info may be available from FormSet
    // plugin - add, don't replace.
    info.addSubmission(handlerParams);
    // Pass the _body parameter from FormOutput on to the handler
    info.getSubmission().put(DefaultPluginManager.PARAM_BODY, params.get(DefaultPluginManager.PARAM_BODY));
    String handlerOutput = null;
    String error = null;
    try {
        // The plugin _can_ modify the parameters, so we make sure
        // they stay with us.
        PluginManager pm = ctx.getEngine().getPluginManager();
        handlerOutput = pm.execute(ctx, handler, info.getSubmission());
        info.setResult(handlerOutput);
        info.setStatus(FormInfo.EXECUTED);
    } catch (PluginException pe) {
        error = "<p class=\"error\">" + pe.getMessage() + "</p>";
        info.setError(error);
        info.setStatus(FormInfo.ERROR);
    }
    // We store the forminfo, so following Form plugin invocations on this
    // page can decide what to do based on its values.
    storeFormInfo(ctx, info);
    if (error != null)
        return error;
    return handlerOutput != null ? handlerOutput : "";
}
Also used : DefaultPluginManager(org.apache.wiki.plugin.DefaultPluginManager) PluginManager(org.apache.wiki.api.engine.PluginManager) PluginException(org.apache.wiki.api.exceptions.PluginException) ResourceBundle(java.util.ResourceBundle)

Example 4 with PluginManager

use of org.apache.wiki.api.engine.PluginManager in project jspwiki by apache.

the class PluginContent method parsePluginLine.

/**
 * 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
 * @since 2.10.0
 */
public static PluginContent parsePluginLine(WikiContext context, String commandline, int pos) throws PluginException {
    PatternMatcher matcher = new Perl5Matcher();
    try {
        PluginManager pm = context.getEngine().getPluginManager();
        if (matcher.contains(commandline, pm.getPluginPattern())) {
            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, String> arglist = pm.parseArgs(args);
            // set wikitext bounds of plugin as '_bounds' parameter, e.g., [345,396]
            if (pos != -1) {
                int end = pos + commandline.length() + 2;
                String bounds = pos + "|" + end;
                arglist.put(PluginManager.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!", e);
    } 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;
}
Also used : PluginException(org.apache.wiki.api.exceptions.PluginException) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) IOException(java.io.IOException) MatchResult(org.apache.oro.text.regex.MatchResult) InternalWikiException(org.apache.wiki.InternalWikiException) PluginManager(org.apache.wiki.api.engine.PluginManager) PatternMatcher(org.apache.oro.text.regex.PatternMatcher) NoSuchElementException(java.util.NoSuchElementException)

Example 5 with PluginManager

use of org.apache.wiki.api.engine.PluginManager in project jspwiki by apache.

the class PluginTag method executePlugin.

private String executePlugin(String plugin, String args, String body) throws PluginException, IOException {
    WikiEngine engine = m_wikiContext.getEngine();
    PluginManager pm = engine.getPluginManager();
    m_evaluated = true;
    Map<String, String> argmap = pm.parseArgs(args);
    if (body != null) {
        argmap.put("_body", body);
    }
    String result = pm.execute(m_wikiContext, plugin, argmap);
    return result;
}
Also used : PluginManager(org.apache.wiki.api.engine.PluginManager) WikiEngine(org.apache.wiki.WikiEngine)

Aggregations

PluginManager (org.apache.wiki.api.engine.PluginManager)6 PluginException (org.apache.wiki.api.exceptions.PluginException)4 IOException (java.io.IOException)3 ResourceBundle (java.util.ResourceBundle)3 NoSuchElementException (java.util.NoSuchElementException)2 InternalWikiException (org.apache.wiki.InternalWikiException)2 WikiEngine (org.apache.wiki.WikiEngine)2 File (java.io.File)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 MatchResult (org.apache.oro.text.regex.MatchResult)1 PatternMatcher (org.apache.oro.text.regex.PatternMatcher)1 Perl5Matcher (org.apache.oro.text.regex.Perl5Matcher)1 AdminBeanManager (org.apache.wiki.api.engine.AdminBeanManager)1 FilterManager (org.apache.wiki.api.engine.FilterManager)1 FilterException (org.apache.wiki.api.exceptions.FilterException)1 NoSuchVariableException (org.apache.wiki.api.exceptions.NoSuchVariableException)1 ProviderException (org.apache.wiki.api.exceptions.ProviderException)1