Package org.zkoss.idom

Examples of org.zkoss.idom.Element


        throw DspException.Aide.wrap(ex);
     
    }
    private void loadTaglib0(String prefix, URL url)
    throws Exception {
      final Element root = new SAXBuilder(true, false, true)
        .build(url).getRootElement();
      _mapper.load(prefix, root);

      final Map acts = new HashMap();
      for (Iterator it = root.getElements("tag").iterator();
      it.hasNext();) {
        final Element e = (Element)it.next();
        final String name = IDOMs.getRequiredElementValue(e, "name");
        final String clsName = IDOMs.getRequiredElementValue(e, "tag-class");
        final Class cls = Classes.forNameByThread(clsName);
        if (!Action.class.isAssignableFrom(cls))
          throw new DspException(cls+" doesn't implement "+Action.class);
View Full Code Here


    //4. Processing the rest of processing instructions at the top level
    for (Iterator it = pis.iterator(); it.hasNext();)
      parse(pgdef, (ProcessingInstruction)it.next());

    //5. Processing from the root element
    final Element root = doc.getRootElement();
    if (root != null)
      parse(pgdef, pgdef, root, new AnnotationHelper(), false);
    return pgdef;
  }
View Full Code Here

    return out != null ? out.toByteArray(): data;
  }

  /*package*/ Object parse(InputStream is, String path)
  throws Exception {
    final Element root = new SAXBuilder(true, false, true).build(is).getRootElement();
    final String name = IDOMs.getRequiredAttributeValue(root, "name");
    if (name.length() == 0)
      throw new UiException("The name attribute must be specified, "+root.getLocator()+", "+path);
    final boolean zk = "zk".equals(name),
      aaas = "zk.aaas".equals(name);
    final String lang = root.getAttributeValue("language");
    final LanguageDefinition langdef = //optional
      lang != null ? LanguageDefinition.lookup(lang): null;
    final String dir = path.substring(0, path.lastIndexOf('/') + 1);
    final boolean cacheable = !"false".equals(root.getAttributeValue("cacheable"));

    final WpdContent wc =
      zk || aaas || !cacheable || isWpdContentRequired(root) ?
        new WpdContent(dir, zk, cacheable): null;

    final Provider provider = getProvider();
    final ByteArrayOutputStream out = new ByteArrayOutputStream(1024*16);
    String depends = null;
    if (zk) {
      write(out, "if(!window.zk){\n");
          //may be loaded multiple times because specified in lang.xml
    } else if (!aaas) {
      depends = root.getAttributeValue("depends");
      if (depends != null && depends.length() == 0)
        depends = null;
      if (depends != null) {
        write(out, "zk.load('");
        write(out, depends);
        write(out, "',");
      } else
        write(out, '(');
      write(out, "function(){if(zk._p=zkpi('");
      write(out, name);
      write(out, '\'');
      if (provider != null && provider.getResource(dir + "wv/zk.wpd") != null)
        write(out, ",true");
      write(out, "))try{\n");
    }

    final Map moldInfos = new HashMap();
    for (Iterator it = root.getElements().iterator(); it.hasNext();) {
      final Element el = (Element)it.next();
      final String elnm = el.getName();
      if ("widget".equals(elnm)) {
        final String wgtnm = IDOMs.getRequiredAttributeValue(el, "name");
        final String jspath = wgtnm + ".js"; //eg: /js/zul/wgt/Div.js
        if (!writeResource(out, jspath, dir, false)) {
          log.error("Widget "+wgtnm+": "+jspath+" not found, "+el.getLocator()+", "+path);
          continue;
        }

        final String wgtflnm = name + "." + wgtnm;
        write(out, "zkreg('");
        write(out, wgtflnm);
        write(out, '\'');
        WidgetDefinition wgtdef = langdef != null ? langdef.getWidgetDefinitionIfAny(wgtflnm): null;
        if (wgtdef != null && wgtdef.isBlankPreserved())
          write(out, ",true");
        write(out, ");");
        if (wgtdef == null)
          continue;

        try {
          boolean first = true;
          for (Iterator e = wgtdef.getMoldNames().iterator(); e.hasNext();) {
            final String mold = (String)e.next();
            final String uri = wgtdef.getMoldURI(mold);
            if (uri == null) continue;

            if (first) {
              first = false;
              write(out, "zk._m={};\n");
            }
             
            write(out, "zk._m['");
            write(out, mold);
            write(out, "']=");

            String[] info = (String[])moldInfos.get(uri);
            if (info != null) { //reuse
              write(out, "[zk._p.p.");
              write(out, info[0]);
              write(out, ",'");
              write(out, info[1]);
              write(out, "'];");
            } else {
              moldInfos.put(uri, new String[] {wgtnm, mold});
              if (!writeResource(out, uri, dir, true)) {
                write(out, "zk.$void;zk.error('");
                write(out, uri);
                write(out, " not found')");
                log.error("Failed to load mold "+mold+" for widget "+wgtflnm+": "+uri+" not found");
              }
              write(out, ';');
            }
          }
          if (!first) {
            write(out, "zkmld(");
            write(out, zk ? "zk.": "zk._p.p.");
            write(out, wgtnm);
            write(out, ",zk._m);");
          }
        } catch (Throwable ex) {
          log.error("Failed to load molds for widget "+wgtflnm+".\nCause: "+Exceptions.getMessage(ex));
        }
      } else if ("script".equals(elnm)) {
        String browser = el.getAttributeValue("browser");
        String jspath = el.getAttributeValue("src");
        if (jspath != null && jspath.length() > 0) {
          if (wc != null
          && (browser != null || jspath.indexOf('*') >= 0)) {
            move(wc, out);
            wc.add(jspath, browser);
          } else {
            if (browser != null && provider != null
            && !Servlets.isBrowser(provider.request, browser))
              continue;
            if (!writeResource(out, jspath, dir, true))
              log.error(jspath+" not found, "+el.getLocator()+", "+path);
          }
        }

        String s = el.getText(true);
        if (s != null && s.length() > 0) {
          write(out, s);
          write(out, '\n'); //might terminate with //
        }
      } else if ("function".equals(elnm)) {
        final MethodInfo mtd = getMethodInfo(el);
        if (mtd != null)
          if (wc != null) {
            move(wc, out);
            wc.add(mtd);
          } else {
            write(out, mtd);
          }
      } else {
        log.warning("Unknown element "+elnm+", "+el.getLocator()+", "+path);
      }
    }
    if (zk) {
      final WebApp wapp = getWebApp();
      if (wapp != null)
View Full Code Here

    }
    return new ByteContent(out.toByteArray(), cacheable);
  }
  private static boolean isWpdContentRequired(Element root) {
    for (Iterator it = root.getElements("script").iterator(); it.hasNext();) {
      final Element el = (Element)it.next();
      if (el.getAttributeValue("browser") != null)
        return true;
    }
    return false;
  }
