Search in sources :

Example 6 with PluginException

use of org.apache.wiki.api.exceptions.PluginException in project jspwiki by apache.

the class TableOfContents method execute.

/**
 *  {@inheritDoc}
 */
public String execute(WikiContext context, Map<String, String> params) throws PluginException {
    WikiEngine engine = context.getEngine();
    WikiPage page = context.getPage();
    ResourceBundle rb = Preferences.getBundle(context, WikiPlugin.CORE_PLUGINS_RESOURCEBUNDLE);
    if (context.getVariable(VAR_ALREADY_PROCESSING) != null) {
        // return rb.getString("tableofcontents.title");
        return "<a href=\"#section-TOC\" class=\"toc\">" + rb.getString("tableofcontents.title") + "</a>";
    }
    StringBuilder sb = new StringBuilder();
    sb.append("<div class=\"toc\">\n");
    sb.append("<div class=\"collapsebox\">\n");
    String title = params.get(PARAM_TITLE);
    sb.append("<h4 id=\"section-TOC\">");
    if (title != null) {
        // sb.append("<h4>"+TextUtil.replaceEntities(title)+"</h4>\n");
        sb.append(TextUtil.replaceEntities(title));
    } else {
        // sb.append("<h4>"+rb.getString("tableofcontents.title")+"</h4>\n");
        sb.append(rb.getString("tableofcontents.title"));
    }
    sb.append("</h4>\n");
    // should we use an ordered list?
    m_usingNumberedList = false;
    if (params.containsKey(PARAM_NUMBERED)) {
        String numbered = params.get(PARAM_NUMBERED);
        if (numbered.equalsIgnoreCase("true")) {
            m_usingNumberedList = true;
        } else if (numbered.equalsIgnoreCase("yes")) {
            m_usingNumberedList = true;
        }
    }
    // if we are using a numbered list, get the rest of the parameters (if any) ...
    if (m_usingNumberedList) {
        int start = 0;
        String startStr = params.get(PARAM_START);
        if ((startStr != null) && (startStr.matches("^\\d+$"))) {
            start = Integer.parseInt(startStr);
        }
        if (start < 0)
            start = 0;
        m_starting = start;
        m_level1Index = start - 1;
        if (m_level1Index < 0)
            m_level1Index = 0;
        m_level2Index = 0;
        m_level3Index = 0;
        m_prefix = params.get(PARAM_PREFIX);
        if (m_prefix == null)
            m_prefix = "";
        m_lastLevel = Heading.HEADING_LARGE;
    }
    try {
        String wikiText = engine.getPureText(page);
        boolean runFilters = "true".equals(engine.getVariableManager().getValue(context, WikiEngine.PROP_RUNFILTERS, "true"));
        if (runFilters) {
            try {
                FilterManager fm = engine.getFilterManager();
                wikiText = fm.doPreTranslateFiltering(context, wikiText);
            } catch (Exception e) {
                log.error("Could not construct table of contents: Filter Error", e);
                throw new PluginException("Unable to construct table of contents (see logs)");
            }
        }
        context.setVariable(VAR_ALREADY_PROCESSING, "x");
        MarkupParser parser = engine.getRenderingManager().getParser(context, wikiText);
        parser.addHeadingListener(this);
        parser.parse();
        sb.append("<ul>\n" + m_buf.toString() + "</ul>\n");
    } catch (IOException e) {
        log.error("Could not construct table of contents", e);
        throw new PluginException("Unable to construct table of contents (see logs)");
    }
    sb.append("</div>\n</div>\n");
    return sb.toString();
}
Also used : WikiPage(org.apache.wiki.WikiPage) PluginException(org.apache.wiki.api.exceptions.PluginException) ResourceBundle(java.util.ResourceBundle) IOException(java.io.IOException) WikiEngine(org.apache.wiki.WikiEngine) IOException(java.io.IOException) InternalWikiException(org.apache.wiki.InternalWikiException) PluginException(org.apache.wiki.api.exceptions.PluginException) FilterManager(org.apache.wiki.api.engine.FilterManager) MarkupParser(org.apache.wiki.parser.MarkupParser)

Example 7 with PluginException

use of org.apache.wiki.api.exceptions.PluginException in project jspwiki by apache.

the class FormInput method execute.

/**
 * Generates a dynamic form element on the WikiPage.
 *
 * {@inheritDoc}
 */
public String execute(WikiContext ctx, Map<String, String> params) throws PluginException {
    String inputName = params.get(PARAM_INPUTNAME);
    String inputValue = params.get(PARAM_VALUE);
    String inputType = params.get(PARAM_TYPE);
    String size = params.get(PARAM_SIZE);
    String checked = params.get(PARAM_CHECKED);
    ResourceBundle rb = Preferences.getBundle(ctx, WikiPlugin.CORE_PLUGINS_RESOURCEBUNDLE);
    if (inputName == null) {
        throw new PluginException(rb.getString("forminput.namemissing"));
    }
    if (inputValue == null) {
        inputValue = "";
    }
    // Don't render if no error and error-only-rendering is on.
    FormInfo info = getFormInfo(ctx);
    Map<String, String> previousValues = null;
    if (info != null) {
        if (info.hide()) {
            // return XhtmlUtil.serialize(XhtmlUtil.element(XHTML.p,rb.getString("forminput.noneedtoshow"))); // nope
            return "<p>" + rb.getString("forminput.noneedtoshow") + "</p>";
        }
        previousValues = info.getSubmission();
    }
    if (previousValues == null) {
        previousValues = new HashMap<String, String>();
    }
    // In order to isolate posted form elements into their own
    // map, prefix the variable name here. It will be stripped
    // when the handler plugin is executed.
    Element field = XhtmlUtil.input(inputType, HANDLERPARAM_PREFIX + inputName, inputValue);
    field.setAttribute(XHTML.ATTR_class, String.valueOf(TextUtil.isPositive(checked) || "checked".equalsIgnoreCase(checked)));
    String oldValue = previousValues.get(inputName);
    if (oldValue != null) {
        field.setAttribute(XHTML.ATTR_value, oldValue);
    }
    if (size != null) {
        field.setAttribute(XHTML.ATTR_size, size);
    }
    // ctx.getEngine().getContentEncoding()
    return XhtmlUtil.serialize(field);
}
Also used : PluginException(org.apache.wiki.api.exceptions.PluginException) Element(org.jdom2.Element) ResourceBundle(java.util.ResourceBundle)

