📄 parser.java
字号:
} else if ("xml".equals(nm)) { noEmpty("xml", val, pi); pgdef.setFirstLine("<?xml " + val + "?>"); } else { log.warning("Ignored unknown attribute: "+nm+", "+pi.getLocator()); } } } /** Process the component directive. */ private void parseComponentDirective(PageDefinition pgdef, ProcessingInstruction pi, Map params) throws Exception { final String name = (String)params.remove("name"); noELnorEmpty("name", name, pi); String macroURI = (String)params.remove("macroURI"); if (macroURI == null) macroURI = (String)params.remove("macro-uri"); //backward compatible (2.4.x) final String extds = (String)params.remove("extends"); final String clsnm = (String)params.remove("class"); ComponentDefinition compdef; if (macroURI != null) { //if (D.ON && log.finerable()) log.finer("macro component definition: "+name); final String inline = (String)params.remove("inline"); noEL("inline", inline, pi); noEL("macroURI", macroURI, pi); //no EL because pagedef must be loaded to resolve //the impl class before creating an instance of macro final boolean bInline = "true".equals(inline); compdef = pgdef.getLanguageDefinition().getMacroDefinition( name, toAbsoluteURI(macroURI, false), bInline, pgdef); if (!isEmpty(clsnm)) { if (bInline) throw new UiException("class not allowed with inline macros, "+pi.getLocator()); noEL("class", clsnm, pi); compdef.setImplementationClass(clsnm); //Resolve later since might defined in zscript } } else if (extds != null) { //extends //if (D.ON && log.finerable()) log.finer("Override component definition: "+name); noEL("extends", extds, pi); final ComponentDefinition ref = pgdef.getLanguageDefinition() .getComponentDefinition(extds); if (ref.isMacro()) throw new UiException("Unable to extend from a macro component, "+pi.getLocator()); compdef = ref.clone(null, name); if (!isEmpty(clsnm)) { noEL("class", clsnm, pi); compdef.setImplementationClass(clsnm); //Resolve later since might defined in zscript } } else { //if (D.ON && log.finerable()) log.finer("Add component definition: name="+name); noELnorEmpty("class", clsnm, pi); final ComponentDefinitionImpl cdi = new ComponentDefinitionImpl(null, pgdef, name, (Class)null); cdi.setCurrentDirectory(getLocator().getDirectory()); //mold URI requires it compdef = cdi; compdef.setImplementationClass(clsnm); //Resolve later since might be defined in zscript } pgdef.addComponentDefinition(compdef); String moldnm = (String)params.remove("moldName"); if (moldnm == null) moldnm = (String)params.remove("mold-name"); //backward comaptible (2.4.x) noEL("moldName", moldnm, pi); String moldURI = (String)params.remove("moldURI"); if (moldURI == null) moldURI = (String)params.remove("mold-uri"); //backward comaptible (2.4.x) if (!isEmpty(moldURI)) compdef.addMold(isEmpty(moldnm) ? "default": moldnm, moldURI.startsWith("class:") ? moldURI: toAbsoluteURI(moldURI, true)); for (Iterator e = params.entrySet().iterator(); e.hasNext();) { final Map.Entry me = (Map.Entry)e.next(); compdef.addProperty((String)me.getKey(), (String)me.getValue()); } } /** Parse the evaluator directive. */ private static void parseEvaluatorDirective(PageDefinition pgdef, ProcessingInstruction pi, Map params) throws Exception { final String clsnm = (String)params.remove("class"); if (clsnm != null && clsnm.length() > 0) { noELnorEmpty("class", clsnm, pi); pgdef.setExpressionFactoryClass(locateClass(clsnm)); } else { //name has the lower priorty final String nm = (String)params.remove("name"); if (nm != null) pgdef.setExpressionFactoryClass(Evaluators.getEvaluatorClass(nm)); } final String imports = (String)params.remove("import"); if (imports != null && imports.length() > 0) { //Note: we cannot use Maps.parse since it cannot handle //"a,b" => (null, a) will be replaced by (null, b) Collection ims = CollectionsX.parse(null, imports, ',', false); for (Iterator it = ims.iterator(); it.hasNext();) { final String im = (String)it.next(); final int k = im.indexOf('='); String nm = k > 0 ? im.substring(0, k).trim(): null; String cn = (k >= 0 ? im.substring(k + 1): im).trim(); if (cn.length() != 0) { final Class cs = locateClass(cn); if (nm == null || nm.length() == 0) { final int j = cn.lastIndexOf('.'); nm = j >= 0 ? cn.substring(j + 1): cn; } pgdef.addExpressionImport(nm, cs); } } } } /** Parse the XEL method. */ private static void parseXelMethod(PageDefinition pgdef, ProcessingInstruction pi, Map params) throws Exception { final String prefix = (String)params.remove("prefix"); noELnorEmpty("prefix", prefix, pi); final String nm = (String)params.remove("name"); noELnorEmpty("name", nm, pi); final String clsnm = (String)params.remove("class"); noELnorEmpty("class", clsnm, pi); final String sig = (String)params.remove("signature"); noELnorEmpty("signature", sig, pi); final Method mtd; try { final Class cls = Classes.forNameByThread(clsnm); mtd = Classes.getMethodBySignature(cls, sig, null); } catch (ClassNotFoundException ex) { throw new UiException("Class not found: "+clsnm+", "+pi.getLocator()); } catch (Exception ex) { throw new UiException("Method not found: "+sig+" in "+clsnm+", "+pi.getLocator()); } if ((mtd.getModifiers() & Modifier.STATIC) == 0) throw new UiException("Not a static method: "+mtd); pgdef.addXelMethod(prefix, nm, new MethodFunction(mtd)); } private static void noELnorEmpty(String nm, String val, Item item) throws UiException { if (isEmpty(val)) throw new UiException(nm + " cannot be empty, "+item.getLocator()); noEL(nm, val, item); } private static void noEL(String nm, String val, Item item) throws UiException { if (val != null && val.indexOf("${") >= 0) throw new UiException(nm+" does not support EL expressions, "+item.getLocator()); } /** Checks whether the value is an empty string. * Note: Like {@link #noEL}, it is OK to be null!! * To check neither null nor empty, use IDOMs.getRequiredAttribute. */ private static void noEmpty(String nm, String val, Item item) throws UiException { if (val != null && val.length() == 0) throw new UiException(nm+" cannot be empty, "+item.getLocator()); } private String toAbsoluteURI(String uri, boolean allowEL) { if (uri != null && uri.length() > 0) { final char cc = uri.charAt(0); if (cc != '/' && cc != '~' && (!allowEL || uri.indexOf("${") < 0) && !Servlets.isUniversalURL(uri)) { final String dir = getLocator().getDirectory(); if (dir != null && dir.length() > 0) return dir.charAt(dir.length() - 1) == '/' ? dir + uri: dir + '/' + uri; } } return uri; } /** Parses the specified elements. */ private void parse(PageDefinition pgdef, NodeInfo parent, Collection items, AnnotationHelper annHelper) throws Exception { final ComponentInfo parentInfo = parent instanceof ComponentInfo ? (ComponentInfo)parent: null; for (Iterator it = items.iterator(); it.hasNext();) { final Object o = it.next(); if (o instanceof Element) { parse(pgdef, parent, (Element)o, annHelper); } else if (o instanceof ProcessingInstruction) { parse(pgdef, (ProcessingInstruction)o); } else if ((o instanceof Text) || (o instanceof CData)) { String label = ((Item)o).getText(), trimLabel = label.trim();; LanguageDefinition parentlang = getLanguageDefinition(parent); if (parentlang == null) parentlang = pgdef.getLanguageDefinition(); if (trimLabel.length() > 0) { //consider as a label if (parentInfo instanceof NativeInfo) { ((NativeInfo)parentInfo).appendChild( new TextInfo(pgdef.getEvaluatorRef(), trimLabel)); } else { final String textAs = parentInfo.getTextAs(); if (textAs != null) { parentInfo.addProperty(textAs, trimLabel, null); } else { if (!parentlang.isRawLabel()) label = trimLabel; parentlang.newLabelInfo(parentInfo, label); } } } } } } private static final LanguageDefinition getLanguageDefinition(NodeInfo node) { for (; node != null; node = node.getParent()) { if (node instanceof ComponentInfo) { LanguageDefinition langdef = ((ComponentInfo)node).getLanguageDefinition(); if (langdef != null) return langdef; } else if (node instanceof PageDefinition) { return ((PageDefinition)node).getLanguageDefinition(); } } return null; } /** Parse an component definition specified in the given element. */ private void parse(PageDefinition pgdef, NodeInfo parent, Element el, AnnotationHelper annHelper) throws Exception { final String nm = el.getLocalName(); final Namespace ns = el.getNamespace(); final String pref = ns != null ? ns.getPrefix(): ""; final String uri = ns != null ? ns.getURI(): ""; LanguageDefinition langdef = pgdef.getLanguageDefinition(); if ("zscript".equals(nm) && isZkElement(langdef, nm, pref, uri)) { parseZScript(parent, el, annHelper); } else if ("attribute".equals(nm) && isZkElement(langdef, nm, pref, uri)) { if (!(parent instanceof ComponentInfo)) throw new UiException("<attribute> cannot be the root element, "+el.getLocator()); parseAttribute((ComponentInfo)parent, el, annHelper); } else if ("custom-attributes".equals(nm) && isZkElement(langdef, nm, pref, uri)) { parseCustomAttributes(parent, el, annHelper); } else if ("variables".equals(nm) && isZkElement(langdef, nm, pref, uri)) { parseVariables(parent, el, annHelper); } else if (LanguageDefinition.ANNO_NAMESPACE.equals(uri)) { parseAnnotation(el, annHelper); } else { //if (D.ON && log.debugable()) log.debug("component: "+nm+", ns:"+ns); final ComponentInfo compInfo; if ("zk".equals(nm) && isZkElement(langdef, nm, pref, uri)) { if (annHelper.clear()) log.warning("Annotations are ignored since <zk> doesn't support them, "+el.getLocator()); compInfo = new ComponentInfo(parent, ComponentDefinition.ZK); } else { boolean prefRequired = uri.startsWith(LanguageDefinition.NATIVE_NAMESPACE_PREFIX); boolean bNative = prefRequired || LanguageDefinition.NATIVE_NAMESPACE.equals(uri); if (!bNative && langdef.isNative() && !langdef.getNamespace().equals(uri)) bNative = prefRequired = ("".equals(pref) && "".equals(uri)) || !LanguageDefinition.exists(uri); //Spec: if pref/URI not specified => native // if uri unknown => native if (bNative) { if (annHelper.clear()) log.warning("Annotations are ignored since native doesn't support them, "+el.getLocator()); final NativeInfo ni; compInfo = ni = new NativeInfo( parent, langdef.getNativeDefinition(), prefRequired && pref.length() > 0 ? pref + ":" + nm: nm); //add declared namespace if starting with native: final Collection dns = el.getDeclaredNamespaces(); if (!dns.isEmpty()) addDeclaredNamespace(ni, dns, langdef); } else { final LanguageDefinition complangdef = isDefaultNS(langdef, pref, uri) ? langdef: LanguageDefinition.lookup(uri); ComponentDefinition compdef = pgdef.getComponentDefinitionMap().get(nm); if (compdef != null) { compInfo = new ComponentInfo(parent, compdef); } else if (complangdef.hasComponentDefinition(nm)) { compdef = complangdef.getComponentDefinition(nm); compInfo = new ComponentInfo(parent, compdef); langdef = complangdef; } else { compdef = complangdef.getDynamicTagDefinition(); if (compdef == null) throw new DefinitionNotFoundException("Component definition not found: "+nm+" in "+complangdef+", "+el.getLocator()); compInfo = new ComponentInfo(parent, compdef, nm); langdef = complangdef; } //process use first because addProperty needs it final String use = el.getAttributeValue("use"); if (use != null) { noEmpty("use", use, el); noEL("use", use, el); compInfo.setImplementationClass(use); //Resolve later since might defined in zscript } } } String ifc = null, unless = null, forEach = null, forEachBegin = null, forEachEnd = null; AnnotationHelper attrAnnHelper = null; for (Iterator it = el.getAttributeItems().iterator(); it.hasNext();) { final Attribute attr = (Attribute)it.next(); final Namespace attrns = attr.getNamespace(); final String attnm = attr.getLocalName(); final String attval = attr.getValue(); if (attrns != null && LanguageDefinition.ANNO_NAMESPACE.equals(attrns.getURI())) { if (attrAnnHelper == null) attrAnnHelper = new AnnotationHelper(); attrAnnHelper.addByRawValue(attnm, attval); } else if ("apply".equals(attnm) && isZkAttr(langdef, attrns)) { compInfo.setApply(attval); } else if ("forward".equals(attnm) && isZkAttr(langdef, attrns)) { compInfo.setForward(attval); } else if ("if".equals(attnm) && isZkAttr(langdef, attrns)) { ifc = attval; } else if ("unless".equals(attnm) && isZkAttr(langdef, attrns)) { unless = attval; } else if ("forEach".equals(attnm) && isZkAttr(langdef, attrns)) { forEach = attval; } else if ("forEachBegin".equals(attnm) && isZkAttr(langdef, attrns)) { forEachBegin = attval;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -