class Controller { Parser parser; Model getModel() { return parser.parseModel(userInput); } Template getTemplate() { return parser.parseTemplate(userInput); } }
protected PageDefinition parse(String path, File file, Object extra) throws Exception { final Locator locator = extra != null ? (Locator) extra : getLocator(_wapp, path); Parser parser = new Parser(_wapp, locator); // Bug ZK-1132 if (file.exists()) { return parser.parse(file, path); } else { InputStream stream = parser.getLocator().getResourceAsStream(path); BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(stream)); PageDefinition pgdef = parser.parse(reader, Servlets.getExtension(path)); pgdef.setRequestPath(path); return pgdef; } finally { if (reader != null) reader.close(); } } }
private void checkZScriptEnabled(Element el) { checkZScriptEnabled(el.getLocator()); }
throw new UiException(message("Namespace, " + uri + ", no longer supported element's annotation", el)); if ("attribute".equals(nm) && isZkElement(langdef, nm, pref, uri, bNativeContent)) { if (!(parent instanceof ComponentInfo)) throw new UiException(message("<attribute> cannot be the root element", el)); parseAttribute(pgdef, (ComponentInfo) parent, el, annHelper); } else if ("template".equals(nm) && isZkElement(langdef, nm, pref, uri, bNativeContent)) { parseItems(pgdef, parseTemplate(parent, el, annHelper), el.getChildren(), annHelper, bNativeContent); if ("attribute".equals(nm) || "template".equals(nm)) return null; if ("zscript".equals(nm) && isZkElement(langdef, nm, pref, uri)) { checkZScriptEnabled(el); parseZScript(parent, el, annHelper); } else if ("custom-attributes".equals(nm) && isZkElement(langdef, nm, pref, uri, bNativeContent)) { parseCustomAttributes(langdef, parent, el, annHelper); } else if ("variables".equals(nm) && isZkElement(langdef, nm, pref, uri, bNativeContent)) { parseVariables(langdef, parent, el, annHelper); } else if ("zk".equals(nm) && isZkElement(langdef, nm, pref, uri)) { parseItems(pgdef, parseZk(parent, el, annHelper), el.getChildren(), annHelper, bNativeContent); } else if (isShadowElement(langdef, pgdef, nm, pref, uri, bNativeContent)) { NodeInfo nodeInfo = parseShadowElement(pgdef, parent, el, annHelper); parseItems(pgdef, nodeInfo, el.getChildren(), annHelper, bNativeContent); if (nodeInfo instanceof ShadowInfo && ((ShadowInfo) nodeInfo).isAnnotationNamespacedRoot()) { annHelper.setIgnoreAnnotNamespace(false); if (isZkSwitch(parent)) throw new UiException(message("Only <zk> can be used in <zk switch>", el));
LanguageDefinition parentlang = getLanguageDefinition(parent); if (parentlang == null) parentlang = pgdef.getLanguageDefinition(); final boolean bZkSwitch = isZkSwitch(parent); parseItem(pgdef, parent, (Element) o, annHelper, bNativeContent, ParsingState.FIRST); if (o instanceof Element) { breakLine = false; preNativeInfo = (NativeInfo) parseItem(pgdef, parent, (Element) o, annHelper, bNativeContent, ParsingState.SECOND); } else if (o instanceof ProcessingInstruction) { breakLine = false; parse(pgdef, (ProcessingInstruction) o); } else if (o instanceof Comment) { breakLine = false; if (breakLine && (o instanceof Text) && isEmpty(label.trim())) { if (isEmpty(label)) continue; if (isEmpty(trimLabel)) continue; throw new UiException(message("Only <zk> can be used in <zk switch>", (Item) o)); if (isXHTML && items.size() <= 1 && isEmpty(trimLabel.trim())) { continue; //don't create Text } else if (isEmpty(trimLabel) && (pi != null && !pi.isBlankPreserved() && !isNativeText(pi)))
final String l = params.remove("language"); if (l != null) { noEL("language", l, pi); lang = l; final String cls = params.remove("class"); if (src != null) { noELnorEmpty("src", src, pi); noEL("directives", dirs, pi); imports.add(new String[] { src, dirs }); noELnorEmpty("class", cls, pi); impclses.add(cls); final String val = me.getValue(); if (val == null) { noELnorEmpty(nm, nm, pi); impclses.add(nm); } else { log.warn(message("Ignored unknown attribute for import: " + nm, pi)); final PageDefinition pgdef = new PageDefinition(langdef, getLocator()); final RequestInfo ri = new RequestInfoImpl(_wapp, null, null, null, getLocator()); final UiFactory uf = ((WebAppCtrl) _wapp).getUiFactory(); for (final String[] imprt : imports) { if (pd == null) throw new UiException("The imported page not found: " + path);
throws Exception { final String name = params.remove("name"); noELnorEmpty("name", name, pi); String templateURI = params.remove("templateURI"); if (templateURI != null) { // assume it is shadow like (<apply>) noEL("templateURI", templateURI, pi); compdef = langdef.getShadowDefinition(name, pgdef, toAbsoluteURI(templateURI)); if (!isEmpty(clsnm)) { noEL("class", clsnm, pi); compdef.setImplementationClass(clsnm); noEL("inline", inline, pi); noEL("macroURI", macroURI, pi); compdef = langdef.getMacroDefinition(name, toAbsoluteURI(macroURI), bInline, pgdef); if (!isEmpty(clsnm)) { if (bInline) throw new UiException(message("class not allowed with inline macros", pi)); noEL("class", clsnm, pi); compdef.setImplementationClass(clsnm); noEL("extends", extds, pi); ref = langdef.getComponentDefinition(extds); } else { throw new UiException(message("Unable to extend from a macro component", pi));
private void parseZScript(NodeInfo parent, Element el, AnnotationHelper annHelper) { if (el.getAttributeItem("forEach") != null) throw new UiException(message("forEach not applicable to <zscript>", el)); if (annHelper.clear()) log.warn(message("Annotations are ignored since <zscript> doesn't support them", el)); noEmpty("language", zslang, el); noEL("language", zslang, el); if (!isEmpty(zsrc)) { //ignore empty (not error) ZScriptInfo zs = null; if (!zsrc.contains("${")) { final URL url = getLocator().getResource(zsrc); if (url != null) zs = new ZScriptInfo(parent, zslang, url, cond); zs = new ZScriptInfo(parent, zslang, zsrc, getLocator(), cond); if (!isEmpty(script.trim())) { final org.zkoss.xml.Locator l = el.getLocator(); int lno = l != null ? l.getLineNumber() : 0;
private void parseAttribute(PageDefinition pgdef, ComponentInfo parent, Element el, AnnotationHelper annHelper) throws Exception { if (el.getAttributeItem("forEach") != null) throw new UiException(message("forEach not applicable to attribute", el)); throw new UiException(message("The name attribute required", el)); noEmpty("name", attnm, el); final ConditionImpl cond = ConditionImpl.getInstance(el.getAttributeValue("if"), el.getAttributeValue("unless")); if (elFound != null) { if (Events.isValid(attnm)) throw new UiException(message("<" + elFound + "> not allowed in an event listener", el)); parseAsProperty(pgdef, parent, attnm, el.getChildren(), annHelper, cond); } else { final String trim = el.getAttributeValue("trim"); noEL("trim", trim, el); final String attval = el.getText(trim != null && "true".equals(trim)); addAttribute(parent, attr.getNamespace(), attnm, attval, cond, el.getLocator());
throw new UiException(message("<custom-attributes> must be used under a component", el)); if (annHelper.clear()) log.warn(message("Annotations are ignored since <custom-attributes> doesn't support them", el)); //old style annotation not supported final String attval = attr.getValue(); final String attvaltrim; if ("if".equals(attnm) && isZkElementAttr(langdef, attrns)) { ifc = attval; } else if ("unless".equals(attnm) && isZkElementAttr(langdef, attrns)) { unless = attval; } else if ("scope".equals(attnm) && isZkElementAttr(langdef, attrns)) { scope = attval; } else if ("composite".equals(attnm) && isZkElementAttr(langdef, attrns)) { composite = attval; } else if ("forEach".equals(attnm) && isZkElementAttr(langdef, attrns)) { throw new UiException(message("forEach not applicable to <custom-attributes>", el)); } else if (parent instanceof ComponentInfo && AnnotationHelper.isAnnotation(attvaltrim = attval.trim())) { if (attrAnnHelper == null) attrAnnHelper = new AnnotationHelper(); applyAttrAnnot(attrAnnHelper, (ComponentInfo) parent, attnm, attvaltrim, false, location(attr)); } else { attrs.put(attnm, attval);
applyAttrAnnot(attrAnnHelper, compInfo, attnm, attval.trim(), false, location(attr)); } else { final String attPref = attrns != null ? attrns.getPrefix() : null; applyAttrAnnot(attrAnnHelper, compInfo, attnm, attvaltrim, true, location(attr)); while (root != null && !(root instanceof Element)) { if (root instanceof Text) { if (!isEmpty(((Text) root).getText().trim())) break; while (item != null) { if ("template".equals(item.getLocalName())) throw new UiException(message(rn + " does not support between templates", (Item) root)); item = item.getNextSibling();
/** Process the init directive. */ private void parseInitDirective(PageDefinition pgdef, ProcessingInstruction pi, Map<String, String> params) throws Exception { final String clsnm = params.remove("class"); final String zsrc = params.remove("zscript"); final Map<String, String> args = new LinkedHashMap<String, String>(params); if (clsnm == null) { if (zsrc == null) throw new UiException(message("Either the class or zscript attribute must be specified", pi)); checkZScriptEnabled(pi.getLocator()); ZScript zs = null; final String zslang = pgdef.getZScriptLanguage(); if (!zsrc.contains("${")) { final URL url = getLocator().getResource(zsrc); if (url != null) zs = new ZScript(zslang, url); //Bug 2929887: defer the error message since it might not be required } if (zs == null) zs = new ZScript(pgdef.getEvaluatorRef(), zslang, zsrc, getLocator()); pgdef.addInitiatorInfo(new InitiatorInfo(new ZScriptInitiator(zs), args)); } else { if (zsrc != null) throw new UiException(message("You cannot specify both class and zscript", pi)); pgdef.addInitiatorInfo(new InitiatorInfo(clsnm, args)); } }
bZkAttr = true; else if (isDefaultNS(langdef, pref, uri) && !compdef.isNative()) bZkAttr = !langdef.isDynamicReservedAttributes("[event]"); else checkZScriptEnabled(xl); final int lno = xl != null ? xl.getLineNumber() : 0; final ZScript zscript = ZScript.parseContent(value, lno); if (LanguageDefinition.CLIENT_NAMESPACE.equals(uri) || "client".equals(uri)) { if (name.length() == 0) throw new UiException(message("Client attribute name required", xl)); if ("use".equals(name)) { if (cond != null) throw new UnsupportedOperationException(message("if and unless not allowed for w:use", xl)); compInfo.setWidgetClass(value); } else {
private String toAbsoluteURI(String uri) { if (uri != null && uri.length() > 0) { final char cc = uri.charAt(0); if (cc != '/' && cc != '~' && !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; }
/** Returns the page definition of the specified raw content in DOM; * never null. * * @param locator the locator used to locate taglib and other resources. * If null, wapp is assumed ({@link WebApp} is also assumed). * @param extension the default extension if the content doesn't specify * an language. In other words, if * the content doesn't specify an language, {@link LanguageDefinition#getByExtension} * is called. * If extension is null and the content doesn't specify a language, * the language called "xul/html" is assumed. * @exception UiException if failed to parse */ public static final PageDefinition getPageDefinitionDirectly(WebApp wapp, Locator locator, Document doc, String extension) { try { return new Parser(wapp, locator).parse(doc, extension); } catch (Exception ex) { throw UiException.Aide.wrap(ex); } }
/** Returns the page definition of the raw content from the specified * reader; never null. * * @param locator the locator used to locate taglib and other resources. * If null, wapp is assumed ({@link WebApp} is also assumed). * @param extension the default extension if the content doesn't specify * an language. In other words, if * the content doesn't specify an language, {@link LanguageDefinition#getByExtension} * is called. * If extension is null and the content doesn't specify a language, * the language called "xul/html" is assumed. * @exception UiException if failed to parse */ public static final PageDefinition getPageDefinitionDirectly(WebApp wapp, Locator locator, Reader reader, String extension) throws IOException { try { return new Parser(wapp, locator).parse(reader, extension); } catch (IOException ex) { throw ex; } catch (Exception ex) { throw UiException.Aide.wrap(ex); } }