Search in sources :

Example 1 with NoSuchVariableException

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

the class VariableLinkNodePostProcessorState method process.

/**
 * {@inheritDoc}
 *
 * @see NodePostProcessorState#process(NodeTracker, JSPWikiLink)
 */
@Override
public void process(final NodeTracker state, final JSPWikiLink link) {
    final String variable = NodePostProcessorStateCommonOperations.inlineLinkTextOnWysiwyg(state, link, m_wysiwygEditorMode);
    if (!m_wysiwygEditorMode) {
        try {
            final String parsedVariable = wikiContext.getEngine().getVariableManager().parseAndGetValue(wikiContext, variable);
            final HtmlInline content = new HtmlInline(CharSubSequence.of(StringEscapeUtils.escapeXml(parsedVariable)));
            NodePostProcessorStateCommonOperations.addContent(state, link, content);
        } catch (final NoSuchVariableException e) {
            NodePostProcessorStateCommonOperations.makeError(state, link, "No such variable: " + variable + " (" + e.getMessage() + ")");
        }
    }
}
Also used : NoSuchVariableException(org.apache.wiki.api.exceptions.NoSuchVariableException) HtmlInline(com.vladsch.flexmark.ast.HtmlInline)

Example 2 with NoSuchVariableException

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

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

the class VariableManager method getValue.

/**
 *  Returns a value of the named variable.  The resolving order is
 *  <ol>
 *    <li>Known "constant" name, such as "pagename", etc.  This is so
 *        that pages could not override certain constants.
 *    <li>WikiContext local variable.  This allows a programmer to
 *        set a parameter which cannot be overridden by user.
 *    <li>HTTP Session
 *    <li>HTTP Request parameters
 *    <li>WikiPage variable.  As set by the user with the SET directive.
 *    <li>jspwiki.properties
 *  </ol>
 *
 *  Use this method only whenever you really need to have a parameter that
 *  can be overridden by anyone using the wiki.
 *
 *  @param context The WikiContext
 *  @param varName Name of the variable.
 *
 *  @return The variable value.
 *
 *  @throws IllegalArgumentException If the name is somehow broken.
 *  @throws NoSuchVariableException If a variable is not known.
 */
public String getValue(WikiContext context, String varName) throws IllegalArgumentException, NoSuchVariableException {
    if (varName == null)
        throw new IllegalArgumentException("Null variable name.");
    if (varName.length() == 0)
        throw new IllegalArgumentException("Zero length variable name.");
    // Faster than doing equalsIgnoreCase()
    String name = varName.toLowerCase();
    for (int i = 0; i < THE_BIG_NO_NO_LIST.length; i++) {
        if (name.equals(THE_BIG_NO_NO_LIST[i]))
            // FIXME: Should this be something different?
            return "";
    }
    try {
        // 
        // Using reflection to get system variables adding a new system variable
        // now only involves creating a new method in the SystemVariables class
        // with a name starting with get and the first character of the name of
        // the variable capitalized. Example:
        // public String getMysysvar(){
        // return "Hello World";
        // }
        // 
        SystemVariables sysvars = new SystemVariables(context);
        String methodName = "get" + Character.toUpperCase(name.charAt(0)) + name.substring(1);
        Method method = sysvars.getClass().getMethod(methodName);
        return (String) method.invoke(sysvars);
    } catch (NoSuchMethodException e1) {
        // 
        if ((context.getVariable(varName)) != null) {
            return context.getVariable(varName).toString();
        }
        // 
        // Well, I guess it wasn't a final straw.  We also allow
        // variables from the session and the request (in this order).
        // 
        HttpServletRequest req = context.getHttpRequest();
        if (req != null && req.getSession() != null) {
            HttpSession session = req.getSession();
            try {
                String s;
                if ((s = (String) session.getAttribute(varName)) != null)
                    return s;
                if ((s = context.getHttpParameter(varName)) != null)
                    return s;
            } catch (ClassCastException e) {
            }
        }
        // 
        // And the final straw: see if the current page has named metadata.
        // 
        WikiPage pg = context.getPage();
        if (pg != null) {
            Object metadata = pg.getAttribute(varName);
            if (metadata != null)
                return metadata.toString();
        }
        // 
        // And the final straw part 2: see if the "real" current page has
        // named metadata. This allows a parent page to control a inserted
        // page through defining variables
        // 
        WikiPage rpg = context.getRealPage();
        if (rpg != null) {
            Object metadata = rpg.getAttribute(varName);
            if (metadata != null)
                return metadata.toString();
        }
        if (varName.startsWith("jspwiki.")) {
            Properties props = context.getEngine().getWikiProperties();
            String s = props.getProperty(varName);
            if (s != null) {
                return s;
            }
        }
        if (varName.equals(VAR_ERROR) || varName.equals(VAR_MSG))
            return "";
        throw new NoSuchVariableException("No variable " + varName + " defined.");
    } catch (Exception e) {
        log.info("Interesting exception: cannot fetch variable value", e);
    }
    return "";
}
Also used : NoSuchVariableException(org.apache.wiki.api.exceptions.NoSuchVariableException) HttpSession(javax.servlet.http.HttpSession) Method(java.lang.reflect.Method) Properties(java.util.Properties) NoSuchVariableException(org.apache.wiki.api.exceptions.NoSuchVariableException) HttpServletRequest(javax.servlet.http.HttpServletRequest)

Example 4 with NoSuchVariableException

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

the class VariableTag method doWikiStartTag.

public final int doWikiStartTag() throws JspException, IOException {
    WikiEngine engine = m_wikiContext.getEngine();
    JspWriter out = pageContext.getOut();
    String msg = null;
    String value = null;
    try {
        value = engine.getVariableManager().getValue(m_wikiContext, getVar());
    } catch (NoSuchVariableException e) {
        msg = "No such variable: " + e.getMessage();
    } catch (IllegalArgumentException e) {
        msg = "Incorrect variable name: " + e.getMessage();
    }
    if (value == null) {
        value = m_default;
    }
    if (value == null) {
        value = msg;
    }
    out.write(TextUtil.replaceEntities(value));
    return SKIP_BODY;
}
Also used : NoSuchVariableException(org.apache.wiki.api.exceptions.NoSuchVariableException) JspWriter(javax.servlet.jsp.JspWriter) WikiEngine(org.apache.wiki.WikiEngine)

Example 5 with NoSuchVariableException

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

the class Feed method getSiteName.

/**
 * Figure out a site name for a feed.
 *
 * @param context the wiki context
 * @return the site name
 */
public static String getSiteName(WikiContext context) {
    WikiEngine engine = context.getEngine();
    String blogname = null;
    try {
        blogname = engine.getVariableManager().getValue(context, VAR_BLOGNAME);
    } catch (NoSuchVariableException e) {
    }
    if (blogname == null) {
        blogname = engine.getApplicationName() + ": " + context.getPage().getName();
    }
    return blogname;
}
Also used : NoSuchVariableException(org.apache.wiki.api.exceptions.NoSuchVariableException) WikiEngine(org.apache.wiki.WikiEngine)

Aggregations

NoSuchVariableException (org.apache.wiki.api.exceptions.NoSuchVariableException)5 WikiEngine (org.apache.wiki.WikiEngine)2 HtmlInline (com.vladsch.flexmark.ast.HtmlInline)1 Method (java.lang.reflect.Method)1 Properties (java.util.Properties)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpSession (javax.servlet.http.HttpSession)1 JspWriter (javax.servlet.jsp.JspWriter)1 WikiContext (org.apache.wiki.WikiContext)1