Search in sources :

Example 11 with PluginException

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

the class IfPlugin method doMatch.

private static boolean doMatch(String content, String pattern) throws PluginException {
    PatternCompiler compiler = new Perl5Compiler();
    PatternMatcher matcher = new Perl5Matcher();
    try {
        Pattern matchp = compiler.compile(pattern, Perl5Compiler.SINGLELINE_MASK);
        // m_exceptPattern = compiler.compile( exceptPattern, Perl5Compiler.SINGLELINE_MASK );
        return matcher.matches(content, matchp);
    } catch (MalformedPatternException e) {
        throw new PluginException("Faulty pattern " + pattern);
    }
}
Also used : PluginException(org.apache.wiki.api.exceptions.PluginException)

Example 12 with PluginException

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

the class ReferringPagesPluginTest method testCount.

@Test
public void testCount() throws Exception {
    String result = null;
    result = manager.execute(context, "{ReferringPagesPlugin show=count}");
    Assert.assertEquals("7", result);
    result = manager.execute(context, "{ReferringPagesPlugin,exclude='*7',show=count}");
    Assert.assertEquals("6", result);
    result = manager.execute(context, "{ReferringPagesPlugin,exclude='*7',show=count,showLastModified=true}");
    String numberResult = result.substring(0, result.indexOf(" "));
    Assert.assertEquals("6", numberResult);
    String dateString = result.substring(result.indexOf("(") + 1, result.indexOf(")"));
    // the date should be parseable:
    SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss dd-MMM-yyyy zzz", engine.newHttpRequest().getLocale());
    df.parse(dateString);
    // test if the proper exception is thrown:
    String expectedExceptionString = "showLastModified=true is only valid if show=count is also specified";
    String exceptionString = null;
    try {
        result = manager.execute(context, "{ReferringPagesPlugin,showLastModified=true}");
    } catch (PluginException pe) {
        exceptionString = pe.getMessage();
    }
    Assert.assertEquals(expectedExceptionString, exceptionString);
}
Also used : PluginException(org.apache.wiki.api.exceptions.PluginException) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Example 13 with PluginException

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

the class JSPWikiMarkupParser method handleHyperlinks.

/**
 *  Gobbles up all hyperlinks that are encased in square brackets.
 */
