Esempio n. 1
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();
    }
  }
Esempio n. 2
0
  // -- private utilities --//
  private final Map loadLabels(Locale locale) {
    WaitLock lock = null;
    for (; ; ) {
      final Object o;
      synchronized (_syncLabels) {
        o = _syncLabels.get(locale);
        if (o == null) _syncLabels.put(locale, lock = new WaitLock()); // lock it
      }

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

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

    if (_jarcharset == null)
      _jarcharset = Library.getProperty("org.zkoss.util.label.classpath.charset", "UTF-8");
    if (_warcharset == null) {
      _warcharset = Library.getProperty("org.zkoss.util.label.web.charset", null);
      if (_warcharset == null)
        _warcharset =
            Library.getProperty(
                "org.zkoss.util.label.WEB-INF.charset", "UTF-8"); // backward compatible
    }

    try {
      // get the class name
      if (locale != null) log.info("Loading labels for " + locale);
      Map labels = new HashMap(512);

      // 1. load from modules
      final ClassLocator locator = new ClassLocator();
      for (Enumeration en =
              locator.getResources(
                  locale == null
                      ? "metainfo/i3-label.properties"
                      : "metainfo/i3-label_" + locale + ".properties");
          en.hasMoreElements(); ) {
        final URL url = (URL) en.nextElement();
        load(labels, url, _jarcharset);
      }

      // 2. load from extra resource
      final List locators;
      synchronized (_locators) {
        locators = new LinkedList(_locators);
      }
      for (Iterator it = locators.iterator(); it.hasNext(); ) {
        Object o = it.next();
        if (o instanceof LabelLocator) {
          final URL url = ((LabelLocator) o).locate(locale);
          if (url != null) load(labels, url, _warcharset);
        } else {
          final LabelLocator2 loc = (LabelLocator2) o;
          final InputStream is = loc.locate(locale);
          if (is != null) {
            final String cs = loc.getCharset();
            load(labels, is, cs != null ? cs : _warcharset);
          }
        }
      }

      // Convert values to ExValue
      toExValue(labels);

      // merge with labels from 'super' locale
      if (locale != null) {
        final String lang = locale.getLanguage();
        final String cnty = locale.getCountry();
        final String var = locale.getVariant();
        final Map superlabels =
            loadLabels(
                var != null && var.length() > 0
                    ? new Locale(lang, cnty)
                    : cnty != null && cnty.length() > 0 ? new Locale(lang, "") : null);
        if (labels.isEmpty()) {
          labels = superlabels.isEmpty() ? Collections.EMPTY_MAP : superlabels;
        } else if (!superlabels.isEmpty()) {
          Map combined = new HashMap(superlabels);
          combined.putAll(labels);
          labels = combined;
        }
      }

      // add to map
      synchronized (_syncLabels) {
        _syncLabels.put(locale, labels);
        cloneLables();
      }
      return labels;
    } catch (Throwable ex) {
      synchronized (_syncLabels) {
        _syncLabels.remove(locale);
        cloneLables();
      }
      throw SystemException.Aide.wrap(ex);
    } finally {
      lock.unlock(); // unlock (always unlock to avoid deadlock)
    }
  }