Skip to main content
summaryrefslogtreecommitdiffstats
blob: 44d53a24f619c4ae49052d18a99a064c5cefad6e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*******************************************************************************
 * Copyright (c) 2006 Oracle Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Cameron Bateman/Oracle - initial API and implementation
 *    
 ********************************************************************************/

package org.eclipse.jst.jsf.designtime.el;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jst.jsf.context.symbol.ISymbol;
import org.eclipse.jst.jsf.context.symbol.source.ISymbolConstants;
import org.eclipse.jst.jsf.designtime.context.DTFacesContext;
import org.eclipse.jst.jsf.designtime.context.IDTExternalContext;
import org.eclipse.jst.jsf.designtime.symbols.DefaultBeanSymbolSourceProvider;
import org.eclipse.jst.jsf.designtime.symbols.DefaultBuiltInSymbolProvider;

/**
 * A design time proxy for the runtime VariableResolver.  This is used to
 * resolve the first element of a var.prop.prop2 type of sub-expression in
 * a JSF EL expression
 * 
 * Clients may sub-class
 *  
 * @author cbateman
 *
 */
public class DefaultDTVariableResolver extends AbstractDTVariableResolver
{
    /**
     * Tries to mirror the JSF 1.1 runtime VariableResolver
     * 
     * @see org.eclipse.jst.jsf.designtime.el.AbstractDTVariableResolver#resolveVariable(org.eclipse.jst.jsf.designtime.context.DTFacesContext, java.lang.String, org.eclipse.core.runtime.IAdaptable)
     */
    public ISymbol resolveVariable(DTFacesContext context, String name, IAdaptable externalContextKey)
    {
        // check implicits first
        final DefaultBuiltInSymbolProvider builtins =
            DefaultBuiltInSymbolProvider.getInstance();

        ISymbol  symbol = builtins.getSymbol(name, externalContextKey, ISymbolConstants.SYMBOL_SCOPE_ALL);

        if (symbol != null)
        {
            return symbol;
        }
        
        // next check the scope maps from request up to application
        final IDTExternalContext externalContext = 
            context.getDTExternalContext(externalContextKey);
        
        if (externalContext == null)
        {
            // TODO: try to find bean here?
            return null;
        }
        
        symbol = externalContext.getRequestMap().get(name);
        
        // check request scope
        if (symbol == null)
        {
            symbol = externalContext.getSessionMap().get(name);
            
            // then check session scope
            if (symbol == null)
            {
                symbol = externalContext.getApplicationMap().get(name);
                
                // if the symbol is not found at any scope, then look for a
                // a bean.
                if (symbol == null)
                {
                    final DefaultBeanSymbolSourceProvider beanProvider =
                        DefaultBeanSymbolSourceProvider.getInstance();
                    
                    symbol = beanProvider.getSymbol(name, externalContextKey, 
                                             ISymbolConstants.SYMBOL_SCOPE_ALL);
                }
            }
        }
        
        return symbol;
    }
    
    /**
     * @param facesContext
     * @param externalContextKey
     * @return all variables
     */
    public ISymbol[] getAllVariables(DTFacesContext facesContext, 
                                     IAdaptable externalContextKey)
    {
        final List  allSymbols = new ArrayList();

        addBuiltins(allSymbols, externalContextKey);

        final IDTExternalContext externalContext =  
            facesContext.getDTExternalContext(externalContextKey);

        if (externalContext != null)
        {
            addExternalContextSymbols(allSymbols, externalContext);
        }

        addBeanSymbols(allSymbols, externalContextKey);
        
        return (ISymbol[]) allSymbols.toArray(ISymbol.EMPTY_SYMBOL_ARRAY);
    }
        
    /**
     * Adds the built-in symbols to the list.  This behaviour is standarized and should
     * not be overriden in general.  However, you may wish to change the default
     * built-in symbol provider with your own.
     * 
     * @param list
     * @param externalContextKey
     */
    protected  void addBuiltins(final List list, final IAdaptable externalContextKey)
    {
        // check implicits first
        final DefaultBuiltInSymbolProvider builtins =
            DefaultBuiltInSymbolProvider.getInstance();

        list.addAll(Arrays.asList(builtins.getSymbols(externalContextKey, 
                                     ISymbolConstants.SYMBOL_SCOPE_ALL)));
    }
    
    /**
     * Simulate resolution of symbols from the request, session, application and none
     * scope maps.  Use a symbol provider instead if you simply want to add
     * new symbols for a tag variable or other symbol source.
     * 
     * @param list
     * @param externalContext
     */
    protected void addExternalContextSymbols(final List list, 
                                           final IDTExternalContext externalContext)
    {
        if (externalContext != null)
        {
            final ISymbol[] externalContextSymbols =
                    externalContext.getMapForScope
                  (ISymbolConstants.SYMBOL_SCOPE_ALL).values().
                                    toArray(ISymbol.EMPTY_SYMBOL_ARRAY);
            list.addAll(Arrays.asList(externalContextSymbols));
        }
    }

    /**
     * Gets all the bean symbols.  If you wish to override it would be advisable
     * to look at and/or sub-class the default bean symbol source provider
     * 
     * @param list
     * @param externalContextKey
     */
    protected void addBeanSymbols(final List list, final IAdaptable externalContextKey)
    {
        final DefaultBeanSymbolSourceProvider beanProvider =
            DefaultBeanSymbolSourceProvider.getInstance();

        final ISymbol[] beanSymbols = 
            beanProvider.getSymbols(externalContextKey, 
                                    ISymbolConstants.SYMBOL_SCOPE_ALL);

        list.addAll(Arrays.asList(beanSymbols));
    }
}

Back to the top