Initialize
[sdk/ide/product.git] / org.eclipse.jst.pagedesigner / src / org / eclipse / jst / pagedesigner / preview / PageExpressionContext.java
1 /*******************************************************************************\r
2  * Copyright (c) 2006 Sybase, Inc. and others.\r
3  *\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     Sybase, Inc. - initial API and implementation\r
11  *     Oracle - move to pluggable EL resolving\r
12  *******************************************************************************/\r
13 package org.eclipse.jst.pagedesigner.preview;\r
14 \r
15 import java.io.BufferedInputStream;\r
16 import java.io.InputStream;\r
17 import java.util.ArrayList;\r
18 import java.util.List;\r
19 import java.util.Map;\r
20 import java.util.Properties;\r
21 \r
22 import javax.servlet.jsp.el.ELException;\r
23 \r
24 import org.eclipse.core.resources.IProject;\r
25 import org.eclipse.core.resources.IStorage;\r
26 import org.eclipse.core.runtime.CoreException;\r
27 import org.eclipse.core.runtime.IConfigurationElement;\r
28 import org.eclipse.core.runtime.IExtension;\r
29 import org.eclipse.core.runtime.IExtensionPoint;\r
30 import org.eclipse.core.runtime.Platform;\r
31 import org.eclipse.jst.jsf.common.ui.internal.logging.Logger;\r
32 import org.eclipse.jst.jsf.common.ui.internal.utils.ResourceUtils;\r
33 import org.eclipse.jst.jsf.core.internal.tld.LoadBundleUtil;\r
34 import org.eclipse.jst.pagedesigner.IJMTConstants;\r
35 import org.eclipse.jst.pagedesigner.PDPlugin;\r
36 import org.eclipse.jst.pagedesigner.jsp.core.pagevar.IPageVariablesProvider;\r
37 import org.eclipse.jst.pagedesigner.jsp.core.pagevar.IVariableInfo;\r
38 import org.w3c.dom.Element;\r
39 \r
40 /**\r
41  * This is a static class. But it has "session" concept in it. To make it static\r
42  * is only to simplify its use.\r
43  * \r
44  * @author mengbo, ian.trimble@oracle.com\r
45  */\r
46 public class PageExpressionContext {\r
47         private static final Logger _log = PDPlugin.getLogger(PageExpressionContext.class);\r
48 \r
49         static PageExpressionContext _current;\r
50 \r
51         List _pageVarProviders = new ArrayList();\r
52 \r
53         private IProject _prj;\r
54 \r
55         /**\r
56          * @param prj\r
57          */\r
58         public PageExpressionContext(IProject prj) {\r
59                 _prj = prj;\r
60         }\r
61 \r
62         /**\r
63          * reset the context.\r
64          */\r
65         public static void reset() {\r
66                 _current = null;\r
67         }\r
68 \r
69         /**\r
70          * Initialize the current context\r
71          * @param prj\r
72          */\r
73         public static void initialize(IProject prj) {\r
74                 _current = new PageExpressionContext(prj);\r
75         }\r
76 \r
77         /**\r
78          * @return the current context\r
79          */\r
80         public static PageExpressionContext getCurrent() {\r
81                 return _current;\r
82         }\r
83 \r
84         /**\r
85          * @param provider\r
86          */\r
87         public void pushPageVarProvider(IPageVariablesProvider provider) {\r
88                 _pageVarProviders.add(provider);\r
89         }\r
90 \r
91         /**\r
92          * @param provider\r
93          */\r
94         public void popPageVarProvider(IPageVariablesProvider provider) {\r
95                 try {\r
96                         _pageVarProviders.remove(_pageVarProviders.size() - 1);\r
97                 } catch (Exception ex) {\r
98                         _log.info("PageExpressionContext.Info.0", ex); //$NON-NLS-1$\r
99                 }\r
100         }\r
101 \r
102         /**\r
103          * This method is for design time expression evaluation.\r
104          * \r
105          * @param expression\r
106          * @param expectedClass \r
107          * @param options Current Element is passed with a key of "ELEMENT"\r
108          * @return the result of evaluating the expression\r
109          * @throws ELException \r
110          */\r
111         public Object evaluateExpression(String expression, Class expectedClass, Map options)\r
112                         throws ELException {\r
113                 //Bug 319317 - Third-party plug-in providing javax.servlet.jsp.el version 2.1 or greater breaks WPE preview\r
114                 String ret = expression;\r
115                 if (options != null) {\r
116                         Object objElement = options.get("ELEMENT"); //$NON-NLS-1$\r
117                         if (objElement instanceof Element) {\r
118                                 ret = ELValueResolver.resolve((Element)objElement, expression);\r
119                         }\r
120                 }\r
121                 return ret;\r
122         }\r
123 \r
124         /**\r
125          * Gets an Object associated with a page variable.\r
126          * @param varName Page variable name.\r
127          * @return The Object associated with the named page variable, or null if the Object cannot be\r
128          * located.\r
129          */\r
130         public Object getPageVariable(String varName) {\r
131                 Object ret = null;\r
132                 // reverse order.\r
133                 for (int k = _pageVarProviders.size() - 1; k >= 0; k--) {\r
134                         IPageVariablesProvider _pageVars = (IPageVariablesProvider) _pageVarProviders.get(k);\r
135                         if (_pageVars != null) {\r
136                                 IVariableInfo[] vars = _pageVars.getBeanInfos();\r
137                                 if (vars != null) {\r
138                                         for (int i = 0; i < vars.length; i++) {\r
139                                                 if (varName.equals(vars[i].getName())) {\r
140                                                         if (vars[i].getMode() == IVariableInfo.RESOURCEBUNDLE) {\r
141                                                                 String resourceName = vars[i].getTypeInfoString();\r
142                                                                 IStorage storage = null;\r
143                                                                 try {\r
144                                                                         storage = LoadBundleUtil.getLoadBundleResource(_prj, resourceName);\r
145                                                                 } catch (CoreException cex) {\r
146                                                                         _log.info("PageExpressionContext.Info.0", cex); //$NON-NLS-1$\r
147                                                                 }\r
148                                                                 if (storage != null) {\r
149                                                                         InputStream input = null;\r
150                                                                         try {\r
151                                                                                 input = new BufferedInputStream(storage.getContents());\r
152                                                                                 Properties properties = new Properties();\r
153                                                                                 properties.load(input);\r
154                                                                                 ret = properties;\r
155                                                                         } catch (Exception ignored) {\r
156                                                                                 //ignore - we'll return null\r
157                                                                         } finally {\r
158                                                                                 ResourceUtils.ensureClosed(input);\r
159                                                                         }\r
160                                                                 }\r
161                                                         }\r
162                                                 }\r
163                                         }\r
164                                 }\r
165                         }\r
166                 }\r
167                 return ret;\r
168         }\r
169 \r
170 \r
171 \r
172         static class ELValueResolver {\r
173 \r
174                 static List<IELValueResolver> elValueResolvers;\r
175 \r
176                 public static String resolve(final Element element, final String elExpression) {\r
177                         String value = elExpression;\r
178                         if (elValueResolvers == null) {\r
179                                 readELValueResolvers();\r
180                         }\r
181                         for (IELValueResolver elValueResolver: elValueResolvers) {\r
182                                 value = elValueResolver.resolve(element, value);\r
183                         }\r
184                         return value;\r
185                 }\r
186 \r
187                 private static void readELValueResolvers() {\r
188                         elValueResolvers = new ArrayList<IELValueResolver>();\r
189                         final IExtensionPoint pdExtPt =\r
190                                 Platform.getExtensionRegistry().getExtensionPoint(\r
191                                                 PDPlugin.getPluginId(), IJMTConstants.EXTENSION_POINT_PAGEDESIGNER);\r
192                         final IExtension[] extensions = pdExtPt.getExtensions();\r
193                         for (final IExtension extension: extensions) {\r
194                                 final IConfigurationElement[] configElements = extension.getConfigurationElements();\r
195                                 for (final IConfigurationElement configElement: configElements) {\r
196                                         if (configElement.getName().equals("elValueResolver")) { //$NON-NLS-1$\r
197                                                 try {\r
198                                                         final Object objValueResolver = configElement.createExecutableExtension("class"); //$NON-NLS-1$\r
199                                                         if (objValueResolver instanceof IELValueResolver) {\r
200                                                                 if (configElement.getContributor().getName().startsWith("org.eclipse.jst")) { //$NON-NLS-1$\r
201                                                                         //add to end (give precedence to contributor-provided resolvers)\r
202                                                                         elValueResolvers.add((IELValueResolver)objValueResolver);\r
203                                                                 } else {\r
204                                                                         //add at beginning (give precedence to contributor-provided resolvers)\r
205                                                                         elValueResolvers.add(0, (IELValueResolver)objValueResolver);\r
206                                                                 }\r
207                                                         }\r
208                                                 } catch(CoreException ce) {\r
209                                                         PDPlugin.log("Error reading extensions for: " + configElement.toString(), ce); //$NON-NLS-1$\r
210                                                 }\r
211                                         }\r
212                                 }\r
213                         }\r
214                 }\r
215 \r
216         }\r
217 \r
218 }\r