com.opensymphony.xwork2.util.TextUtils Maven / Gradle / Ivy
/*
* Copyright (c) 2002-2003 by OpenSymphony
* All rights reserved.
*/
package com.opensymphony.xwork2.util;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
/**
* Utilities for common String manipulations.
*
* This is a class contains static methods only and is not meant to be instantiated.
* It was brought in from oscore trunk revision 147, and trimmed to only contain
* methods used by XWork.
*
* @author Joe Walnes
* @author Patrick Kan
* @author Mike Cannon-Brookes
* @author Hani Suleiman
* @author Joseph B. Ottinger
* @author Scott Farquhar
*
* @version $Revision: 147 $
*/
public class TextUtils {
public final static String htmlEncode(String s) {
return htmlEncode(s, true);
}
/**
* Escape html entity characters and high characters (eg "curvy" Word quotes).
* Note this method can also be used to encode XML.
* @param s the String to escape.
* @param encodeSpecialChars if true high characters will be encode other wise not.
* @return the escaped string
*/
public final static String htmlEncode(String s, boolean encodeSpecialChars) {
s = noNull(s);
StringBuffer str = new StringBuffer();
for (int j = 0; j < s.length(); j++) {
char c = s.charAt(j);
// encode standard ASCII characters into HTML entities where needed
if (c < '\200') {
switch (c) {
case '"':
str.append(""");
break;
case '&':
str.append("&");
break;
case '<':
str.append("<");
break;
case '>':
str.append(">");
break;
default:
str.append(c);
}
}
// encode 'ugly' characters (ie Word "curvy" quotes etc)
else if (encodeSpecialChars && (c < '\377')) {
String hexChars = "0123456789ABCDEF";
int a = c % 16;
int b = (c - a) / 16;
String hex = "" + hexChars.charAt(b) + hexChars.charAt(a);
str.append("" + hex + ";");
}
//add other characters back in - to handle charactersets
//other than ascii
else {
str.append(c);
}
}
return str.toString();
}
/**
* Join an Iteration of Strings together.
*
* Example
*
*
* // get Iterator of Strings ("abc","def","123");
* Iterator i = getIterator();
* out.print( TextUtils.join(", ",i) );
* // prints: "abc, def, 123"
*
*
* @param glue Token to place between Strings.
* @param pieces Iteration of Strings to join.
* @return String presentation of joined Strings.
*/
public final static String join(String glue, Iterator pieces) {
StringBuffer s = new StringBuffer();
while (pieces.hasNext()) {
s.append(pieces.next().toString());
if (pieces.hasNext()) {
s.append(glue);
}
}
return s.toString();
}
/**
* Join an array of Strings together.
*
* @param glue Token to place between Strings.
* @param pieces Array of Strings to join.
* @return String presentation of joined Strings.
*
* @see #join(String, java.util.Iterator)
*/
public final static String join(String glue, String[] pieces) {
return join(glue, Arrays.asList(pieces).iterator());
}
/**
* Join a Collection of Strings together.
*
* @param glue Token to place between Strings.
* @param pieces Collection of Strings to join.
* @return String presentation of joined Strings.
*
* @see #join(String, java.util.Iterator)
*/
public final static String join(String glue, Collection pieces) {
return join(glue, pieces.iterator());
}
/**
* Return string, or defaultString if
* string is null or "".
* Never returns null.
*
* Examples:
*
* // prints "hello"
* String s=null;
* System.out.println(TextUtils.noNull(s,"hello");
*
* // prints "hello"
* s="";
* System.out.println(TextUtils.noNull(s,"hello");
*
* // prints "world"
* s="world";
* System.out.println(TextUtils.noNull(s, "hello");
*
*
* @param string the String to check.
* @param defaultString The default string to return if string is null or ""
* @return string if string is non-empty, and defaultString otherwise
* @see #stringSet(java.lang.String)
*/
public final static String noNull(String string, String defaultString) {
return (stringSet(string)) ? string : defaultString;
}
/**
* Return string, or "" if string
* is null. Never returns null.
* Examples:
*
* // prints 0
* String s=null;
* System.out.println(TextUtils.noNull(s).length());
*
* // prints 1
* s="a";
* System.out.println(TextUtils.noNull(s).length());
*
* @param string the String to check
* @return a valid (non-null) string reference
*/
public final static String noNull(String string) {
return noNull(string, "");
}
/**
* Check whether string has been set to
* something other than "" or null.
* @param string the String to check
* @return a boolean indicating whether the string was non-empty (and non-null)
*/
public final static boolean stringSet(String string) {
return (string != null) && !"".equals(string);
}
/**
* Verify That the given String is in valid URL format.
* @param url The url string to verify.
* @return a boolean indicating whether the URL seems to be incorrect.
*/
public final static boolean verifyUrl(String url) {
if (url == null) {
return false;
}
if (url.startsWith("https://")) {
// URL doesn't understand the https protocol, hack it
url = "http://" + url.substring(8);
}
try {
new URL(url);
return true;
} catch (MalformedURLException e) {
return false;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy