Search in sources :

Example 21 with PluginException

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

the class DefaultPluginManager method newWikiPlugin.

/**
 * Creates a {@link WikiPlugin}.
 *
 * @param pluginName plugin's classname
 * @param rb {@link ResourceBundle} with i18ned text for exceptions.
 * @return a {@link WikiPlugin}.
 * @throws PluginException if there is a problem building the {@link WikiPlugin}.
 */
public WikiPlugin newWikiPlugin(String pluginName, ResourceBundle rb) throws PluginException {
    WikiPlugin plugin = null;
    WikiPluginInfo pluginInfo = m_pluginClassMap.get(pluginName);
    try {
        if (pluginInfo == null) {
            pluginInfo = WikiPluginInfo.newInstance(findPluginClass(pluginName));
            registerPlugin(pluginInfo);
        }
        if (!checkCompatibility(pluginInfo)) {
            String msg = "Plugin '" + pluginInfo.getName() + "' not compatible with this version of JSPWiki";
            log.info(msg);
        } else {
            plugin = pluginInfo.newPluginInstance(m_searchPath, m_externalJars);
        }
    } catch (ClassNotFoundException e) {
        throw new PluginException(MessageFormat.format(rb.getString("plugin.error.couldnotfind"), pluginName), e);
    } catch (InstantiationException e) {
        throw new PluginException(MessageFormat.format(rb.getString("plugin.error.cannotinstantiate"), pluginName), e);
    } catch (IllegalAccessException e) {
        throw new PluginException(MessageFormat.format(rb.getString("plugin.error.notallowed"), pluginName), e);
    } catch (Exception e) {
        throw new PluginException(MessageFormat.format(rb.getString("plugin.error.instantationfailed"), pluginName), e);
    }
    return plugin;
}
Also used : PluginException(org.apache.wiki.api.exceptions.PluginException) WikiPlugin(org.apache.wiki.api.plugin.WikiPlugin) PluginException(org.apache.wiki.api.exceptions.PluginException) NoSuchElementException(java.util.NoSuchElementException) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException) IOException(java.io.IOException) InternalWikiException(org.apache.wiki.InternalWikiException)

Example 22 with PluginException

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

the class Image method execute.

/**
 *  {@inheritDoc}
 */
public String execute(WikiContext context, Map<String, String> params) throws PluginException {
    WikiEngine engine = context.getEngine();
    String src = getCleanParameter(params, PARAM_SRC);
    String align = getCleanParameter(params, PARAM_ALIGN);
    String ht = getCleanParameter(params, PARAM_HEIGHT);
    String wt = getCleanParameter(params, PARAM_WIDTH);
    String alt = getCleanParameter(params, PARAM_ALT);
    String caption = getCleanParameter(params, PARAM_CAPTION);
    String link = getCleanParameter(params, PARAM_LINK);
    String target = getCleanParameter(params, PARAM_TARGET);
    String style = getCleanParameter(params, PARAM_STYLE);
    String cssclass = getCleanParameter(params, PARAM_CLASS);
    // String map     = getCleanParameter( params, PARAM_MAP );
    String border = getCleanParameter(params, PARAM_BORDER);
    String title = getCleanParameter(params, PARAM_TITLE);
    if (src == null) {
        throw new PluginException("Parameter 'src' is required for Image plugin");
    }
    if (target != null && !validTargetValue(target)) {
        // not a valid value so ignore
        target = null;
    }
    try {
        AttachmentManager mgr = engine.getAttachmentManager();
        Attachment att = mgr.getAttachmentInfo(context, src);
        if (att != null) {
            src = context.getURL(WikiContext.ATTACH, att.getName());
        }
    } catch (ProviderException e) {
        throw new PluginException("Attachment info failed: " + e.getMessage());
    }
    StringBuilder result = new StringBuilder();
    result.append("<table border=\"0\" class=\"imageplugin\"");
    if (title != null) {
        result.append(" title=\"" + title + "\"");
    }
    if (align != null) {
        if (align.equals("center")) {
            result.append(" style=\"margin-left: auto; margin-right: auto; text-align:center; vertical-align:middle;\"");
        } else {
            result.append(" style=\"float:" + align + ";\"");
        }
    }
    result.append(">\n");
    if (caption != null) {
        result.append("<caption>" + caption + "</caption>\n");
    }
    // move css class and style to the container of the image,
    // so it doesn't affect the caption
    result.append("<tr><td");
    if (cssclass != null) {
        result.append(" class=\"" + cssclass + "\"");
    }
    if (style != null) {
        result.append(" style=\"" + style);
        // Make sure that we add a ";" to the end of the style string
        if (result.charAt(result.length() - 1) != ';')
            result.append(";");
        result.append("\"");
    }
    result.append(">");
    if (link != null) {
        result.append("<a href=\"" + link + "\"");
        if (target != null) {
            result.append(" target=\"" + target + "\"");
        }
        result.append(">");
    }
    result.append("<img src=\"" + src + "\"");
    if (ht != null)
        result.append(" height=\"" + ht + "\"");
    if (wt != null)
        result.append(" width=\"" + wt + "\"");
    if (alt != null)
        result.append(" alt=\"" + alt + "\"");
    if (border != null)
        result.append(" border=\"" + border + "\"");
    // if( map != null )    result.append(" map=\""+map+"\"");
    result.append(" />");
    if (link != null)
        result.append("</a>");
    result.append("</td></tr>\n");
    result.append("</table>\n");
    return result.toString();
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) PluginException(org.apache.wiki.api.exceptions.PluginException) Attachment(org.apache.wiki.attachment.Attachment) AttachmentManager(org.apache.wiki.attachment.AttachmentManager) WikiEngine(org.apache.wiki.WikiEngine)

