Search in sources :

Example 1 with PluginManager

use of org.apache.wiki.plugin.PluginManager in project jspwiki by apache.

the class PluginContent method executeParse.

/**
 *{@inheritDoc}
 */
@Override
public void executeParse(final Context context) throws PluginException {
    final PluginManager pm = context.getEngine().getManager(PluginManager.class);
    if (pm.pluginsEnabled()) {
        final ResourceBundle rb = Preferences.getBundle(context, Plugin.CORE_PLUGINS_RESOURCEBUNDLE);
        final Map<String, String> params = getParameters();
        final Plugin plugin = pm.newWikiPlugin(getPluginName(), rb);
        try {
            if (plugin instanceof ParserStagePlugin) {
                ((ParserStagePlugin) plugin).executeParser(this, context, params);
            }
        } catch (final ClassCastException e) {
            throw new PluginException(MessageFormat.format(rb.getString("plugin.error.notawikiplugin"), getPluginName()), e);
        }
    }
}
Also used : PluginManager(org.apache.wiki.plugin.PluginManager) ParserStagePlugin(org.apache.wiki.api.plugin.ParserStagePlugin) PluginException(org.apache.wiki.api.exceptions.PluginException) ResourceBundle(java.util.ResourceBundle) ParserStagePlugin(org.apache.wiki.api.plugin.ParserStagePlugin) Plugin(org.apache.wiki.api.plugin.Plugin)

Example 2 with PluginManager

use of org.apache.wiki.plugin.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}
 */
@Override
public String execute(final Context ctx, final Map<String, String> params) throws PluginException {
    // If there is no HTTP request, returns immediately.
    if (ctx.getHttpRequest() == null) {
        return "";
    }
    final ResourceBundle rb = Preferences.getBundle(ctx, Plugin.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.
    final String formName = params.get(PARAM_FORM);
    final String submitForm = ctx.getHttpParameter(PARAM_FORMNAMEHIDDEN);
    final String populator = params.get(PARAM_POPULATE);
    if (formName == null || !formName.equals(submitForm)) {
        // the context, otherwise we'll just hide.
        if (!PARAM_HANDLER.equals(populator))
            return "";
    // If population was allowed, we should first
    }
    final String handler = params.get(PARAM_HANDLER);
    if (handler == null || handler.isEmpty()) {
        // 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>";
    }
    final String sourcePage = ctx.getPage().getName();
    final String submitServlet = ctx.getURL(ContextEnum.PAGE_VIEW.getRequestContext(), 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.
    final 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.
        final PluginManager pm = ctx.getEngine().getManager(PluginManager.class);
        handlerOutput = pm.execute(ctx, handler, info.getSubmission());
        info.setResult(handlerOutput);
        info.setStatus(FormInfo.EXECUTED);
    } catch (final 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.plugin.PluginManager) PluginException(org.apache.wiki.api.exceptions.PluginException) ResourceBundle(java.util.ResourceBundle)

Example 3 with PluginManager

use of org.apache.wiki.plugin.PluginManager in project jspwiki by apache.

the class PluginTag method executePlugin.

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

Example 4 with PluginManager

use of org.apache.wiki.plugin.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(final Context context, final String commandline, final int pos) throws PluginException {
    final PatternMatcher matcher = new Perl5Matcher();
    try {
        final PluginManager pm = context.getEngine().getManager(PluginManager.class);
        if (matcher.contains(commandline, pm.getPluginPattern())) {
            final MatchResult res = matcher.getMatch();
            final String plugin = res.group(2);
            final String args = commandline.substring(res.endOffset(0), commandline.length() - (commandline.charAt(commandline.length() - 1) == '}' ? 1 : 0));
            final Map<String, String> arglist = pm.parseArgs(args);
            // set wikitext bounds of plugin as '_bounds' parameter, e.g., [345,396]
            if (pos != -1) {
                final int end = pos + commandline.length() + 2;
                final String bounds = pos + "|" + end;
                arglist.put(PluginManager.PARAM_BOUNDS, bounds);
            }
            return new PluginContent(plugin, arglist);
        }
    } catch (final ClassCastException e) {
        log.error("Invalid type offered in parsing plugin arguments.", e);
        throw new InternalWikiException("Oops, someone offered !String!", e);
    } catch (final NoSuchElementException e) {
        final String msg = "Missing parameter in plugin definition: " + commandline;
        log.warn(msg, e);
        throw new PluginException(msg);
    } catch (final IOException e) {
        final 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.plugin.PluginManager) PatternMatcher(org.apache.oro.text.regex.PatternMatcher) NoSuchElementException(java.util.NoSuchElementException)

Example 5 with PluginManager

use of org.apache.wiki.plugin.PluginManager in project jspwiki by apache.

the class PluginContent method invoke.

/**
 *{@inheritDoc}
 */
@Override
public String invoke(final Context context) {
    String result;
    final Boolean wysiwygVariable = context.getVariable(Context.VAR_WYSIWYG_EDITOR_MODE);
    boolean wysiwygEditorMode = false;
    if (wysiwygVariable != null) {
        wysiwygEditorMode = wysiwygVariable;
    }
    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.
            final String cmdLine = m_params.get(CMDLINE).replaceAll(LINEBREAK, ELEMENT_BR);
            result = result + cmdLine + PLUGIN_END;
        } else {
            final Boolean b = context.getVariable(Context.VAR_EXECUTE_PLUGINS);
            if (b != null && !b) {
                return BLANK;
            }
            final Engine engine = context.getEngine();
            final Map<String, String> parsedParams = new HashMap<>();
            // Parse any variable instances from the string
            for (final Map.Entry<String, String> e : m_params.entrySet()) {
                String val = e.getValue();
                val = engine.getManager(VariableManager.class).expandVariables(context, val);
                parsedParams.put(e.getKey(), val);
            }
            final PluginManager pm = engine.getManager(PluginManager.class);
            result = pm.execute(context, m_pluginName, parsedParams);
        }
    } catch (final Exception e) {
        if (wysiwygEditorMode) {
            result = "";
        } else {
            // log.info("Failed to execute plugin",e);
            final ResourceBundle rb = Preferences.getBundle(context, Plugin.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.plugin.PluginManager) HashMap(java.util.HashMap) ResourceBundle(java.util.ResourceBundle) HashMap(java.util.HashMap) Map(java.util.Map) Engine(org.apache.wiki.api.core.Engine) InternalWikiException(org.apache.wiki.InternalWikiException) IOException(java.io.IOException) PluginException(org.apache.wiki.api.exceptions.PluginException) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

PluginManager (org.apache.wiki.plugin.PluginManager)5 PluginException (org.apache.wiki.api.exceptions.PluginException)4 ResourceBundle (java.util.ResourceBundle)3 IOException (java.io.IOException)2 NoSuchElementException (java.util.NoSuchElementException)2 InternalWikiException (org.apache.wiki.InternalWikiException)2 Engine (org.apache.wiki.api.core.Engine)2 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 ParserStagePlugin (org.apache.wiki.api.plugin.ParserStagePlugin)1 Plugin (org.apache.wiki.api.plugin.Plugin)1 DefaultPluginManager (org.apache.wiki.plugin.DefaultPluginManager)1