Exemple #1
0
 /**
  * Setup this processor before processing the event by calling {@link #process}.
  *
  * <p>Note: it doesn't invoke {@link ExecutionCtrl#onActivate}
  */
 public void setup() {
   SessionsCtrl.setCurrent(_desktop.getSession());
   final Execution exec = _desktop.getExecution();
   ExecutionsCtrl.setCurrent(exec);
   ((ExecutionCtrl) exec).setCurrentPage(getPage());
 }
Exemple #2
0
 /**
  * Cleanup this process after processing the event by calling {@link #process}.
  *
  * <p>Note: Don't call this method if the event process executes in the same thread.
  */
 public void cleanup() {
   ExecutionsCtrl.setCurrent(null);
   SessionsCtrl.setCurrent((Session) null);
 }
  public void redraw(Writer out) throws java.io.IOException {
    // Note: _tag == null can NOT be handled specially
    final Execution exec = Executions.getCurrent();
    final boolean root =
        getParent() == null
            && (getPage().isComplete()
                || (exec != null && "complete".equals(ExecutionsCtrl.getPageRedrawControl(exec))));
    if (exec == null
        || exec.isAsyncUpdate(null)
        || (!root && !HtmlPageRenders.isDirectContent(exec))) {
      super.redraw(out); // renderProperties (assume in zscript)
      return;
    }

    Writer oldout = null;
    if (exec != null
        && !HtmlPageRenders.isZkTagsGenerated(exec)
        && exec.getAttribute(ATTR_TOP_NATIVE) == null) { // need to check topmost native only
      String tn;
      if (root
          || "html".equals(tn = _tag != null ? _tag.toLowerCase() : "")
          || "body".equals(tn)
          || "head".equals(tn)) {
        exec.setAttribute(ATTR_TOP_NATIVE, Boolean.TRUE);
        oldout = out;
        out = new StringWriter();
      }
    }

    out.write(getPrologHalf());

    // children
    Component child = getFirstChild();
    if (child == null) {
      // need to invoke outStandalone to generate response if any (Bug 3009925)
      // however, it is not required if not root (since others will invoke)
      if (root) HtmlPageRenders.outStandalone(exec, null, out);
    } else {
      if (root) HtmlPageRenders.setDirectContent(exec, true);
      do {
        Component next = child.getNextSibling();
        if (child instanceof Native
            || ((ComponentCtrl) child).getExtraCtrl() instanceof DirectContent) {
          ((ComponentCtrl) child).redraw(out);
        } else {
          HtmlPageRenders.setDirectContent(exec, false);
          HtmlPageRenders.outStandalone(exec, child, out);
          HtmlPageRenders.setDirectContent(exec, true);
        }
        child = next;
      } while (child != null);
    }

    out.write(getEpilogHalf());

    if (oldout != null) {
      exec.removeAttribute(ATTR_TOP_NATIVE);

      // order: <html><head><zkhead><body>
      // 1. replace <zkhead/> if found
      // 2. insert before </head> if found
      // 3. insert after <body> if found
      // 4. insert after <html> if found
      // 5. insert at the end if none of above found
      final StringBuffer sb = ((StringWriter) out).getBuffer();
      if (!HtmlPageRenders.isZkTagsGenerated(exec)) {
        int jhead = -1, // anchor of header
            junav = -1, // anchor of unavailable
            head = -1, // index of <head>
            heade = -1, // index of </head>
            html = -1; // index of <html>
        for (int j = 0, len = sb.length(); (j = sb.indexOf("<", j)) >= 0; ) {
          ++j;
          if (jhead < 0 && startsWith(sb, "zkhead", j)) {
            int l = Strings.indexOf(sb, '>', j) + 1;
            sb.delete(jhead = --j, l); // jhead found
            len = sb.length();
          } else if (head < 0 && startsWith(sb, "head", j)) {
            head = Strings.indexOf(sb, '>', j) + 1;
          } else if (html < 0 && startsWith(sb, "html", j)) {
            html = Strings.indexOf(sb, '>', j) + 1;
          } else if (junav < 0 && startsWith(sb, "body", j)) {
            junav = Strings.indexOf(sb, '>', j) + 1; // junav found
            break; // done
          } else if (sb.charAt(j) == '/' && startsWith(sb, "head", ++j)) {
            heade = j - 2;
          }
        }

        boolean disableUnavailable = false;
        if (jhead < 0
            && ((jhead = heade) < 0) // use </head> if no <zkhead>
            && ((jhead = head) < 0) // use <head> if no </head> (though unlikely)
            && ((jhead = junav) < 0) // use <body> if no <head>
            && ((jhead = html) < 0)) { // use <html> if no <body>
          if (_tag != null) {
            final String tn = _tag.toLowerCase();
            if ("div".equals(tn) || "span".equals(tn)) {
              l_loop:
              for (int j = 0, len = sb.length(); j < len; ++j)
                switch (sb.charAt(j)) {
                  case '>':
                    disableUnavailable = true; // make output cleaner
                    jhead = j + 1; // found
                  case '=': // it might have something depends on JS
                  case '"':
                    break l_loop;
                }
            }
          }
          if (jhead < 0) jhead = 0; // insert at head if not found
        }

        final String msg = HtmlPageRenders.outUnavailable(exec);
        // called if disableUnavailable (so it won't be generated later)
        if (msg != null && !disableUnavailable) {
          if (junav < 0) {
            if (html >= 0) junav = sb.lastIndexOf("</html");
          }
          if (junav >= 0) sb.insert(junav < jhead ? jhead : junav, msg);
          else sb.append(msg);
        }

        final String zktags = HtmlPageRenders.outHeaderZkTags(exec, getPage());
        if (zktags != null) sb.insert(jhead, zktags);
      }

      oldout.write(sb.toString());
    }
  }