String util for GWT client side (Smart GWT) : Utility « GWT « Java






String util for GWT client side (Smart GWT)

     


/*
 * Smart GWT (GWT for SmartClient)
 * Copyright 2008 and beyond, Isomorphic Software, Inc.
 *
 * Smart GWT is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 3
 * is published by the Free Software Foundation.  Smart GWT is also
 * available under typical commercial license terms - see
 * http://smartclient.com/license
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 */

//package com.smartgwt.client.util;

public class StringUtil {

    /**
     * Convert all tag symbols ( <  and > ) into displayable HTML
     * by changing them to   <  and  >   respectively.
     *
     * @param str    the string to convert
     * @param prefix text to tack onto the beginning of result (eg: "<PRE>")
     * @param suffix text to tack onto the end of result (eg: "</PRE>")
     * @return prefix + converted text + suffix as a single string
     */
    public static native String convertTags(String str, String prefix, String suffix)/*-{
        return str == null ? null : new $wnd.String(str).convertTags(prefix, suffix);
    }-*/;

    /**
     * Convert plain text into into displayable HTML.
     * <p/>
     * This prevents HTML-special characters like &lt; and &gt; from being interpreted as tags, and
     * preserves line breaks and extra spacing.
     * <pre>
     *      converts       to
     *      --------         ---------------------------
     *        &          &amp;
     *        <          &lt;
     *        >          &gt;
     *        \r,\n,\r\n1space <BR>&nbsp;
     *        \r,\n,\r\n     <BR>
     *        \t         &nbsp;&nbsp;&nbsp;&nbsp;
     *        2 spaces       1space&nbsp;
     *           </pre>
     *
     * @param str the string to convert
     * @return the plain text into into displayable HTM
     */
    public static native String asHTML(String str)/*-{
        return str == null ? null : new $wnd.String(str).asHTML();
    }-*/;

    /**
     * Convert plain text into into displayable HTML.
     * <p/>
     * This prevents HTML-special characters like &lt; and &gt; from being interpreted as tags, and
     * preserves line breaks and extra spacing.
     * <pre>
     *      converts       to
     *      --------         ---------------------------
     *        &          &amp;
     *        <          &lt;
     *        >          &gt;
     *        \r,\n,\r\n1space <BR>&nbsp;
     *        \r,\n,\r\n     <BR>
     *        \t         &nbsp;&nbsp;&nbsp;&nbsp;
     *        2 spaces       1space&nbsp;
     *           </pre>
     *
     * @param str        the string to convert
     * @param noAutoWrap
     * @return the plain text into into displayable HTM
     */
    public static native String asHTML(String str, boolean noAutoWrap)/*-{
        return str == null ? null : new $wnd.String(str).asHTML(noAutoWrap);
    }-*/;

    /**
     * Reverses {@link #asHTML(String)}.
     *
     * @param str the input str
     * @return unescaped HTML
     */
    public static native String unescapeHTML(String str)/*-{
        return str == null ? null : new $wnd.String(str).unescapeHTML();
    }-*/;

    /**
     * Escapes special characters in XML values - so called 'unparsed data'
     * <pre>
     * " -> &quot;
     * ' -> &apos;
     * & -> &amp;
     * < -> &lt;
     * > -> &gt;
     * \r -> &x000D;
     * </pre>
     *
     * @param str the String to escape
     * @return xml safe String
     */
    public static native String makeXMLSafe(String str)/*-{
        return str == null ? null : $wnd.isc.makeXMLSafe(str);
    }-*/;

    /**
     * Abbreviates a String using ellipses. StringUtils.abbreviate("abcdefg", 6) = "abc..."
     *
     * @param str      the String to abbreviate
     * @param maxWidth maximum length of result String, must be at least 4
     * @return the abbreviated String
     * @throws IllegalArgumentException when the width is too small
     */
    public static String abbreviate(String str, int maxWidth) {
        if (str == null) {
            return null;
        }
        if (str.length() < maxWidth) {
            return str;
        }
        if (maxWidth < 4) {
            throw new IllegalArgumentException("Minimum required width is 4");
        }

        return str.substring(0, maxWidth - 3) + "...";
    }
}

   
    
    
    
    
  








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.Formatting functions for GWT client side (Ext GWT)
9.Helper class to decode and encode objects to and from Json (Ext 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