Example 23 with PluginException

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

the class IndexPlugin method execute.

/**
 * {@inheritDoc}
 */
public String execute(WikiContext context, Map<String, String> params) throws PluginException {
    String include = params.get(PARAM_INCLUDE);
    String exclude = params.get(PARAM_EXCLUDE);
    Element masterDiv = getElement("div", "index");
    Element indexDiv = getElement("div", "header");
    masterDiv.addContent(indexDiv);
    try {
        List<String> pages = listPages(context, include, exclude);
        context.getEngine().getPageSorter().sort(pages);
        char initialChar = ' ';
        Element currentDiv = new Element("div", xmlns_XHTML);
        for (String name : pages) {
            if (name.charAt(0) != initialChar) {
                if (initialChar != ' ') {
                    indexDiv.addContent(" - ");
                }
                initialChar = name.charAt(0);
                masterDiv.addContent(makeHeader(String.valueOf(initialChar)));
                currentDiv = getElement("div", "body");
                masterDiv.addContent(currentDiv);
                indexDiv.addContent(getLink("#" + initialChar, String.valueOf(initialChar)));
            } else {
                currentDiv.addContent(", ");
            }
            currentDiv.addContent(getLink(context.getURL(WikiContext.VIEW, name), name));
        }
    } catch (ProviderException e) {
        log.warn("could not load page index", e);
        throw new PluginException(e.getMessage());
    }
    // serialize to raw format string (no changes to whitespace)
    XMLOutputter out = new XMLOutputter(Format.getRawFormat());
    return out.outputString(masterDiv);
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) ProviderException(org.apache.wiki.api.exceptions.ProviderException) Element(org.jdom2.Element) PluginException(org.apache.wiki.api.exceptions.PluginException)

Example 24 with PluginException

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

the class InsertPage method execute.

/**
 *  {@inheritDoc}
 */
