Ejemplo n.º 1
0
  /**
   * Outputs the HTML tags of the given component to the given writer.
   *
   * @param path the request path. If null, the servlet path is assumed.
   * @param out the output (never null).
   * @param richlet the richlet to run. If you have only one component to show and no need process
   *     it under an execution, you could use {@link #render(javax.servlet.ServletContext,
   *     javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse,
   *     org.zkoss.zk.ui.Component, String, java.io.Writer)} instead.
   * @since 5.0.5
   */
  public static final void render(
      ServletContext ctx,
      HttpServletRequest request,
      HttpServletResponse response,
      Richlet richlet,
      String path,
      Writer out)
      throws ServletException, IOException {
    if (path == null) path = Https.getThisServletPath(request);

    WebManager webman = WebManager.getWebManagerIfAny(ctx);
    if (webman == null) {
      final String ATTR = "org.zkoss.zkplus.embed.updateURI";
      String updateURI = Library.getProperty(ATTR);
      if (updateURI == null) updateURI = "/zkau";
      else updateURI = Utils.checkUpdateURI(updateURI, ATTR);
      webman = new WebManager(ctx, updateURI);
    }

    final Session sess = WebManager.getSession(ctx, request);
    final WebApp wapp = sess.getWebApp();
    final WebAppCtrl wappc = (WebAppCtrl) wapp;
    final Object old =
        I18Ns.setup(sess, request, response, wapp.getConfiguration().getResponseCharset());
    Execution exec = null;
    try {
      final Desktop desktop = webman.getDesktop(sess, request, response, path, true);
      if (desktop == null) // forward or redirect
      return;

      final RequestInfo ri =
          new RequestInfoImpl(wapp, sess, desktop, request, PageDefinitions.getLocator(wapp, path));
      sess.setAttribute(Attributes.GAE_FIX, new Integer(0));
      ((SessionCtrl) sess).notifyClientRequest(true);

      final UiFactory uf = wappc.getUiFactory();
      final Page page = WebManager.newPage(uf, ri, richlet, response, path);
      exec = new ExecutionImpl(ctx, request, response, desktop, page);
      exec.setAttribute(Attributes.PAGE_REDRAW_CONTROL, "page");
      exec.setAttribute(Attributes.PAGE_RENDERER, new PageRenderer(exec));

      wappc.getUiEngine().execNewPage(exec, richlet, page, out);
      // no need to set device type here, since UiEngine will do it later
    } finally {
      I18Ns.cleanup(request, old);
      if (exec != null) {
        exec.removeAttribute(Attributes.PAGE_REDRAW_CONTROL);
        exec.removeAttribute(Attributes.PAGE_RENDERER);
      }
    }
  }
Ejemplo n.º 2
0
  private static Map loadSymbols(Locale locale) {
    WaitLock lock = null;
    for (; ; ) {
      final Object o;
      synchronized (_symbols) {
        o = _symbols.get(locale);
        if (o == null) _symbols.put(locale, lock = new WaitLock()); // lock it
      }

      if (o instanceof Map) return (Map) o;
      if (o == null) break; // go to load the symbols

      // wait because some one is creating the servlet
      if (!((WaitLock) o).waitUntilUnlock(5 * 60 * 1000))
        log.warn(
            "Take too long to wait loading localized symbol: "
                + locale
                + "\nTry to load again automatically...");
    } // for(;;)

    try {

      // the following implementation is referred to
      // org.zkoss.zk.ui.http.Wpds#getDateJavaScript()
      final Map<String, Object> map = new HashMap<String, Object>();
      final Calendar cal = Calendar.getInstance(locale);
      int firstDayOfWeek = Utils.getFirstDayOfWeek();
      cal.clear();

      if (firstDayOfWeek < 0) firstDayOfWeek = cal.getFirstDayOfWeek();
      map.put("DOW_1ST", Integer.valueOf(firstDayOfWeek - Calendar.SUNDAY));

      final boolean zhlang = locale.getLanguage().equals("zh");
      SimpleDateFormat df = new SimpleDateFormat("E", locale);
      final String[] sdow = new String[7], s2dow = new String[7];
      for (int j = firstDayOfWeek, k = 0; k < 7; ++k) {
        cal.set(Calendar.DAY_OF_WEEK, j);
        sdow[k] = df.format(cal.getTime());
        if (++j > Calendar.SATURDAY) j = Calendar.SUNDAY;

        if (zhlang) {
          s2dow[k] = sdow[k].length() >= 3 ? sdow[k].substring(2) : sdow[k];
        } else {
          final int len = sdow[k].length();
          final char cc = sdow[k].charAt(len - 1);
          s2dow[k] = cc == '.' || cc == ',' ? sdow[k].substring(0, len - 1) : sdow[k];
        }
      }
      df = new SimpleDateFormat("G", locale);
      map.put("ERA", df.format(new java.util.Date()));

      Calendar ec = Calendar.getInstance(Locale.ENGLISH);
      Calendar lc = Calendar.getInstance(locale);
      map.put("YDELTA", Integer.valueOf(lc.get(Calendar.YEAR) - ec.get(Calendar.YEAR)));

      df = new SimpleDateFormat("EEEE", locale);
      final String[] fdow = new String[7];
      for (int j = firstDayOfWeek, k = 0; k < 7; ++k) {
        cal.set(Calendar.DAY_OF_WEEK, j);
        fdow[k] = df.format(cal.getTime());
        if (++j > Calendar.SATURDAY) j = Calendar.SUNDAY;
      }

      df = new SimpleDateFormat("MMM", locale);
      final String[] smon = new String[12], s2mon = new String[12];
      for (int j = 0; j < 12; ++j) {
        cal.set(Calendar.MONTH, j);
        smon[j] = df.format(cal.getTime());

        if (zhlang) {
          s2mon[j] =
              smon[0].length() >= 2
                  ? // remove the last
                  // char
                  smon[j].substring(0, smon[j].length() - 1)
                  : smon[j];
        } else {
          final int len = smon[j].length();
          final char cc = smon[j].charAt(len - 1);
          s2mon[j] = cc == '.' || cc == ',' ? smon[j].substring(0, len - 1) : smon[j];
        }
      }

      df = new SimpleDateFormat("MMMM", locale);
      final String[] fmon = new String[12];
      for (int j = 0; j < 12; ++j) {
        cal.set(Calendar.MONTH, j);
        fmon[j] = df.format(cal.getTime());
      }

      map.put("SDOW", sdow);
      if (Objects.equals(s2dow, sdow)) map.put("S2DOW", sdow);
      else map.put("S2DOW", s2dow);
      if (Objects.equals(fdow, sdow)) map.put("FDOW", sdow);
      else map.put("FDOW", fdow);

      map.put("SMON", smon);
      if (Objects.equals(s2mon, smon)) map.put("S2MON", smon);
      else map.put("S2MON", s2mon);

      if (Objects.equals(fmon, smon)) map.put("FMON", smon);
      else map.put("FMON", fmon);

      // AM/PM available since ZK 3.0
      df = new SimpleDateFormat("a", locale);
      cal.set(Calendar.HOUR_OF_DAY, 3);
      final String[] ampm = new String[2];
      ampm[0] = df.format(cal.getTime());
      cal.set(Calendar.HOUR_OF_DAY, 15);
      ampm[1] = df.format(cal.getTime());

      map.put("APM", ampm);

      synchronized (_symbols) {
        _symbols.put(locale, map);
        cloneSymbols();
      }

      return map;
    } finally {
      lock.unlock();
    }
  }