private Element handleHyperlinks(String linktext, int pos) {
    ResourceBundle rb = Preferences.getBundle(m_context, InternationalizationManager.CORE_BUNDLE);
    StringBuilder sb = new StringBuilder(linktext.length() + 80);
    if (m_linkParsingOperations.isAccessRule(linktext)) {
        return handleAccessRule(linktext);
    }
    if (m_linkParsingOperations.isMetadata(linktext)) {
        return handleMetadata(linktext);
    }
    if (m_linkParsingOperations.isPluginLink(linktext)) {
        try {
            PluginContent pluginContent = PluginContent.parsePluginLine(m_context, linktext, pos);
            // 
            if (pluginContent != null) {
                addElement(pluginContent);
                pluginContent.executeParse(m_context);
            }
        } catch (PluginException e) {
            log.info(m_context.getRealPage().getWiki() + " : " + m_context.getRealPage().getName() + " - Failed to insert plugin: " + e.getMessage());
            // log.info( "Root cause:",e.getRootThrowable() );
            if (!m_wysiwygEditorMode) {
                ResourceBundle rbPlugin = Preferences.getBundle(m_context, WikiPlugin.CORE_PLUGINS_RESOURCEBUNDLE);
                return addElement(makeError(MessageFormat.format(rbPlugin.getString("plugin.error.insertionfailed"), m_context.getRealPage().getWiki(), m_context.getRealPage().getName(), e.getMessage())));
            }
        }
        return m_currentElement;
    }
    try {
        LinkParser.Link link = m_linkParser.parse(linktext);
        linktext = link.getText();
        String linkref = link.getReference();
        // 
        if (m_linkParsingOperations.isVariableLink(linktext)) {
            Content el = new VariableContent(linktext);
            addElement(el);
        } else if (m_linkParsingOperations.isExternalLink(linkref)) {
            // It's an external link, out of this Wiki
            callMutatorChain(m_externalLinkMutatorChain, linkref);
            if (m_linkParsingOperations.isImageLink(linkref)) {
                handleImageLink(linkref, linktext, link.hasReference());
            } else {
                makeLink(EXTERNAL, linkref, linktext, null, link.getAttributes());
                addElement(outlinkImage());
            }
        } else if (link.isInterwikiLink()) {
            // It's an interwiki link
            // InterWiki links also get added to external link chain
            // after the links have been resolved.
            // FIXME: There is an interesting issue here:  We probably should
            // URLEncode the wikiPage, but we can't since some of the
            // Wikis use slashes (/), which won't survive URLEncoding.
            // Besides, we don't know which character set the other Wiki
            // is using, so you'll have to write the entire name as it appears
            // in the URL.  Bugger.
            String extWiki = link.getExternalWiki();
            String wikiPage = link.getExternalWikiPage();
            if (m_wysiwygEditorMode) {
                makeLink(INTERWIKI, extWiki + ":" + wikiPage, linktext, null, link.getAttributes());
            } else {
                String urlReference = m_engine.getInterWikiURL(extWiki);
                if (urlReference != null) {
                    urlReference = TextUtil.replaceString(urlReference, "%s", wikiPage);
                    urlReference = callMutatorChain(m_externalLinkMutatorChain, urlReference);
                    if (m_linkParsingOperations.isImageLink(urlReference)) {
                        handleImageLink(urlReference, linktext, link.hasReference());
                    } else {
                        makeLink(INTERWIKI, urlReference, linktext, null, link.getAttributes());
                    }
                    if (m_linkParsingOperations.isExternalLink(urlReference)) {
                        addElement(outlinkImage());
                    }
                } else {
                    Object[] args = { extWiki };
                    addElement(makeError(MessageFormat.format(rb.getString("markupparser.error.nointerwikiref"), args)));
                }
            }
        } else if (linkref.startsWith("#")) {
            // It defines a local footnote
            makeLink(LOCAL, linkref, linktext, null, link.getAttributes());
        } else if (TextUtil.isNumber(linkref)) {
            // It defines a reference to a local footnote
            makeLink(LOCALREF, linkref, linktext, null, link.getAttributes());
        } else {
            int hashMark = -1;
            // 
            // Internal wiki link, but is it an attachment link?
            // 
            String attachment = m_engine.getAttachmentManager().getAttachmentInfoName(m_context, linkref);
            if (attachment != null) {
                callMutatorChain(m_attachmentLinkMutatorChain, attachment);
                if (m_linkParsingOperations.isImageLink(linkref)) {
                    attachment = m_context.getURL(WikiContext.ATTACH, attachment);
                    sb.append(handleImageLink(attachment, linktext, link.hasReference()));
                } else {
                    makeLink(ATTACHMENT, attachment, linktext, null, link.getAttributes());
                }
            } else if ((hashMark = linkref.indexOf('#')) != -1) {
                // It's an internal Wiki link, but to a named section
                String namedSection = linkref.substring(hashMark + 1);
                linkref = linkref.substring(0, hashMark);
                linkref = MarkupParser.cleanLink(linkref);
                callMutatorChain(m_localLinkMutatorChain, linkref);
                String matchedLink = m_linkParsingOperations.linkIfExists(linkref);
                if (matchedLink != null) {
                    String sectref = "section-" + m_engine.encodeName(matchedLink + "-" + wikifyLink(namedSection));
                    sectref = sectref.replace('%', '_');
                    makeLink(READ, matchedLink, linktext, sectref, link.getAttributes());
                } else {
                    makeLink(EDIT, linkref, linktext, null, link.getAttributes());
                }
            } else {
                // It's an internal Wiki link
                linkref = MarkupParser.cleanLink(linkref);
                callMutatorChain(m_localLinkMutatorChain, linkref);
                String matchedLink = m_linkParsingOperations.linkIfExists(linkref);
                if (matchedLink != null) {
                    makeLink(READ, matchedLink, linktext, null, link.getAttributes());
                } else {
                    makeLink(EDIT, linkref, linktext, null, link.getAttributes());
                }
            }
        }
    } catch (ParseException e) {
        log.info("Parser failure: ", e);
        Object[] args = { e.getMessage() };
        addElement(makeError(MessageFormat.format(rb.getString("markupparser.error.parserfailure"), args)));
    }
    return m_currentElement;
}
Also used : PluginException(org.apache.wiki.api.exceptions.PluginException) Content(org.jdom2.Content) ResourceBundle(java.util.ResourceBundle)

Example 14 with PluginException

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

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

the class CurrentTimePlugin method execute.

// private static Logger log = Logger.getLogger( CurrentTimePlugin.class );
/**
 *  {@inheritDoc}
 */
public String execute(WikiContext context, Map<String, String> params) throws PluginException {
    String formatString = params.get("format");
    try {
        SimpleDateFormat fmt;
        if (formatString != null) {
            fmt = new SimpleDateFormat(formatString);
        } else {
            fmt = Preferences.getDateFormat(context, TimeFormat.DATETIME);
        }
        // Now.
        Date d = new Date();
        return fmt.format(d);
    } catch (IllegalArgumentException e) {
        ResourceBundle rb = Preferences.getBundle(context, WikiPlugin.CORE_PLUGINS_RESOURCEBUNDLE);
        throw new PluginException(rb.getString("currenttimeplugin.badformat") + e.getMessage());
    }
}
Also used : PluginException(org.apache.wiki.api.exceptions.PluginException) ResourceBundle(java.util.ResourceBundle) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

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