Example 8 with PluginException

use of org.apache.wiki.api.exceptions.PluginException in project jspwiki by apache.

the class FormOpen method execute.

/**
 *  {@inheritDoc}
 */
public String execute(WikiContext ctx, Map<String, String> params) throws PluginException {
    ResourceBundle rb = Preferences.getBundle(ctx, WikiPlugin.CORE_PLUGINS_RESOURCEBUNDLE);
    String formName = params.get(PARAM_FORM);
    if (formName == null) {
        throw new PluginException(MessageFormat.format(rb.getString("formopen.missingparam"), PARAM_FORM));
    }
    String hide = params.get(PARAM_HIDEFORM);
    String sourcePage = ctx.getPage().getName();
    String submitServlet = params.get(PARAM_SUBMITHANDLER);
    if (submitServlet == null)
        submitServlet = ctx.getURL(WikiContext.VIEW, sourcePage);
    String method = params.get(PARAM_METHOD);
    if (method == null)
        method = "post";
    if (!(method.equalsIgnoreCase("get") || method.equalsIgnoreCase("post"))) {
        throw new PluginException(rb.getString("formopen.postorgetonly"));
    }
    FormInfo info = getFormInfo(ctx);
    if (info != null) {
        // exists and is for this form, fine.
        if (formName.equals(info.getName())) {
            log.debug("Previous FormInfo for this form was found in context.");
            // error only, we need to exit now.
            if (hide != null && HIDE_SUCCESS.equals(hide) && info.getStatus() == FormInfo.EXECUTED) {
                info.setHide(true);
                return "<p>" + rb.getString("formopen.noneedtoshow") + "</p>";
            }
        } else {
            // This would mean that a new form was started without
            // closing an old one.  Get rid of the garbage.
            info = new FormInfo();
        }
    } else {
        // No previous FormInfo available; store now, so it'll be
        // available for upcoming Form input elements.
        info = new FormInfo();
        storeFormInfo(ctx, info);
    }
    info.setName(formName);
    info.setAction(submitServlet);
    StringBuilder tag = new StringBuilder(40);
    tag.append("<div class=\"wikiform\">\n");
    tag.append("<form action=\"" + submitServlet);
    tag.append("\" name=\"" + formName);
    tag.append("\" accept-charset=\"" + ctx.getEngine().getContentEncoding());
    tag.append("\" method=\"" + method + "\" enctype=\"application/x-www-form-urlencoded\">\n");
    tag.append("  <input type=\"hidden\" name=\"" + PARAM_FORMNAMEHIDDEN);
    tag.append("\" value=\"" + formName + "\"/>\n");
    return tag.toString();
}
Also used : PluginException(org.apache.wiki.api.exceptions.PluginException) ResourceBundle(java.util.ResourceBundle)

Example 9 with PluginException

use of org.apache.wiki.api.exceptions.PluginException 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 10 with PluginException

use of org.apache.wiki.api.exceptions.PluginException in project jspwiki by apache.

the class Denounce method execute.

/**
 *  {@inheritDoc}
 */
public String execute(WikiContext context, Map<String, String> params) throws PluginException {
    String link = params.get(PARAM_LINK);
    String text = params.get(PARAM_TEXT);
    boolean linkAllowed = true;
    if (link == null) {
        throw new PluginException("Denounce: No parameter " + PARAM_LINK + " defined!");
    }
    HttpServletRequest request = context.getHttpRequest();
    if (request != null) {
        linkAllowed = !matchHeaders(request);
    }
    if (text == null)
        text = link;
    if (linkAllowed) {
        // FIXME: Should really call TranslatorReader
        return "<a href=\"" + link + "\">" + text + "</a>";
    }
    return c_denounceText;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) PluginException(org.apache.wiki.api.exceptions.PluginException)

Aggregations

PluginException (org.apache.wiki.api.exceptions.PluginException)25 ResourceBundle (java.util.ResourceBundle)10 WikiPage (org.apache.wiki.WikiPage)5 IOException (java.io.IOException)4 SimpleDateFormat (java.text.SimpleDateFormat)4 WikiEngine (org.apache.wiki.WikiEngine)4 ProviderException (org.apache.wiki.api.exceptions.ProviderException)4 Element (org.jdom2.Element)4 NoSuchElementException (java.util.NoSuchElementException)3 InternalWikiException (org.apache.wiki.InternalWikiException)3 PluginManager (org.apache.wiki.api.engine.PluginManager)3 WikiPlugin (org.apache.wiki.api.plugin.WikiPlugin)3 Date (java.util.Date)2 MalformedPatternException (org.apache.oro.text.regex.MalformedPatternException)2 MatchResult (org.apache.oro.text.regex.MatchResult)2 PatternMatcher (org.apache.oro.text.regex.PatternMatcher)2 Perl5Matcher (org.apache.oro.text.regex.Perl5Matcher)2 WikiContext (org.apache.wiki.WikiContext)2 AuthorizationManager (org.apache.wiki.auth.AuthorizationManager)2 HtmlInline (com.vladsch.flexmark.ast.HtmlInline)1