@SuppressWarnings("unchecked")
public String execute(WikiContext context, Map<String, String> params) throws PluginException {
    WikiEngine engine = context.getEngine();
    StringBuilder res = new StringBuilder();
    String clazz = params.get(PARAM_CLASS);
    String includedPage = params.get(PARAM_PAGENAME);
    String style = params.get(PARAM_STYLE);
    String defaultstr = params.get(PARAM_DEFAULT);
    int section = TextUtil.parseIntParameter(params.get(PARAM_SECTION), -1);
    int maxlen = TextUtil.parseIntParameter(params.get(PARAM_MAXLENGTH), -1);
    if (style == null)
        style = DEFAULT_STYLE;
    if (maxlen == -1)
        maxlen = Integer.MAX_VALUE;
    if (includedPage != null) {
        WikiPage page = null;
        try {
            String pageName = engine.getFinalPageName(includedPage);
            if (pageName != null) {
                page = engine.getPage(pageName);
            } else {
                page = engine.getPage(includedPage);
            }
        } catch (ProviderException e) {
            res.append("<span class=\"error\">Page could not be found by the page provider.</span>");
            return res.toString();
        }
        if (page != null) {
            // 
            // Check for recursivity
            // 
            List<String> previousIncludes = (List<String>) context.getVariable(ATTR_RECURSE);
            if (previousIncludes != null) {
                if (previousIncludes.contains(page.getName())) {
                    return "<span class=\"error\">Error: Circular reference - you can't include a page in itself!</span>";
                }
            } else {
                previousIncludes = new ArrayList<String>();
            }
            previousIncludes.add(page.getName());
            context.setVariable(ATTR_RECURSE, previousIncludes);
            // 
            // Check for permissions
            // 
            AuthorizationManager mgr = engine.getAuthorizationManager();
            if (!mgr.checkPermission(context.getWikiSession(), PermissionFactory.getPagePermission(page, "view"))) {
                res.append("<span class=\"error\">You do not have permission to view this included page.</span>");
                return res.toString();
            }
            /**
             *  We want inclusion to occur within the context of
             *  its own page, because we need the links to be correct.
             */
            WikiContext includedContext = (WikiContext) context.clone();
            includedContext.setPage(page);
            String pageData = engine.getPureText(page);
            String moreLink = "";
            if (section != -1) {
                try {
                    pageData = TextUtil.getSection(pageData, section);
                } catch (IllegalArgumentException e) {
                    throw new PluginException(e.getMessage());
                }
            }
            if (pageData.length() > maxlen) {
                pageData = pageData.substring(0, maxlen) + " ...";
                moreLink = "<p><a href=\"" + context.getURL(WikiContext.VIEW, includedPage) + "\">More...</a></p>";
            }
            res.append("<div style=\"" + style + "\"" + (clazz != null ? " class=\"" + clazz + "\"" : "") + ">");
            res.append(engine.textToHTML(includedContext, pageData));
            res.append(moreLink);
            res.append("</div>");
            // 
            // Remove the name from the stack; we're now done with this.
            // 
            previousIncludes.remove(page.getName());
            context.setVariable(ATTR_RECURSE, previousIncludes);
        } else {
            if (defaultstr != null) {
                res.append(defaultstr);
            } else {
                res.append("There is no page called '" + includedPage + "'.  Would you like to ");
                res.append("<a href=\"" + context.getURL(WikiContext.EDIT, includedPage) + "\">create it?</a>");
            }
        }
    } else {
        res.append("<span class=\"error\">");
        res.append("You have to define a page!");
        res.append("</span>");
    }
    return res.toString();
}
Also used : WikiContext(org.apache.wiki.WikiContext) ProviderException(org.apache.wiki.api.exceptions.ProviderException) WikiPage(org.apache.wiki.WikiPage) PluginException(org.apache.wiki.api.exceptions.PluginException) List(java.util.List) ArrayList(java.util.ArrayList) AuthorizationManager(org.apache.wiki.auth.AuthorizationManager) WikiEngine(org.apache.wiki.WikiEngine)

Example 25 with PluginException

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

the class UndefinedPagesPlugin method execute.

/**
 *  {@inheritDoc}
 */
public String execute(WikiContext context, Map<String, String> params) throws PluginException {
    ReferenceManager refmgr = context.getEngine().getReferenceManager();
    Collection links = refmgr.findUncreated();
    super.initialize(context, params);
    links = filterAndSortCollection(links);
    String wikitext = null;
    if (m_lastModified) {
        throw new PluginException("parameter " + PARAM_LASTMODIFIED + " is not valid for the UndefinedPagesPlugin");
    }
    if (m_show.equals(PARAM_SHOW_VALUE_COUNT)) {
        wikitext = "" + links.size();
    } else {
        wikitext = wikitizeCollection(links, m_separator, ALL_ITEMS);
    }
    return makeHTML(context, wikitext);
}
Also used : PluginException(org.apache.wiki.api.exceptions.PluginException) Collection(java.util.Collection) ReferenceManager(org.apache.wiki.ReferenceManager)

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