public String getValue()

in jspwiki-main/src/main/java/org/apache/wiki/variables/DefaultVariableManager.java [147:258]


    public String getValue( final Context context, final String varName ) throws IllegalArgumentException, NoSuchVariableException {
        if( varName == null ) {
            throw new IllegalArgumentException( "Null variable name." );
        }
        if( varName.isEmpty() ) {
            throw new IllegalArgumentException( "Zero length variable name." );
        }
        // Faster than doing equalsIgnoreCase()
        final String name = varName.toLowerCase();

        for( final String value : THE_BIG_NO_NO_LIST ) {
            if( name.equals( value ) ) {
                return ""; // FIXME: Should this be something different?
            }
        }

        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";
            //    }
            //
            final SystemVariables sysvars = new SystemVariables( context );
            final String methodName = "get" + Character.toUpperCase( name.charAt( 0 ) ) + name.substring( 1 );
            final Method method = sysvars.getClass().getMethod( methodName );
            return ( String )method.invoke( sysvars );
        } catch( final NoSuchMethodException e1 ) {
            //
            //  It is not a system var. Time to handle the other cases.
            //
            //  Check if such a context variable exists, returning its string representation.
            //
            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).
            //
            final HttpServletRequest req = context.getHttpRequest();
            if( req != null && req.getSession() != null ) {
                final HttpSession session = req.getSession();

                try {
                    String s = ( String )session.getAttribute( varName );

                    if( s != null ) {
                        return s;
                    }

                    s = context.getHttpParameter( varName );
                    if( s != null ) {
                        return s;
                    }
                } catch( final ClassCastException e ) {
                    LOG.debug( "Not a String: " + varName );
                }
            }

            //
            // And the final straw: see if the current page has named metadata.
            //
            final Page pg = context.getPage();
            if( pg != null ) {
                final 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
            //
            final Page rpg = context.getRealPage();
            if( rpg != null ) {
                final Object metadata = rpg.getAttribute( varName );
                if( metadata != null ) {
                    return metadata.toString();
                }
            }

            //
            // Next-to-final straw: attempt to fetch using property name. We don't allow fetching any other
            // properties than those starting with "jspwiki.".  I know my own code, but I can't vouch for bugs
            // in other people's code... :-)
            //
            if( varName.startsWith("jspwiki.") ) {
                final Properties props = context.getEngine().getWikiProperties();
                final String s = props.getProperty( varName );
                if( s != null ) {
                    return s;
                }
            }

            //
            //  Final defaults for some known quantities.
            //
            if( varName.equals( VAR_ERROR ) || varName.equals( VAR_MSG ) ) {
                return "";
            }

            throw new NoSuchVariableException( "No variable " + varName + " defined." );
        } catch( final Exception e ) {
            LOG.info("Interesting exception: cannot fetch variable value", e );
        }
        return "";
    }