1 package org.apache.myfaces.config; 2 3 7 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 11 import javax.faces.context.ExternalContext; 12 13 20 public class MyfacesConfig 21 { 22 private static final Log log = LogFactory.getLog(MyfacesConfig.class); 23 24 private static final String APPLICATION_MAP_PARAM_NAME = MyfacesConfig.class.getName(); 25 26 private static final String INIT_PARAM_PRETTY_HTML = "org.apache.myfaces.PRETTY_HTML"; 27 private static final boolean INIT_PARAM_PRETTY_HTML_DEFAULT = true; 28 29 private static final String INIT_PARAM_ALLOW_JAVASCRIPT = "org.apache.myfaces.ALLOW_JAVASCRIPT"; 30 private static final boolean INIT_PARAM_ALLOW_JAVASCRIPT_DEFAULT = true; 31 32 private static final String INIT_PARAM_DETECT_JAVASCRIPT = "org.apache.myfaces.DETECT_JAVASCRIPT"; 33 private static final boolean INIT_PARAM_DETECT_JAVASCRIPT_DEFAULT = false; 34 35 private static final String INIT_PARAM_AUTO_SCROLL = "org.apache.myfaces.AUTO_SCROLL"; 36 private static final boolean INIT_PARAM_AUTO_SCROLL_DEFAULT = false; 37 38 private boolean _prettyHtml; 39 private boolean _detectJavascript; 40 private boolean _allowJavascript; 41 private boolean _autoScroll; 42 43 public static MyfacesConfig getCurrentInstance(ExternalContext extCtx) 44 { 45 MyfacesConfig myfacesConfig = (MyfacesConfig)extCtx 46 .getApplicationMap().get(APPLICATION_MAP_PARAM_NAME); 47 if (myfacesConfig != null) return myfacesConfig; 48 49 myfacesConfig = new MyfacesConfig(); 50 extCtx.getApplicationMap().put(APPLICATION_MAP_PARAM_NAME, myfacesConfig); 51 52 myfacesConfig.setPrettyHtml(getBooleanInitParameter(extCtx, INIT_PARAM_PRETTY_HTML, 53 INIT_PARAM_PRETTY_HTML_DEFAULT)); 54 myfacesConfig.setAllowJavascript(getBooleanInitParameter(extCtx, INIT_PARAM_ALLOW_JAVASCRIPT, 55 INIT_PARAM_ALLOW_JAVASCRIPT_DEFAULT)); 56 myfacesConfig.setDetectJavascript(getBooleanInitParameter(extCtx, INIT_PARAM_DETECT_JAVASCRIPT, 57 INIT_PARAM_DETECT_JAVASCRIPT_DEFAULT)); 58 myfacesConfig.setAutoScroll(getBooleanInitParameter(extCtx, INIT_PARAM_AUTO_SCROLL, 59 INIT_PARAM_AUTO_SCROLL_DEFAULT)); 60 return myfacesConfig; 61 } 62 63 private static boolean getBooleanInitParameter(ExternalContext externalContext, 64 String paramName, 65 boolean defaultValue) 66 { 67 String strValue = externalContext.getInitParameter(paramName); 68 if (strValue == null) 69 { 70 if (log.isInfoEnabled()) log.info("No context init parameter '" + paramName + "' found, using default value " + defaultValue); 71 return defaultValue; 72 } 73 else if (strValue.equalsIgnoreCase("true") || strValue.equalsIgnoreCase("on") || strValue.equalsIgnoreCase("yes")) 74 { 75 return true; 76 } 77 else if (strValue.equalsIgnoreCase("false") || strValue.equalsIgnoreCase("off") || strValue.equalsIgnoreCase("no")) 78 { 79 return false; 80 } 81 else 82 { 83 if (log.isWarnEnabled()) log.warn("Wrong context init parameter '" + paramName + "' (='" + strValue + "'), using default value " + defaultValue); 84 return defaultValue; 85 } 86 } 87 88 public boolean isPrettyHtml() 89 { 90 return _prettyHtml; 91 } 92 93 public void setPrettyHtml(boolean prettyHtml) 94 { 95 _prettyHtml = prettyHtml; 96 } 97 98 public boolean isDetectJavascript() 99 { 100 return _detectJavascript; 101 } 102 103 public void setDetectJavascript(boolean detectJavascript) 104 { 105 _detectJavascript = detectJavascript; 106 } 107 108 113 public boolean isAllowJavascript() 114 { 115 return _allowJavascript; 116 } 117 118 public void setAllowJavascript(boolean allowJavascript) 119 { 120 _allowJavascript = allowJavascript; 121 } 122 123 public boolean isAutoScroll() 124 { 125 return _autoScroll; 126 } 127 128 public void setAutoScroll(boolean autoScroll) 129 { 130 _autoScroll = autoScroll; 131 } 132 } 133 | Popular Tags |