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.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » GWT » Utility 




Formatting functions for GWT client side (Ext GWT)
     

/*
 * Ext GWT - Ext for GWT
 * Copyright(c) 2007-2009, Ext JS, LLC.
 [email protected]
 
 * 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)
10.GWT DOM util
11.Replace string for GWT
12.GWT style util
13.gwt DateTimeFormat
14.A utility class that provides utility methods to work with arrays for GWT.
15.Date compare util for GWT
16.GWT DOM utility functions for use when programmatically constructing a UI.
17.Generate a faster string comparison than String.equals() for web mode code.
18.Js Array Util
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.