📄 definitionloaders.java
字号:
/* DefinitionLoaders.java{{IS_NOTE Purpose: Description: History: Mon Aug 29 21:57:08 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.ui.metainfo;import java.lang.reflect.Field;import java.util.Iterator;import java.util.ListIterator;import java.util.List;import java.util.LinkedList;import java.util.Set;import java.util.HashSet;import java.util.Map;import java.util.HashMap;import java.util.Enumeration;import java.util.Collections;import java.net.URL;import java.io.IOException;import org.zkoss.lang.D;import org.zkoss.lang.Classes;import org.zkoss.util.IllegalSyntaxException;import org.zkoss.util.logging.Log;import org.zkoss.util.resource.Locator;import org.zkoss.util.resource.ClassLocator;import org.zkoss.idom.Document;import org.zkoss.idom.Element;import org.zkoss.idom.ProcessingInstruction;import org.zkoss.idom.input.SAXBuilder;import org.zkoss.idom.util.IDOMs;import org.zkoss.el.Taglib;import org.zkoss.web.servlet.JavaScript;import org.zkoss.web.servlet.StyleSheet;import org.zkoss.zk.ui.UiException;import org.zkoss.zk.ui.metainfo.LanguageDefinition;import org.zkoss.zk.ui.metainfo.ComponentDefinition;/** * Utilities to load language definitions. * * @author tomyeh */public class DefinitionLoaders { private static final Log log = Log.lookup(DefinitionLoaders.class); private static List _addons = Collections.EMPTY_LIST; /** Adds a language addon. * * <p>Note: this method can be called only before definitions have * been loaded. * * @exception IllegalStateException if definitions has been loaded. */ public static void addLanguage(Locator locator, URL url) { if (locator == null || url == null) throw new IllegalArgumentException("null"); if (_addons == null) throw new IllegalStateException("Definition has been loaded. You cannot add more."); if (_addons == Collections.EMPTY_LIST) _addons = new LinkedList(); _addons.add(new Object[] {locator, url}); } /** Loads all lang.xml found in /metainfo/zk. */ /*package*/ static void load() { final ClassLocator locator = new ClassLocator(); //1. process lang.xml (no particular dependency) try { for (Enumeration en = locator.getResources("metainfo/zk/lang.xml"); en.hasMoreElements();) { final URL url = (URL)en.nextElement(); if (log.debugable()) log.debug("Loading "+url); final Document doc = new SAXBuilder(false, false, true).build(url); if (checkVersion(url, doc)) parse(doc, locator, false); } } catch (Exception ex) { throw UiException.Aide.wrap(ex); //abort } //2. process lang-addon.xml (with dependency) try { final List xmls = locator.getDependentXMLResources( "metainfo/zk/lang-addon.xml", "addon-name", "depends"); for (Iterator it = xmls.iterator(); it.hasNext();) { final ClassLocator.Resource res = (ClassLocator.Resource)it.next(); if (checkVersion(res.url, res.document)) { try { parse(res.document, locator, true); } catch (Exception ex) { log.error("Failed to load addon", ex); //keep running } } } } catch (Exception ex) { log.error("Failed to load addon", ex); //keep running } //3. process other addon (from ConfigParser) for (Iterator it = _addons.iterator(); it.hasNext();) { final Object[] p = (Object[])it.next(); final Locator loc = (Locator)p[0]; final URL url = (URL)p[1]; try { parse(new SAXBuilder(false, false, true).build(url), loc, true); } catch (Exception ex) { log.error("Failed to load addon: "+url, ex); //keep running } } _addons = null; //prevent addLanguage being called again } /** Checks and returns whether the loaded document's version is correct. */ private static boolean checkVersion(URL url, Document doc) throws Exception { final Element el = doc.getRootElement().getElement("version"); if (el != null) { final String clsnm = IDOMs.getRequiredElementValue(el, "version-class"); final String uid = IDOMs.getRequiredElementValue(el, "version-uid"); final Class cls = Classes.forNameByThread(clsnm); final Field fld = cls.getField("UID"); final String uidInClass = (String)fld.get(null); if (uid.equals(uidInClass)) { return true; } else { log.info("Ignore "+url+"\nCause: version not matched; expected="+uidInClass+", xml="+uid); return false; } } else { log.info("Ignore "+url+"\nCause: version not specified"); return false; //backward compatible } } private static void parse(Document doc, Locator locator, boolean addon) throws Exception { final Element root = doc.getRootElement(); final String lang = IDOMs.getRequiredElementValue(root, "language-name"); final LanguageDefinition langdef; if (addon) { if (log.debugable()) log.debug("Addon language to "+lang+" from "+root.getElementValue("addon-name", true)); langdef = LanguageDefinition.lookup(lang); if (!root.getElements("case-insensitive").isEmpty()) throw new UiException("You can not specify case-insensitive in addon"); } else { final String ns = (String)IDOMs.getRequiredElementValue(root, "namespace"); if (log.debugable()) log.debug("Load language: "+lang+", "+ns); String clientType = root.getElementValue("client-type", true); if (clientType == null || clientType.length() == 0) clientType = "html"; //default final Map pagemolds = parseMolds(root); final String desktopURI = (String)pagemolds.get("desktop"); final String pageURI = (String)pagemolds.get("page"); if (desktopURI == null || pageURI == null) throw new IllegalSyntaxException("Both desktop and page molds must be specified, "+root.getLocator()); final List exts = parseExtensions(root); if (exts.isEmpty()) throw new IllegalSyntaxException("The extension must be specified for "+lang); langdef = new LanguageDefinition( clientType, lang, ns, exts, desktopURI, pageURI, locator); for (Iterator it = root.getElements("case-insensitive").iterator(); it.hasNext();) { final Element el = (Element)it.next(); final String s = el.getText(true); langdef.setCaseInsensitive(!"false".equalsIgnoreCase(s)); } } parsePI(langdef, doc); parseLabelTemplate(langdef, root); parseDynamicTag(langdef, root); parseMacroTemplate(langdef, root); for (Iterator it = root.getElements("javascript").iterator(); it.hasNext();) { final Element el = (Element)it.next(); final String src = el.getAttributeValue("src"); final String ctn = el.getText(true); final JavaScript js; if (src != null && src.length() > 0) { if (ctn != null && ctn.length() > 0) throw new UiException("You cannot specify the content if the src attribute is specified, "+el.getLocator()); final String charset = el.getAttributeValue("charset"); js = new JavaScript(src, charset); } else if (ctn != null && ctn.length() > 0) { js = new JavaScript(ctn); } else { throw new UiException("You must specify either the src attribute or the content, "+el.getLocator()); } langdef.addJavaScript(js); } for (Iterator it = root.getElements("javascript-module").iterator(); it.hasNext();) { final Element el = (Element)it.next(); langdef.addJavaScriptModule( IDOMs.getRequiredAttributeValue(el, "name"), IDOMs.getRequiredAttributeValue(el, "version")); } for (Iterator it = root.getElements("stylesheet").iterator(); it.hasNext();) { final Element el = (Element)it.next(); final String href = el.getAttributeValue("href"); final String ctn = el.getText(true); final StyleSheet ss; if (href != null && href.length() > 0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -