📄 zkfns.java
字号:
/* ZkFns.java{{IS_NOTE Purpose: Description: History: Tue Jun 7 11:09:48 2005, Created by tomyeh}}IS_NOTECopyright (C) 2005 Potix Corporation. All Rights Reserved.{{IS_RIGHT This program is distributed under GPL Version 2.0 in the hope that it will be useful, but WITHOUT ANY WARRANTY.}}IS_RIGHT*/package org.zkoss.zk.fn;import java.util.LinkedHashSet;import java.util.Set;import java.util.Map;import java.util.Iterator;import java.util.List;import java.util.LinkedList;import java.util.Collection;import java.util.ArrayList;import java.util.Set;import java.util.Locale;import java.util.Calendar;import java.text.SimpleDateFormat;import java.text.DecimalFormatSymbols;import java.io.Writer;import java.io.IOException;import javax.servlet.ServletRequest;import org.zkoss.lang.Strings;import org.zkoss.lang.Objects;import org.zkoss.util.CacheMap;import org.zkoss.util.Locales;import org.zkoss.util.logging.Log;import org.zkoss.web.fn.ServletFns;import org.zkoss.web.servlet.JavaScript;import org.zkoss.web.servlet.StyleSheet;import org.zkoss.zk.ui.Component;import org.zkoss.zk.ui.WebApp;import org.zkoss.zk.ui.Desktop;import org.zkoss.zk.ui.Page;import org.zkoss.zk.ui.Execution;import org.zkoss.zk.ui.Executions;import org.zkoss.zk.ui.UiException;import org.zkoss.zk.ui.util.Configuration;import org.zkoss.zk.ui.util.ThemeProvider;import org.zkoss.zk.ui.sys.PageCtrl;import org.zkoss.zk.ui.metainfo.LanguageDefinition;import org.zkoss.zk.ui.http.WebManager;import org.zkoss.zk.au.AuResponse;/** * Utilities for using EL. * * @author tomyeh */public class ZkFns { private static final Log log = Log.lookup(ZkFns.class); /** Denotes whether style sheets are generated for this request. */ private static final String ATTR_LANG_CSS_GENED = "javax.zkoss.zk.lang.css.generated"; //Naming with javax to be able to shared among portlets /** Denotes whether JavaScripts are generated for this request. */ private static final String ATTR_LANG_JS_GENED = "javax.zkoss.zk.lang.js.generated"; //Naming with javax to be able to shared among portlets protected ZkFns() {} /** Redraw the specified component into the specified out. * * @param comp the component. If null, nothing happens * @param out the output. If null, the current output * will be used. */ public static final void redraw(Component comp, Writer out) throws IOException { if (comp == null) return; //nothing to do if (out == null) out = getCurrentOut(); try { comp.redraw(out); } catch (Throwable ex) { //Commons-el sometime eat exception, so show more info to debug log.realCauseBriefly("Failed to redraw "+comp, ex); if (ex instanceof IOException) throw (IOException)ex; throw UiException.Aide.wrap(ex); } } /** Returns the current writer to generate the output. * @since 3.0.0 */ public static final Writer getCurrentOut() throws IOException { return ServletFns.getCurrentOut(); } /** Returns JavaScript for handling the specified response. */ public static final String outResponseJavaScripts(Collection responses) { if (responses == null || responses.isEmpty()) return ""; final StringBuffer sb = new StringBuffer(256) .append("zk.addInit(function(){\n"); for (Iterator it = responses.iterator(); it.hasNext();) { final AuResponse response = (AuResponse)it.next(); sb.append("zk.process('").append(response.getCommand()) .append("',"); final String[] data = response.getData(); final int datanum = data != null ? data.length: 0; sb.append(datanum); for (int j = 0; j < datanum; ++j) { sb.append(",\""); if (data[j] != null) sb.append(Strings.escape(data[j], "\"\\\n\r")); sb.append('"'); } sb.append(");\n"); } return sb.append("});").toString(); } /** Returns HTML tags to include all JavaScript files and codes that are * required when loading a ZUML page. * * <p>Note: it assumes {@link Executions#getCurrent} is available. * * <p>FUTURE CONSIDERATION: we might generate the inclusion on demand * instead of all at once. */ public static final String outLangJavaScripts(String action) { final ServletRequest request = ServletFns.getCurrentRequest(); if (WebManager.getRequestLocal(request, ATTR_LANG_JS_GENED) != null) return ""; //nothing to generate WebManager.setRequestLocal(request, ATTR_LANG_JS_GENED, Boolean.TRUE); if (action == null) throw new IllegalArgumentException("null"); final Desktop desktop = Executions.getCurrent().getDesktop(); final WebApp wapp = desktop.getWebApp(); final Configuration config = wapp.getConfiguration(); final String deviceType = desktop.getDeviceType(); final StringBuffer sb = new StringBuffer(1536); final Set jses = new LinkedHashSet(37); for (Iterator it = LanguageDefinition.getByDeviceType(deviceType).iterator(); it.hasNext();) jses.addAll(((LanguageDefinition)it.next()).getJavaScripts()); for (Iterator it = jses.iterator(); it.hasNext();) append(sb, (JavaScript)it.next()); sb.append("\n<script type=\"text/javascript\">\n") .append("zk_action=\"").append(action) .append("\";\nzk_procto=") .append(config.getProcessingPromptDelay()) .append(";\nzk_tipto=") .append(config.getTooltipDelay()) .append(";\nzk_ver='").append(wapp.getVersion()) .append("';\n"); if (!config.isDisableBehindModalEnabled()) sb.append("zk.ndbModal=true;\n"); boolean cache = config.isKeepDesktopAcrossVisits(); if (!cache) { final Page page = getFirstPage(desktop); if (page != null) { Boolean b = ((PageCtrl)page).getCacheable(); cache = b != null && b.booleanValue(); } } if (cache) sb.append("zk.keepDesktop=true;\n"); if (config.getPerformanceMeter() != null) sb.append("zk.pfmeter=true;\n"); sb.append("zk.eru={"); final int[] cers = config.getClientErrorReloadCodes(); boolean first = true; for (int j = 0; j < cers.length; ++j) { final String uri = config.getClientErrorReload(cers[j]); if (uri != null) { if (first) first = false; else sb.append(','); sb.append("e").append(cers[j]).append(":'") .append(Strings.escape(uri, "'\\")).append('\''); } } sb.append("};\n"); for (Iterator it = LanguageDefinition.getByDeviceType(deviceType).iterator(); it.hasNext();) { final LanguageDefinition langdef = (LanguageDefinition)it.next(); //Generate module versions final Set mods = langdef.getJavaScriptModules().entrySet(); if (!mods.isEmpty()) { for (Iterator e = mods.iterator(); e.hasNext();) { final Map.Entry me = (Map.Entry)e.next(); sb.append("\nzk.mods[\"").append(me.getKey()) .append("\"]=\"").append(me.getValue()).append("\";"); } } } sb.append("\n</script>\n"); final String uamsg = desktop.getDevice().getUnavailableMessage(); if (uamsg != null) sb.append("<noscript>\n").append(uamsg).append("\n</noscript>\n"); return sb.toString(); } /** Returns the first page of a desktop. */ private static Page getFirstPage(Desktop desktop) { final Collection pgs = desktop.getPages(); return pgs.isEmpty() ? null: (Page)pgs.iterator().next(); } private static void append(StringBuffer sb, JavaScript js) { sb.append("\n<script type=\"text/javascript\""); if (js.getSrc() != null) { String url; try { url = ServletFns.encodeURL(js.getSrc()); } catch (javax.servlet.ServletException ex) { throw new UiException(ex); } //Note: Jetty might encode jessionid into URL, which //Dojo cannot handle, so we have to remove it int j = url.lastIndexOf(';'); if (j > 0 && url.indexOf('.', j + 1) < 0 && url.indexOf('/', j + 1) < 0) url = url.substring(0, j); sb.append(" src=\"").append(url).append('"'); final String charset = js.getCharset(); if (charset != null) sb.append(" charset=\"").append(charset).append('"'); sb.append('>'); } else { sb.append(">\n").append(js.getContent()); } sb.append("\n</script>"); } /** Returns HTML tags to include all style sheets that are * defined in all languages. * * <p>Note: it assumes {@link Executions#getCurrent} is available. * * <p>In addition to style sheets defined in lang.xml and lang-addon.xml, * it also include: * <ol> * <li>The style sheet specified in the theme-uri parameter.</li> * </ol> * * <p>FUTURE CONSIDERATION: we might generate the inclusion on demand * instead of all at once. */ public static final String outLangStyleSheets() { final ServletRequest request = ServletFns.getCurrentRequest(); if (WebManager.getRequestLocal(request, ATTR_LANG_CSS_GENED) != null) return ""; //nothing to generate WebManager.setRequestLocal(request, ATTR_LANG_CSS_GENED, Boolean.TRUE); final StringBuffer sb = new StringBuffer(512); final Execution exec = Executions.getCurrent(); for (Iterator it = getStyleSheets(exec).iterator(); it.hasNext();) append(sb, (StyleSheet)it.next(), exec, null); return sb.toString(); } /** Returns a list of {@link StyleSheet} that shall be generated * to the client for the specified execution. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -