TextUtils.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (c) 2002-2003 by OpenSymphony
  3. * All rights reserved.
  4. */
  5. package com.hcks.cmfds.commons.util;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8. import java.util.Arrays;
  9. import java.util.Collection;
  10. import java.util.Iterator;
  11. /**
  12. *
  13. * 拷贝之前版本的源码,此版本缺少这个类
  14. * Utilities for common String manipulations.
  15. *
  16. * This is a class contains static methods only and is not meant to be instantiated.
  17. * It was brought in from oscore trunk revision 147, and trimmed to only contain
  18. * methods used by XWork.
  19. *
  20. * @author <a href="mailto:joe@truemesh.com">Joe Walnes</a>
  21. * @author <a href="mailto:pkan@internet.com">Patrick Kan</a>
  22. * @author <a href="mailto:mcannon@internet.com">Mike Cannon-Brookes</a>
  23. * @author <a href="mailto:hani@fate.demon.co.uk">Hani Suleiman</a>
  24. * @author <a href="mailto:joeo@adjacency.org">Joseph B. Ottinger</a>
  25. * @author <a href="mailto:scott@atlassian.com">Scott Farquhar</a>
  26. *
  27. * @version $Revision: 147 $
  28. */
  29. public class TextUtils {
  30. public final static String htmlEncode(String s) {
  31. return htmlEncode(s, true);
  32. }
  33. /**
  34. * Escape html entity characters and high characters (eg "curvy" Word quotes).
  35. * Note this method can also be used to encode XML.
  36. * @param s the String to escape.
  37. * @param encodeSpecialChars if true high characters will be encode other wise not.
  38. * @return the escaped string
  39. */
  40. public final static String htmlEncode(String s, boolean encodeSpecialChars) {
  41. s = noNull(s);
  42. StringBuffer str = new StringBuffer();
  43. for (int j = 0; j < s.length(); j++) {
  44. char c = s.charAt(j);
  45. // encode standard ASCII characters into HTML entities where needed
  46. if (c < '\200') {
  47. switch (c) {
  48. case '"':
  49. str.append("&quot;");
  50. break;
  51. case '&':
  52. str.append("&amp;");
  53. break;
  54. case '<':
  55. str.append("&lt;");
  56. break;
  57. case '>':
  58. str.append("&gt;");
  59. break;
  60. default:
  61. str.append(c);
  62. }
  63. }
  64. // encode 'ugly' characters (ie Word "curvy" quotes etc)
  65. else if (encodeSpecialChars && (c < '\377')) {
  66. String hexChars = "0123456789ABCDEF";
  67. int a = c % 16;
  68. int b = (c - a) / 16;
  69. String hex = "" + hexChars.charAt(b) + hexChars.charAt(a);
  70. str.append("&#x" + hex + ";");
  71. }
  72. //add other characters back in - to handle charactersets
  73. //other than ascii
  74. else {
  75. str.append(c);
  76. }
  77. }
  78. return str.toString();
  79. }
  80. /**
  81. * Join an Iteration of Strings together.
  82. *
  83. * <h5>Example</h5>
  84. *
  85. * <pre>
  86. * // get Iterator of Strings ("abc","def","123");
  87. * Iterator i = getIterator();
  88. * out.print( TextUtils.join(", ",i) );
  89. * // prints: "abc, def, 123"
  90. * </pre>
  91. *
  92. * @param glue Token to place between Strings.
  93. * @param pieces Iteration of Strings to join.
  94. * @return String presentation of joined Strings.
  95. */
  96. @SuppressWarnings("unchecked")
  97. public final static String join(String glue, Iterator pieces) {
  98. StringBuffer s = new StringBuffer();
  99. while (pieces.hasNext()) {
  100. s.append(pieces.next().toString());
  101. if (pieces.hasNext()) {
  102. s.append(glue);
  103. }
  104. }
  105. return s.toString();
  106. }
  107. /**
  108. * Join an array of Strings together.
  109. *
  110. * @param glue Token to place between Strings.
  111. * @param pieces Array of Strings to join.
  112. * @return String presentation of joined Strings.
  113. *
  114. * @see #join(String, java.util.Iterator)
  115. */
  116. public final static String join(String glue, String[] pieces) {
  117. return join(glue, Arrays.asList(pieces).iterator());
  118. }
  119. /**
  120. * Join a Collection of Strings together.
  121. *
  122. * @param glue Token to place between Strings.
  123. * @param pieces Collection of Strings to join.
  124. * @return String presentation of joined Strings.
  125. *
  126. * @see #join(String, java.util.Iterator)
  127. */
  128. @SuppressWarnings("unchecked")
  129. public final static String join(String glue, Collection pieces) {
  130. return join(glue, pieces.iterator());
  131. }
  132. /**
  133. * Return <code>string</code>, or <code>defaultString</code> if
  134. * <code>string</code> is <code>null</code> or <code>""</code>.
  135. * Never returns <code>null</code>.
  136. *
  137. * <p>Examples:</p>
  138. * <pre>
  139. * // prints "hello"
  140. * String s=null;
  141. * System.out.println(TextUtils.noNull(s,"hello");
  142. *
  143. * // prints "hello"
  144. * s="";
  145. * System.out.println(TextUtils.noNull(s,"hello");
  146. *
  147. * // prints "world"
  148. * s="world";
  149. * System.out.println(TextUtils.noNull(s, "hello");
  150. * </pre>
  151. *
  152. * @param string the String to check.
  153. * @param defaultString The default string to return if <code>string</code> is <code>null</code> or <code>""</code>
  154. * @return <code>string</code> if <code>string</code> is non-empty, and <code>defaultString</code> otherwise
  155. * @see #stringSet(java.lang.String)
  156. */
  157. public final static String noNull(String string, String defaultString) {
  158. return (stringSet(string)) ? string : defaultString;
  159. }
  160. /**
  161. * Return <code>string</code>, or <code>""</code> if <code>string</code>
  162. * is <code>null</code>. Never returns <code>null</code>.
  163. * <p>Examples:</p>
  164. * <pre>
  165. * // prints 0
  166. * String s=null;
  167. * System.out.println(TextUtils.noNull(s).length());
  168. *
  169. * // prints 1
  170. * s="a";
  171. * System.out.println(TextUtils.noNull(s).length());
  172. * </pre>
  173. * @param string the String to check
  174. * @return a valid (non-null) string reference
  175. */
  176. public final static String noNull(String string) {
  177. return noNull(string, "");
  178. }
  179. /**
  180. * Check whether <code>string</code> has been set to
  181. * something other than <code>""</code> or <code>null</code>.
  182. * @param string the <code>String</code> to check
  183. * @return a boolean indicating whether the string was non-empty (and non-null)
  184. */
  185. public final static boolean stringSet(String string) {
  186. return (string != null) && !"".equals(string);
  187. }
  188. /**
  189. * Verify That the given String is in valid URL format.
  190. * @param url The url string to verify.
  191. * @return a boolean indicating whether the URL seems to be incorrect.
  192. */
  193. public final static boolean verifyUrl(String url) {
  194. if (url == null) {
  195. return false;
  196. }
  197. if (url.startsWith("https://")) {
  198. // URL doesn't understand the https protocol, hack it
  199. url = "http://" + url.substring(8);
  200. }
  201. try {
  202. new URL(url);
  203. return true;
  204. } catch (MalformedURLException e) {
  205. return false;
  206. }
  207. }
  208. }