Formatting functions for GWT client side (Ext GWT) : Utility « GWT « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Java » GWT » UtilityScreenshots 
Formatting functions for GWT client side (Ext GWT)
  

/*
 * Ext GWT - Ext for GWT
 * Copyright(c) 2007-2009, Ext JS, LLC.
 * licensing@extjs.com
 
 * http://extjs.com/license
 */
//package com.extjs.gxt.ui.client.util;

import java.util.Iterator;
import java.util.Map;

/**
 * Formatting functions.
 */
public class Format {

  public static native String camelize(String s)/*-{
    return (s.match(/\-/gi) ? s.toLowerCase().replace(/\-(\w)/gi, function(a, c){return c.toUpperCase();}) : s);
  }-*/;

  public static String capitalize(String value) {
    return value == null ? value : value.substring(01).toUpperCase() + value.substring(1).toLowerCase();
  }

  /**
   * Truncate a string and add an ellipsis ('...') to the end if it exceeds the
   * specified length.
   
   @param value the string to truncate
   @param len the maximum length to allow before truncating
   @return the converted text
   */
  public static String ellipse(String value, int len) {
    if (value != null && value.length() > len) {
      return value.substring(0, len - 3"...";
    }
    return value;
  }

  public static native String escape(String s)/*-{
    return s.replace(/('|\\)/g, "\\$1");
  }-*/;

  public static String fileSize(int size) {
    if (size < 1024) {
      return size + " bytes";
    else if (size < 1048576) {
      return (Math.round(((size * 101024)) 10" KB";
    else {
      return (Math.round(((size * 101048576)) 10" MB";
    }
  }

  public static String htmlDecode(String value) {
    return value == null ? value
        : value.replace("&amp;""&").replace("&gt;"">").replace("&lt;""<").replace("&quot;""\"");
  }

  public static String htmlEncode(String value) {
    return value == null ? value
        : value.replace("&""&amp;").replace(">""&gt;").replace("<""&lt;").replace("\"""&quot;");
  }

  public static native String hyphenize(String name/*-{
    return name.replace(/([A-Z])/g, "-$1" ).toLowerCase();
  }-*/;

  public static native String stripScripts(String v/*-{
    return !v ? v : String(v).replace(/(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)/ig, "");
  }-*/;

  public static native String stripTags(String v/*-{
    return !v ? v : String(v).replace(/<\/?[^>]+>/gi, "");
  }-*/;

  /**
   * Substitutes the indexed parameters.
   
   @param text the text
   @param param the parameter
   @return the new text
   */
  public static String substitute(String text, int param) {
    return substitute(text, safeRegexReplacement("" + param));
  }

  /**
   * Substitutes the named parameters. The passed keys and values must be
   * Strings.
   
   @param text the text
   @param params the parameters
   @return the new text
   */
  public static String substitute(String text, Map<String, Object> params) {
    Iterator<String> it = params.keySet().iterator();
    while (it.hasNext()) {
      String key = it.next();
      text = text.replaceAll("\\{" + key + "}", safeRegexReplacement(params.get(key).toString()));
    }
    return text;
  }



  /**
   * Substitutes the indexed parameters.
   
   @param text the text
   @param params the parameters
   @return the new text
   */
  public static String substitute(String text, Object... params) {
    for (int i = 0; i < params.length; i++) {
      Object p = params[i];
      if (p == null) {
        p = "";
      }
      text = text.replaceAll("\\{" + i + "}", safeRegexReplacement(p.toString()));
    }
    return text;
  }

  /**
   * Substitutes the named parameters. The passed keys and values must be
   * Strings.
   
   @param text the text
   @param keys the parameter names
   @param params the parameter values
   @return the new text
   */
  public static String substitute(String text, String[] keys, Map<String, Object> params) {
    for (int i = 0; i < keys.length; i++) {
      text = text.replaceAll("\\{" + keys[i"}", safeRegexReplacement((Stringparams.get(keys[i])));
    }
    return text;
  }

  /**
   * Replace any \ or $ characters in the replacement string with an escaped \\
   * or \$.
   
   @param replacement the regular expression replacement text
   @return null if replacement is null or the result of escaping all \ and $
   *         characters
   */
  private static String safeRegexReplacement(String replacement) {
    if (replacement == null) {
      return replacement;
    }

    return replacement.replaceAll("\\\\""\\\\\\\\").replaceAll("\\$""\\\\\\$");
  }
}

   
    
  
SmartGWT.zip( 9,880 k)
Related examples in the same category
1.Use reflection to generate the async interface from the Service interface as per GWT standard
2.Array Utils for client side GWT
3.A simple number formatting/ parsing class
4.GWT window utility
5.Implement java.util.regex.Pattern with Javascript RegExp object
6.GWT color class
7.DOM related helper methods (Smart GWT)
8.Helper class to decode and encode objects to and from Json (Ext GWT)
9.String util for GWT client side (Smart GWT)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.