001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sam/tags/sakai_2-4-1/samigo-app/src/java/org/sakaiproject/tool/assessment/ui/listener/util/ContextUtil.java $
003: * $Id: ContextUtil.java 22608 2007-03-14 19:27:17Z lydial@stanford.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the"License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.tool.assessment.ui.listener.util;
021:
022: import java.io.Serializable;
023: import java.util.Iterator;
024: import java.util.Map;
025: import org.sakaiproject.util.ResourceLoader;
026: import java.util.Locale;
027:
028: import java.text.NumberFormat;
029:
030: import javax.faces.context.FacesContextFactory;
031: import javax.faces.lifecycle.Lifecycle;
032: import javax.faces.lifecycle.LifecycleFactory;
033: import javax.faces.FactoryFinder;
034: import javax.faces.application.Application;
035: import javax.faces.application.ApplicationFactory;
036: import javax.faces.context.FacesContext;
037: import java.util.ArrayList;
038: import javax.servlet.*;
039: import javax.servlet.http.*;
040: import org.apache.commons.logging.Log;
041: import org.apache.commons.logging.LogFactory;
042: import org.sakaiproject.component.cover.ServerConfigurationService;
043:
044: /**
045: * <p>Description: Action Listener helper utility</p>
046: * <p>Copyright: Copyright (c) 2004</p>
047: * <p>Organization: Sakai Project</p>
048: * @author Ed Smiley
049: * @version $Id: ContextUtil.java 22608 2007-03-14 19:27:17Z lydial@stanford.edu $
050: */
051:
052: public class ContextUtil {
053:
054: private static Log log = LogFactory.getLog(ContextUtil.class);
055:
056: private static ServletContext M_servletContext = null;
057:
058: /**
059: * Determine if we have been passed a parameter ending in the param string,
060: * else null. We are doing an endsWith test, since the default JSF renderer
061: * embeds the parent identity in the HTML id string; we look for the id that was
062: * specified in the JSF.
063: *
064: *
065: * @param lookup JSF id String
066: * @return String the full parameter
067: */
068: public static String lookupParam(String lookup) {
069: FacesContext context = FacesContext.getCurrentInstance();
070: Map requestParams = context.getExternalContext()
071: .getRequestParameterMap();
072:
073: Iterator iter = requestParams.keySet().iterator();
074: while (iter.hasNext()) {
075: String currKey = (String) iter.next();
076: if (currKey.endsWith(lookup)) {
077: return (String) requestParams.get(currKey);
078: }
079: }
080: return null;
081: }
082:
083: /**
084: * Determine if we have been passed a parameter that contains a given string,
085: * else null. Typically this would be where you want to check for one of a set
086: * of similar commandLinks or commandButtons, such as the sortBy headings in
087: * evaluation.
088: *
089: * @param paramPart String to look for
090: * @return String last part of full parameter, corresponding to JSF id
091: */
092: public static String paramLike(String paramPart) {
093: FacesContext context = FacesContext.getCurrentInstance();
094: Map requestParams = context.getExternalContext()
095: .getRequestParameterMap();
096:
097: Iterator iter = requestParams.keySet().iterator();
098: while (iter.hasNext()) {
099: String currKey = (String) iter.next();
100:
101: int location = currKey.indexOf(paramPart);
102: if (location > -1) {
103: return currKey.substring(location);
104: }
105: }
106: return null;
107: }
108:
109: /**
110: * Determine if we have been passed a parameter that contains a given string,
111: * return ArrayList of these Strings, else return empty list.
112: *
113: * Typically this would be where you want to check for one of a set
114: * of similar radio buttons commandLinks or commandButtons.
115: *
116: * @param paramPart String to look for
117: * @return ArrayList of last part Strings of full parameter, corresponding to JSF id
118: */
119: public static ArrayList paramArrayLike(String paramPart) {
120: FacesContext context = FacesContext.getCurrentInstance();
121: Map requestParams = context.getExternalContext()
122: .getRequestParameterMap();
123: ArrayList list = new ArrayList();
124:
125: Iterator iter = requestParams.keySet().iterator();
126: while (iter.hasNext()) {
127: String currKey = (String) iter.next();
128:
129: int location = currKey.indexOf(paramPart);
130: if (location > -1) {
131: list.add(currKey.substring(location));
132: }
133: }
134: return list;
135:
136: }
137:
138: /**
139: * Determine if we have been passed a parameter that contains a given string,
140: * else null. Typically this would be where you want to check for one of a set
141: * of similar commandLinks or commandButtons, such as the sortBy headings in
142: * evaluation.
143: *
144: * @param paramPart String to look for
145: * @return String the value of the first hit
146: */
147: public static String paramValueLike(String paramPart) {
148: FacesContext context = FacesContext.getCurrentInstance();
149: Map requestParams = context.getExternalContext()
150: .getRequestParameterMap();
151:
152: Iterator iter = requestParams.keySet().iterator();
153: while (iter.hasNext()) {
154: String currKey = (String) iter.next();
155:
156: int location = currKey.indexOf(paramPart);
157: if (location > -1) {
158: return (String) requestParams.get(currKey);
159: }
160: }
161: return null;
162: }
163:
164: /**
165: * Determine if we have been passed a parameter that contains a given string,
166: * return ArrayList of the corresponding values, else return empty list.
167: *
168: * Typically this would be where you want to check for one of a set
169: * of similar radio buttons commandLinks or commandButtons.
170: *
171: * @param paramPart String to look for
172: * @return ArrayList of corresponding values
173: */
174: public static ArrayList paramArrayValueLike(String paramPart) {
175: FacesContext context = FacesContext.getCurrentInstance();
176: Map requestParams = context.getExternalContext()
177: .getRequestParameterMap();
178: ArrayList list = new ArrayList();
179:
180: Iterator iter = requestParams.keySet().iterator();
181: while (iter.hasNext()) {
182: String currKey = (String) iter.next();
183:
184: int location = currKey.indexOf(paramPart);
185: if (location > -1) {
186: list.add((String) requestParams.get(currKey));
187: }
188: }
189: return list;
190:
191: }
192:
193: /**
194: * Helper method to look up backing bean.
195: * Don't forget to cast!
196: * e.g. (TemplateBean) ContextUtil.lookupBean("template")
197: * @param context the faces context
198: * @return the backing bean
199: * @throws FacesException
200: */
201: public static Serializable lookupBean(String beanName) {
202: FacesContext facesContext = FacesContext.getCurrentInstance();
203: ApplicationFactory factory = (ApplicationFactory) FactoryFinder
204: .getFactory(FactoryFinder.APPLICATION_FACTORY);
205: Application application = factory.getApplication();
206: Serializable bean = (Serializable) application
207: .getVariableResolver().resolveVariable(facesContext,
208: beanName);
209: return bean;
210: }
211:
212: /**
213: * Helper method to look up backing bean, when OUTSIDE faces in a servlet.
214: * Don't forget to cast!
215: * e.g. (TemplateBean) ContextUtil.lookupBean("template")
216: *
217: * @param beanName
218: * @param request servlet request
219: * @param response servlet response
220: * @return the backing bean
221: */
222: public static Serializable lookupBeanFromExternalServlet(
223: String beanName, HttpServletRequest request,
224: HttpServletResponse response) {
225: // prepare lifecycle
226: LifecycleFactory lFactory = (LifecycleFactory) FactoryFinder
227: .getFactory(FactoryFinder.LIFECYCLE_FACTORY);
228: Lifecycle lifecycle = lFactory
229: .getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
230:
231: FacesContextFactory fcFactory = (FacesContextFactory) FactoryFinder
232: .getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
233:
234: // in the integrated environment, we can't get the ServletContext from the
235: // HttpSession of the request - because the HttpSession is webcontainer-wide,
236: // its not tied to a particular servlet.
237: ServletContext servletContext = M_servletContext;
238: if (servletContext == null) {
239: servletContext = request.getSession().getServletContext();
240: }
241:
242: FacesContext facesContext = fcFactory.getFacesContext(
243: servletContext, request, response, lifecycle);
244:
245: ApplicationFactory factory = (ApplicationFactory) FactoryFinder
246: .getFactory(FactoryFinder.APPLICATION_FACTORY);
247: Application application = factory.getApplication();
248: Serializable bean = (Serializable) application
249: .getVariableResolver().resolveVariable(facesContext,
250: beanName);
251: return bean;
252: }
253:
254: /**
255: * Called by LoginServlet
256: */
257: public static void setServletContext(ServletContext context) {
258: M_servletContext = context;
259: }
260:
261: /**
262: * Gets a localized message string based on the locale determined by the
263: * FacesContext.
264: * @param key The key to look up the localized string
265: */
266: public static String getLocalizedString(String bundleName,
267: String key) {
268: //Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
269: ResourceLoader rb = new ResourceLoader(bundleName);
270: return rb.getString(key);
271: }
272:
273: public static String getLocalizedString(HttpServletRequest request,
274: String bundleName, String key) {
275: //Locale locale = request.getLocale();
276: ResourceLoader rb = new ResourceLoader(bundleName);
277: return rb.getString(key);
278: }
279:
280: public static String getStringInUnicode(String string) {
281: String s = "";
282: char[] charArray = string.toCharArray();
283: for (int i = 0; i < charArray.length; i++) {
284: char ch = charArray[i];
285: s += toUnicode(ch);
286: }
287: log.debug("***unicode=" + s);
288: return s;
289: }
290:
291: private static char hexdigit(int v) {
292: String symbs = "0123456789ABCDEF";
293: return symbs.charAt(v & 0x0f);
294: }
295:
296: private static String hexval(int v) {
297: return String.valueOf(hexdigit(v >>> 12))
298: + String.valueOf(hexdigit(v >>> 8))
299: + String.valueOf(hexdigit(v >>> 4))
300: + String.valueOf(hexdigit(v));
301: }
302:
303: private static String toUnicode(char ch) {
304: int val = (int) ch;
305: if (val == 10)
306: return "\\n";
307: else if (val == 13)
308: return "\\r";
309: else if (val == 92)
310: return "\\\\";
311: else if (val == 34)
312: return "\\\"";
313: else if (val == 39)
314: return "\\\'";
315: else if (val < 32 || val > 126)
316: return "\\u" + hexval(val);
317: else
318: return String.valueOf(ch);
319: }
320:
321: public static String getRoundedValue(String orig, int maxdigit) {
322: Float origfloat = new Float(orig);
323: return getRoundedValue(origfloat, maxdigit);
324: }
325:
326: public static String getRoundedValue(Float orig, int maxdigit) {
327: NumberFormat nf = NumberFormat.getInstance();
328: nf.setMaximumFractionDigits(maxdigit);
329: String newscore = nf.format(orig);
330: return newscore;
331: }
332:
333: public static String escapeApostrophe(String input) {
334: // this is needed to escape the ' in some firstname and lastname, that caused javascript error , SAK-4121
335: // no longer needed because we don't pass firstname and lastname in f:param. but we'll keep this method here
336: String regex = "'";
337: String replacement = "\\\\'";
338: String output = input.replaceAll(regex, replacement);
339: return output;
340: }
341:
342: public static String getProtocol() {
343: return ServerConfigurationService.getServerUrl();
344: }
345:
346: public static String stringWYSIWYG(String s) {// this is to detect an empty in WYSIWYG FF1.5
347:
348: if ((s != null) && (" ".equals(s.trim())))
349: s = "";
350: return s;
351: }
352:
353: public static String getRelativePath(String url) {
354: // replace whitespace with %20
355: String protocol = getProtocol();
356: url = replaceSpace(url);
357: String location = url;
358: int index = url.lastIndexOf(protocol);
359: if (index == 0) {
360: location = url.substring(protocol.length());
361: }
362: return location;
363: }
364:
365: private static String replaceSpace(String tempString) {
366: String newString = "";
367: char[] oneChar = new char[1];
368: for (int i = 0; i < tempString.length(); i++) {
369: if (tempString.charAt(i) != ' ') {
370: oneChar[0] = tempString.charAt(i);
371: String concatString = new String(oneChar);
372: newString = newString.concat(concatString);
373: } else {
374: newString = newString.concat("%20");
375: }
376: }
377: return newString;
378: }
379:
380: }
|