All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.clickntap.platform.StringUtil Maven / Gradle / Ivy

There is a newer version: 1.189
Show newest version
package com.clickntap.platform;

/**
 * Collection of string utilities.
 * 
 */

public class StringUtil {

  /**
   * 
   */
  public static final String EMPTY_STRING = ""; //$NON-NLS-1$

  /**
   * Trim a string. Removes leading and trailing blanks. If the resulting string is empty, normalizes the string to an null string.
   * 
   * @param value the string to trim
   * @return the trimmed string, or null if the string is empty
   */

  public static String trimString(String value) {
    if (value == null)
      return null;
    value = value.trim();
    if (value.length() == 0)
      return null;
    return value;
  }

  /**
   * Convert an integer to an HTML RGB value. The result is of the form #hhhhhh. The input rgb integer value will be clipped into the range 0 ~ 0xFFFFFF
   * 
   * @param rgb the integer RGB value
   * @return the value as an HTML RGB string
   */

  public static String toRgbText(int rgb) {
    // clip input value.

    if (rgb > 0xFFFFFF)
      rgb = 0xFFFFFF;
    if (rgb < 0)
      rgb = 0;

    String str = "000000" + Integer.toHexString(rgb); //$NON-NLS-1$
    return "#" + str.substring(str.length() - 6); //$NON-NLS-1$
  }

  /**
   * Check if the locale string is a valid locale format, with the language, country and variant separated by underbars.
   * 

* The language argument is a valid ISO Language Code. . These codes are the lower-case, two-letter codes. *

* The country argument is a valid ISO Country Code. These codes are the upper-case, two-letter codes. *

* If the language is missing, the string should begin with an underbar. (Can't have a locale with just a variant -- the variant must accompany a valid language or country code). Examples: "en", "de_DE", "_GB", "en_US_WIN", "de__POSIX", "fr__MAC" * * @param locale string representing a locale * @return true if the locale is a valid locale, false if the locale is not valid. */ public static boolean isValidLocale(String locale) { // TODO: needs to confirm if BIRT support limited collection of locale. return true; } /** * Reports if a string is blank. A string is considered blank either if it is null, is an empty string, of consists entirely of white space. *

* For example, *

    *
  • null, "" and " " are blank strings *
* * @param str the string to check * @return true if the string is blank, false otherwise. */ public static boolean isBlank(String str) { return trimString(str) == null; } /** * Reports if a string is empty. A string is considered empty either if it is null, is an empty string. *

* For example, *

    *
  • Both null and "" are empty strings *
  • " " is not empty string. *
* * @param value the string to check * @return true if the string is empty, false otherwise. */ public static boolean isEmpty(String value) { if (value == null || value.length() == 0) return true; return false; } /** * Returns if the two string are null or equal. The {@link java.lang.String#equals(String)}is used to compare two strings. * * @param str1 the string to compare * @param str2 the string to compare * @return true, if the two string are null, or the two string are equal with case sensitive. */ public static boolean isEqual(String str1, String str2) { return str1 == str2 || (str1 != null && str1.equals(str2)); } /** * Returns if the two string are null or equal. The {@link java.lang.String#equalsIgnoreCase(String)}is used to compare two strings. * * @param str1 the string to compare * @param str2 the string to compare * @return true, if the two string are null, or the two string are equal with case sensitive. */ public static boolean isEqualIgnoreCase(String str1, String str2) { return str1 == str2 || (str1 != null && str1.equalsIgnoreCase(str2)); } /** * Extract file name (without path and suffix) from file name with path and suffix. *

* For example: *

*

    *
  • "c:\home\abc.xml" => "abc" *
  • "c:\home\abc" => "abc" *
  • "/home/user/abc.xml" => "abc" *
  • "/home/user/abc" => "abc" *
* * @param filePathName the file name with path and suffix * @return the file name without path and suffix */ public static String extractFileName(String filePathName) { if (filePathName == null) return null; int dotPos = filePathName.lastIndexOf('.'); int slashPos = filePathName.lastIndexOf('\\'); int backSlashPos = filePathName.lastIndexOf('/'); slashPos = slashPos > backSlashPos ? slashPos : backSlashPos; if (dotPos > slashPos) { return filePathName.substring(slashPos > 0 ? slashPos + 1 : 0, dotPos); } return filePathName.substring(slashPos > 0 ? slashPos + 1 : 0); } /** * Extract file name (without path but with suffix) from file name with path and suffix. *

* For example: *

*

    *
  • "c:\home\abc.xml" => "abc.xml" *
  • "c:\home\abc" => "abc" *
  • "/home/user/abc.xml" => "abc.xml" *
  • "/home/user/abc" => "abc" *
* * @param filePathName the file name with path and suffix * @return the file name without path but with suffix */ public static String extractFileNameWithSuffix(String filePathName) { if (filePathName == null) return null; int slashPos = filePathName.lastIndexOf('\\'); int backSlashPos = filePathName.lastIndexOf('/'); slashPos = slashPos > backSlashPos ? slashPos : backSlashPos; return filePathName.substring(slashPos > 0 ? slashPos + 1 : 0); } /** * Extracts the libaray namespace from the given qualified reference value. *

* For example, *

    *
  • "LibA" is extracted from "LibA.style1" *
  • null is returned from "style1" *
* * @param qualifiedName the qualified reference value * @return the library namespace */ public static String extractNamespace(String qualifiedName) { if (qualifiedName == null) return null; int pos = qualifiedName.indexOf('.'); if (pos == -1) return null; return StringUtil.trimString(qualifiedName.substring(0, pos)); } /** * Extracts the name from the given qualified reference value. * *

* For example, *

    *
  • "style1" is extracted from "LibA.style1" *
  • "style1" is returned from "style1" *
* * @param qualifiedName the qualified reference value * @return the name */ public static String extractName(String qualifiedName) { if (qualifiedName == null) return null; int pos = qualifiedName.indexOf('.'); if (pos == -1) return qualifiedName; return StringUtil.trimString(qualifiedName.substring(pos + 1)); } /** * Builds the qualified reference value. *

* For example, *

    *
  • ("LibA", "style1") => "LibA.style1" *
  • (" ", "style1) => "style1" *
* * @param namespace the library namespace to indicate which library the reference is using. * @param value the actual reference value * @return the qualified reference value */ public static String buildQualifiedReference(String namespace, String value) { if (StringUtil.isBlank(namespace)) return value; return namespace + "." + value; //$NON-NLS-1$ } /** * Trims the quotes. *

* For example, *

    *
  • ("a.b") => a.b *
  • ("a.b) => "a.b *
  • (a.b") => a.b" *
* * @param value the string may have quotes * @return the string without quotes */ public static String trimQuotes(String value) { if (value == null) return value; value = value.trim(); if (value.startsWith("\"") && value.endsWith("\"")) //$NON-NLS-1$ //$NON-NLS-2$ return value.substring(1, value.length() - 1); if (value.startsWith("'") && value.endsWith("'")) //$NON-NLS-1$ //$NON-NLS-2$ return value.substring(1, value.length() - 1); return value; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy