Search in sources :

Example 6 with WikiContext

use of org.apache.wiki.WikiContext in project jspwiki by apache.

the class CachingProvider method refreshMetadata.

// 
// FIXME: Kludge: make sure that the page is also parsed and it gets all the
// necessary variables.
// 
private void refreshMetadata(WikiPage page) {
    if (page != null && !page.hasMetadata()) {
        RenderingManager mgr = m_engine.getRenderingManager();
        try {
            String data = m_provider.getPageText(page.getName(), page.getVersion());
            WikiContext ctx = new WikiContext(m_engine, page);
            MarkupParser parser = mgr.getParser(ctx, data);
            parser.parse();
        } catch (Exception ex) {
            log.debug("Failed to retrieve variables for wikipage " + page);
        }
    }
}
Also used : WikiContext(org.apache.wiki.WikiContext) RenderingManager(org.apache.wiki.render.RenderingManager) IOException(java.io.IOException) NoRequiredPropertyException(org.apache.wiki.api.exceptions.NoRequiredPropertyException) ProviderException(org.apache.wiki.api.exceptions.ProviderException) MarkupParser(org.apache.wiki.parser.MarkupParser)

Example 7 with WikiContext

use of org.apache.wiki.WikiContext in project jspwiki by apache.

the class PluginContent method getText.

/**
 * The main invocation for the plugin.  When the getText() is called, it
 * invokes the plugin and returns its contents.  If there is no Document
 * yet, only returns the plugin name itself.
 *
 * @return The plugin rendered according to the options set in the WikiContext.
 */
public String getText() {
    WikiDocument doc = (WikiDocument) getDocument();
    if (doc == null) {
        return getPluginName();
    }
    WikiContext context = doc.getContext();
    if (context == null) {
        log.info("WikiContext garbage-collected, cannot proceed");
        return getPluginName();
    }
    return invoke(context);
}
Also used : WikiContext(org.apache.wiki.WikiContext)

Example 8 with WikiContext

use of org.apache.wiki.WikiContext in project jspwiki by apache.

the class VariableContent method getValue.

/**
 *   Evaluates the variable and returns the contents.
 *
 *   @return The rendered value of the variable.
 */
public String getValue() {
    String result = "";
    WikiDocument root = (WikiDocument) getDocument();
    if (root == null) {
        // See similar note in PluginContent
        return m_varName;
    }
    WikiContext context = root.getContext();
    if (context == null)
        return "No WikiContext available: INTERNAL ERROR";
    Boolean wysiwygEditorMode = (Boolean) context.getVariable(RenderingManager.WYSIWYG_EDITOR_MODE);
    if (wysiwygEditorMode != null && wysiwygEditorMode.booleanValue()) {
        result = "[" + m_varName + "]";
    } else {
        try {
            result = context.getEngine().getVariableManager().parseAndGetValue(context, m_varName);
        } catch (NoSuchVariableException e) {
            result = MarkupParser.makeError("No such variable: " + e.getMessage()).getText();
        }
    }
    return StringEscapeUtils.escapeXml(result);
}
Also used : WikiContext(org.apache.wiki.WikiContext) NoSuchVariableException(org.apache.wiki.api.exceptions.NoSuchVariableException)

Example 9 with WikiContext

use of org.apache.wiki.WikiContext in project jspwiki by apache.

the class BugReportHandler method execute.

/**
 *  {@inheritDoc}
 */
public String execute(WikiContext context, Map<String, String> params) throws PluginException {
    String title;
    String description;
    String version;
    String submitter = null;
    SimpleDateFormat format = new SimpleDateFormat(DEFAULT_DATEFORMAT);
    ResourceBundle rb = Preferences.getBundle(context, WikiPlugin.CORE_PLUGINS_RESOURCEBUNDLE);
    title = params.get(PARAM_TITLE);
    description = params.get(PARAM_DESCRIPTION);
    version = params.get(PARAM_VERSION);
    Principal wup = context.getCurrentUser();
    if (wup != null) {
        submitter = wup.getName();
    }
    if (title == null)
        throw new PluginException(rb.getString("bugreporthandler.titlerequired"));
    if (title.length() == 0)
        return "";
    if (description == null)
        description = "";
    if (version == null)
        version = "unknown";
    Properties mappings = parseMappings(params.get(PARAM_MAPPINGS));
    try {
        StringWriter str = new StringWriter();
        PrintWriter out = new PrintWriter(str);
        Date d = new Date();
        // 
        // Outputting of basic data
        // 
        out.println("|" + mappings.getProperty(PARAM_TITLE, "Title") + "|" + title);
        out.println("|" + mappings.getProperty("date", "Date") + "|" + format.format(d));
        out.println("|" + mappings.getProperty(PARAM_VERSION, "Version") + "|" + version);
        if (submitter != null) {
            out.println("|" + mappings.getProperty("submitter", "Submitter") + "|" + submitter);
        }
        // 
        for (Iterator<Map.Entry<String, String>> i = params.entrySet().iterator(); i.hasNext(); ) {
            Map.Entry<String, String> entry = i.next();
            if (entry.getKey().equals(PARAM_TITLE) || entry.getKey().equals(PARAM_DESCRIPTION) || entry.getKey().equals(PARAM_VERSION) || entry.getKey().equals(PARAM_MAPPINGS) || entry.getKey().equals(PARAM_PAGE) || entry.getKey().startsWith("_")) {
            // Ignore this
            } else {
                // 
                // If no mapping has been defined, just ignore
                // it.
                // 
                String head = mappings.getProperty(entry.getKey(), entry.getKey());
                if (head.length() > 0) {
                    out.println("|" + head + "|" + entry.getValue());
                }
            }
        }
        out.println();
        out.println(description);
        out.close();
        // 
        // Now create a new page for this bug report
        // 
        String pageName = findNextPage(context, title, params.get(PARAM_PAGE));
        WikiPage newPage = new WikiPage(context.getEngine(), pageName);
        WikiContext newContext = (WikiContext) context.clone();
        newContext.setPage(newPage);
        context.getEngine().saveText(newContext, str.toString());
        MessageFormat formatter = new MessageFormat("");
        formatter.applyPattern(rb.getString("bugreporthandler.new"));
        String[] args = { "<a href=\"" + context.getViewURL(pageName) + "\">" + pageName + "</a>" };
        return formatter.format(args);
    } catch (RedirectException e) {
        log.info("Saving not allowed, reason: '" + e.getMessage() + "', can't redirect to " + e.getRedirect());
        throw new PluginException("Saving not allowed, reason: " + e.getMessage());
    } catch (WikiException e) {
        log.error("Unable to save page!", e);
        return rb.getString("bugreporthandler.unable");
    }
}
Also used : WikiContext(org.apache.wiki.WikiContext) WikiException(org.apache.wiki.api.exceptions.WikiException) MessageFormat(java.text.MessageFormat) PluginException(org.apache.wiki.api.exceptions.PluginException) WikiPage(org.apache.wiki.WikiPage) StringWriter(java.io.StringWriter) SimpleDateFormat(java.text.SimpleDateFormat) Principal(java.security.Principal) PrintWriter(java.io.PrintWriter) RedirectException(org.apache.wiki.api.exceptions.RedirectException)

Example 10 with WikiContext

use of org.apache.wiki.WikiContext in project jspwiki by apache.

the class JSPWikiMarkupParser method getCleanTranslator.

/**
 *  Does a lazy init.  Otherwise, we would get into a situation
 *  where HTMLRenderer would try and boot a TranslatorReader before
 *  the TranslatorReader it is contained by is up.
 */
private JSPWikiMarkupParser getCleanTranslator() {
    if (m_cleanTranslator == null) {
        WikiContext dummyContext = new WikiContext(m_engine, m_context.getHttpRequest(), m_context.getPage());
        m_cleanTranslator = new JSPWikiMarkupParser(dummyContext, null);
        m_cleanTranslator.m_allowHTML = true;
    }
    return m_cleanTranslator;
}
Also used : WikiContext(org.apache.wiki.WikiContext)

Aggregations

WikiContext (org.apache.wiki.WikiContext)90 WikiPage (org.apache.wiki.WikiPage)63 Test (org.junit.Test)40 TestEngine (org.apache.wiki.TestEngine)11 StringReader (java.io.StringReader)9 WikiEngine (org.apache.wiki.WikiEngine)9 ProviderException (org.apache.wiki.api.exceptions.ProviderException)8 IOException (java.io.IOException)7 InputStream (java.io.InputStream)7 Before (org.junit.Before)7 BufferedReader (java.io.BufferedReader)6 StringWriter (java.io.StringWriter)6 Collection (java.util.Collection)6 InputStreamReader (java.io.InputStreamReader)5 Date (java.util.Date)5 LinkCollector (org.apache.wiki.LinkCollector)5 Attachment (org.apache.wiki.attachment.Attachment)5 Properties (java.util.Properties)4 MockHttpServletRequest (net.sourceforge.stripes.mock.MockHttpServletRequest)4 WikiDocument (org.apache.wiki.parser.WikiDocument)4