FileDocCategorySizeDatePackage
TextUtils.javaAPI DocMyID3 for Android13170Wed Oct 08 18:22:32 BST 2008org.cmc.music.util

TextUtils

public class TextUtils extends Object implements BasicConstants

Fields Summary
public static final String
kALPHABET_NUMERALS
public static final String
kALPHABET_LOWERCASE
public static final String
kALPHABET_UPPERCASE
public static final String
kALPHABET
public static final String
kFILENAME_SAFE
Constructors Summary
Methods Summary
public java.lang.StringescapeHTMLSpecialCharacters(java.lang.String s)
Replace characters having special meaning inside HTML tags with their escaped equivalents, using character entities such as '&'.

The escaped characters are :

  • <
  • >
  • "
  • '
  • \
  • &

This method ensures that arbitrary text appearing inside a tag does not "confuse" the tag. For example, HREF='Blah.do?Page=1&Sort=ASC' does not comply with strict HTML because of the ampersand, and should be changed to HREF='Blah.do?Page=1&Sort=ASC'. This is commonly seen in building query strings. (In JSTL, the c:url tag performs this task automatically.)

		final StringBuffer result = new StringBuffer();

		final StringCharacterIterator iterator = new StringCharacterIterator(s);
		char character = iterator.current();
		while (character != StringCharacterIterator.DONE)
		{
			if (character == '<")
			{
				result.append("<");
			}
			else if (character == '>")
			{
				result.append(">");
			}
			else if (character == '\"")
			{
				result.append(""");
			}
			else if (character == '\'")
			{
				result.append("'");
			}
			else if (character == '\\")
			{
				result.append("\");
			}
			else if (character == '&")
			{
				result.append("&");
			}
			else
			{
				//the char is not a special one
				//add it to the result as is
				result.append(character);
			}
			character = iterator.next();
		}
		return result.toString();
	
public static java.lang.Stringfilter(java.lang.String s, java.lang.String filter)

		StringBuffer result = new StringBuffer();

		for (int i = 0; i < s.length(); i++)
		{
			char c = s.charAt(i);

			if (filter.indexOf(c) >= 0)
				result.append(c);
		}

		return result.toString();
	
public static intfindFirstWhiteSpace(java.lang.String s)

		if (s == null)
			return -1;
		for (int i = 0; i < s.length(); i++)
		{
			char c = s.charAt(i);
			if (Character.isWhitespace(c))
				return i;
		}
		return -1;
	
public java.lang.StringforRegex(java.lang.String aRegexFragment)
Replace characters having special meaning in regular expressions with their escaped equivalents.

The escaped characters include :

  • .
  • \
  • ?, * , and +
  • &
  • :
  • { and }
  • [ and ]
  • ( and )
  • ^ and $

		final StringBuffer result = new StringBuffer();

		final StringCharacterIterator iterator = new StringCharacterIterator(
				aRegexFragment);
		char character = iterator.current();
		while (character != StringCharacterIterator.DONE)
		{
			/*
			 * All literals need to have backslashes doubled.
			 */
			if (character == '.")
			{
				result.append("\\.");
			}
			else if (character == '\\")
			{
				result.append("\\\\");
			}
			else if (character == '?")
			{
				result.append("\\?");
			}
			else if (character == '*")
			{
				result.append("\\*");
			}
			else if (character == '+")
			{
				result.append("\\+");
			}
			else if (character == '&")
			{
				result.append("\\&");
			}
			else if (character == ':")
			{
				result.append("\\:");
			}
			else if (character == '{")
			{
				result.append("\\{");
			}
			else if (character == '}")
			{
				result.append("\\}");
			}
			else if (character == '[")
			{
				result.append("\\[");
			}
			else if (character == ']")
			{
				result.append("\\]");
			}
			else if (character == '(")
			{
				result.append("\\(");
			}
			else if (character == ')")
			{
				result.append("\\)");
			}
			else if (character == '^")
			{
				result.append("\\^");
			}
			else if (character == '$")
			{
				result.append("\\$");
			}
			else
			{
				//the char is not a special one
				//add it to the result as is
				result.append(character);
			}
			character = iterator.next();
		}
		return result.toString();
	
public static intgetLineCount(java.lang.String s)

		if (s == null || s.length() < 1)
			return 0;
		String lines[] = TextUtils.split(s, newline);
		return lines.length;
	
public static java.lang.Stringhead(java.lang.String s, int count)

		if (s == null || s.length() < 1)
			return s;
		String lines[] = TextUtils.split(s, newline);
		if (lines.length < count)
			count = lines.length;
		String lines2[] = new String[count];
		System.arraycopy(lines, 0, lines2, 0, count);
		return TextUtils.join(lines2, newline);
	
public static java.lang.StringinsertSpacesBeforeCaps(java.lang.String s)

		StringBuffer result = new StringBuffer();

		char prev = 0;
		for (int i = 0; i < s.length(); i++)
		{
			char c = s.charAt(i);

			if (Character.isLetter(c) && (i > 0) && Character.isLetter(prev)
					&& Character.isLowerCase(prev) && Character.isUpperCase(c))
				result.append(' ");

			result.append(c);

			prev = c;
		}

		return result.toString();
	
private static booleanisNumeric(java.lang.String s, boolean integer)

		if (s == null)
			return false;

		char chars[] = s.toCharArray();
		if (chars.length < 1)
			return false;

		boolean has_period = integer;
		boolean has_digit = false;

		for (int i = 0; i < chars.length; i++)
		{
			char c = chars[i];
			if (i == 0 && c == '-")
				;
			else if (Character.isDigit(c))
				has_digit = true;
			else if (c == '." && !has_period)
			{
				has_period = true;
			}
			else
				return false;
		}
		return has_digit;
	
public static booleanisNumericDecimal(java.lang.String s)

		return isNumeric(s, false);
	
public static booleanisNumericInteger(java.lang.String s)

		return isNumeric(s, true);
	
public static final java.lang.Stringjoin(java.lang.String[] splits, java.lang.String token)

		StringBuffer result = new StringBuffer();

		for (int i = 0; i < splits.length; i++)
		{
			if (i > 0)
				result.append(token);

			result.append(splits[i]);
		}

		return result.toString();
	
public static java.lang.Stringpad_left(java.lang.String s, int length, java.lang.String padding)

		for (int i = 0; i < 10000 && s.length() < length; i++)
			s = padding + s;
		return s;
	
public static java.lang.Stringreplace(java.lang.String s, java.lang.String find, java.lang.String replaceto)

		if (s == null)
			return s;

		StringBuffer result = new StringBuffer();

		int index;
		while ((index = s.indexOf(find)) >= 0)
		{
			result.append(s.substring(0, index));
			result.append(replaceto);
			s = s.substring(index + find.length());
		}
		result.append(s);

		return result.toString();
	
public static final java.lang.String[]split(java.lang.String s, char token)

		return split(s, "" + token);
	
public static final java.lang.String[]split(java.lang.String s, java.lang.String token)

		//		if (s == null)
		//			return s;
		//
		Vector result = new Vector();

		int index;
		while ((index = s.indexOf(token)) >= 0)
		{
			result.add(s.substring(0, index));
			s = s.substring(index + token.length());
		}
		result.add(s);

		String splits[] = new String[result.size()];
		for (int i = 0; i < result.size(); i++)
			splits[i] = (String) result.get(i);
		return splits;
	
public static java.lang.Stringtail(java.lang.String s, int count)

		if (s == null || s.length() < 1)
			return s;
		String lines[] = TextUtils.split(s, newline);
		if (lines.length < count)
			count = lines.length;
		String lines2[] = new String[count];
		System.arraycopy(lines, lines.length - count, lines2, 0, count);
		return TextUtils.join(lines2, newline);
	
public java.lang.StringtoDisableHTMLTags(java.lang.String aText)
Return aText with all start-of-tag and end-of-tag characters replaced by their escaped equivalents.

If user input may contain tags which must be disabled, then call this method, not {@link #forHTMLTag}. This method is used for text appearing outside of a tag, while {@link #forHTMLTag} is used for text appearing inside an HTML tag.

It is not uncommon to see text on a web page presented erroneously, because all special characters are escaped (as in {@link #forHTMLTag}). In particular, the ampersand character is often escaped not once but twice : once when the original input occurs, and then a second time when the same item is retrieved from the database. This occurs because the ampersand is the only escaped character which appears in a character entity.

		final StringBuffer result = new StringBuffer();
		final StringCharacterIterator iterator = new StringCharacterIterator(
				aText);
		char character = iterator.current();
		while (character != StringCharacterIterator.DONE)
		{
			if (character == '<")
			{
				result.append("<");
			}
			else if (character == '>")
			{
				result.append(">");
			}
			else
			{
				//the char is not a special one
				//add it to the result as is
				result.append(character);
			}
			character = iterator.next();
		}
		return result.toString();
	
public static java.lang.StringtoSafeFilename(java.lang.String s)


	    
	
		StringBuffer result = new StringBuffer();
		//		char prev = 0;

		s = s.trim();

		for (int i = 0; i < s.length(); i++)
		{
			char c = s.charAt(i);

			if (kFILENAME_SAFE.indexOf(c) < 0)
				;
			else
				result.append(c);
		}

		String filtered = result.toString();

		while (filtered.startsWith("."))
			filtered = filtered.substring(1);
		while (filtered.endsWith("."))
			filtered = filtered.substring(0, filtered.length() - 1);

		return filtered;
	
public static java.lang.StringtoTitleCase(java.lang.String s)

		StringBuffer result = new StringBuffer();
		char prev = 0;
		if (s.startsWith("The Jackson 5 - I'Ll Be There"))
			System.out.println(s);

		for (int i = 0; i < s.length(); i++)
		{
			char c = s.charAt(i);
			//			char next = 0;
			//			if ((i + 1) < s.length())
			//				next = s.charAt(i + 1);

			if (Character.isLetter(c))
			{
				if (i == 0)
					result.append(Character.toUpperCase(c));
				else if ((prev == '\'"))
					//					else if ((prev == '\'') && Character.isLetter(next))
					result.append(Character.toLowerCase(c));
				else if (!Character.isLetter(prev))
					result.append(Character.toUpperCase(c));
				else
					result.append(Character.toLowerCase(c));
			}
			else
				result.append(c);
			prev = c;
		}
		return result.toString();
	
public static java.util.VectortokenizeByWhiteSpace(java.lang.String s, boolean trim)

		Vector result = new Vector();
		int index;
		while ((s.length() > 0) && ((index = findFirstWhiteSpace(s)) >= 0))
		{
			String left = s.substring(0, index);
			result.add(left);
			s = s.substring(index + 1);
			if (trim)
				s = s.trim();
		}
		if (s.length() > 0)
			result.add(s);
		return result;
	
public static java.util.VectortokenizeString(java.lang.String s, java.lang.String token)

		Vector result = new Vector();
		int index;
		while ((s.length() > 0) && ((index = s.indexOf(token)) >= 0))
		{
			String left = s.substring(0, index);
			result.add(left);
			s = s.substring(index + token.length());
		}
		if (s.length() > 0)
			result.add(s);
		return result;
	
public java.lang.StringurlEncode(java.lang.String aURLFragment)
Synonym for URLEncoder.encode(String, "UTF-8").

Used to ensure that HTTP query strings are in proper form, by escaping special characters such as spaces.

An example use case for this method is a login scheme in which, after successful login, the user is redirected to the "original" target destination. Such a target might be passed around as a request parameter. Such a request parameter will have a URL as its value, as in "LoginTarget=Blah.jsp?this=that&blah=boo", and would need to be URL-encoded in order to escape its special characters.

It is important to note that if a query string appears in an HREF attribute, then there are two issues - ensuring the query string is valid HTTP (it is URL-encoded), and ensuring it is valid HTML (ensuring the ampersand is escaped).

		String result = null;
		try
		{
			result = URLEncoder.encode(aURLFragment, "UTF-8");
		}
		catch (UnsupportedEncodingException ex)
		{
			throw new RuntimeException("UTF-8 not supported", ex);
		}
		return result;