View Full Code Here

    response.getOutputStream().write(data);
    response.flushBuffer();
  }
  /*package*/ Object parse(InputStream is, String path)
  throws Exception {
    final Element root = new SAXBuilder(true, false, true).build(is).getRootElement();
    final String lang = IDOMs.getRequiredAttributeValue(root, "language");
    if (lang.length() == 0)
      throw new UiException("The language attribute must be specified, "+root.getLocator()+", "+path);

    final List items = new LinkedList();
    for (Iterator it = root.getElements().iterator(); it.hasNext();) {
      final Element el = (Element)it.next();
      final String elnm = el.getName();
      if ("stylesheet".equals(elnm)) {
        final String href = IDOMs.getRequiredAttributeValue(el, "href");
        if (href.length() != 0)
          items.add(href);
        else
          log.warning("Ingored stylesheet: href required, " + el.getLocator()+", "+path);
      } else if ("function".equals(elnm)) {
        final MethodInfo mtd = getMethodInfo(el);
        if (mtd != null) items.add(mtd);
      } else
        log.warning("Ignored unknown element, " + el.getLocator()+", "+path);
    }
    return new WcsInfo(lang, items);
  }
View Full Code Here

   */
  private static
  void parseLabelTemplate(LanguageDefinition langdef, Element el) {
    el = el.getElement("label-template");
    if (el != null) {
      final Element raw = el.getElement("raw");
      langdef.setLabelTemplate(
        IDOMs.getRequiredElementValue(el, "component-name"),
        IDOMs.getRequiredElementValue(el, "component-attribute"),
        raw != null && !"false".equals(raw.getText(true)));
    }
  }
View Full Code Here

    //if (log.finerable()) log.finer(el);
  }
  private static List parseExtensions(Element elm) {
    final List exts = new LinkedList();
    for (Iterator it = elm.getElements("extension").iterator(); it.hasNext();) {
      final Element el = (Element)it.next();
      final String ext = el.getText(true);
      if (ext.length() != 0) {
        for (int j = 0, len = ext.length(); j < len; ++j) {
          final char cc = ext.charAt(j);
          if ((cc < 'a' || cc > 'z') && (cc < 'A' || cc > 'Z')
          && (cc < '0' || cc > '9'))
View Full Code Here

    return IDOMs.parseParams(elm, "attribute", "attribute-name", "attribute-value");
  }

  private static void parseAnnots(ComponentDefinitionImpl compdef, Element top) {
    for (Iterator it = top.getElements("annotation").iterator(); it.hasNext();) {
      final Element el = (Element)it.next();
      final String annotName = IDOMs.getRequiredElementValue(el, "annotation-name");
      final Map annotAttrs = parseAttrs(el);
      final String prop = el.getElementValue("property-name", true);
      if (prop == null || prop.length() == 0)
        compdef.addAnnotation(annotName, annotAttrs);
      else
        compdef.addAnnotation(prop, annotName, annotAttrs);
    }
View Full Code Here

  }

  private static
  void parseLang(Document doc, Locator locator, URL url, boolean addon)
  throws Exception {
    final Element root = doc.getRootElement();
    final String lang = IDOMs.getRequiredElementValue(root, "language-name");
    final LanguageDefinition langdef;
    final Device device;
    if (addon) {
      if (log.debugable()) log.debug("Addon language to "+lang+" from "+root.getElementValue("addon-name", true));
      langdef = LanguageDefinition.lookup(lang);
      device = Devices.getDevice(langdef.getDeviceType());

      if (root.getElement("case-insensitive") != null)
        throw new UiException("case-insensitive not allowed in addon");
    } else {
      final String ns =
        (String)IDOMs.getRequiredElementValue(root, "namespace");
      final String deviceType =
        (String)IDOMs.getRequiredElementValue(root, "device-type");

      //if (log.debugable()) log.debug("Load language: "+lang+", "+ns);

      PageRenderer pageRenderer = (PageRenderer)
        locateClass(IDOMs.getRequiredElementValue(root, "renderer-class"))
        .newInstance();

      final List exts = parseExtensions(root);
      if (exts.isEmpty())
        throw new UiException("The extension must be specified for "+lang);

      String ignoreCase = root.getElementValue("case-insensitive", true);
      String bNative = root.getElementValue("native-namespace", true);

      langdef = new LanguageDefinition(
        deviceType, lang, ns, exts, pageRenderer,
        "true".equals(ignoreCase), "true".equals(bNative), locator);
      device = Devices.getDevice(deviceType);
    }

    parsePI(langdef, doc);
    parseLabelTemplate(langdef, root);
    parseDynamicTag(langdef, root);
    parseMacroTemplate(langdef, root);
    parseNativeTemplate(langdef, root);

    for (Iterator it = root.getElements("library-property").iterator();
    it.hasNext();) {
      final Element el = (Element)it.next();
      final String nm = IDOMs.getRequiredElementValue(el, "name");
      final String val = IDOMs.getRequiredElementValue(el, "value");
      Library.setProperty(nm, val);
    }
    for (Iterator it = root.getElements("system-property").iterator();
    it.hasNext();) {
      final Element el = (Element)it.next();
      final String nm = IDOMs.getRequiredElementValue(el, "name");
      final String val = IDOMs.getRequiredElementValue(el, "value");
      System.setProperty(nm, val);
    }

    for (Iterator it = root.getElements("javascript").iterator();
    it.hasNext();) {
      final Element el = (Element)it.next();
      String src = el.getAttributeValue("src"),
        pkg = el.getAttributeValue("package");
      final boolean merge = "true".equals(el.getAttributeValue("merge"));
      final boolean ondemand = "true".equals(el.getAttributeValue("ondemand"));
      if (pkg != null) {
        if (src != null)
          log.warning("The src attribute ignored because package is specified, "+el.getLocator());
        if (!ondemand && !merge) {
          src = "~." + device.packageToPath(pkg);
          pkg = null;
        }
      }

      final String ctn = el.getText(true);
      final JavaScript js;
      if (pkg != null && pkg.length() > 0) {
        if (ondemand) {
          langdef.removeJavaScript("~." + device.packageToPath(pkg));
          langdef.removeMergeJavaScriptPackage(pkg);
        } else {
          langdef.addMergeJavaScriptPackage(pkg);
        }
        continue; //TODO
      } else 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 {
        log.warning("Ignored: none of the src or package attribute, or the content specified, "+el.getLocator());
        continue;
      }
      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) {
        if (ctn != null && ctn.length() > 0)
          throw new UiException("You cannot specify the content if the href attribute is specified, "+el.getLocator());
        ss = new StyleSheet(href, el.getAttributeValue("type"), el.getAttributeValue("media"), false);
      } else if (ctn != null && ctn.length() > 0) {
        ss = new StyleSheet(ctn, el.getAttributeValue("type"), el.getAttributeValue("media"), true);
      } else {
        throw new UiException("You must specify either the href attribute or the content, "+el.getLocator());
      }
      langdef.addStyleSheet(ss);
    }

    for (Iterator it = root.getElements("zscript").iterator();
    it.hasNext();) {
      final Element el = (Element)it.next();
      final String zslang;
      final Attribute attr = el.getAttributeItem("language");
      if (attr == null) {
        zslang = "Java";
      } else {
        zslang = attr.getValue();
        if (zslang == null || zslang.length() == 0)
          throw new UiException("The language attribute cannot be empty, "+attr.getLocator());
      }
      final String s = el.getText(true);
      final String eachTime = el.getAttributeValue("each-time");
      if ("true".equals(eachTime))
        langdef.addEachTimeScript(zslang, s);
      else
        langdef.addInitScript(zslang, s);
    }

    for (Iterator it = root.getElements("component").iterator();
    it.hasNext();) {
      final Element el = (Element)it.next();
      final String name =
        IDOMs.getRequiredElementValue(el, "component-name");

      String clsnm = el.getElementValue("component-class", true);
      Class cls = null;
      if (clsnm != null) {
        if (clsnm.length() > 0) {
          noEL("component-class", clsnm, el);
          try {
            cls = locateClass(clsnm);
          } catch (Throwable ex) { //Feature 1873426
            log.warning("Component "+name+" ignored. Reason: unable to load "+clsnm+" due to "
              +ex.getClass().getName()+": "+ex.getMessage()
              +(ex instanceof NoClassDefFoundError?"":"\n"+el.getLocator()));
            log.debug(ex);
            //keep processing (Feature 2060367)
          }
        } else {
          clsnm = null;
        }
      }

      final String macroURI = el.getElementValue("macro-uri", true);
      final ComponentDefinitionImpl compdef;
      if (macroURI != null && macroURI.length() != 0) {
        if (log.finerable()) log.finer("macro component definition: "+name);

        final String inline = el.getElementValue("inline", true);
        compdef = (ComponentDefinitionImpl)
          langdef.getMacroDefinition(
            name, macroURI, "true".equals(inline), null);

        if (cls != null)
          compdef.setImplementationClass(cls);
        else if (clsnm != null)
          compdef.setImplementationClass(clsnm);

        compdef.setDeclarationURL(url);
        langdef.addComponentDefinition(compdef);
      } else if (el.getElement("extends") != null) { //extends
        final String extnm = el.getElementValue("extends", true);
        if (log.finerable()) log.finer("Extends component definition, "+name+", from "+extnm);
        final ComponentDefinition ref = langdef.getComponentDefinitionIfAny(extnm);
        if (ref == null) {
          log.warning("Component "+name+" ignored. Reason: extends a non-existent component "+extnm+".\n"+el.getLocator());
            //not throw exception since the derived component might be
            //ignored due to class-not-found
          continue;
        }

        if (ref.isMacro())
          throw new UiException("Unable to extend from a macro component, "+el.getLocator());

        if (extnm.equals(name)) {
          compdef = (ComponentDefinitionImpl)ref;
        } else {
          compdef = (ComponentDefinitionImpl)
            ref.clone(ref.getLanguageDefinition(), name);
          compdef.setDeclarationURL(url);
        }

        if (cls != null)
          compdef.setImplementationClass(cls);
        else if (clsnm != null)
          compdef.setImplementationClass(clsnm);

        langdef.addComponentDefinition(compdef);
          //Note: setImplementationClass before addComponentDefinition
      } else {
        if (log.finerable()) log.finer("Add component definition: name="+name);

        if (cls == null && clsnm == null)
          throw new UiException("component-class is required, "+el.getLocator());
        compdef = cls != null ?
          new ComponentDefinitionImpl(langdef, null, name, cls):
          new ComponentDefinitionImpl(langdef, null, name, clsnm);
        compdef.setDeclarationURL(url);
        langdef.addComponentDefinition(compdef);
      }

      String s = el.getElementValue("text-as", true);
      if (s != null) { //empty means cleanup (for overriding)
        noEL("text-as", s, el);
        compdef.setTextAs(s);
      }

      s = el.getElementValue("preserve-blank", true);
      if (s != null && !"false".equals(s))
        compdef.setBlankPreserved(true);

      String wgtnm = el.getElementValue("widget-class", true);
      WidgetDefinition wgtdef = null;
      if (wgtnm != null) {
        if (!withEL(wgtnm))
          wgtdef = getWidgetDefinition(langdef, compdef, wgtnm);
        compdef.setDefaultWidgetClass(wgtnm);
      }

      s = el.getElementValue("component-apply", true);
      if (s == null) s = el.getElementValue("apply", true); //backward-compatible
      compdef.setApply(s);

      for (Iterator i = el.getElements("mold").iterator(); i.hasNext();) {
        final Element e = (Element)i.next();
        final String nm = IDOMs.getRequiredElementValue(e, "mold-name");
        final String moldURI = e.getElementValue("mold-uri", true);
        String cssURI = e.getElementValue("css-uri", true);
        final String wn = e.getElementValue("widget-class", true);
        noEL("mold-uri", moldURI, e); //5.0 limitation
        noEL("css-uri", cssURI, e);

        compdef.addMold(nm, wn != null ? wn: wgtnm);

        WidgetDefinition wd = wn == null ? wgtdef:
          withEL(wn) ? null: getWidgetDefinition(langdef, compdef, wn);
        if (moldURI != null) {
          if (wd != null)
            wd.addMold(nm, moldURI);
          else
            log.error("Mold "+nm+" for "+name+" ignored because "+
              ((wn != null && withEL(wn)) || (wgtnm != null && withEL(wgtnm)) ?
                "widget-class contains EL expressions":"widget-class is required")
              +", "+e.getLocator());
        }

        if (cssURI != null && cssURI.length() > 0) {
          final char cc = cssURI.charAt(0);
          if (cc != '/' && cc != '~') {
            String n = wn != null ? wn: wgtnm;
            if (!withEL(n)) {
              int k = n.lastIndexOf('.');
              cssURI = "~." + device.toAbsolutePath(
                n.substring(0, k).replace('.', '/') + '/' + cssURI);
            } else {
              log.error("Absolute path required for cssURI, since the widget class contains EL expressions, "+e.getLocator());
            }
          }
          langdef.addCSSURI(cssURI);
        }
      }

      for (Iterator e = parseCustAttrs(el).entrySet().iterator(); e.hasNext();) {
        final Map.Entry me = (Map.Entry)e.next();
        compdef.addCustomAttribute((String)me.getKey(), (String)me.getValue());
      }

      for (Iterator e = parseProps(el).entrySet().iterator(); e.hasNext();) {
        final Map.Entry me = (Map.Entry)e.next();
        compdef.addProperty((String)me.getKey(), (String)me.getValue());
      }

      parseAnnots(compdef, el);
    }
View Full Code Here

    final Map clses = new HashMap();

    Exception excp = null;
    for (Iterator it = root.getElements("function").iterator();
    it.hasNext();) {
      final Element e = (Element)it.next();

      final String name = IDOMs.getRequiredElementValue(e, "name");
      final String clsnm = IDOMs.getRequiredElementValue(e, "function-class");
      final String sig = IDOMs.getRequiredElementValue(e, "function-signature");
      final Class cls;
      try {
        cls = Classes.forNameByThread(clsnm);
      } catch (ClassNotFoundException ex) {
        log.error("Class not found: "+clsnm+", "+e.getLocator(), ex);
        excp = ex;
        continue; //to report as many errors as possible
      }

      try {
        final Method mtd = Classes.getMethodBySignature(cls, sig, null);
        if ((mtd.getModifiers() & Modifier.STATIC) != 0)
          mtds.put(name, new MethodFunction(mtd));
        else
          log.error("Not a static method: "+mtd);
      } catch (ClassNotFoundException ex) {
        log.error("Relavant class not found when loading "+clsnm+", "+e.getLocator(), ex);
        excp = ex;
        continue;
      } catch (NoSuchMethodException ex) {
        log.error("Method not found in "+clsnm+": "+sig+" "+e.getLocator(), ex);
        excp = ex;
        continue;
      } catch (IllegalSyntaxException ex) {
        log.error("Illegal Signature: "+sig+" "+e.getLocator(), ex);
        excp = ex;
        continue;
      }
    }

    for (Iterator it = root.getElements("import").iterator();
    it.hasNext();) {
      final Element e = (Element)it.next();
      final String name = IDOMs.getRequiredElementValue(e, "import-name");
      final String clsnm = IDOMs.getRequiredElementValue(e, "import-class");
      try {
        clses.put(name, Classes.forNameByThread(clsnm));
      } catch (ClassNotFoundException ex) {
        log.error("Class not found: "+clsnm+", "+e.getLocator(), ex);
        excp = ex;
      }
    }

    if (excp != null)
View Full Code Here

TOP

Related Classes of org.zkoss.idom.Element

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.