Exemple #1
0
  /** Ssets the locale context-wide based on a call to {@link JiveGlobals#getLocale()}. */
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
      throws IOException, ServletException {
    final String pathInfo = ((HttpServletRequest) request).getPathInfo();

    if (pathInfo == null) {
      // Note, putting the locale in the application at this point is a little overkill
      // (ie, every user who hits this filter will do this). Eventually, it might make
      // sense to just set the locale in the user's session and if that's done we might
      // want to honor a preference to get the user's locale based on request headers.
      // For now, this is just a convenient place to set the locale globally.
      Config.set(context, Config.FMT_LOCALE, JiveGlobals.getLocale());
    } else {
      try {
        String[] parts = pathInfo.split("/");
        String pluginName = parts[1];
        ResourceBundle bundle = LocaleUtils.getPluginResourceBundle(pluginName);
        LocalizationContext ctx = new LocalizationContext(bundle, JiveGlobals.getLocale());
        Config.set(request, Config.FMT_LOCALIZATION_CONTEXT, ctx);
      } catch (Exception e) {
        // Note, putting the locale in the application at this point is a little overkill
        // (ie, every user who hits this filter will do this). Eventually, it might make
        // sense to just set the locale in the user's session and if that's done we might
        // want to honor a preference to get the user's locale based on request headers.
        // For now, this is just a convenient place to set the locale globally.
        Config.set(context, Config.FMT_LOCALE, JiveGlobals.getLocale());
      }
    }
    // Move along:
    chain.doFilter(request, response);
  }
Exemple #2
0
  /**
   * Reformats a string where lines that are longer than <tt>width</tt> are split apart at the
   * earliest wordbreak or at maxLength, whichever is sooner. If the width specified is less than 5
   * or greater than the input Strings length the string will be returned as is.
   *
   * <p>Please note that this method can be lossy - trailing spaces on wrapped lines may be trimmed.
   *
   * @param input the String to reformat.
   * @param width the maximum length of any one line.
   * @return a new String with reformatted as needed.
   */
  public static String wordWrap(String input, int width, Locale locale) {
    // protect ourselves
    if (input == null) {
      return "";
    } else if (width < 5) {
      return input;
    } else if (width >= input.length()) {
      return input;
    }

    // default locale
    if (locale == null) {
      locale = JiveGlobals.getLocale();
    }

    StringBuilder buf = new StringBuilder(input);
    boolean endOfLine = false;
    int lineStart = 0;

    for (int i = 0; i < buf.length(); i++) {
      if (buf.charAt(i) == '\n') {
        lineStart = i + 1;
        endOfLine = true;
      }

      // handle splitting at width character
      if (i > lineStart + width - 1) {
        if (!endOfLine) {
          int limit = i - lineStart - 1;
          BreakIterator breaks = BreakIterator.getLineInstance(locale);
          breaks.setText(buf.substring(lineStart, i));
          int end = breaks.last();

          // if the last character in the search string isn't a space,
          // we can't split on it (looks bad). Search for a previous
          // break character
          if (end == limit + 1) {
            if (!Character.isWhitespace(buf.charAt(lineStart + end))) {
              end = breaks.preceding(end - 1);
            }
          }

          // if the last character is a space, replace it with a \n
          if (end != BreakIterator.DONE && end == limit + 1) {
            buf.replace(lineStart + end, lineStart + end + 1, "\n");
            lineStart = lineStart + end;
          }
          // otherwise, just insert a \n
          else if (end != BreakIterator.DONE && end != 0) {
            buf.insert(lineStart + end, '\n');
            lineStart = lineStart + end + 1;
          } else {
            buf.insert(i, '\n');
            lineStart = i + 1;
          }
        } else {
          buf.insert(i, '\n');
          lineStart = i + 1;
          endOfLine = false;
        }
      }
    }

    return buf.toString();
  }