Search in sources :

Example 16 with PluginException

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

the class DefaultPluginManager method execute.

/**
 *  Executes a plugin class in the given context.
 *  <P>Used to be private, but is public since 1.9.21.
 *
 *  @param context The current WikiContext.
 *  @param classname The name of the class.  Can also be a
 *  shortened version without the package name, since the class name is searched from the
 *  package search path.
 *
 *  @param params A parsed map of key-value pairs.
 *
 *  @return Whatever the plugin returns.
 *
 *  @throws PluginException If the plugin execution failed for
 *  some reason.
 *
 *  @since 2.0
 */
public String execute(WikiContext context, String classname, Map<String, String> params) throws PluginException {
    if (!m_pluginsEnabled) {
        return "";
    }
    ResourceBundle rb = Preferences.getBundle(context, WikiPlugin.CORE_PLUGINS_RESOURCEBUNDLE);
    boolean debug = TextUtil.isPositive(params.get(PARAM_DEBUG));
    try {
        // 
        // Create...
        // 
        WikiPlugin plugin = newWikiPlugin(classname, rb);
        if (plugin == null) {
            return "Plugin '" + classname + "' not compatible with this version of JSPWiki";
        }
        // 
        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 (ClassCastException e) {
        throw new PluginException(MessageFormat.format(rb.getString("plugin.error.notawikiplugin"), classname), e);
    }
}
Also used : PluginException(org.apache.wiki.api.exceptions.PluginException) ResourceBundle(java.util.ResourceBundle) WikiPlugin(org.apache.wiki.api.plugin.WikiPlugin)

Example 17 with PluginException

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

the class DefaultPluginManager method execute.

/**
 *  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 = Preferences.getBundle(context, WikiPlugin.CORE_PLUGINS_RESOURCEBUNDLE);
    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, String> 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"), commandline));
    } 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"), commandline));
    }
    // to be invisible, then we should return an empty string.
    return commandline;
}
Also used : PluginException(org.apache.wiki.api.exceptions.PluginException) ResourceBundle(java.util.ResourceBundle) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) IOException(java.io.IOException) PatternMatcher(org.apache.oro.text.regex.PatternMatcher) MatchResult(org.apache.oro.text.regex.MatchResult) NoSuchElementException(java.util.NoSuchElementException)

Example 18 with PluginException

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

the class FormSelect method buildSelect.

/**
 * Builds a Select element.
 */
private Element buildSelect(Map<String, String> pluginParams, Map<String, String> ctxValues, ResourceBundle rb) throws PluginException {
    String inputName = pluginParams.get(PARAM_INPUTNAME);
    if (inputName == null) {
        throw new PluginException(rb.getString("formselect.namemissing"));
    }
    String inputValue = pluginParams.get(PARAM_VALUE);
    String previousValue = ctxValues.get(inputName);
    // 
    // We provide several ways to override the separator, in case
    // some input application the default value.
    // 
    String optionSeparator = pluginParams.get("separator");
    if (optionSeparator == null) {
        optionSeparator = ctxValues.get("separator." + inputName);
    }
    if (optionSeparator == null) {
        optionSeparator = ctxValues.get("select.separator");
    }
    if (optionSeparator == null) {
        optionSeparator = ";";
    }
    String optionSelector = pluginParams.get("selector");
    if (optionSelector == null) {
        optionSelector = ctxValues.get("selector." + inputName);
    }
    if (optionSelector == null) {
        optionSelector = ctxValues.get("select.selector");
    }
    if (optionSelector == null) {
        optionSelector = "*";
    }
    if (optionSelector.equals(optionSeparator)) {
        optionSelector = null;
    }
    if (inputValue == null) {
        inputValue = "";
    }
    // If values from the context contain the separator, we assume
    // that the plugin or something else has given us a better
    // list to display.
    boolean contextValueOverride = false;
    if (previousValue != null) {
        if (previousValue.indexOf(optionSeparator) != -1) {
            inputValue = previousValue;
            previousValue = null;
        } else {
            // If a context value exists, but it's not a list,
            // it'll just override any existing selector
            // indications.
            contextValueOverride = true;
        }
    }
    String[] options = inputValue.split(optionSeparator);
    int previouslySelected = -1;
    Element[] optionElements = new Element[options.length];
    // 
    for (int i = 0; i < options.length; i++) {
        int indicated = -1;
        options[i] = options[i].trim();
        if (optionSelector != null && options[i].startsWith(optionSelector)) {
            options[i] = options[i].substring(optionSelector.length());
            indicated = i;
        }
        if (previouslySelected == -1) {
            if (!contextValueOverride && indicated > 0) {
                previouslySelected = indicated;
            } else if (previousValue != null && options[i].equals(previousValue)) {
                previouslySelected = i;
            }
        }
        // huh?
        // optionElements[i] = new option( options[i] );
        // optionElements[i].addElement( options[i] );
        optionElements[i] = XhtmlUtil.element(XHTML.option, options[i]);
    }
    if (previouslySelected > -1) {
        optionElements[previouslySelected].setAttribute(XHTML.ATTR_selected, "true");
    }
    Element select = XhtmlUtil.element(XHTML.select);
    select.setAttribute(XHTML.ATTR_name, HANDLERPARAM_PREFIX + inputName);
    for (Element option : optionElements) {
        select.addContent(option);
    }
    return select;
}
Also used : PluginException(org.apache.wiki.api.exceptions.PluginException) Element(org.jdom2.Element)

Example 19 with PluginException

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

the class FormTextarea method buildTextArea.

private Element buildTextArea(Map<String, String> params, Map<String, String> previousValues, ResourceBundle rb) throws PluginException {
    String inputName = params.get(PARAM_INPUTNAME);
    String rows = params.get(PARAM_ROWS);
    String cols = params.get(PARAM_COLS);
    if (inputName == null) {
        throw new PluginException(rb.getString("formtextarea.namemissing"));
    }
    // 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.element(XHTML.textarea);
    field.setAttribute(XHTML.ATTR_name, HANDLERPARAM_PREFIX + inputName);
    if (rows != null) {
        field.setAttribute(XHTML.ATTR_rows, rows);
    }
    if (cols != null) {
        field.setAttribute(XHTML.ATTR_cols, cols);
    }
    if (previousValues != null) {
        String oldValue = previousValues.get(inputName);
        if (oldValue != null) {
            field.addContent(oldValue);
        } else {
            oldValue = params.get(PARAM_VALUE);
            if (oldValue != null) {
                field.addContent(oldValue);
            }
        }
    }
    return field;
}
Also used : PluginException(org.apache.wiki.api.exceptions.PluginException) Element(org.jdom2.Element)

Example 20 with PluginException

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

the class WeblogPlugin method execute.

/**
 *  {@inheritDoc}
 */
@SuppressWarnings("unchecked")
public String execute(WikiContext context, Map<String, String> params) throws PluginException {
    Calendar startTime;
    Calendar stopTime;
    int numDays = DEFAULT_DAYS;
    WikiEngine engine = context.getEngine();
    AuthorizationManager mgr = engine.getAuthorizationManager();
    // 
    // Parse parameters.
    // 
    String days;
    DateFormat entryFormat;
    String startDay = null;
    boolean hasComments = false;
    int maxEntries;
    String weblogName;
    if ((weblogName = params.get(PARAM_PAGE)) == null) {
        weblogName = context.getPage().getName();
    }
    if ((days = context.getHttpParameter("weblog." + PARAM_DAYS)) == null) {
        days = params.get(PARAM_DAYS);
    }
    if ((params.get(PARAM_ENTRYFORMAT)) == null) {
        entryFormat = Preferences.getDateFormat(context, TimeFormat.DATETIME);
    } else {
        entryFormat = new SimpleDateFormat(params.get(PARAM_ENTRYFORMAT));
    }
    if (days != null) {
        if (days.equalsIgnoreCase("all")) {
            numDays = Integer.MAX_VALUE;
        } else {
            numDays = TextUtil.parseIntParameter(days, DEFAULT_DAYS);
        }
    }
    if ((startDay = params.get(PARAM_STARTDATE)) == null) {
        startDay = context.getHttpParameter("weblog." + PARAM_STARTDATE);
    }
    if (TextUtil.isPositive(params.get(PARAM_ALLOWCOMMENTS))) {
        hasComments = true;
    }
    maxEntries = TextUtil.parseIntParameter(params.get(PARAM_MAXENTRIES), Integer.MAX_VALUE);
    // 
    // Determine the date range which to include.
    // 
    startTime = Calendar.getInstance();
    stopTime = Calendar.getInstance();
    if (startDay != null) {
        SimpleDateFormat fmt = new SimpleDateFormat(DEFAULT_DATEFORMAT);
        try {
            Date d = fmt.parse(startDay);
            startTime.setTime(d);
            stopTime.setTime(d);
        } catch (ParseException e) {
            return "Illegal time format: " + startDay;
        }
    }
    // 
    // Mark this to be a weblog
    // 
    context.getPage().setAttribute(ATTR_ISWEBLOG, "true");
    // 
    // We make a wild guess here that nobody can do millisecond
    // accuracy here.
    // 
    startTime.add(Calendar.DAY_OF_MONTH, -numDays);
    startTime.set(Calendar.HOUR, 0);
    startTime.set(Calendar.MINUTE, 0);
    startTime.set(Calendar.SECOND, 0);
    stopTime.set(Calendar.HOUR, 23);
    stopTime.set(Calendar.MINUTE, 59);
    stopTime.set(Calendar.SECOND, 59);
    StringBuilder sb = new StringBuilder();
    try {
        List<WikiPage> blogEntries = findBlogEntries(engine, weblogName, startTime.getTime(), stopTime.getTime());
        Collections.sort(blogEntries, new PageDateComparator());
        sb.append("<div class=\"weblog\">\n");
        for (Iterator<WikiPage> i = blogEntries.iterator(); i.hasNext() && maxEntries-- > 0; ) {
            WikiPage p = i.next();
            if (mgr.checkPermission(context.getWikiSession(), new PagePermission(p, PagePermission.VIEW_ACTION))) {
                addEntryHTML(context, entryFormat, hasComments, sb, p);
            }
        }
        sb.append("</div>\n");
    } catch (ProviderException e) {
        log.error("Could not locate blog entries", e);
        throw new PluginException("Could not locate blog entries: " + e.getMessage());
    }
    return sb.toString();
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) Calendar(java.util.Calendar) WikiPage(org.apache.wiki.WikiPage) PluginException(org.apache.wiki.api.exceptions.PluginException) Date(java.util.Date) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) AuthorizationManager(org.apache.wiki.auth.AuthorizationManager) ParseException(java.text.ParseException) WikiEngine(org.apache.wiki.WikiEngine) SimpleDateFormat(java.text.SimpleDateFormat) PagePermission(org.apache.wiki.auth.permissions.PagePermission)

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